Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to adjust div width with the siz of text inside it [duplicate]

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?

like image 923
Laura Avatar asked May 16 '20 16:05

Laura


People also ask

How do you auto adjust the div width according to content in it?

Using inline-block property: Use display: inline-block property to set a div size according to its content.

How do I keep my div the same size?

if you set width:500px; and height:300px; (or whatever size you want) the div should stay the same size. Also, you can set overflow:hidden; so that if whatever is in the div goes beyond those bounds, it is not shown, and does not resize the div. Save this answer. Show activity on this post.

How do I adjust text in a div?

Using CSS, you can center text in a div in multiple ways. The most common way is to use the text-align property to center text horizontally. Another way is to use the line-height and vertical-align properties. The last way exclusively applies to flex items and requires the justify-content and align-items properties.


2 Answers

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>
like image 101
praag Avatar answered Nov 15 '22 04:11

praag


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>
like image 36
Ifaruki Avatar answered Nov 15 '22 03:11

Ifaruki