Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid overlapping rows in inline element with a background color applied

Tags:

html

css

I got a CSS question related to this fiddle: http://jsfiddle.net/r584e/
Here the relevant screenshot

line height: 1em (left) and 0.8em (right)

Sometimes I've got to style an inline element in a such way, trying to almost avoid space between rows and applying a background only under the text. As you can see, the first paragraph has a link inside, in which I set line-height: 1em . The paragraph on the right has instead a line-height: 0.8em;. (Note: I know in this way I could roughly cut some letters - like q,g,p,... but the text is uppercase so it's not really a problem)

In the second paragraph rows are actually closer (as I want) but unfortunately each row is also partially overlapping the previous one (unless you remove the background color applied) and this is not good (e.g. see the word «uppercase» on the bottom), so here my questions:

  1. how can I get the rows closer (like paragraph on the right) without them overlapping each other and defining a background color (no matter the element in which it is applied but it has to stay under the text, not fill a whole block)
  2. Optionally there is a way to add an horizontal padding to each line of text?

Feel free to change the CSS and/or markup. I'm looking for a pure CSS workaround.
An optimal solution should work on modern browser and, if possible, at least from IE8+

Thank you in advance. =)

Edit:
About 2nd question, using @thirtydot solution I can add space (to the right) using white-space: pre-wrap applied on the span element

like image 365
Fabrizio Calderan Avatar asked Jan 31 '12 10:01

Fabrizio Calderan


People also ask

How do you add a background color using inline styling?

To set the background color with inline styles in React: Set the style prop on the element. Set the backgroundColor property to the specific color.

How do I make two elements overlap in HTML?

You could create the divs with absolute position and add a positive z-index to the one you want to be in front. You could put the widths of the container and of the divs with percentages instead of fixed values. e.g. container width: 100%, #left width 60% and #right width 60%.


1 Answers

Simply add a wrapper element inside the em, such as a span, and apply position: relative.

See: http://jsbin.com/axefaf

<a href="#"><em style="line-height: 0.8em">
    <span>This is an uppercase multirow text inside a link element</span>
</em></a>

span {
    position: relative;
}

This works in all modern browsers and IE8, but does not work in IE7.

like image 195
thirtydot Avatar answered Sep 28 '22 22:09

thirtydot