Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set paragraph width automatically in CSS?

Tags:

Here's the deal. Instead of just underlining my paragraph and using text-align, I want to add a dotted border underneath and center it inside the paragraph's parent div. This is my code that is not working. (It adds the border across the whole div instead of just the paragraph)

p {   border-bottom:1px dotted;   margin-left:auto;   margin-right:auto;   text-align:center; } 

The paragraph seems to be taking on the width of the parent div. Is there a way to set the paragraph's width to the text it contains? (It seems that margin:auto would work if possible.)

like image 226
user2020058 Avatar asked Jun 18 '13 17:06

user2020058


People also ask

How do I set auto width in CSS?

Using width, max-width and margin: auto; Then, you can set the margins to auto, to horizontally center the element within its container. The element will take up the specified width, and the remaining space will be split equally between the two margins: This <div> element has a width of 500px, and margin set to auto.

How do you control paragraph width in CSS?

To set a specific width for Paragraph element using CSS, set width property with required value for the selected paragraph elements in styles(inline/external).

What is CSS width Auto?

When an element has auto as a value for width, it can have margin, padding, and border without becoming bigger than its parent element. The width of its content box will be the content itself with the subtraction of margin, padding, and border.

How do I fix the width in CSS?

To convert it to a fixed-width layout, simply add a fixed with to the #wrapper and set the margins to auto. Setting the margins to auto will cause the left and right margins to be equal no matter how wide the browser window is, which will cause your fixed-width layout to be positioned in the center of the browser.


Video Answer


1 Answers

Paragraphs are going to expand to the width of their containers. To make it not do that, you could try:

p { display: inline-block; } 

Fiddle with example: http://jsfiddle.net/HuFZL/1/

Also, you may want to wrap the p tags in an additional div, if you need them to clear other paragraphs/elements.

like image 115
SlightlyCuban Avatar answered Sep 18 '22 08:09

SlightlyCuban