親 div の 100% ではなく、ボディの 100% 幅の内部 div を作成したいと考えています。これは可能ですか?
レイアウトは次のようになります。
<body>
<div> /** Width:900px; **/
<div> /** This I want 100% of BODY, not of parent div **/
</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;
position:absolute;
left:0;
right:0;
}
.child {
height:100px;
background:black;
margin:10px 0;
}
------------- * *
ポジショニングなしの2番目の回答ですが、ここで使用したいくつかのトリックがあるので、以下のコードとデモを確認してください:-
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;
}
vh および vw ユニットを使用できます
.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>
これを試して:
.inner {
margin-left: -50vw;
left: 50%;
position: fixed;
height: 100vw;
width: 100vw;
top: 0;
}
.outer {
width: 900px;
/* your outer div style*/
}
<body>
<div class="outer"> /** Width:900px; **/
<div class="inner"> /** This 100% of BODY, not of parent div **/
</div>
</div>
</body>
あなたが求めていることは不可能だと思います。代わりに、レイアウトを再考することを検討する必要があります。私はよく次のようなことをしていることに気づきます。
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);
}
これはトリックを作りました。jQuery スクリプト:
$(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;');
});
レイアウトを次のように変更することを検討してください。
次に、特定のルールを適用する DIV に ID タグを適用するだけです。
<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;
}