Web Front-end interview guidance (19th): CSS style-how to clear element floating ?, Interview guidance css
Question comment
This is a frequently asked question and a frequently encountered problem in web design. The interviewer hopes to use this interview question to understand your basic skills in web design, if such a question cannot be answered, the interviewer will be disappointed. The probability of a successful interview is very small.
Q & A ideas
You can first answer the commonly used clearing methods at work and explain why you use them. Then, you can explain some other clearing methods to illustrate your broad thinking and rich knowledge. The floating mode is as follows:Four Types.
1. clear floating with clear: both
Example 1: Use div |
|
Html code |
Css code |
<Div class = "box"> <Div class = "div"> </div> <Div class = "clear"> </div> </Div> |
. Box {width: 300px; margin: 0 auto; border: 10px solid #000 ;} . Div {width: 200px; height: 200px; background: red; float: left ;} . Clear {height: 0px; font-size: 0; clear: both; overflow: hidden ;} |
Example 2: Use <br clear = "all"> |
<Div class = "box"> <Div class = "div"> </div> <Br clear = "all"/> </Div> |
. Box {width: 300px; margin: 0 auto; border: 10px solid #000 ;} . Div {width: 200px; height: 200px; background: red; float: left ;} |
Example 3: pseudo-Class Object: after + zoom: 1 (recommended) |
<Div class = "box clear"> <Div class = "div"> </div> </Div> |
. Box {margin: 0 auto; border: 10px solid #000 ;} . Div {width: 200px; height: 200px; background: red; float: left ;} . Clear {zoom: 1 ;} . Clear: after {display: block; clear: both; content: ""; visibility: hidden; height: 0} |
2. Use the overflow attribute
Html code |
Css code |
<Div class = "box"> <Div class = "div1"> </div> </Div> |
. Box {width: 300px; border: 1px solid #000;Overflow: auto;} . Div1 {width: 260px; height: 400px; background: Red; float: left ;} Note:Overflow: auto;,Overflow: hidden;Yes |
|
|
|
|
3. Use the display attribute
Html code |
Css code |
<Div class = "box"> <Div class = "div"> </div> </Div> |
. Box {width: 300px; margin: 0 auto; border: 10px solid #000; display: inline-block ;} . Div {width: 200px; height: 200px; background: red; float: left ;} Note: The parent element cannot be horizontally centered. Use text-align: center for the parent element. |
|
|
|
|
4. Parent-level element floating
Html code |
Css code |
<Div class = "box"> <Div class = "div"> </div> </Div> |
. Box {width: 300px; margin: 0 auto; border: 10px solid #000; float: left ;} . Div {width: 200px; height: 200px; background: red; float: left ;} Note: The parent element cannot be horizontally centered and can be located. Position: relative; Left: 50%; Margin-left:-150px; |
|
|
|
|