Css implements two-column layout, one fixed width, one adaptive width, and two css columns.

Source: Internet
Author: User

Css implements two-column layout, one fixed width, one adaptive width, and two css columns.

Whether it is left or right, the width of one side is fixed, and the width of one side is adaptive.

Many topics in the blog Park are also designed in this way. My blog also has a fixed width on the right side and an adaptive screen layout on the left side.

Html code:

<Div id = "wrap"> <div id = "sidebar" style = "height: 500px; background: red; color: # fff; "> fixed width </div> <div id =" content "style =" height: 500px; background: #000; color: # fff; "> adaptive area </div>

There are several implementation methods:

1. fixed width area floating, adaptive area without width and set margin

The following uses the fixed width on the right side and adaptive width on the left side as an example:

Css code:

#sidebar {  float: right; width: 300px;}#content {  margin-right: 300px;}

Implementation:

The right side remains unchanged, and the left side adapts to the remaining size of the screen.

But in fact, this method has limitations, that is, in the html StructureThe sidebar must be earlier than the content.!

But I need sidebar after content! Because my content is the main content of the webpage, I don't want the main content to be placed behind the secondary content instead.

The first method described above is invalid.

The following method is required.

2. Use float with margin

First, adjust the html structure:

<Div id = "wrap"> <div id = "content" style = "height: 500px; background: #000; color: # fff; "> <div class =" contentInner "> adaptive area </div> <div id =" sidebar "style =" height: 500px; background: red; color: # fff; "> fixed width area </div>

Css code:

#content {  margin-left: -300px; float: left; width: 100%;}#content .contentInner{  margin-left:300px;}#sidebar {  float: right; width: 300px;}

In this way, the actual width of contentInner is-300px.

3. Use absolute positioning for fixed-width areas, and set margin for adaptive areas.

Html structure:

<Div id = "wrap"> <div id = "content" style = "height: 500px; background: #000; color: # fff; "> my current structure is in front </div> <div id =" sidebar "style =" height: 500px; background: red; color: # fff; "> fixed width area </div>

Css code:

#wrap{  position:relative;}#content {  margin-right:300px;}#sidebar {  position:absolute;  width:300px;  right:0;  top:0;}
4. Use display: table to implement

Html structure:

<Div id = "wrap"> <div id = "content" style = "height: 500px; background: #000; color: # fff; "> my current structure is in front </div> <div id =" sidebar "style =" height: 500px; background: red; color: # fff; "> fixed width area </div>

Css code:

#wrap{  display:table;  width:100%;}#content {  display:table-cell;}#sidebar { width:300px;  display:table-cell;}

Of course, the last method is incompatible with IE7 and the following browsers, because IE7 does not recognize display as a table.

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.