Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Width equal to content [duplicate]

Tags:

css

width

I'm experiencing some trouble with the width property of CSS. I have some paragraphs inside a div. I'd like to make the width of the paragraphs equal to their content, so that their green background looks like a label for the text. What I get instead is that the paragraphs inherit the width of the div father node which is wider.

#container {
  width: 30%;
  background-color: grey;
}

#container p {
  background-color: green;
}
<div id="container">
  <p>Sample Text 1</p>
  <p>Sample Text 2</p>
  <p>Sample Text 3</p>
</div>
like image 718
user3067088 Avatar asked Dec 04 '13 19:12

user3067088


People also ask

How do I make div width equal content?

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

Is width and height same?

Height: When a rectangle is drawn with horizontal and vertical sides, the word height makes it clear which dimension is meant; height labels how high (how tall) the rectangle is. That makes it easy to indicate the other dimension—how wide the rectangle is from side to side—by using the word width.

What is width max content?

The max-width property defines the maximum width of an element. If the content is larger than the maximum width, it will automatically change the height of the element. If the content is smaller than the maximum width, the max-width property has no effect.

How do you make div width expand with its content using CSS?

Syntax: width: length|percentage|auto|initial|inherit; Property Values: width: auto; It is used to set width property to its default value.


3 Answers

I set width as max-content and it worked for me.

width: max-content;

like image 164
Sarneet Kaur Avatar answered Oct 08 '22 09:10

Sarneet Kaur


By default p tags are block elements, which means they take 100% of the parent width.

You can change their display property with:

#container p {
   display:inline-block;
}

But it puts the elements side by side.

To keep each element on its own line you can use:

#container p {
   clear:both;
   float:left;
}

(If you use float and need to clear after floated elements, see this link for different techniques: http://css-tricks.com/all-about-floats/)

Demo: http://jsfiddle.net/CvJ3W/5/

Edit

If you go for the solution with display:inline-block but want to keep each item in one line, you can just add a <br> tag after each one:

<div id="container">
  <p>Sample Text 1</p><br/>
  <p>Sample Text 2</p><br/>
  <p>Sample Text 3</p><br/>
</div>

New demo: http://jsfiddle.net/CvJ3W/7/

like image 39
DaniP Avatar answered Oct 08 '22 09:10

DaniP


The solution with inline-block forces you to insert <br> after each element.
The solution with float forces you to wrap all elements with "clearfix" div.

Another elegant solution is to use display: table for elements.

With this solution you don't need to insert line breaks manually (like with inline-block), you don't need a wrapper around your elements (like with floats) and you can center your element if you need.

http://jsfiddle.net/8md3jy4r/3/

like image 14
Ruslan Stelmachenko Avatar answered Oct 08 '22 11:10

Ruslan Stelmachenko