[CSS]CSS Advanced - CSS Aligning Elements
개발관련/CSS/CSS3 2012. 10. 24. 01:16 |Center Aligning Using the margin Property
Block elements can be aligned by setting the left and right margins to "auto".
Note: Using margin:auto will not work in IE8 and earlier, unless a !DOCTYPE is declared.
Setting the left and right margins to auto specifies that they should split the available margin equally. The result is a centered element:
Example
{
margin-left:auto;
margin-right:auto;
width:70%;
background-color:#b0e0e6;
}
Try it yourself »
Tip: Aligning has no effect if the width is 100%.
Note: In IE5 there is a margin handling bug for block elements. To make the example above work in IE5, add some extra code. Try it yourself
Left and Right Aligning Using the position Property
One method of aligning elements is to use absolute positioning:
Note: Absolute positioned elements are removed from the normal flow, and can overlap elements.
Crossbrowser Compatibility Issues
When aligning elements like this, it is always a good idea to predefine margin and padding for the <body> element. This is to avoid visual differences in different browsers.
There is a problem with IE8 and earlier, when using the position property. If a container element (in our case <div class="container">) has a specified width, and the !DOCTYPE declaration is missing, IE8 and earlier versions will add a 17px margin on the right side. This seems to be space reserved for a scrollbar. Always set the !DOCTYPE declaration when using the position property:
Example
{
margin:0;
padding:0;
}
.container
{
position:relative;
width:100%;
}
.right
{
position:absolute;
right:0px;
width:300px;
background-color:#b0e0e6;
}
Try it yourself »
Left and Right Aligning Using the float Property
One method of aligning elements is to use the float property:
Crossbrowser Compatibility Issues
When aligning elements like this, it is always a good idea to predefine margin and padding for the <body> element. This is to avoid visual differences in different browsers.
There is a problem with IE8 and earlier when using the float property. If the !DOCTYPE declaration is missing, IE8 and earlier versions will add a 17px margin on the right side. This seems to be space reserved for a scrollbar. Always set the !DOCTYPE declaration when using the float property:
Example
{
margin:0;
padding:0;
}
.right
{
float:right;
width:300px;
background-color:#b0e0e6;
}
Try it yourself »
CSS Aligning Elements
Center aligning with margin
Left/Right aligning with position
Left/Right aligning with position - Crossbrowser solution
Left/Right aligning with float
Left/Right aligning with float - Crossbrowser solution
'개발관련 > CSS/CSS3' 카테고리의 다른 글
[CSS]CSS Advanced - CSS Pseudo-elements (0) | 2012.10.24 |
---|---|
[CSS]CSS Advanced - CSS Pseudo-classes (0) | 2012.10.24 |
[CSS]CSS Advanced - CSS Floating (0) | 2012.10.23 |
[CSS]CSS Advanced - CSS Positioning (0) | 2012.10.23 |
[CSS]CSS Advanced - CSS Display (0) | 2012.10.23 |