I have this image:
How can I used part of the .png file as an image? I want to use upper part for my <a class="non_check"> and lower part for my <a class="checked"> classes as background.
You're going to want to use CSS image sprites. With image sprites, you can show just the part of the image you want to use.
For example:
a.checked {
width: 20px;
height: 20px;
display: block;
background:url('image_here.png') 0px -20px;
}
a.unchecked {
width: 20px;
height: 20px;
display: block;
background:url('image_here.png') 0px 0px;
}
The background property defines the image that will be the background of the div.
The numbers following the image link define the position (the first number is the left and the second number is the top).
JS Fiddle Example
CSS Sprites: What They Are, Why They’re Cool, and How To Use Them
You should use appropriate CSS. You could combine fixed width and height and background, background-position properties, like this:
.checkbox
{
width: 23px;
height: 42px;
background: url(checkbox-icons.png);
}
.checkbox.unchecked
{
background-position: 0 0;
}
.checkbox.checked
{
background-position: 0 -21px;
}
Fixed size causes only the desired rectangle of the background to be visible and by changing the background-position property, you can define exactly which fragment should it be.
You need a layer (like DIV or SPAN) half the height of an image.
You place the image to the background and by setting background-position, you can move the image parts to the view or out.
You will be using background-position for this with a :hover. Something like this:
.checkbox {
background:#ffffff url('checkbox.png') no-repeat 0 0;}
width:23px
height:21px;
}
.checkbox:hover {
background:#ffffff url('checkbox.png') no-repeat 0 21px;}
width:23px
height:21px;
}
Related
Good afternoon stackoverflow community,
I have a file with some images inside it, and I want to use each part of this image separately without creating different files. So I started to look for ways to give position through CSS to "chop" the piece that I want to show.
I tried using properties like clipwithout success. The closest I got was giving a height and background-position when inserting some random text inside the DIV.
Here's a fiddle I did to demonstrate it, but since I couldn't update a image to it I just made with background-color.
.icon {
float: left;
background-color:#6495ED;
background-position: 0 0px;
height: 29.2px;
}
http://jsfiddle.net/PatrickBDC/HaGNa/
Is there a way to show that background, the same exact size but without the text?
Thank you very much!
You need to have a width for your div as well if the div is empty. try something like this
.icon {
float: left;
background: url () no-repeat 0 0;
height: 29.2px;
width:60px;
}
If you would like your div to display without content, you need to specify width in your CSS:
.icon {
float: left;
background-color:#6495ED;
background-position: 0 0px;
height: 29.2px;
width: 50px;
}
Here is a working fiddle.
Just add width: 100px; and height: 50px;property in your code. You can add width and height as per the part of the image which you want to show.
here is a background image for a header I want to repeat
how do I repeat the image horizontaly because the left and right are not the same?
In case you want to make the image fit a variable width, you need to set a part that needs to repeat. There is no way of doing this with one image, since you can't mirror the image.
You will need to manually mirror the image. An example could be:
Html:
<div class="left-image side"></div>
<div class="center"></div>
<div class="right-image side"></div>
The CSS would be something like:
.side {
position: absolute;
top: 0;
height: *height of the image*;
width: *width of the image*;
}
.left-image {
background: url('*link to left image*') no-repeat 0 0 transparent;
left: 0;
}
.right-image {
right: 0;
background: url('*link to right image*') no-repeat 0 0 transparent;
}
.center {
height: *height of lowest part, or height of image*;
width: 100%;
background: url(*url of image*) repeat-x 0 0 transparent;
/* or */
background: *color*;
}
The center could either be a repeating image, or just the background color in width the height of your image. This depends on the image you're using.
I hope this helps.
if you want to have the black portion through out then,
You cant repeat this image, as this will not be giving good effect, try just cutting a small part of it and repeat that, it will distribute itself evenly.
You have to make an extra image, the smallest part and declare both images in your background css
background:url(xxx.jpg),url(yy.gif)
or cut the image at the angle and just repeat the smaller image.
Just position the image to the right and set a bg color the same color as the image.
NB...will not work with transparent image.
JSFiddle Demo
header {
height:96px;
background-color:black; /* or whatever color of image is */
background-image:url(http://i.stack.imgur.com/oF6ey.png);
background-position:top right;
background-repeat:no-repeat;
}
i'm new to css sprits. i have added a small red color arrow like image to all links in a DIV ID. it looks like this.(image attached)
how to get some padding after the background image ? i mean some space between image and text using CSS.
CSS
#maincontent a:link {
background: url(images/css-images.png) no-repeat top left;
background-position: 0 0;
width: 4px;
height: 12px;
display:inline;
}
HTML
<div id="maincontent">
Btech III
</div>
i tried adding to css padding right, but it is giving some space after text not after image.
You want to use padding on your link, this will leave the background where it is but move the text, try padding-left: 25px;. But adding padding will add to the width so you will want to adjust the width of your link and reduce it by the amount of padding you have added (maybe not in this example)
Also your example image isn't loading
Try this:
#maincontent a:link {
background: url(images/css-images.png) no-repeat top left;
background-position: 0 0;
width: 4px;
padding-left: 25px;
height: 12px;
display:inline;
}
just apply a padding-left or a text-indent to your link
Is it possible to hide the specific area of a CSS background image using clip? for example I have multiple icons on image but I want to show only one icon. Because area of Div is greater then the icon size so other unneeded icons are also showing. Can I hide them without making another image for that single icon?
Jsfiddle http://jsfiddle.net/jitendravyas/FyMW6/1/
Basically you create a container for each image which you use to dictate the area of the image.
http://jsfiddle.net/FyMW6/4/
HTML:
<div class="button">
<div class="text">Lorem ipsum dolor sit amet</div>
<div id="house" class="icons"></div>
</div>
CSS:
.button {
width: 300px;
float: left;
border:1px solid red
}
.text {
float: left;
width: 245px;
}
.icons {
background-image: url("http://www.smtusa.com/uploads/cssexample.jpg");
background-repeat: no-repeat;
width: 50px;
height: 50px;
float: right;
}
#house {
background-position: -56px -45px;
}
#gear {
background-position: -56px -106px;
}
No, I don't think so, at least not in CSS2.
You can't apply the clip property to a background image.
CSS3 has the background-clip property that seems to do exactly what you need. It comes with limited browser support at the moment, though. CSS has the background-clip property but it allows only to specify which bounding box the image is rendered in.
There may be no solution for this except using another container for the icon.
easiest way is to set the class to display: none;
I have a Background Image [through CSS], I want to Link a Specific Part of that Image like it says "Home" and I want to point it to my site's home.
I think the Image Mapping works only with normal images and I can't find a way to do it with Background Image [Actually I haven't tried.]
So Can anyone please tell me how to do that?
Thanks :)
Use a 1px transparent png over the background image
Set the size of the png to the size of the background or link you want to make
Now you can either just link that transparent png, or map it
If you're having trouble fitting the png in the space, float it or use position:relative to get the overlaying transparent image to where you need it.
Lets say we have
HTML
<div>
</div>
CSS
div
{
width: 100px; /*the same width of the background image*/
height: 100px; /*the same height of the background image*/
background-image: url("#image");
position: relative;
}
div>a
{
position: absolute;
top: 10px /*the distance of the "home" part of the image from the top*/
left: 20px /*the distance of the "home" part of the image from left*/
width: 10px /*the width of the "home" part of the image*/
height: 5px /*the height of the "home" part of the image*/
display: block;
}
div>a:hover
{
outline: 1px solid black;
}