26

Is it possible to add padding before line-break? As in, making from this enter image description here to this enter image description here.

Current CSS code:

span.highlight { background: #0058be; color: #FFF; padding: 2px 5px; }
Gajus
  • 69,002
  • 70
  • 275
  • 438

6 Answers6

19

I had to add an extra margin-left:0; to make the two lines start at the same point.

This can be done with pure CSS. Create a solid box-shadow to the left and right of the highlight in the same color (and use margin to correct the spacing). For your case:

span.highlight { 
  background: #0058be;
  color: #FFF;
  box-shadow:5px 0 0 #0058be, -5px 0 0 #0058be;
  padding: 2px 0;
  margin:0 5px;
}
Marcel Korpel
  • 21,536
  • 6
  • 60
  • 80
Tonez
  • 319
  • 2
  • 5
2

It took some tryouts, but here it is: the single- and multi-line highlighter with additional padding.

HTML:

<h3>Welcome to guubo.com, Gajus Kuizinas</h3>
<p><span>So far you have joined: </span><em>Networks guubo.com</em><ins></ins></p>

CSS:

h3 {
  padding-left: 5px;
}
p {
    background: #0058be;
    position: relative;
    padding: 0 5px;
    line-height: 23px;
    text-align: justify;
    z-index: 0;
}
p span {
    background: #fff;
    padding: 2px 0 2px 5px;
    position: relative;
    left: -5px;
}
p em {
    background-color: #0058be;
    color: #fff;
    padding: 2px 5px;
}
ins {
    position: absolute;
    width: 100%;
    line-height: 23px;
    height: 23px;
    right: -5px;
    bottom: 0;
    background: #fff;
    z-index: -1;
}

The trick is to style the whole paragraph with a blue background, and only put white background on top of that at the beginning and the end. Doing so assures blue background elsewhere...;)

Two main disadvantages:

  • The highlighted text has to start at the first line (but does not necessarily have to flow into a second),
  • The paragraph has to be aligned with justification.

Tested in Opera 11, Chrome 11, IE7, IE8, IE9, FF4 and Safari 5 with all DTD's.

See edit history for the previous less successful attempts.

NGLN
  • 43,011
  • 8
  • 105
  • 200
  • This gets a little weird when you make the viewport smaller on the jsfiddle demo you provided, but otherwise seems OK at first glance. – Wesley Murch May 24 '11 at 23:34
  • Well, weird indeed, but it's what questioner asked for. But I suspect that short texts are concerned, then it is not all that crazy. – NGLN May 25 '11 at 16:13
  • what about if you have dynamically loaded in contents? – owenmelbz Jul 06 '12 at 15:18
  • @Owen Dynamic content: why should that be a problem? Sure, you have to put some extra elements in, but are there better solutions yet? – NGLN Jul 08 '12 at 19:07
  • thats what I was thinking about, if a client was adding news through a cms how would it be applied, well admittedly this is a slightly different situation but I've constructed this plugin to use for our clients that need this effect, seems to work pretty happily. http://jsfiddle.net/OwenMelbz/rd8Qc/ – owenmelbz Jul 09 '12 at 08:35
1

You can achieve this using just box shadow, with no messy padding or margins.

The trick is to use box-shadow's spread option, and the padding on wrapped inline elements behaves as you expect.

.highlight {
    background: black;
    color: white;
    box-shadow: 0 0 0 5px black;
}
Adrian Macneil
  • 13,017
  • 5
  • 57
  • 70
0

display: block will achieve part of what you want, but of course it will make the span a block element, and so you won't get the wrapping behaviour seen in your example.

Your screenshot holds the clue to what you need to try and do: you need to impose a margin to the left and right on your "normal" paragraph text, and then have the span disregard this (and include its padding), to achieve an "overhang" of your blue highlight when compared to the rest of your text. You can't do that with straight CSS on your span, because it covers two lines and obviously "left" and "right" only refer to the span, and not the individual pieces of text contained therein.

Straight CSS isn't the answer here. You might want to take a look at this question, which uses a jQuery filter to grab the first word in an entity, etc.:

jQuery first word selector

Community
  • 1
  • 1
Ben
  • 7,548
  • 31
  • 45
0

Maybe you can use this technique. http://samcroft.co.uk/2011/jquery-plugin-for-inline-text-backgrounds/

Esben
  • 11
  • Welcome to Stack Overflow! Whilst this may theoretically answer the question, [it would be preferable](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Kev Aug 09 '11 at 11:42
-1

The closest thing, if it really matters that much I'd say is to add display: inline-block;

Fredrik
  • 2,016
  • 3
  • 15
  • 26
  • How come this is better than `block`? Both will produce the same result. – Gajus May 23 '11 at 19:04
  • 1
    No it doesn't. Check this out for a explanation of inline-block: http://www.quirksmode.org/css/display.html#inlineblock – Fredrik May 23 '11 at 19:13
  • 1
    However depending on the width of the surrounding element there is of course a possibility that it looks exactly the same. – Fredrik May 23 '11 at 19:16