1. Setting floating Properties
Floating can be achieved by setting the float property of the block. such as Float:right, the block will be detached from the general layout, floating on the right side of the display. At this point, the other block ignores the placeholder for the block and is arranged in a general layout. So there is the case that floating block elements overlap with other elements.
Clear float: To eliminate a block that is covered by other floating blocks, set the clear property, such as Clear:right, the right side of the block is not allowed to float, and when other blocks float to the right, they are placed underneath the floating block. Clear:both, no floats are allowed on either side of the block.
A two-column layout can be achieved with floating properties:
. superblock{
? ? width:250px;
? ? height:300px;
? ? background:red;
? ? margin:0 Auto;
}
. block_common{
? ? height:100px;
? ? width:100px;
? ? font-size:30px;
}
. block1{
? ? Color:orange;
? ? Background:blue;
? ? Float:right;
}
. block2{
? ? Color:blue;
? ? Background:orange;
? ? Float:left;
}
Note that because the float is detached from the general layout, not only the block below the floating element ignores the floating height, the parent element of the floating block also ignores its height, and if the height attribute is not set in. Superclass, the parent element cannot be displayed because the element does not include any other elements at the height of 0. This setting height is not very good, because the intuitive superclass is included in the two floating blocks, if superclass below there are other blocks, do not set the height of superclass will make the following block also move up, although you can set the following block clear: Both to solve, but it is not a good solution.
2.clearfix
For the above problems, you can add an element below the superclass, and set the Clear:both, can achieve good results, but no more than a div, is still not a good solution. Completing a similar work can be done by adding:
. superblock:after{
? ? Content: "";
? ? Clear:both;
? ? Display:block;
}
At this point, a block is added after Superblock, the default is the inline element, so you need to change to block, set the property to clear:both,content content as "".
Set the style to a reusable property: Clearfix:
. clearfix:after{
? ? Content: "";
? ? Clear:both;
? ? Display:block;
}
This can be achieved if you need to reuse a property in a block that needs to clear the float.
Clearfix of complete logic:
. Clearfix () {
? ? &:before,
? ? &:after{
? ? ? ? Content: "";
? ? ? ? display:table;
? ? }
? ? &:after{
????? Clear:both;
? ? }
}
In addition to reusing styles, refer to the block properties that need to be cleared for floating. Classfix () can:
. superblock {
??.. clearfix ();
}
3. Block element placement Anywhere
In addition to float, there is another way out of the general placement process: Position:absolute properties:
. block{
? ? Position:absolute;
? ? top:10px;
? ? left:10px;
}
At this point, the. Block is 10px away from the top, and the 10px position on the left is displayed.
If you want to display in a relative parent class, add a property to the parent element: position:relative, or you can add the property to the parent class's parent class, which is displayed in a relative position in the parent class of the parent class, and you can think of the default attribute position:relative in the top-level element. The position:absolute attribute is then searched in the ancestor chain to find the element with the Position:relative attribute, and it is the relative standard.
Web learning note floating and placing of _03_ block elements