Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

css - The background size of a span element

I have a span element and I gave it a background-color, is there is any way to control this particular background-size?

I tried background-size: auto 2px; (2px for example) but it had no effects on it, what I want from the background color is to be a little bit smaller -height wise-, I want it to be from the top edge of the number to the bottom edge of it only, not bigger, is it possible?

button {
    background-color: rgb(41, 184, 250);
    border: 1px solid transparent;
    border-radius: 1em;
    transition: button 20ms ease;
}
span {
    background-color: white;
    background-size: auto 2px;
}
<button type="button">Unread Messages
        <span>9</span>
    </button>

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

1 Answer

0 votes
by (71.8m points)

background-size apply to only image so make your color an image using gradient:

button {
    background-color: rgb(41, 184, 250);
    border: 1px solid transparent;
    border-radius: 1em;
    transition: button 20ms ease;
}
span {
    background: linear-gradient(white,white);
    background-size: auto 10px;
    background-position:center;
    background-repeat:no-repeat;
}
<button type="button">Unread Messages
        <span>9</span>
    </button>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...