(Turn around) use CSS to implement a three-column adaptive layout (fixed width on both sides, middle adaptive)

Source: Internet
Author: User

The so-called three-column adaptive layout refers to both sides of the fixed width, the middle block width adaptive. This question in NetEase in this year to push front-end engineers interview when also asked. I am mainly divided into two categories, one is based on the implementation of the position tradition, a class is based on the CSS3 new feature elastic box model layout implementation.

1. Layout based on traditional attributes such as position and marginHere are also divided into three methods, respectively, the absolute positioning method, the Grail layout, self-floating method. 1). Absolute Positioning MethodThe principle of absolute positioning is to use the left and right sides of the absolute positioning, because the absolute positioning to make it out of the document flow, the back of the center will flow naturally to them, and then use the margin property, leaving the width of the left and right elements, so that the middle element can adapt to the screen width. The code is as follows: Document code:
[HTML]View Plain copy
  1. <! DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>layout_box</title>
  6. <link rel="stylesheet" type= "text/css" href=". /css/layout_box.css ">
  7. </head>
  8. <body>
  9. <h3> Implement three-column-width adaptive layouts </h3>
  10. <div id = "left"> I am </div>
  11. <div id = "right"> I'm on </div>
  12. <div id = "center"> I am the middle </div>
  13. </Body>
  14. </html>
CSS Code:
[CSS]View Plain copy
  1. html,body{ margin: 0px;   Width: 100%;}
  2. H3{height: 100px;   Margin:20px 0 0;}
  3. #left,#right {width: 200px;  Height: 200px; Background-color: #ffe6b8;   Position: absolute;top:120px;}
  4. #left {left:0px;}
  5. #right {right: 0px;}
  6. #center {margin:2px 210px; Background-color: #eee;   Height: 200px;}
The advantages of the method layout, three div order can be arbitrarily changed. Not enough because of absolute positioning, so if there is something else on the page, the top value needs to be handled with care, preferably a CSS style can be initialized, as in the above example added a title, if the style is not initialized, the values on both sides and the middle will be misaligned. In addition, compression occurs when the browser window is smaller than 200px.as a result, you can see that the middle column width scales with the screen size. 2). Use self-floating methodthe principle of self-floating method is to use Float:left and float:right,float respectively to leave the left and right two elements out of the document flow, the intermediate elements are normal in the normal document flow, using margin to specify the left and right margin to a positioning. HTML code: [HTML]View Plain copy
  1. <h3> Positioning

    >
  2. <div id = "left_self"> I'm left </div>
  3. <div id = "right_self"> I'm right </div>
  4. <div id = "center_self"> I am the middle </div>
CSS Code: [CSS]View Plain copy
  1. #left_self,#right_self { width: 200px;  Height: 200px; background-color: #ffe6b8}
  2. #left_self {float: left ;}
  3. #right_self {float: right ;}
  4. #center_self {margin: 0 210px;  Height: 200px; background-color: #a0b3d6}
the advantage of the layout method is affected by the outside, but the shortage is the order of three elements, the center must be placed at the end, which is not the same as the absolute location, center occupies the position of the document flow, so be sure to put in the last, left and right two element position is not related. When the browser window is small, the element on the right will be hit down one line. 3). Holy Grail layout the principle of the Grail layout is the margin negative method. With the Grail layout, you first need to include a div outside the center element, including a div that needs to set the Float property to form a BFC, and set the width to match the margin value of the left block, which is referenced here. This explains the Holy Grail layout in particular detail. implementation code: HTML Document:    [HTML]View Plain copy
  1. <h3> Using margin negative method for layout </h3>
  2. <div id = "wrap">
  3. <div id = "center"></div>
  4. </div>
  5. <div id = "left_margin"></div>
  6. <div id = "right_margin"></div>
CSS Code: [CSS]View Plain copy
  1. #wrap { width: 100%;  Height: 100px; Background-color: #fff;   float: Left ;}
  2. #wrap #center { margin:0 210px; height: 100px;   Background-color: #ffe6b8;}
  3. #left_margin,#right_margin { float: left ; Width: 200px; Height: 100px; Background-color:darkorange}
  4. #left_margin {margin-left: -100%; Background-color:lightpink}
  5. #right_margin {margin-left: -200px;}
This method is very common in the website layout, also is the interview frequently test center, the advantage is three columns interrelated, has certain resistance. It is important to note that the middle part of the layout must be placed in front, the left and right order is not limited. For left fast margin negative value must be equal to the width of wrap. three ways to achieve three columns of Web page width adaptive layout method see.

2, CSS3 new features

Wrap a div around the perimeter, set to Display:flex, flex:1 in the middle, but the box model is next to each other, you can use margin to control the margin.

Code:

[HTML]View Plain copy
  1. <div id = "box">
  2. <div id = "left_box"></div>
  3. <div id = "center_box"></div>
  4. <div id = "right_box"></div>
  5. </div>

CSS style:

[CSS]View Plain copy
  1. #box {width:100%;  Display:flex; height: 100px;   margin: 10px;}
  2. #left_box,#right_box {width: 200px;  Height: 100px; margin: 10px; Background-color:lightpink}
  3. #center_box {flex:1; height: 100px;  margin: 10px; Background-color:lightgreen}

Note: Center must be placed in the middle.

As follows:


There are many other features of the CSS layout, and the next step is to study the position, and the display properties.

(Turn around) use CSS to implement a three-column adaptive layout (fixed width on both sides, middle adaptive)

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.