Translated from equal height columns with cross-browser CSS and no hacks by Matthew James Taylor, some of them have been changed based on my understanding, so that some beginners can better understand them.
It is not easy to create a high-profile multi-column layout with pure CSS. This tutorial will focus on analyzing multiple problems that occur in the Multi-column layout, and then provide you with a simple solution for the whole browser, do not use images, scripts, CSS hacks, and pass verification in the strictest XHTML specifications.
The crux of the problem
As shown in, the background height is not uniform due to inconsistent content of each column. The background is actually the content (the standard box model is composed of the content area , Whitelist Area , Border Area And Boundary And the background exists in the first three; IE is the first two. Then, how can we increase the height of those shorter columns so that the height of all columns is equal to the height of the highest column? This is really tricky, because we don't know the height of each column, and we don't know which column is the highest. We cannot simply give all columns a clear height, which may lead to a large margin of white space for all columns, or the lack of height will lead to the failure to display all the content of some columns! In reality, the content length is dynamic, so the height of each column is dynamic. We must realize that there is no "fixed" word in the network. The resolution of each person's display is different, and the browser font size settings are also different, which will affect the display height of the content.
Separate content from background
The first step to Solving High problems is to split it into two smaller ones that can be solved separately. In this way, we use a column to correspond to a DIV, and now a column to correspond to a DIV, a content to be loaded, and a background to be displayed. This separation helps us to control each element and combine them in another more effective way, which will soon be described in the following chapter.
Use floating nesting to solve the problem of the highest column height.
This is the key to solving multiple columns. The only way to make the height of a div equal to the height of the highest column is to make the DIV contain all columns. In other words, by placing all columns in a container, the height of the container is the height of the highest column. This is a very useful structure.
Structure layer of three-column single row Layout Code .
<Div id = "container1"> <Div id = "col1"> column 1 </div> <Div id = "col2"> Column 2 </div> <Div id = ""col3"> column 3 </div>
Corresponding presentation layer code.
# Container1 {float: Left; width: 100% ;}# col1 {float: Left; width: 30%; Background: Red ;}# col2 {float: Left; width: 40%; background: yellow ;}# col3 {float: Left; width: 30%; Background: green ;}
In order to use this structure to work normally in all the browsers, we must make the outer container move to the left or right, And the DIV containing each column of the child element also floats, it doesn't matter which direction. Float uses them to align up to the peripheral container for whitelist, and the floating container automatically adjusts its height and width to include floating child elements. However, if the container is defined with the height and width, it does not automatically adjust its display area to adapt to the display of child elements. However, in IE 6 and earlier versions, sub-floating elements can support peripheral elements. Fortunately, IE7 and IE8 have corrected this non-conforming approach.
Add a container to display the background
The next step is to add additional containers so that they can be nested with each other, and make the number of all peripheral containers (added with the original one) equal to the total number of columns: 3. These three containers are used to display the background of each column. Note that we have removed the background of the original columns and moved them to these containers.
The new structure layer Code allows new containers to be nested on the original peripheral containers!
<Div id = "container3"> <Div id = "container2"> <Div id = "container1"> <Div id = "col1"> column 1 </div> <Div id = "col2"> Column 2 </div> <Div id = "col3"> column 3 </div>
New Presentation Layer Code, new containers are all floating elements!
All elements float left to set the container width to 100% so that they can fill the page width. The background color is removed from the content Div and added to the container. Note: In the original article, the width of all containers is set to 100%. In fact, this is not necessary because in floating nesting, the external container must be able to accommodate the size of internal sub-elements!
# Container3 {float: Left;/* width: 100%; */Background: green;} # container2 {float: Left;/* width: 100%; */Background: yellow ;} # container1 {float: Left; width: 100%; Background: Red ;}# col1 {float: Left; width: 30% ;}# col2 {float: Left; width: 40% ;} # col3 {float: Left; width: 30% ;}
Move containers with relative positioning
Now, all containers are perfectly tired and show only one background color. We must also display other backgrounds. Therefore, we need to use relative positioning to place these containers like a ladder. This can use the negative boundary technology, but the damn ie5.5 box model is inconsistent with W3C. To be compatible, we have to use CSS hack. As this article does not use CSS hack, give up. First, move # container2 to the right of 30%. Because # container2 is packed with # container1, and # container1 is mounted with the three columns, the rightmost column is removed from the right of the browser. The container # container3 at the bottom of the most peripheral layer is exposed, and the container 30% on the left is exposed. Then we move the upper-layer container # container1 to the right and 40% to the right. So the second-layer container that was originally covered was exposed, and 40% to the left of the container was displayed, while # container1 only has 100%-30%-40% = 30%; the top column only has the rightmost column, but it is displayed as the leftmost column. In this way, the background is displayed as normal!
Corresponding Presentation Layer Code
# Container3 {float: Left;/* width: 100%; * // * unnecessary code is commented out! */Background: green;} # container2 {float: Left;/* width: 100%; * // * unnecessary code is commented out! */Background: yellow; position: relative; Right: 30% ;}# container1 {float: Left; width: 100%; Background: red; position: relative; Right: 40% ;} # col1 {float: Left; width: 30% ;}# col2 {float: Left; width: 40% ;}# col3 {float: Left; width: 30% ;}
Move the content of each column back to the original position
Since the top three columns are all rounded to the right (PAGE) 70%, we can move them all back to the left to 70%! Finally, because the first two background containers are moved to the right and removed from the browser, they are all connected to the right of the browser, we can set the overflow of the bottom-layer background container to hide the removed parts.
# Container3 {float: Left;/* width: 100%; * // * unnecessary code is commented out! */Background: green; overflow: hidden; position: relative;} # container2 {float: Left;/* width: 100%; * // * unnecessary code is commented out! */Background: yellow; position: relative; Right: 30% ;}# container1 {float: Left; width: 100%; Background: red; position: relative; Right: 40% ;} # col1 {float: Left; width: 30%; position: relative; left: 70%; }# col2 {float: Left; width: 40%; position: relative; left: 70% ;} # col3 {float: Left; width: 30%; position: relative; left: 70% ;}
Add padding to a column)
Finally, we add a whitelist to make each column look more generous, rather than filling it up. However, if we add a whitelist, there may be some browsers that are ill and wrong. In IE 6 and earlier versions, the weird box model will produce some results that we don't want to see. For example, a box with PX width and 20 PX padding is regarded as PX width in IE, and PX is correct in other browsers. The padding should be added to the width of the element.
But rest assured that we can solve this problem completely without relying on padding. On the contrary, we narrow the columns a bit (the column width minus the padding on both sides), and then use relative positioning to move them to the correct position. For example, if we use a 2% complement, the 30% columns will be reduced to 26%, And the 40% columns will be reduced to 36%. When moving back columns with relative positioning, you need to remember that the columns are now narrow, so when they move left as originally, each one needs to move farther away than the previous one.
Complete CSS
In order to keep the layout in a small width, I added overflow: hidden in each content column. This will cut the stuff beyond the column width and prevent it from interfering with other la S. I reiterate that this is only an issue with IE. All other browsers will maintain a correct layout, regardless of the Xiami in the column. If you really want to do this, you can use the IE condition annotation to write rules only for IE.
# Container3 {float: Left; Background: green; overflow: hidden; position: relative;} # container2 {float: Left; Background: yellow; position: relative; Right: 30% ;} # container1 {float: Left; width: 100%; Background: red; position: relative; Right: 40% ;}# col1 {float: Left; width: 26%; position: relative; left: 72%; overflow: hidden;} # col2 {float: Left; width: 36%; position: relative; left: 76%; overflow: hidden;} # col3 {float: left; width: 26%; position: relative; left: 80%; overflow: hidden ;}
<Meta content = "text/html; charset = UTF-8 "http-equiv =" Content-Type "> <meta http-equiv =" Page-Enter "content =" revealtrans (duration = 1.0, transition = 12) "> <meta http-equiv =" Page-Exit "content =" blendtrans (duration = 0.5) "> <br/> <style type =" text/CSS "># header {Background: # ebcc67 ;}# footer {Background: # a9ea00 ;}# container3 {float: left;/* width: 100%; */Background: green; overflow: hidden; position: relative ;} # Container2 {float: Left;/* width: 100%; */Background: yellow; position: relative; Right: 30%;} # container1 {float: Left; width: 100%; Background: red; position: relative; Right: 40% ;}# col1 {float: Left; width: 26%; position: relative; left: 72%; overflow: hidden ;} # col2 {float: Left; width: 36%; position: relative; left: 76%; overflow: hidden;} # col3 {float: Left; width: 26%; position: relative; left: 80 %; Overflow: hidden ;}</style> appengine caused great repercussions this week. It provides an easy-to-use method to run a web application written in Python. However, Ruby's <SPAN class = 'wp _ keywordlink '> community </span> does not need to look at Google's face. It already has a simple deployment solution. Let's look at two new solutions, both of which are built on the public basis used by rails developers. Column 1 <p> Heroku and morph labs are hosting providers of Ruby on Rails and provide a complete environment for running Ruby on Rails Applications. Compared with traditional publishers, they not only provide you with a server, but also provide interesting tools and interfaces to help you work more easily, this frees you from heavy installation, configuration, management, and server security. Both use Amazon's EC2 grid computing technology to run applications, so you don't have to worry about scalability and performance issues. </P> Column 2 <p> another important feature favored by Heroku and morph laboratories is painless deployment. It takes only a few minutes to deploy a new application: Let's take a look at Heroku's instant deployment demo and the 6-minute deployment demo of morph exchange. How easy is deployment. </P> column 3 <br/> the two bring such similarity. Infoq interviewed related personnel of the two projects and provided more information. </P> James Lindenbaum describes the unique features of Heroku: </P> <BLOCKQUOTE> <p> Heroku is undoubtedly the simplest deployment platform for Rails Applications. Simply put the code in and start and run it. No one can do this. Heroku handles everything, from version control to auto scaling collaboration (based on Amazon EC2 ). We provide a complete set of tools to develop and manage applications, whether through web interfaces or new extended APIs. </P> </BLOCKQUOTE> <p> An interesting feature of Heroku is the browser-based development environment. It is based on gyre's development experience. gyre's developer Adam Wiggins is also a member of Heroku. Therefore, if you want to quickly modify and append an application, you only need to edit the <SPAN class = 'wp _ keywordlink '> Source Code </span> of the running application in the browser, it is automatically deployed after modification. Of course, using the browser IDE is not the only way to access the Heroku service. You can also look at Heroku's API and external git access. </P> <p>
Run code
Okay,ArticleFor more examples, see the original author's website, or click the following link: 2 columns, 3 columns, 4 columns, and 5 columns.