css sprites - a hover with images not loading correctly - css

I have some css sprites on my site in the header but when the page loads it often loads several of the 8 separate images just below where it's meant to be. Then I wait a few seconds or hover my mouse over them and it goes back to the correct positions.
Here's my CSS:
.x {
display: inline-block }
.x a {
display:block;
width:100px;
height:100px;
overflow:hidden;}
.x a:hover img {
margin-left:-100px;}
and then the HTML goes like this:
<div class='x'><a href='link' alt='y'><img src=
'image' /></a></div>
<div class='x'><a href='link' alt='y'><img
src='image' />
</a></div>
for 8 separate 100x100 squares in a row.

The way to define css sprite is a bit different then how you are doing it.
Here is an example of how this can be achieved.
/* This is how to define a main image
.sprite { background: url("../link/to/spriteimage.png") 0px 0px; width: 32px; height: 32px; }
/* Assign an image like this way, by changing the position
.sprite.icon1 { background-position: -32px -32px; }
.sprite.icon1_hover { background-position: -64px -32px; }
Demo

Related

Replacing a Logo using CSS

My problem is that it doesn't replace the logo itself, I have been trying to solve this problem for a few days now during some of my spare time (I am new, hence why it has been so long).
Not sure how to solve this problem.
Code and Image below to provide more detail:
.navbar-brand {
width:200px;
height:200px;
box-sizing:border-box;
padding-left: 200px;
/*width of the image*/
background: url(https://web.archive.org/web/20180921071933im_/https://www.rolimons.com/images/logo-56x56.png) left top no-repeat;
}
The first R logo is supposed to replace the second R logo, instead it creates a separate one
Without seeing your HTML my guess is there is a child element inside .navbar-brand. So when you add the background image and padding-left you are making room for your new logo but the old one is still there.
If you inspect the logo area I bet you have an img element, another element, or a pseudo element that you have to style or hide like one of these:
Style:
.navbar-brand .some-other-element-class {
width: 200px;
height: 200px;
box-sizing: border-box;
padding-left: 200px;
/*width of the image*/
background: url(https://web.archive.org/web/20180921071933im_/https://www.rolimons.com/images/logo-56x56.png) left top no-repeat;
}
Hide:
.navbar-brand img {
display: none;
}
.navbar-brand::after {
display: none;
}
Edit
I think you're site is https://www.rolimons.com/ based on the image url, if so then my assumption that there is an img tag as a child of .navbar-brand is correct.
If you want the "new" logo to replace the old one you can use the hide technique above, BUT replacing the img src would probably be the better path forward if you can change that.
If You want to replace the logo with CSS you can hide the old logo image and set the new logo image as a background image.
<div id="logo_outer">
<img src="Logo.png">
</div>
<Style>
#logo_outer {
width: 200px;
height: 200px;
background-image: url(img url );
background-repeat: no-repeat;
background-position: center;
background-size: auto;
}
#logo_outer img {
display: none;
}
</style>

positioning background-image (multi image grid) with css

I have a 26x104 px image (so vertical oriented) containing 4 logo's, each a 26x26 px.
I want to use those 4 logo's as background for 4 hyperlinks.
So I have this:
HTML (this is the sequence I want)
<div class="rightfloat">
<a class="facebook"></a>
<a class="linkedin"></a>
<a class="twitter"></a>
<a class="mail"></a>
</div>
CSS:
.facebook, .linkedin, .twitter, .mail {
display: block;
width: 26px;
height: 26px;
background-image: url('style/socialmedia.png');
background-repeat: no-repeat;
}
.facebook {
background-position: 0px 0px;
}
.linkedin {
background-position: 0px -26px;
}
.twitter {
background-position: 0px -52px;
}
.mail {
background-position: 0px -78px;
}
For some reason only the first one appears but with other testing it also happend that the last icon appeared at the second link...so appearantely I don't get the logics behind it.
I looked for some example/tutorial but I can't find one.
Can someone give me a hand?
SOLUTION: I have put the coördinates in negative values and it worked...
You have to put the values as negative:
Working demo
SOLUTION: I have put the coördinates in negative values and it worked...

CSS image to become smaller when browser is resized

I have used a background image on the webpage and used this code in the css which makes it nicely resize when browser is resized.
body{
background: url("images/back.jpg") no-repeat ;
background-size: cover;
}
I need to place some other image on top of the background image at a specific place ( vase on table) .but when i do that then the background gets resized but the vase image remains in the same place and same size when browser is resized as shown in second picture below.
see the vase in these two images
browser in full size
resized browser
how can i make the vase image also get resized just like the background
I recently ran into exactly the same issue creating a hidden object game which needed images placed on top of a background image to maintain their position regardless of browser dimensions.
Here's what I did:
You can include a template version of the background image as an actual <img> with visibility:hidden (so it's not visible but still takes up it's space in the DOM and base the size (and background image size) based on that.
HTML:
<div class="image-container">
<img src="http://www.w3.org/html/logo/downloads/HTML5_Logo_512.png" class="img-template">
<div class="item"></div>
</div>
CSS:
/* This is your container with the background image */
.image-container {
background:url('http://www.w3.org/html/logo/downloads/HTML5_Logo_512.png') no-repeat;
background-size:100%;
overflow-x: hidden;
position:relative;
}
/* This is the template that resizes the DIV based on background image size */
img.img-template {
visibility: hidden;
width:100%;
height:auto;
}
/* This is the item you want to place (plant pot) */
.item {
position: absolute;
left: 14.6%;
bottom: 80.3%;
width: 15%;
height: 15%;
background: yellow;
border: 2px solid black;
}
Here is a working example: http://jsfiddle.net/cfjbF/3/
Try making the image relative position and setting the alignment manually.
http://jsfiddle.net/cfjbF/1/
<head>
<style>
body {
background: #000000;
}
#image1 {
background: #008000;
position: relative;
left: 50px;
height: 50px;
width: 50px;
}
</style>
</head>
<body>
<div id="image1"></div>
</body>
Solution for your Problem:
https://stackoverflow.com/a/7660978/1256403
OR
http://buildinternet.com/2009/07/quick-tip-resizing-images-based-on-browser-window-size/

Read images from a image sheet

I have one image sheet like the one in this link (http://jquerytools.org/media/img/demo-navi.jpg).
I would like to get only a component of that image.
How can we get only component of image in CSS?
The correct terminology is CSS Sprite.
You can achieve this using background positioning:
div
{
background-image:url('http://jquerytools.org/media/img/demo-navi.jpg');
background-position:-20px 80px;
width:100px;
height:80px;
}​​​
http://jsfiddle.net/Lz46r/
You can try this: http://www.guistuff.com/css/css_imagetech1.html
Cropping X and Y
That first image was kind of a softball. All the cool kids know how to take advantage of cropping an image in both axes. There are several reasons for doing this: You may have images of different sizes and want to place all of them within one file, for example. If you only crop on one axis, you'd be saving a file with the largest possible width or height of the array of images you want to use. Also, there are compression elements that you may want to take advantage of in the PNG file format, like keeping images with the same background color in the same horizontal row, and then having several rows.
Whatever the reason, there actually isn't much more to this than what we've seen so far. Here's another example image:
.icons
{
display: block;
width: 40px;
height: 40px;
background-image: url(images/sixicons.png);
background-repeat: no-repeat;
}
You can deduce from this class that the width and height of each icon is 40 pixels, and that the image file name is sixicons.png. I didn't create a very challanging example this time for X/Y cropping in the sense that all of the sub-images are of the same size. As you'll see, however, even if they weren't, you'd still be using a simimlar (though not exact) technique.
First, let's crop the top-left icon:
.icon_1 { background-position: 0px 0px; }
The markup would be:
<span class="icons icon_1"></span>
That was, of course, the easiest one. Now let's say we want the middle-bottom icon:
.icon_5 { background-position: -40px -40px; }
Let's see the CSS for all of the icons:
.icon_1 { background-position: 0px 0px; }
.icon_2 { background-position: -40px 0px; }
.icon_3 { background-position: -80px 0px; }
.icon_4 { background-position: 0px -40px; }
.icon_5 { background-position: -40px -40px; }
.icon_6 { background-position: -80px -40px; }
This should do what you need:
http://jsfiddle.net/8Eucw/1/
CSS:
#aBit {
background-image: url('http://www.google.co.uk/images/nav_logo107.png');
background-position-x: 114px;
background-position-y: 63px;
width: 18px;
height: 18px;
}​
HTML:
<img src="http://www.google.co.uk/images/nav_logo107.png" /><br />
<hr />
<img id="aBit" />​
You need to use CSS Sprites. There are some very simple tutorials here.

CSS Sprite Full Page Background: background-position

I have an image, the top 80px of which I want to use for some other purpose, and remaining image, I want to set as a full page background image.
I tried setting:
background-position: 0px -80px
but it does not work.
How to properly use css sprite (background position) and full page background image?
Either of these (link or link) will generate a sprite for you and the corresponding css.
Once you have that completed use the css classes for their corresponding areas like:
.image1Background {
background-image: url("thesprite.png"),
left: -80px;
top: 0px;
}
.image2Background {
background-image: url("thesprite.png"),
left: 0px;
top: 0px;
}
<body class="image1Background">
<div class="image2Background">
</div>
</body>
Sprites are generally used for a lot of little icons to reduce the number of requests needed to download them to the client.
i would use a div for the 80px image and the background img as body background.. something like:
body { background-image: url(background.gif) }
#imgtop { height: 100%; width: 100%; background-image: url(80px_image.gif) }

Resources