From the CSS Learning notes in the Display Properties article, we know that Div is a block element that will have a single line, even if the div's width is small, like this
Applying the inline property of the display property allows Div to share a row with the Div, in addition to this method, there is no other way to achieve this effect, the answer is yes, that is the following to introduce the CSS floating characteristics, floating from the literal point of view, is floating up, moving up, So who floats, and who moves up? Look down and you'll soon know the answer.
The float of an element is defined as the element that sets the floating property that floats above the standard flow, out of control of the standard document flow. After the element floats, it will still affect the layout of the standard flow, although it is out of standard flow.
In CSS, floating is defined by the Float property, which has the following basic syntax: The selector {float: property value;}, the default value is None (not floating), and the left to float, it can be understood to float up to the left of the arrangement, right is floating, Naturally, it is arranged on the right.
Now let's set the float for div2 and see what happens.
1 <!DOCTYPE HTML>2 <HTMLLang= "en">3 <Head>4 <MetaCharSet= "UTF-8">5 <title>Floating</title>6 <styletype= "Text/css">7 . Div1{Background-color:Red;Height:50px;width:400px;}8 . Div2{Background-color:Blue;Height:100px;width:100px;float: Left;}9 . Div3{Background-color:Gray;Height:150px;width:400px;}Ten . Div4{Background-color:Green;Height:30px;width:600px;} One </style> A </Head> - <Body> - <Divclass= "Div1">Div1</Div> the <Divclass= "Div2">Div2</Div> - <Divclass= "Div3">This is Div3 this is div3 this is Div3 this is the Div3 this is div3 this is Div3 this is the div3 it's a div3 it's the div3 it's the one that's div3 this is the DIV3 which is the Div3</Div> - <Divclass= "Div4">Div4</Div> - </Body> + </HTML>
At this time the position of the DIV2 has not changed, and Div3 moved upward, and Div2 shared a row, the equivalent of Div2 not occupy the space of the page, but affected the text layout in Div3. From here it can be clearly seen that the floating object Div2 first floated up, and then the object behind the Div3 will be moved to its original position, which also answers the first question raised.
Floating is a block element exclusive row of the behavior of the cancellation, the block from the original document flow pattern, it can be understood as it floated up, its original place is empty, its contents separated out, so that the object behind it when it does not exist.
Next to the DIV3 also set a float, what unexpected results will appear?
At this point, because Div2 and Div3 also set the float, so all out of the standard flow, so div4 up and Div1 formed a new standard flow, and the floating element is "floating" on the elements of the standard flow, so Div4 was Div2,div3 blocked part.
From the above phenomenon can be seen, to div3 set floating, Div3 is immediately followed by Div2 behind, but Div2 is also set up floating, but Div2 did not follow the div1, here can draw a conclusion:
The floating element a arranges position, which is related to the previous element (block level). If the previous element has a float, the top of the A element is aligned with the top of the previous element (that is, immediately after the previous element), and if a row does not fit, the a element is squeezed to the next line, and if the previous element is a standard flow, the top of the A element is aligned with the bottom of the previous element.
If the DIV4 is also set to the left float, the effect is as follows
Let's analyze it, first look at DIV4, its previous element Div3 set the left float, so the top of the DIV4 and div3 the top of the alignment, and then see Div3,div3 the previous element Div2 also set the left float, so the top of Div3 and the top of the div2 to it, Now look at the previous element of Div2,div2 is DIV1, but Div1 does not set the Float property, which belongs to the standard flow, so the top of DIV2 is aligned with the bottom of the div1.
Maybe someone would think what to do if you want DIV4 to have a single line? This will need to use the CSS to clear the float, clear the floating keyword is clear, it has the following values
Let's see Div2 set the left floating phenomenon
Because the previous element of the Div3 Div2 set the left float, so Div3 occupies the element div2 space, because the div3 of the width and height of div2 is large, so Div3 is blocked by div2 part, we know that the standard flow elements are not set floating property, So need to clear out the div2 element floating to DIV3 caused by the impact, because Div2 is left floating, so for div3 clear left floating
The phenomenon and originally did not set the float, good, now Div2 and Div3 at the same time set the left floating, I think it should be easier to know how to let Div4 exclusive line.
Clearing floating other property values can be self-analogous and tested. With the above foundation, we can make the following more common page layouts.
1 <!DOCTYPE HTML>2 <HTMLLang= "en">3 <Head>4 <MetaCharSet= "UTF-8">5 <title>Common layouts</title>6 <styletype= "Text/css">7 . Head{Background-color:Red;Height:50px;width:500px;}8 . Sidebar{Background-color:Blue;Height:200px;width:100px;float: Left;}9 . Main{Background-color:Gray;Height:200px;width:400px;float: Left;}Ten . Foot{Background-color:Green;Height:30px;width:500px;Clear: Left;} One </style> A </Head> - <Body> - <Divclass= "Head">Head</Div> the <Divclass= "sidebar">Sidebar</Div> - <Divclass= "Main">Main</Div> - <Divclass= "Foot">Foot</Div> - </Body> + </HTML>View Code
CSS Learning Note 08 Floating