height: 100%
内の要素で使用すると、高さが固定されdiv
ていない限り機能しないことがわかりました。div
ここにフィドルがあります。
HTML
<div id='header' class='bodyRect'>
<div id='welcome'>
<h1>This is a welcome message</h1>
<p>Welcome to this website.</p>
<p>It is websitey.</p>
<p>Click <a id='toggle' href='#'>here</a> to toggle fixed vs 100% height to see the problem.</p>
</div>
<div id='logo'></div>
<span class='clearfix'></span>
</div>
CSS
body {
text-align: center;
}
.bodyRect {
border: 1px solid black;
padding: 10px;
margin: 10px;
}
#welcome {
width: 70%; float: left
}
#logo {
width: 30%; float: right;
background-color: red;
height: 100%;
}
.clearfix {
display: block;
clear: both;
visibility: hidden;
line-height: 0;
height: 0;
}
JS (テスト用。固定高と非固定高を切り替えるので、問題を確認できます)
var fixedHeight = false
document.getElementById('toggle').onclick = function(e) {
e.preventDefault()
fixedHeight = !fixedHeight
var newHeight = fixedHeight ? '500px' : '100%'
alert('height is now ' + newHeight)
document.getElementById('logo').style.height = newHeight
}
フィドルとこのコードでわかるように、親が固定属性を持っている場合div
にのみ、子は親を埋めるために展開されます。div
div
height
ただし、固定の高さは使用したくありません。親の高さdiv
を変更できるようにしたい。div
親で固定の高さを使用せずに、親を埋めるために子を拡張するにはどうすればよいですか?