开发者

100% expandable container div with outside margin

i have a mind bobbling question.

I need a 100% width, 100% height container with 20px margin that expands with content. Is this at all possible? The closest i got was with this method:

#container {
    position:absolute;
    top:0;
    bottom:0px;
    left:0;
    right:0;
    margin:20px;
}

but then it 开发者_Go百科wont expand in height with content.

Anybody know the divine technique for this?


I'm pretty sure it isn't possible to do with a single element, but if you don't mind having 3 spacer div elements, this solution works in all browsers:

<html>
<head>
<style type="text/css">
* {
    margin: 0;
}
html, body {
    height: 100%; padding: 0; /* padding 0 is for jsfiddle */
}
#wrapper {
    min-height: 100%;
    height: auto !important;
    height: 100%;
    margin-left: 20px;
    margin-right: 20px;
    margin-bottom: -20px; /* the bottom margin is the negative value of the spacer height */
    background-color: #ccc;
}
.spacer.edge {
    background-color: white; /* same as body background */ 
}
.spacer {
    height: 20px;
 }

</style>
</head>
<body>
    <div id="wrapper">
        <div class="spacer edge"></div>
        <!-- content here -->
        <div class="spacer"></div>
    </div>
    <div class="spacer edge"></div>
</body>
</html>

Here's a fiddle to play with: http://jsfiddle.net/dTyTW/


I you want to expand the div with the content , then you need to set position : relative and in order to stick towards the bottom padding-bottom also need to be set.

#container {
position:relative;
top:20px;
bottom:20px;
left:20px;
right:20px;
padding-bottom: 80%;
border:1px solid red;

}

Values can be adjusted as per the requirement. Try here


Remove the margin and give each position a 20px.

Also remove the bottom.

Add padding-bottom:20px;

#container {
    position:absolute;
    top:20px;
    left:20px;
    right:20px;
    padding-bottom:20px; 
}

http://jsfiddle.net/jasongennaro/w7dQP/3/

EDIT

If you are not opposed to using some jQuery, you could also do this

var h = $(document).height();
var h2 = $('#container').height();

if(h2 < h){
    $('#container').height(h);
}

This ensures that if the div is smaller than the browser viewport, it will expand to fit it.

Once the text is as big or bigger, the styles will take care of it.

http://jsfiddle.net/jasongennaro/w7dQP/8/


<html>
<style>
body
{
  margin:0px;
}
div
{
  position: absolute;
  padding: 20px;
}
img
{
  position: relative;
  width: 100%;
}
</style>
<body>
  <div>
    <img src="http://manual.businesstool.dk/screen.jpg" />
  </div>
</body>
</html>

Use the padding property... look up the box model.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜