I am designing a webpage that contains a background image
like arrow for label. I have designed the image in Photoshop.
My problem is that the image size is not expanding and reducing as
per text.
So please help me, what should I do? Is there any method to do this?
HTML:
<label><span>First Name</span></label>
CSS:
label
{
background-image:url('./images/lblimg.png');
width:auto;
display:inline-block;
}
span
{
line-height:50px;text-align:center;
}
3
Answers
Use the style:
With
background-size:cover
the image will grow as large as possible so that the background area is completely covered by it.you need to set the
background-size
property to100%
for bothwith
andheight
.You have two options, both using CSS3
background-size
:Use
background-size: cover
: the background image will grow as large as possible until it covers the whole background area.Use
background-size: 100% 100%
: the background image will occupy 100% of the width and height of the container.In your particular case, the second option will be better as you want to display the whole arrow without risking some parts getting hidden.
In your code, it would look like this:
Edit (as per CSS2 request by OP): This cannot be achieved exclusively with CSS2 unless you change the HTML code. In that case you would actually not be using a background-image, but directly an image that would be behind the text.
This is how the HTML would look:
This is how the CSS would look:
You can see an example of the CSS3 and CSS2 solutions on this JSFiddle: http://jsfiddle.net/othnjk5x/