Css implements fixed width on the left bar and adaptive width on the right bar.

Source: Internet
Author: User

Example

The code is as follows: Copy code

<! Doctype html>
<Html lang = "zh-CN">
<Head>
<Meta charset = "UTF-8">
<Title> fixed width in the left column and absolute positioning method in adaptive mode in the right column </title>
<Style type = "text/css">
Body {
Margin: 0;
            }
# Nav {
Top: 0;
Left: 0;
Width: 230px;
Height: 600px;
Background: # ccc;
Position: absolute;
            }
# Main {
Height: 600px;
Margin-left: 230px;
Background: # 0099ff;
            }
</Style>
</Head>
<Body>
<Div id = "container">
<Div id = "nav">
Left Column
</Div>
<Div id = "main">
Right column
</Div>
</Div>
</Body>
</Html>

It looks nice, ..

Because the left column uses absolute positioning and is out of the document stream, there is a defect that the container cannot be opened when the height of the left column is greater than that of the right column, this defect does not affect the layout of the two columns, but if there is a bottom column under the two columns, the bottom column and the left column will overlap:

The code is as follows: Copy code

 

<! Doctype html>
<Html lang = "zh-CN">
<Head>
<Meta charset = "UTF-8">
<Title> fixed width in the left column and absolute positioning method in adaptive mode in the right column </title>
<Style type = "text/css">
Body {
Margin: 0;
            }
# Nav {
Top: 0;
Left: 0;
Width: 230px;
Height: 600px;
Background: # ccc;
Position: absolute;
            }
# Main {
Height: 400px;
Margin-left: 230px;
Background: # 0099ff;
            }
# Footer {
Text-align: center;
Background: #009000;
            }
</Style>
</Head>
<Body>
<Div id = "container">
<Div id = "nav">
Left Column
</Div>
<Div id = "main">
Right column
</Div>
</Div>
<Div id = "footer">
Bottom Bar
</Div>
</Body>
</Html>

Let's take a look at the second method: fixed width on the left bar and adaptive negative margin method on the right bar:

The code is as follows: Copy code

 

<! Doctype html>
<Html lang = "zh-CN">
<Head>
<Meta charset = "UTF-8">
<Title> fixed width in the left column and adaptive negative margin method in the right column </title>
<Style type = "text/css">
Body {
Margin: 0;
            }
# Container {
Margin-left: 230px;
_ Zoom: 1;
/* The left column of IE6 compatibility disappears */
            }
# Nav {
Float: left;
Width: 230px;
Height: 600px;
Background: # ccc;
Margin-left:-230px;
Position: relative;
/* The left column of IE6 compatibility disappears. IE6 is really hard to worry about. >_< */
            }
# Main {
Height: 600px;
Background: # 0099ff;
            }
</Style>
</Head>
<Body>
<Div id = "container">
<Div id = "nav">
Left Column
</Div>
<Div id = "main">
Right column
</Div>
</Div>
</Body>
</Html>

In this way, no matter how the height of the two columns changes, there will be no problem:

The code is as follows: Copy code

 

<! Doctype html>
<Html lang = "zh-CN">
<Head>
<Meta charset = "UTF-8">
<Title> fixed width in the left column and adaptive negative margin method in the right column </title>
<Style type = "text/css">
Body {
Margin: 0;
            }
# Container {
Margin-left: 230px;
_ Zoom: 1;
/* The left column of IE6 compatibility disappears */
            }
# Nav {
Float: left;
Width: 230px;
Height: 600px;
Background: # ccc;
Margin-left:-230px;
Position: relative;
/* The left column of IE6 compatibility disappears. IE6 is really hard to worry about. >_< */
            }
# Main {
Height: 400px;
Background: # 0099ff;
            }
# Footer {
Clear: both;
Text-align: center;
Background: #009000;
            }
</Style>
</Head>
<Body>
<Div id = "container">
<Div id = "nav">
Left Column
</Div>
<Div id = "main">
Right column
</Div>
</Div>
<Div id = "footer">
Bottom Bar
</Div>
</Body>
</Html>

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.