简体   繁体   中英

CSS column layout

I'm going round in circles with a CSS layout. I basically want it like:

<-------><-------------->
         <------><------>
  400px    50%      50%

So its 3 colums, one fixed size, and the other two taking up 50% each of the remaining space. I cant seem to make the second and third take up 50% of the remaining space.

Any help would be greatly appreciated, thanks very much :)

I tried a couple of variations. Below works in Chrome 2, Firefox 3.5 and IE8:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>
<head>
<title>NLR</title>
<style type="text/css">
html, body, div { margin: 0; border: 0 none; padding: 0; }
div { height: 500px; border-collapse: collapse; }
#wrapper { padding-left: 400px; }
#nav { width: 400px; margin-left: -400px; float: left; background: yellow; }
#main { overflow: hidden; background: blue; }
#left { float: left; width: 50%; background: red; height: 300px; }
#right { float: right; width: 50%; background: green; }
</style>
</head>
<body>
<div id="wrapper">
<div id="nav"></div>
<div id="main">
  <div id="left"></div>
  <div id="right"></div>
</div>
</div>
</body>
</html>

the markup:

<div id="left">some content</div>
<div id="main">
    <div>more content</div>
    <div>still more content</div>
</div>

the css:

#left {
    float: left;
    width: 400px;
    margin-right: -405px; /* throwing in a little extra */
}

#main {
    margin-left: 405px; /* matching the margin of #left */
}

#main > div {
    width: 50%; /* may need to make it 49.9% for some browsers */
}

First of all it's always a good practice to WRAP your layout. In the following example:

+---------------BODY-----------------+
|<---DIV#1---><--------DIV#2-------->|
| <---DIV3--> | <--DIV4--><--DIV5--> |
| |         | | |        |         | |
| |         | | |        |         | |
| |         | | |        |         | |
| |         | | |        |         | |
| |         | | |__________________| |
| |_________| | |____CLEAR DIV_____| |
+-----------------------------------+
  • DIV0 : main-wrapper
  • DIV1 : sidebar-wrapper
  • DIV2 : content-wrapper
  • DIV3 : sidebar-content
  • DIV4 : content-left
  • DIV5 : content-right
  • CLEAR DIV: to clear the floats.

Your best bet would be to have it set up like so:

Wrappers are pretty important and make a lot of things much easier. To center things inside of wrappers, you can set margin left/right of the divs inside of the wrappers to "auto".

<div class="bigwrapper">
 <div class="400pxdiv">
  Content for 400pxdiv
 </div>
 <div class="rightwrapper">
  <div class="50percent1">
   50percent1's content
  </div>
  <div class="50percent2">
   50percent2's content
  </div>
 </div>
</div>

This works for me in Firefox.

<!DOCTYPE html>
<title>Test</title>
<style>
  #A { width: 400px; float:left; outline: thin solid pink; }
  #B { margin-left: 400px; overflow: hidden; outline: thin solid pink; }
  #B1, #B2 { width:50%; float:left; outline: thin solid pink; }
</style>
<div id=A>
  A
</div>
<div id=B>
  <div id=B1>
    B1
  </div>
  <div id=B2>
    B2
  </div>
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM