问题
I want to make my inner div 100% width of the body, not 100% of the parent div. Is this possible?
The layout looks like this:
<body>
<div> /** Width:900px; **/
<div> /** This I want 100% of BODY, not of parent div **/
</div>
</div>
</body>
回答1:
i hope you are looking like this........... see the DEMO
UPDATED DEMO 2 AS PER YOUR CURRENT REQUIREMENTS
CSS
.parent {
background:red;
width:900px;
margin:0 auto;
padding:10px;
-moz-box-sizing:border-box;
-webkit-box-sizing:border-box;
box-sizing:border-box;
}
.inner {
background:green;
height:100px;
position:absolute;
left:0;
right:0;
}
.child {
height:100px;
background:black;
margin:10px 0;
}
-------------**
Second Answer with without positioning but with a some trick what i used here so please check it the code & demo mentioned below :-
HTML
<body>
<div class="parent"> /** Width:900px; **/
<div class="child"></div>
</div>
<div class="inner"> /** This I want 100% of BODY, not of parent div **/</div>
<div class="parent">
<div class="child"></div>
<div class="child"></div>
</div>
</body>
CSS
.parent {
background:red;
width:900px;
margin:0 auto;
padding:10px;
-moz-box-sizing:border-box;
-webkit-box-sizing:border-box;
box-sizing:border-box;
}
.inner {
background:green;
height:100px;
}
.child {
height:100px;
background:black;
margin:10px 0;
}
DEMO
回答2:
you can use vh an vw units
.parent {
width: 900px;
height: 400px;
background-color: red;
}
.child {
width: 100vw;
height: 200px;
background-color: blue;
}
<html>
<body>
<div class="parent">
<div class="child">
</div>
</div>
</body>
</html>
回答3:
Consider changing your layoiut to something like the following:
http://jsfiddle.net/KpTHz/
Then you can just apply ID tags to DIVs you want to apply specific rules to.
<div class="outer">
<div class="inner">HEADER</div>
</div>
<div class="outer">
<div class="inner">CONTENT</div>
</div>
<div class="outer">
<div class="inner">FOOTER</div>
</div>
.outer {
width:100%;
background:#ccc;
}
.inner {
width:920px;
background:#999;
margin:0 auto 20px;
padding:20px;
}
回答4:
I think what you are asking for isn't possible. Instead you should consider rethinking your layout. I often find myself doing stuff like this:
html:
<div id="top">
<div class="wrapper"></div>
</div>
<div id="content">
<div class="wrapper"></div>
</div>
<div id="footer">
<div class="wrapper"></div>
</div>
css:
#top {
background: red;
}
#content {
background: orange;
}
#footer {
background: yellow;
}
.wrapper {
width: 860px;
margin: 0 auto;
padding: 20px;
background: rgba(255, 255, 255, 0.2);
}
demo - http://jsfiddle.net/bxGH2/
回答5:
This made the trick. A jQuery script:
$(document).ready(function () {
var width = $(window).width();
$('.pane-block-8').attr('style', 'width:' + width + 'px; left:-26.5% !important;');
});
$(window).resize(function () {
var width = $(window).width();
$('.pane-block-8').attr('style', 'width:' + width + 'px; left:-26.5% !important;');
});
回答6:
Overriding the min-width ( min-width:100% ) stopped the container from growing to the size of the contents.
Details: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/fieldset informs: "Unlike almost any other element, the WHATWG HTML Rendering spec suggests min-width: min-content as part of the default style for , and many browsers implement such styling (or something that approximates it)."
来源:https://stackoverflow.com/questions/13511249/make-div-inside-parent-100-width-of-body-not-parent-div