CSS: highlighted text effect
Asked Answered
J

5

5

I'm trying to produce a highlighted text effect with a bit of padding, but the padding is only applied to the beginning and end, not new lines.

#highlight {
background: rgba(255,230,0,0.5);
padding: 3px 5px;
margin: -3px -5px;
line-height: 1.7;
border-radius: 3px;
}

<span id=highlight>text<br>here</span>

Please see here: http://jsfiddle.net/CNJZK/7/

Are there any pure-CSS fixes to get the internal ("sharp") edges to extend a bit farther? i.e. like in this image: https://i.sstatic.net/ak2EM.jpg

Julietjulieta answered 11/9, 2013 at 16:34 Comment(3)
Is the idea to highlight text within a larger block of text? Your example doesn't show this but I feel that is what you are getting at.Pazpaza
Yes. Perhaps this clarifies: jsfiddle.net/CNJZK/7 halts the highlighting abruptly at the beginning/end of each line, when visually, this is what I'd like to happen: i.imgur.com/j8mIJZS.jpgJulietjulieta
possible duplicate of How to add horizontal padding to every line in one multi-line wrapped sentence?Pazpaza
H
5

Try setting the display on your span to inline-block:

#highlight {
    background: rgba(255, 230, 0, 0.5);
    padding: 3px 5px;
    margin: -3px -5px;
    line-height: 1.7;
    border-radius: 3px;
    display:inline-block;
}

jsFiddle example

Headrail answered 11/9, 2013 at 16:35 Comment(1)
Inline-block creates a rectangular block of highlight. I'm looking for something that shrinks to the text (as with inline), but extends just a little farther. Perhaps it's clearer with more text: jsfiddle.net/CNJZK/5Julietjulieta
E
3

If you're not limited to a specific HTML standard, you could take a look at the <mark> tag, which was introduced with HTML5. This site gives you a quick overlook.

Hope it helps!

Esp answered 6/8, 2014 at 8:35 Comment(0)
H
2

In case anyone is still looking for an answer:

p {
    box-shadow: 0px 0px 0px 5px yellow;
    display: inline;
    line-height: 2;
    margin: -5px -5px;
    background-color: yellow;
    border-radius: 1px;
}

See jsfiddle here.

Hydra answered 9/6, 2018 at 19:57 Comment(0)
L
2

Solution is this CSS:

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

https://css-tricks.com/almanac/properties/b/box-decoration-break/

Lanford answered 15/1, 2019 at 8:24 Comment(1)
Thanks! This is the only working answer to this question here.Mattress
B
0

You need to set the display to inline-block or block.

#highlight {
    display: inline-block;
    /* ... */
}
Blanketyblank answered 11/9, 2013 at 16:35 Comment(2)
A block creates a rectangle of highlight around the entire text (including things before/after the span). I'd like something that fits the text, with a bit of padding at the beginning/end of lines: i.imgur.com/j8mIJZS.jpgJulietjulieta
Hmmm...I can't figure out a way. Sorry.Blanketyblank

© 2022 - 2024 — McMap. All rights reserved.