5

My idea is to make each element within divs that have the .main_content class, to have a width equal to the width of the text inside it. How do I this?

As you can see, the width of each one (span, h2, p and h6) is the same as the .main_content div. The red border demonstrates this.

So, how do I make the width of each div fit the text within those divs? (With only 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: I've been trying to manually set the size of width of each element. But there is too much code. Is there any clever way to to this?

SherylHohman
  • 16,580
  • 17
  • 88
  • 94
Laura
  • 675
  • 10
  • 32

5 Answers5

3

You can try with below css and see if it works.

.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>
G-Cyrillus
  • 101,410
  • 14
  • 105
  • 129
praag
  • 31
  • 2
1

Just 1 CSS function: width: fit-content Note here: It doesnt work in IE. Try it out:

.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>
bill.gates
  • 14,145
  • 3
  • 19
  • 47
1

Use flexbox on .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>
Láďa Durchánek
  • 1,199
  • 8
  • 16
1

You can simply use:

width: max-content;

for all the child elements and this will set the with for of each one (span, h2, p and h6) width of the text inside it.

.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>
palaѕн
  • 72,112
  • 17
  • 116
  • 136
1

add width: max-content to your .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>
dan
  • 81
  • 4