Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Line break (like <br>) using only css

Tags:

html

css

styling

Is it possible in pure css, that is without adding additional html tags, to make a line break like <br>? I want the line break after the <h4> element, but not before:

HTML

<li>   Text, text, text, text, text. <h4>Sub header</h4>   Text, text, text, text, text. </li> 

CSS

h4 {   display: inline; } 

I have found many questions like this, but always with answers like "use display: block;", which I can't do, when the <h4> must stay on the same line.

like image 740
Steeven Avatar asked Jun 07 '12 14:06

Steeven


People also ask

How do I break a line in CSS without br?

Use block-level elements to break the line without using <br> tag.

Can you style a BR in CSS?

Technically, yes, you can target a <br> element with CSS directly or by applying a class to it and targeting the class. That being said, a <br> generates a line-break and it is only a line-break. As this element has no content, there are only few styles that make sense to apply on it, like clear or position .

How do you put a line break in a div tag?

You can insert line breaks in HTML with the <br> tag, which is equivalent to a carriage return on a keyboard.


2 Answers

It works like this:

h4 {     display:inline; } h4:after {     content:"\a";     white-space: pre; } 

Example: http://jsfiddle.net/Bb2d7/

The trick comes from here: https://stackoverflow.com/a/66000/509752 (to have more explanation)

like image 169
adriantoine Avatar answered Sep 22 '22 04:09

adriantoine


Try

h4{ display:block;} 

in your css

http://jsfiddle.net/ZrJP6/

like image 36
Philip Kirkbride Avatar answered Sep 25 '22 04:09

Philip Kirkbride