Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Same padding at start and end of each line

I have a span element with some text in it. I want the text to have a solid background color as well as start/end each line with the same amount of padding.

This jsfiddle shows the problem: http://jsfiddle.net/VwsQg/

I found this very similar question: Add padding at the beginning and end of each line of text, but i need some space between each line as well.

This image illustrates the result I'm trying to accomplish:

enter image description here

Thank you in advance.

like image 323
Martin Avatar asked Sep 08 '12 12:09

Martin


People also ask

What is the order of padding?

When three values are specified, the first padding applies to the top, the second to the right and left, the third to the bottom. When four values are specified, the paddings apply to the top, right, bottom, and left in that order (clockwise).

What is padding inline?

The padding-inline CSS shorthand property defines the logical inline start and end padding of an element, which maps to physical padding properties depending on the element's writing mode, directionality, and text orientation.

When using the padding property are you allowed to use negative values?

The padding property in CSS defines the innermost portion of the box model, creating space around an element's content, inside of any defined margins and/or borders. Padding values are set using lengths or percentages, and cannot accept negative values. The initial, or default, value for all padding properties is 0 .

What is the CSS property that changes the amount of spacing within an element?

Padding is used to create space around an element's content, inside of any defined borders.


1 Answers

This can be done without using JavaScript or an extra element for each line.

HTML

<span class="marker"><span class="marker"><span class="marker">
    consectetur adipiscing elit. Nam at sem eu ligula porttitor iaculis volutpat
    non lacus.
</span></span></span>

CSS

.marker {
    background: #f77;
    padding: 3px 0;
    position: relative;
    left: -10px;
    line-height: 30px;
}

.marker .marker { left: 20px; }

.marker .marker .marker { left: -10px; }

See how it works on fiddle 3tP8m.

Note: An ancestor of .marker element should have proper padding to contain this element correctly.

All credits of this technic goes to Artem Polikarpov. See his original advice: “How to mark up the text on flexible bottom layer” (in Russian).

like image 183
Pavlo Avatar answered Oct 16 '22 13:10

Pavlo