Wordpress hover over image - wordpress

I am trying to create a hover over image from a single split PNG
How do I enable it so when the image is not hovered over, the top image will view, but when they hover over, the bottom one will show.

The technique you are asking for is called "CSS-Sprites". Here's a tutorial
It uses the background-position style. For the default state of your element, just set the image as background. Note that you need a fixed height (half the height of your sprite) to hide the second part of the image. You also need a width, because your button will contain no content, just a background. For the hover state, use a negative background-position:
.button-foo{
display: block;
height: 29px;
width: 110px;
background: url("http://i.imgur.com/sJu5vvo.png") no-repeat scroll left top transparent;
}
.button-foo:hover{
background-position: 0 -29px;
}
This means the image is moved up so the top icon in there is above the visible area of your button.

Try to make sprites there is many applications out there. Google Css sprites generator.
Or try this one its free http://csssprites.com. Then its just simple css or jquery if u want any effects.

Related

Some Images flicker in IE8

I have Isolated the code to these div image swaps. The second image flickers, regardless of position. I can't figure it out? The images are basically the same it is for a menu image hover states. Is there something wrong with my code for IE8?
<div class="home"> </div>
<div class="image2"> </div>
Here is the CSS:
.home{
background-image: url('home.png');
background-repeat:no-repeat;
}
.home:hover {
background-image: url('home_hover.png');
background-repeat:no-repeat;
}
.image2{
background-image: url('image2.png');
background-repeat:no-repeat;
}
.image2:hover {
background-image: url('image2_hover.png');
background-repeat:no-repeat;
}
The first image does not flicker. The CSS is the same, I can reposition the DIV and get the same results. Why does the image flicker? It doesn't seem to matter if I put a height or width attribute to it either.
The flickering is due to the fact that you are using a different image for your hover effect. When your page initially loads, home.png and image2.png will be loaded and not your hover images. Your hover images of home_hover.png and image2_hover.png will only be loaded when you first hover the elements they are applied to. Which means, the moment you hover a request is made to the server for that background image. This takes a few short moments but is long enough to create the flicker.
I would suggest combining the default states and the hover states of each image into a single image. If you have an image that is 100px by 50px then your combined image would be 100px by 100px. Place the default state at the top of the image file and the hover at the bottom.
Your next step would then be to set dimensions on the element that has the rollover. The CSS would something like this:
.home {
background-image: url(home.png);
background-position: 0 0;
display: block;
width: 100px;
height: 50p;x
}
.home:hover {
background-position: 0 -50px;
}
What's happening here is that you are creating a window 100px by 50px in size. That dimension matches the top half of your image file which is the default state. The extra 50px of your image remain hidden.
Using background position allows us to shift the image relative to the element. We use -50px to shift the image up which reveals the lower half of the image which is your hover state.
NOTE: You also do not need to use for your DIVs.

How do I make 2 images appear with a hover?

Basically, I want to create a button on my web page that, when the mouse hovers over it, reveals two images, one above the other. The bottom image that appears is supposed to be an animated .gif, the second is a .gif with a word ('blog', 'music', etc.) in white and a transparent background (so that the .gif behind it can still be seen). I've tried to add the words over top of the animated .gif, but its color pallet doesn't allow white (only a light grey, which is unacceptable according to the design).
I'm pretty new to html/css, so there might be a better way to do this all together.
You will want to have a with an link inside of it. The css should be as follows:
div.background_image{
background: url('path/to_your/image.gif') no-repeat 0 0;
}
a.top_image:hover{
width: Xpx;
height: Xpx;
display: block;
background:transparent url('path/to_your/image.gif') no-repeat 0 0;
}
Make the link dimensions that same size as your background image div. Should be good to go. This assumes that you want your background image div always present. The :hover is what will make the image appear on rollover

css sprite as background, limited portion?

I need to place an icon of 48x48 as background. I have this icon in my image sprite where of course there are many other images.
Is there a way to show as background only a porition of the image?
thanks
EDIT: Is there a way to do this without setting width-height of the backgrounded element? (I am not sure if acutally i can set a width-height)
Edit2: this is what i need: http://jsfiddle.net/pdxnj/
Thanks
Set the width and height of the element to 48px.
.element{
width: 48px;
height: 48px;
}
Set the background of the element to your image
.element{
background-image: url('image.png');
}
Move the background so that the top left corner of the icon is positioned correctly.
.element{
background-position: 20px 94px;
}
The two numbers in background-position are the X and Y coordinates (respectively) where the top left corner of your 48px by 48px is in your sprite image. So maybe it's actually 96px 0px or something.
EDIT
If you can't control the width and height of the element you are trying to put the background in, but you can add new DOM elements, you can try adding a span inside the element you really want to put the image as a background for.
It would look something like:
<div id="noControl">
<span id="justCreated">
</span>
</div>
and the CSS would look exactly the same as above, except you would need to treat the inline span as a block element:
#justCreated{
display: inline-block;
}
EDIT 2
If you have control over new DOM elements, and want to make your sprite the background without messing with a span, just add another div inside your original one.
Would wind up looking like:
<div id="noControl">
<div id="justCreated">
ALL of the content that used to be inside #noControl
</div>
</div>
and the CSS for it would be
#justCreated{
width: 48px;
height: 48px;
background-image: url('image.png');
background-position: 96px 0px;
z-index: -200;
/* z-index of all the contents needs to be not set, or set to larger than -200 */
}
This is all theoretical, but it SHOULD work.
This way, you can apply the sprite sizing to a block element without messing with the inline stuff. This may affect CSS if it addresses elements by child status (like #noControl > a), because you are inserting a div between the parent and the child.
I am still researching whether you can do this at all if you have no control over the DOM at all.
simple answer no, but by using html elements you can. Html element hight and width should match the background portion of image.
You can if you're not going to be setting a repeating background. Otherwise no.
To do this, you need to play around with the background offset, and width/height of the actual element that you're setting the background on.
it will depend on how much whitespace is around it in the sprite whether it will fit where you need it to without showing parts of other images.. however you could e.g. put a span where you want the image and crop the span to 48x48 so that it only shows the icon itself. it kind of depends what you want to use it for and how the sprite is built
It's better using ::before or ::after so you can easily define your image size without having overflow problems!
This is possible. You need to display that in a 48x48 div then set position: absolute style for the div and define left and top too for it. Also set z-index: 0 for the div so that it appears under everything.

What's wrong with my menu background image?

The web site is here.
See that blue bar behind the menu items? It's 40 pixels high and one pixel wide, and used as a repeating background - so, why does it look strange after the right-most menu item?
The image is here, if anyone needs it.
The image is used thusly:
.menu_bar
{
background-image: url("http://leonixsolutions.com/images/menu_background.jpg");
background-repeat: repeat-x;
font-size: large;
padding-left: 160px;
padding-top: 5px;
text-align: center;
}
Nothing is wrong with it,
Your menu container div.pd_menu_01 has a background color #ffffff remove it and you'll be fine...
The real problem here is that .pd_menu_01 extends too far to the right (and with its white background, covers the gradient). Try making it inline-block with an auto width, or something similar, so that it doesn't extend further to the right than it has to. You could also set its background color to transparent (but in my browser, .pd_menu_01 makes the page too wide and thus introduces unnecessary and ugly horizontal scrollbars, so the width solution is still relevant).
.pd_menu_01 has a background color of white declared, and is a div. Since divs are block level elements, they have a default width of 100%.
Either declare the div to be display:inline, wrap the menu in a span instead of a div, or make the background color transparent instead of white.

CSS - How to control the gap between background image and container

Is it possible that I can create a margin/padding between the background image and container that holds the image? In other words, I need to move the background image sprite_global_v3.png 20px to the right of the left border of #nav-primary.
Here the position "0 -470px" are used to pick the right picture from sprite. And I don't know how to apply a padding/margin of 20px in order to achieve what I expected.
#nav-primary {
background:url("http://static02.linkedin.com/scds/common/u/img/sprite/sprite_global_v3.png") no-repeat scroll 0 -470px transparent;
}
<div id="nav-primary">
<span>Hello World</span>
</div>
Based on http://www.w3schools.com/css/css_background.asp
body
{
background-image:url('img_tree.png');
background-repeat:no-repeat;
background-position:right top;
}
If I understood correctly, the background-position is used to control the alignment of the background image. Now I need to control alignment and choose the right picture from a sprite. I don't know whether or not I can mix it together.
Thank you
No, there is no concept of padding/margin for background images.
Options:
1) Positioning the background (as already stated). The key is that the container would have to have fixed dimensions.
2) Nest a container inside a parent container. Parent gets the padding, child gets the background image.
Given that you are trying to do this with a sprite, both are likely options since a sprite has to have a fixed sized container anyways. For option 1, you'd need to make sure your sprite images have enough white space between each other in the file.
No, you can't mix them together.
You can place an image at an offset from the corner:
background-image: url('img_tree.png');
background-repeat: no-repeat;
background-position: 20px 20px;
But you can't combine this with the sprite techinque. This technique uses the fact that the element is smaller than the background image to clip the image, but you can't clip the background image 20 pixels into the element.
You can specify the exact position of the background to the pixel.
If you wanted a 10-pixel gap on the left-hand side, for example:
#nav-primary {
background:url("http://static02.linkedin.com/scds/common/u/img/sprite/sprite_global_v3.png") no-repeat scroll transparent;
background-position:10px 0px;
}
That being said, it looks like you already specified it to be set at (0, -470). Does that not work?
The background-position property allows for percentages and values, e.g. "20px 0", which I think is what you're looking for.

Resources