[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.