CSS flytande element

CSS gör det möjligt att kontrollera hur HTML-element ska visas.

<div class="wrapper">
   <div class="one">
   Lorem ipsum dolor sit amet.
   </div>
   <div class="two">
   Consectetur adipisicing elit.
   </div>
   <div class="three">
   Sed do eiusmod tempor incididunt.
   </div>
</div>

Egenskapen float

CSS-egenskapen "float" kontrollerar om och hur ett element ska visas inuti ett föräldra-element. Egenskapen kan anta värdena "left", "right" och "none". Standardvärdet för egenskapen "float" är "none" och det gör att ett element automatiskt fyller ut hela föräldra-elementets bredd.

CSS

.one {
   background-color:blue;
   float:none;
}
.two {
   background-color:yellow;
   float:left;
}
.three {
   background-color:green;
   float:right;
}

Layout med CSS

CSS egenskapen "float" används ofta tillsammans med egenskapen "width" för att skapa den grundläggande layouten för en hemsida. Detta gör att elementen kommer att "flyta" bredvid varandra med fasta bredder och på detta sätt skaps ett ramverk för sidans innehåll. I nedanstående exempel visas en vanlig blogglayout med en kolumn för blogginläggen och en kolumn för menuer mm.

CSS

.wrapper {
   background-color:blue;
   width:200px;
}
.one {
   background-color:blue;
   float:none;
}
.two {
   background-color:yellow;
   float:left;
   width:70px;
}
.three {
   background-color:green;
   float:left;
   width:130px;
}

Läs mer om hur man skapar layout i kapitlet om boxmodellen

Position --- en egen sida!!!

Css-egenskapen "position" bestämmer hur ett element ska positioneras på en webbida.

Värden för position
Värde Resultat
absolute Elementet visas i förhållande till elementet body, eller ett nestlat element med egenskapen position ädr värdet är satt till "relative"
fixed Elementet visas iförhållande till webbläsarfönstret. Fungerar inte i alla webbläsare.
relative Elementet ...
static Standardinställning. Elementet visas på sin vanliga plats.

 

CSS

body {
   background-color:red;
}
.wrapper {
   background-color:blue;

   width:200px;

}
.one {
   background-color:blue;
   position:relative;

}
.two {
   background-color:yellow;
   float:left;
   width:50px;
}
.three {
   background-color:green;
   float:left;
   width:150px;
}

 

//Skapa menyer...

 

//Boxmodellen för sidlayout

 

PÅ ny sida: elemntvisning...

Cursor

Examples

How to display an element as an inlin-e element.
This example demonstrates how to display an element as an inline element.

How to display an element as a block element
This example demonstrates how to display an element as a block element.

A simple use of the float property
Let an image float to the right in a paragraph.

An image with border and margins that floats to the right in a paragraph
Let an image float to the right in a paragraph. Add border and margins to the image.

An image with a caption that floats to the right
Let an image with a caption float to the right.

Let the first letter of a paragraph float to the left
Let the first letter of a paragraph float to the left and style the letter.

Creating a horizontal menu
Use float with a list of hyperlinks to create a horizontal menu.

Creating a homepage without tables
Use float to create a homepage with a header, footer, left content and main content.

Position:relative
This example demonstrates how to position an element relative to its normal position.

Position:absolute
This example demonstrates how to position an element using an absolute value.

How to make an element invisible
This example demonstrates how to make an element invisible. Do you want the element to show or not?

How to make a table element collapse
This example demonstrates how to make a table element collapse.

Change the cursor
This example demonstrates how to change the cursor.

Clear the sides of an element
This example demonstrates how clear the sides of other floating elements.


CSS Classification Properties

The CSS classification properties allow you to control how to display an element, set where an image will appear in another element, position an element relative to its normal position, position an element using an absolute value, and how to control the visibility of an element.

Browser support: IE: Internet Explorer, F: Firefox, N: Netscape.

W3C: The number in the "W3C" column indicates in which CSS recommendation the property is defined (CSS1 or CSS2).

Property Description Values IE F N W3C
clear Sets the sides of an element where other floating elements are not allowed left
right
both
none
4 1 4 1
cursor Specifies the type of cursor to be displayed url
auto
crosshair
default
pointer
move
e-resize
ne-resize
nw-resize
n-resize
se-resize
sw-resize
s-resize
w-resize
text
wait
help
4 1 6 2
display Sets how/if an element is displayed none
inline
block
list-item
run-in
compact
marker
table
inline-table
table-row-group
table-header-group
table-footer-group
table-row
table-column-group
table-column
table-cell
table-caption
4 1 4 1
float Sets where an image or a text will appear in another element left
right
none
4 1 4 1
position Places an element in a static, relative, absolute or fixed position static
relative
absolute
fixed
4 1 4 2
visibility Sets if an element should be visible or invisible visible
hidden
collapse
4 1 6 2