CSS Sprite Sheet using multiple backgrounds - html

I'm using a CSS Sprite Sheet technology and have a problem with multiple backgrounds.
In this website - https://www.w3schools.com/css/tryit.asp?filename=trycss_sprites_img you can see how to set a background from a sprite sheet but my case is a bit different.
Simple code:
#nav1 {
background: url(https://n3olukas.000webhostapp.com/images/nav-icons.png) -165px -19px no-repeat, url(https://n3olukas.000webhostapp.com/images/x3_1.png) no-repeat;
width: auto;
height: 40px;
background-size: 319px 349px, auto;
}
<div id="nav1"></div>
And the problem is I don't want to show these 2 icons. I want to show only the first one:
How could I make it? I've tried height and width properties but I think it's not for multiple backgrounds.

It is not possible to crop each image in a multiple-background setting separately. So if you want to keep the yellow bar, but only show one icon on it, consider using a pseudo-element, or an actual DOM element reserved to displaying single icons. E.g. here with an <i>:
#nav1 {
background: url(https://n3olukas.000webhostapp.com/images/x3_1.png) no-repeat;
background-size: auto;
height: 40px;
width: auto;
}
i.icon1 {
background: url(https://n3olukas.000webhostapp.com/images/nav-icons.png) -165px -19px no-repeat;
background-size: 319px 349px;
display: inline-block;
height: 40px;
width: 40px;
}
<div id="nav1"><i class="icon1"></i></div>
If you want to make sure it stays in the background, use z-index. If you want to make sure it doesn't interfere with the content of #nav1, use position: absolute; top: 0; left: 0 as well.

You would have to specify a width.
#nav1 {
background: url(https://n3olukas.000webhostapp.com/images/nav-icons.png) -165px -19px no-repeat;
width: 40px;
height: 40px;
background-size: 319px 349px, auto;
position: relative;
}
#nav1:after {
content: "";
background: url(https://n3olukas.000webhostapp.com/images/x3_1.png) no-repeat;
width: 232px;
height: 40px;
position: absolute;
z-index: -1;
}
<div id="nav1"></div>

Related

How to move a background image to the right side (when there is already another background image)?

I tried to get a background image on the right side of the header, but when I use: background-position: right; or float: right; then it doesn't do anything and I'm not sure if it's possible to style one background image specifically in css when they are both in the same class.
(the image on the left is good as it is, just an example to show that it has multiple backgrounds)
https://jsfiddle.net/qeysvr6c/82/
/* Using example images, since I don't know how to add patterns into jsfiddle without having to download them */
.h1_content {
background: url(https://upload.wikimedia.org/wikipedia/commons/5/55/Magnifying_glass_icon.svg) no-repeat; /* pattern for the header, left magnifying glass */
background-size: 4%;
background-color: #00a8f3;
color: white;
}
.h1_content::after {
content: "";
background: url(https://upload.wikimedia.org/wikipedia/commons/5/55/Magnifying_glass_icon.svg);
background-position: right; /* Doesn't go right? */
background-repeat: no-repeat;
position: absolute;
background-size: cover;
width: 50px;
height: 50px;
}
<article class="text_bottom">
<section class="section_test">
<h1 class="h1_content">Topic here</h1>
<p>Random text here</p>
</section>
</article>
Add position:relative to the heading and right:0 to the :after psuedo-element. Since the :after is position:absolute it will dock to the position relative parent.
There's other ways you could do this with one element, by using multiple background images for example. But this should do the trick.
/* Using example images, since I don't know how to add patterns into jsfiddle without having to download them */
.h1_content {
background: url(https://upload.wikimedia.org/wikipedia/commons/5/55/Magnifying_glass_icon.svg) no-repeat; /* pattern for the header, left magnifying glass */
background-size: 4%;
background-color: #00a8f3;
color: white;
position: relative;
}
.h1_content::after {
content: "";
background: url(https://upload.wikimedia.org/wikipedia/commons/5/55/Magnifying_glass_icon.svg);
background-position: right; /* Doesn't go right? */
background-repeat: no-repeat;
position: absolute;
right: 0;
background-size: cover;
width: 50px;
height: 50px;
}
<article class="text_bottom">
<section class="section_test">
<h1 class="h1_content">Topic here</h1>
<p>Random text here</p>
</section>
</article>
the ::after pesudo element doesnt have the enough width to go to the right so if you give it the space it will be pushed to the right and background-size should be 50px or contain to maintain its ratio :
.h1_content::after {
content: "";
background: url(https://upload.wikimedia.org/wikipedia/commons/5/55/Magnifying_glass_icon.svg);
background-repeat: no-repeat;
background-position: right;
position: absolute;
background-size: contain;
width: 85%;
height: 50px;
}
or simply just give the parent element position relative and the child should have right:0 :
.h1_content::after {
content: "";
background: url(https://upload.wikimedia.org/wikipedia/commons/5/55/Magnifying_glass_icon.svg);
background-repeat: no-repeat;
background-position: right;
position: absolute;
right: 0;
background-size: cover;
width: 50px;
height: 50px;
}

How to make background-img round and put into center?

I am trying to make my background-img round and put it into center. I am trying with code given below:
.jumbotronhh
{
background-image: url('http://simplelize.com/wp content/uploads/2013/03/old-camera-620x350.jpg');
background-repeat: no-repeat;
background-position: 50%;
border-radius: 50%;
height: 300px;
width: 300px; *//If I don't use this line then the background picture stays in center in a rectangular form but after using this I got the bg-img circle but it moves at the left side of the screen..*
}
what to do?! I am totally novice.. pls help..
You could put the image behind everything else to look like an actual background-image, by creating a div class and setting some z-index.
<div class="bg-image">
<img src="mybackground.jpg">
</div>
And CSS:
.bg-image {
position: relative;
min-width: 100%;
min-height: auto;
}
.bg-image img {
position: relative;
width: 100%;
height: auto;
z-index: -100;
border-radius: 30px 30px 30px 30px;
}
Since you really can't use the border-radius in background properties.
I tested your code with another picture and it works fine
Let me know if you mean another thing.
HTML:
<div class="jumbotronhh"></div>
CSS:
.jumbotronhh
{
background-image: url('http://goo.gl/amTgah');
background-repeat: no-repeat;
background-position: 50%;
border-radius: 50%;
height: 300px;
width: 300px;
}

Resize PNGs with Window Resize

Newbie here hacking away at this little project:
http://development.puretapecult.divshot.io/
And my question is, how do I automatically resize the .pngs in the center of the screen when the browser size collapses, or when it is viewed on a mobile browser?
Do I have to use #media queries for mutliple viewing sizes, and create multiple classes for each png?
Any help appreciated.
CSS classes that modify the images:
.spinner-outer {
display: block;
width: 327px;
height: 327px;
position: absolute;
left: 50%;
margin-left: -163px;
border-radius: 50%;
background: url(spinner-outer.png) center center no-repeat #32302e;
}
.spinner-center {
height: 200px;
width: 200px;
position: absolute;
background: url(spinner-center.png) center center no-repeat;
border-radius: 50%;
left: 50%;
top: 50%;
margin: -99px;
pointer-events: none;
}
.play-sprite {
position: absolute;
top: 50%;
left: 50%;
margin: -35px 0 0 -35px;
background: url(play-sprite.png) 0px 0px no-repeat;
height: 70px;
width: 70px;
}
I would use media queries to change the height and width of the divs. Note that you do not need to create multiple classes for different sizes. Just use multiple media queries like this:
#media (max-width: 768px) {
.spinner-outer {
width: 200px;
height: 200px;
}
}
You'll also need to specify that you want your background image to fit the size of the div or it won't change sizes when the div does. Use the CSS3 property background-size as long as you're comfortable not supporting old browsers.
.spinner-outer {
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
More info on background-size and some alternative techniques if you want to support older browsers: https://css-tricks.com/perfect-full-page-background-image/.
Try somthing like this.
HTML:
<div class="image-wrapper">// Div will always re-size with page.
<img src="[src]" />
</div>
CSS:
.image-wrapper{
max-width:90%;
height:auto !important;
position: relative;
display:block;
margin:0 auto;
}
.image-wrapper img{
max-width:100% !important;
height:auto !important;
display:block;
}
Or you can use bootstrap and add a class to image like so.
.img-responsive
Makes an image responsive (will scale nicely to the parent element)
<img src="[src]" class="img-responsive" alt="[Alt]">

CSS Sprites not working in blogger

i have been trying to get my hover sprite to work in the blogger platform, but only the first image will show up.
my css is as follows:
.imagesprite {
display: block;
background-image: url(xxx);
width: 400px;
height: 400px;
overflow: hidden;
background-position: 0 0;
}
.imagesprite:hover {
width: 400px;
height: -400px;
overflow: hidden;
background-position: 0, -400px;
}
nothing happens then my mouse hovers.
Nico is correct. Width and height generally do not change on hover. The only thing you actually need in your :hover statement is the new background-position (which doesn't take a comma). If .imagesprite is a div, you probably don't need display: block or overflow: hidden, either.
.imagesprite {
background-image: url(xxx);
width: 400px;
height: 400px;
background-position: 0 0;
}
.imagesprite:hover {
background-position: 0 -400px;
}

How to make background image go in the "background" using CSS/HTML

I want to fill my page with a background image and have the text aligned in place with that background. With the below code, the background image loads at the top of the page, and the text goes under it. I know I can use the "background: " function, but the way it is done in my below code allows for automatic resizing, regardless of browser size (i.e., mobile devices have small browser sizes). So, I just want the background image to go behind the text.
<html>
<head>
<title>Title</title>
<style>
img.bg
{
min-height: 100%;
min-width; 781;
width: 100%;
height: auto;
top: 0;
left: 0;
z-index: -1;
}
#media screen and (max-width: 781)
{
img.bg
{
left: 50%;
margin-left: -390.5;
}
}
#container
{
position: relative;
width: 781;
margin: 50 px auto;
height: 758;
border: 1px solid black
}
#left
{
position: relative;
left: 1.280409731113956%;
top: 14.51187335092348%;
padding-top: 10px;
padding-left: 10px;
padding-right: 10px;
width: 50%;
height: 50%;
color: #FFFFFF;
position: relative;
}
p
{
font: 14px Georgia;
}
</style>
</head>
HTML
<img class="bg" src="background.jpg">
<div id="container">
<div id="left">
<p>
Text
</p>
</div>
</div>
</body>
</html>
Make your BG image have a z-index of 1, and your #container div to have a z-index of 2. Does that work?
img {
position: relative;
z-index: 1;
}
#container {
position: relative;
z-index: 2;
top: 0px;
left: 0px; /*or whatever top/left values you need*/
}
Just use position: fixed for your background image http://dabblet.com/gist/3136606
img.bg {
min-height: 100%;
min-width: 781px;
width: 100%;
top: 0;
left: 0;
position: fixed;
z-index: -1;
}
EDIT (I wish there was a way to make it more visible than this)
OK, after reading the comments for the original question, I understand that the purpose is to have a background that scales nicely for any display sizes.
Unfortunately, quite a lot of mobile devices have a problem with position: fixed - you can read more about this here.
So the best solution in this case is to use a background image, not an img tag, having the background-size set to 100% (which will stretch the image - example), or to cover (which will scale the image such that it completely covers the screen - example)
Well, maybe you can also try that css:
body{
background: url(images/bg.jpg) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
it's should cover all youre page even when page size is changed