[Css Interview Questions] The Three divs require horizontal alignment. The width of the left and right divs is fixed to PX, and the middle DIV is filled with the remaining width (at least two methods). div100px

Source: Internet
Author: User

[Css Interview Questions] The Three divs require horizontal alignment. The width of the left and right divs is fixed to PX, and the middle DIV is filled with the remaining width (at least two methods). div100px

This is a problem I encountered during an interview with a company. I didn't answer it !!

 

So you must pay attention to your friends !!

 

Change the browser width to see the effect:

Left and right

 

Then let's look at the Code:

Method 1: (floating)

<Style type = "text/css">. left ,. right ,. center {border: 1px solid; height: 100px; text-align: center; line-height: 100px; font-size: 50px ;}. left {float: left; width: 100px ;}. right {float: right; width: 100px ;}. center {margin: 0 100px ;} </style> <div class = "left"> left </div> <div class = "right"> right </div> <div class = "center"> medium </div>

 

Method 2: (absolute positioning)

<Style type = "text/css">. container {position: relative ;}. left ,. right ,. center {position: absolute;/* Add absolute positioning */top: 0; border: 1px solid; height: 100px; text-align: center; line-height: 100px; font-size: 50px ;}. left {left: 0; width: 100px ;}. right {right: 0; width: 100px ;}. center {width: auto; left: 100px; right: 100px ;} </style> <div class = "container"> <div class = "left"> left </div> <div class = "right"> right </div> <div class = "center"> medium </div>

 

Shortly after the release, a small partner commented on the third method below.

Method 3: (fiex)

Browser support

    

Currently, mainstream browsers do not support the box-flex attribute.

Internet Explorer 10 is supported by the-ms-flex private attribute.

Firefox is supported by the private property-moz-box-flex.

Safari and Chrome support webkit-box-flex through private properties.

Note:Internet Explorer 9 and earlier IE versions do not support elastic boxes.

<Style type = "text/css">. container {display:-moz-box;/* Firefox */display:-webkit-box;/* Safari and Chrome */display: box ;}. left ,. right ,. center {border: 1px solid; height: 100px; text-align: center; line-height: 100px; font-size: 50px ;}. left {width: 100px ;}. right {width: 100px ;}. center {-moz-box-flex: 1.0;/* Firefox */-webkit-box-flex: 1.0;/* Safari and Chrome */box-flex: 1.0 ;} </style> <div class = "container"> <div class = "left"> left </div> <div class = "center"> medium </div> <div class = "right"> right </div>

 

Of course there are still many methods. If you have any better method, I hope you can comment it out below and we will all learn it together.

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.