Hi I have two columns of content within a container, the first column has text and the second is a span with a background sprite image. The problem is when I get to smaller screen resolutions, I want the background sprite image to have a width in percentage to be able to scale it along with the H5 with a percentage width, is there a way to do this?
h5{
float:left;
display:block;
width:800px;
}
.sprite{
background-image: url("assets/img/website_sprite_a.png");
background-position: -60px -60px;
float:left;
display:block;
width:64px;
}
<div class="container">
<h5>Title
</h5>
<span class="sprite">
</span>
</div>
Here's how to create responsive background images with CSS: Use the background-size property to encompass the viewport. Give this property a cover value that will tell a browser to scale the background image's heights and width so that they always remain equal to or greater than the height/width of the device viewport.
First upload the image file and add the CSS to your stylesheet. Then replace your images with <img> tag to load the sprite. CSS classes are generated from the image filenames you upload, so for example: <img src="icon. png"> might become <img class="icon" alt="" src="data:...">
Step 1 : Open the sprite image in the Microsoft paint tool, and find the background position of a specific part. Above, we use the first row for the demo, here we show position of the first icon on Facebook. Now we can give width and height according to smaller images and differences of icons in sprite image.
In your case I would go with a single background-image
, but in the case you will have a lot of images or you really want to do this you can use the background-size property.
From MDN:
The background-size CSS property specifies the size of the background images. The size of the image can be fully constrained or only partially in order to preserve its intrinsic ratio.
.sprite{
background-image: url("assets/img/website_sprite_a.png");
background-position: -30% -30%; //use % instead pixels
float:left;
display:block;
width:64px;
background-size: 100%; //play with this
}
You also should read this:
I have played a little bit with this on JSFIddle. Resize the browser to see the effect.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With