如果将block
元素放在元素内,inline-block
则内联块将扩展以适应其中最宽的块。
<h1>Original Structure</h1>
<div id="latest-text1">
<p id="text-wrapper1">
<span style="display: inline-block;" id="title1">ABFDFDFJEKJRKEREJRKE</span>
<span style="display: inline-block;" id="subtitle1">GJKJGKEJKEJFKAJKEJRE</span>
</p>
</div>
<h1>Same, but using <code>block</code></h1>
<div id="latest-text2">
<p id="text-wrapper2">
<span style="display: block;" id="title2">ABFDF DFJEKJ RKER EJRKE</span>
<span style="display: block;" id="subtitle2">GJKJ GKEJKEJF KAJKEJRE</span>
</p>
</div>
<h1>Restructured as HTML5</h1>
<p>This also gets rid of inline styles, which are a leading cause of nightmares.</p>
<section id="latest-text3">
<header>
<hgroup>
<h1>ABFDF DFJEKJ RKER EJRKE</h1>
<h2>GJKJ GKEJKEJF KAJKEJRE</h2>
</hgroup>
</header>
<article>
<p> ... the ... first ... paragraph ... </p>
<p> ... the ... second ... paragraph ... </p>
</article>
</section>
不是绝对必要的<hgroup>
,但它确实具有最大的语义意义。
和 CSS
#latest-text1, #latest-text2, section#latest-text3 {
margin: 1em;
padding: 1em;
border: 1px solid black;
}
#latest-text1 {
background-color: #f0f0ff;
}
#latest-text2 {
background-color: #f0fff0;
}
section#latest-text3 {
background-color: #fff0f0;
}
#latest-text3 {
display: inline-block; /* THE IMPORTANT ONE */
}
大多数 CSS 只是为了帮助可视化整个事情,在http://jsfiddle.net/J3NzC/
通过使用适当的类,可以将 HTML5 重做为 HTML4 <div>
,例如使用<div class="section">
和<div class="hgroup">