Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force single line of text in element to fill width with CSS

Tags:

html

text

css

I have a div element that holds one line of text. How can I get this text to fill the width 100%?

text-align:justify is only working on elements with several lines of text, and has no effect on the div with single line. Manually adjusting the letter-spacing and word-spacing is ugly and ineffective.

Are there any other alternatives?

like image 574
Alix Përry Avatar asked Jan 18 '14 01:01

Alix Përry


People also ask

How do I make text fit in one line in CSS?

If you want to limit the text length to one line, you can clip the line, display an ellipsis or a custom string. All these can be done with the CSS text-overflow property, which determines how the overflowed content must be signalled to the user.

How do I make text full width in CSS?

Just had to add text-justify: inter-character; to my styles, to have all chars across the full container width.

How do you make an element occupy full width?

If you set the width to 100% on the body element you will have a full page width. This is essentially equivalent to not setting a width value and allowing the default. If you want to use the body element as a smaller container and let the HTML element fill the page, you could set a max-width value on the body.

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

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


1 Answers

Try this:

div {   text-align: justify; }  div:after {   content: "";   display: inline-block;   width: 100%; } 

jsFiddle

Take a look here for more info.

like image 113
mutil Avatar answered Oct 11 '22 13:10

mutil