Web Front-end interview guidance (19th): CSS style-how to clear element floating ?, Interview guidance css

Source: Internet
Author: User

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;

 

     

 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.