为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用其中的文本大小调整div宽度

如何使用其中的文本大小调整div宽度

撒科打诨 2022-08-18 15:54:16
我的想法是使s中具有类的每个元素的宽度等于其中文本的宽度。我该怎么做?div.main_content如您所见,每个 (和 ) 的宽度与 .红色边框说明了这一点。spanh2,ph6.main_contentdiv那么,如何使每个 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:我一直在尝试手动设置每个元素的大小。但是代码太多了。有什么聪明的方法可以做到这一点吗?widt
查看完整描述

5 回答

?
阿波罗的战车

TA贡献1862条经验 获得超6个赞

你可以尝试使用下面的css,看看它是否有效。


.main_content {

  width: 100%;

  height: 400px;

  font-weight: 800;

}


.main_content>* {

  border: 1px solid red;

  display: table;

  margin: 0 auto;

  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>


查看完整回答
反对 回复 2022-08-18
?
慕妹3146593

TA贡献1820条经验 获得超9个赞

只有1个CSS函数:注意:它在IE中不起作用。试试看:width: fit-content


.main_content {

   width: 100%;

  height: 400px;

  font-weight: 800;


}


.main_content > * {

  border: 1px solid red;

  display: block;

  margin-bottom: 30px;

  text-align: center; 

  width: -moz-fit-content;

  width: fit-content;

}

<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>


查看完整回答
反对 回复 2022-08-18
?
红颜莎娜

TA贡献1842条经验 获得超12个赞

在以下位置使用 flexbox:.main_content


.main_content {

   width: 100%;

  height: 400px;

  font-weight: 800;

  display: flex;

  flex-direction: column;

  align-items: center;

}


.main_content > * {

  border: 1px solid red;

  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>


查看完整回答
反对 回复 2022-08-18
?
开心每一天1111

TA贡献1836条经验 获得超13个赞

添加到您的 .width: max-content.main_content > * {


.main_content {

   width: 100%;

  height: 400px;

  font-weight: 800;

}


.main_content > * {

  border: 1px solid red;

  display: block;

  margin-bottom: 30px;

  text-align: center;

  width: max-content;

}

<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>


查看完整回答
反对 回复 2022-08-18
?
鸿蒙传说

TA贡献1865条经验 获得超7个赞

您只需使用:


width: max-content;

对于所有子元素,这将设置其中文本的每个(,,和)宽度的with。spanh2ph6


.main_content {

  width: 100%;

  height: 400px;

  font-weight: 800;

}


.main_content>* {

  border: 1px solid red;

  display: block;

  margin-bottom: 30px;

  text-align: center; 

  width: max-content;

}

<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>


查看完整回答
反对 回复 2022-08-18
  • 5 回答
  • 0 关注
  • 187 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号