Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS - apply padding to inline element across two lines

Tags:

html

css

I have to create a style to apply a background color and padding to a header element, resulting the following intended appearance (Photoshop mock-up):

My CSS is as follows (edited to show relevant rules)

h1 {
    color: #fff;
    line-height: 1.4;
    background: #41a293;
    padding: 2px 4px; 
    display:inline;
}

This produces the following result:

I get padding at the start of the element ('S'), and at the very end ('e'), but not where the text breaks. The break happens due to the width of it's parent DIV. This will happen often and is necessary.

Is there any way to ensure the element gets even padding at the points where the text breaks?

Many thanks Dave

EDIT - I should have also said that the text content will display via a CMS (Wordpress).

like image 940
Dave Avatar asked Aug 27 '11 14:08

Dave


People also ask

Does padding apply to inline elements?

When it comes to margins and padding, browsers treat inline elements differently. You can add space to the left and right on an inline element, but you cannot add height to the top or bottom padding or margin of an inline element.

How do you add padding to inline CSS?

Syntax. The padding-inline property may be specified with one or two values. If one value is given, it is used as the value for both padding-inline-start and padding-inline-end . If two values are given, the first is used for padding-inline-start and the second for padding-inline-end .

How do you put a space between two inline elements in HTML?

HTML Break (<br>) Tag If you want to prevent a line break between two words, use a non-breaking space. If you want to insert a line break, use the HTML break tag, written as <br>.

How do you specify padding for all 4 sides?

The syntax for the CSS padding property (with 1 value) is: padding: all; When one single value is provided, the padding value will apply to all four sides of the element (ie: top, right, bottom, left).


3 Answers

If you're okay with the effect only being visible in WebKit/Blink browsers, you should use

-webkit-box-decoration-break: clone;
box-decoration-break: clone;

Which does exactly what you want, like here: http://jsfiddle.net/Cnuzh/13/

like image 83
Lennart Schoors Avatar answered Oct 24 '22 21:10

Lennart Schoors


Inline elements do not support padding and margin well, so make it block or inline-block (which is not cross browser) Why are you making it inline btw?

like image 2
Pankaj Phartiyal Avatar answered Oct 24 '22 22:10

Pankaj Phartiyal


if you add

white-space:pre-wrap;

to your h1 it will look like this :

enter image description here

Still trying to figure a way to add the space before the (i) !

EDIT : Check this out : http://jsfiddle.net/ahmad/6qVVD/10/

--Still need a way to wrap the last word with jQuery--

Wrapping the last word with a span using jQuery

$('h1').each(function(index, element) {
    var heading = $(element), word_array, last_word, first_part;

    word_array = heading.html().split(/\s+/); // split on spaces
    last_word = word_array.pop();             // pop the last word
    first_part = word_array.join(' ');        // rejoin the first words together

    heading.html([first_part, ' <span>', last_word, '</span>'].join(''));
});

Working example : http://jsfiddle.net/6qVVD/12/

As you see it's working perfectly

like image 2
Ahmad Alfy Avatar answered Oct 24 '22 21:10

Ahmad Alfy