简体   繁体   中英

Percent vs. pixels in fluid layout

I would like to build a fluid layout and would like to achieve something like

width:100%-200px;

ie have a div with content, call it div id="content" with a fixed margin on either side. I have tried to use the trick of putting the div id="content" into another div container with a margin, but I don't know how to fill out the background of div id="content" . Is there a way of telling the div id="content" to use 100% of the available space as background, such that the width of the content plus the width of the margin does not exceed 100% of the browser window size?

Having the DIV set to be 100% with a margin of XXX on either side won't work as that will exceed the size of the browser window.

You could try the following:

body {
    padding:0 2%;
}

#content {
    width:96%;
}

http://jsfiddle.net/YYhvT/

Use position absolute...

#content {
  position: absolute;
  left: 0;
  right: 200px;
}

See my Fiddle .

PS Advantage is that you don't need values on other elements.

You can put a container around the content and give it 200px left/right padding. This should do the trick (at least, from what I understand of what you are trying to accomplish). Also see this code example:

<!doctype html>
<html>
<head>
    <style type="text/css">
        body { margin: 0 50px; }
        #container { padding: 0 200px; background: #FF0000; }
        #content { width: 100%; background: #00FF00; }
    </style>
</head>
<body>
    <div id="container">
        <div id="content">
            Here goes my content
        </div>
    </div>
</body>
</html>

Note that the body margin is just for illustrating purposes, to let you see the background differences.

(I would post a jsFiddle, however I am not able to use it since I can only use IE7 at this point.)

here is my solution, html:

<div id="content" class="content">
  My Content
</div>​

css:

.content {
  position: absolute;
  right: 100px;
  left: 100px;
  background-color:#A5112C;
}​

and link to it: http://jsfiddle.net/MPYHs/

also if you want to put sort of image as a background I suggest you use small pattern like https://ssl.gstatic.com/android/market_images/web/background_stripes.gif

hope it helps,

regards

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