Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
866 views
in Technique[技术] by (71.8m points)

css - span background-color & padding problems

I have a design I want to implement that involves title text appearing with its own background color, padded by 10px, over an image, par example:

http://i.stack.imgur.com/E7EpS.png

The first example in this picture works well, and is simple:

.greenbox {width:520px; height:261px; position:relative;}
.greenbox span { padding:0 10px; background:#000; position:absolute; left:0; bottom:40px; }

Trouble arises when the text overflows onto another line, then the span elements padding does not effect the text on the line breaks, it renders like so:

http://i.stack.imgur.com/pY18f.png

Anyone know of an alternative, or how they would set this design out so that the background color & padding was consistent?

Thanks in advance

Edit: I had simplified the code to make it concise, but had missed a vital part. Actually it's like this:

.greenbox {width:520px; height:261px; position:relative;}
.greenbox a {position:absolute; left:0; bottom:40px; }
.greenbox span { padding:0 10px; background:#000; }

With the html as:

<div class="greenbox">

    <a href="link"><span>The Title Goes Here</span></a>

</div>

Thus the span remains inline, wrapped in a absolute position anchor.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I've tackled something similar before: Add padding at the beginning and end of each line of text

I've robbed that solution from myself, and fit it to your case.

Note that the line-height and padding adjustments can be very tricky to get right.

See: http://jsbin.com/ahoyug

HTML:

<div class="greenbox">
    <a href="#"><span><span>
        The Title Goes Here, with overflow
    </span></span></a>
</div>

CSS:

.greenbox {width:500px; height:200px; position:relative; background:green}

.greenbox > a {
    font: 50px sans-serif;
    line-height: 1.14;
    padding: 0;
    border-left: 20px solid #000;
    position: absolute;
    left: 0;
    bottom: 60px;
    text-decoration: none;
    color: #fff
}
.greenbox > a > span {
    background: #000
}
.greenbox > a > span > span {
    position: relative;
    left: -10px
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...