我的想法是使带有 .main_content
类的 div
元素中的每个元素的宽度等于其内部文本的宽度。我该怎么做呢?
正如您所看到的,每个元素 (span
, h2,
p
和 h6
) 的宽度与 .main_content
div
的宽度相同。红色边框说明了这一点。
那么,我如何使每个 div
的宽度适应这些 div
中的文本呢?(只使用 CSS)
.main_content {
width: 100%;
height: 400px;
font-weight: 800;
}
.main_content > * {
border: 1px solid red;
display: block;
margin-bottom: 30px;
text-align: center;
}
<div class="first_article">
<div class="first_content">
<div class="main_content">
<span class="growth">Growth</span>
<h2>5 compelling user referral campaign examples</h2>
<p>Jumpstarts your user referral engine with these 5 approaches toreferral campaigns from popular apps.
</p>
<h6>Author</h6>
</div>
</div>
</div>
P: 我一直在尝试手动设置每个元素的width
大小。但是代码太多了。有没有什么聪明的方法可以做到这一点?
display:table
并且 支持 很广泛。 - Rainbow