Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS padding automatic linebreak

I've following html:

<div class="container">
  <div class="header">
    <h1>
        a very long long long, really very very long headline ...
    </h1>
  </div>
</div>

and following css:

.container{
  width:200px;
}

.header h1{
  background-color: #e0e0e0;
  display: inline;
  padding: 2px 8px;
  color: black;
  font-size: 16px;
  font-weight: normal;
  line-height: 30px;
}

The problem is, that the browser adds a linebreak because of the long header and small width of the wrapping container. Thats fine. But the padding-left will not be added to the broken second and further lines. I can do this with an negative text-indent and positive padding-left to .header. but the background-color will not be moved to left so it seems as would be there still a zero padding.

How can I change this? Any trick available?

Great greetings, Falk

like image 618
Falk Avatar asked Sep 29 '14 11:09

Falk


People also ask

How do you automatically break lines in CSS?

word-wrap: break-word; is the current one. white-space: pre-wrap; is instead for something different! It preserves all whitespaces and linebreaks are ONLY done at \n (a linebreak in the source), <br> and as necessary to fill line boxes.

How do you prevent line breaks?

Use white-space: nowrap; or give that link more space by setting li 's width to greater values. I prevented line break in li items using display: inline; . Maybe this will also help others with similar problems. Its important to be careful with display: inline as it can have side effects.

How do I make a div break line?

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


1 Answers

Try changing display: inline; to display: inline-block;.

As I understand it, left and right padding on inline elements will be applied to the beginning and end of the element, regardless of whether there's any line break in between. On a block (or inline-block) element, the padding is applied to the left and right of the entire element.

See https://developer.mozilla.org/en-US/docs/Web/CSS/display for a lot more information!

like image 141
Olly Hodgson Avatar answered Sep 19 '22 10:09

Olly Hodgson