We suppose that we have the following image (is a single file with 4 radio buttons, seems same but it is for example, the icons could be different):
I saw, a time ago, how to get an image by specifing background-position in CSS style. How to do that ?
Means, how to get the icon from lower-right side in CSS instead splitting that file in four icons separately ?
Sorry if this question is obsolete but I'm beginner in CSS.
Have a look at the background-position attribute.
MDN has an article about it, including examples.
There are also a number of articles available about image-sprites with CSS.
Let's say your image is 100px by 100px and each button takes up 50px by 50px (including some possible whitespace).
Positioning for background images starts in the upper left-hand corner. So 0, 0.
Your CSS might look something like this:
background-image: url( ../images/button-sprite.png);
background-position: 0, 0; // top left
background-position: -50px, 0; // top right
background-position: 0, -50px; // bottom left
background-position: -50px, -50px; // bottom right
Not that the above shouldn't be used all together. You would use a separate background-position for each button.
Sometimes the positioning can seem counter intuitive since you'll usually be using negative values to get to the part of the image you need.
I tend to remind myself that if I were to add padding to an element it would push it down. Basically the same goes for background positioning. If we used 50px (positive) we would be "pushing the image down" instead of pulling it up to where we need it. Make sense?
you can adjust the sprite images in css through background-position in top,left, bottom,right.
In css background position:-
background-position:top,left refers with positive values like:-if need sprite image from top,left so you can give positive values to adjust the image like:-
background-position: 5, 10; // top left
so this mentioned above position will adjust the top & left background position.
And if you need bottom right background position so you can use the method
background-position: 0, -50px; // bottom left
background-position: -50px, -50px; // bottom right
so this mentioned above position will adjust the bottom & right background position.
So you are using Image Sprites.
With CSS, we can show just the part of the image we need.
#navlist{position:relative;}
#navlist li{margin:0;padding:0;list-style:none;position:absolute;top:0;}
#navlist li, #navlist a{height:44px;display:block;}
#home{left:0px;width:46px;}
#home{background:url('img_navsprites.gif') 0 0;}
#prev{left:63px;width:43px;}
#prev{background:url('img_navsprites.gif') -47px 0;}
#next{left:129px;width:43px;}
#next{background:url('img_navsprites.gif') -91px 0;}
Jangid's response is not quite correct.
With CSS, we can show just the part of the image we need.
This is not true; no CSS currently allows "clipping" of an area from a sprite image. All that is possible is to set the positioning as per the standard (left, top, right, bottom or offset).
You must make sure that if your graphics are going to be used as the background to a FLUID element (i.e. with an automatic height/width) that your images are spaced out well enough. If you do not space them out enough, then you may well see more than one image in the background, not just the part you want.
For example, your sprite contains 36 images, all 32px square, spaced neatly on a grid of 40px.
If the element to which you apply the background image is wider and/or taller than 40px, you will see part of other images as the background, which is obviously not satisfactory.
Hope this helps.
Related
I don't have an idea even where to start. Like on the attached image I want to make a centered div (yellow rectangle) and two img elements that, regardless of the resolution (black rectangle), are pointing to the specific area of this div (red arrows).
You can use position: relative for the specific element (that you want to point at),
put the arrows (their html code) inside of the elemnt (that you want to point at) and then you can use position: absolute; with top, left, right, bottom properties to position the arrows relatively to the center point.
I've an image. Can I derive its background from CSS styling??
Following is the image. I want a similar background in my div.
PS: I can use it as background-image, but I dont want to. So, please dont suggest this solution.
The easiest way will be to use a gradient.
background:-webkit-linear-gradient(left, #743D90, #954792); /*for chrome&safari*/
background:-moz-linear-gradient(left, #743D90, #954792); /*for firefox*/
background:-o-linear-gradient(left, #743D90, #954792); /*for opera*/
background:-ms-linear-gradient(left, #743D90, #954792); /*for IE*/
background:linear-gradient(left, #743D90, #954792); /*standard*/
makes a gradient with the colors of your image. There are so many lines of CSS so it will work in nearly all browsers.
Example:
http://jsfiddle.net/6uMzc/4/
Hope this will work for you.
Absolutely it can be done.
Wrap the image tag inside a div.
<div class="bg">
<img src="xxx.jpg" alt="image">
</div>
Note the image must have transparent background(this should be made sure by image editing tools like photoshop)
Now style the div with background gradient and size as something below.
.bg{
height:100px;
width:200px;
background:-webkit-linear-gradient(left, red, green);
background:-moz-linear-gradient(left, red, green);
background:-o-linear-gradient(left, red, green);
background:-ms-linear-gradient(left, red, green);
background:linear-gradient(left, red, green);
}
If I understand you correctly, you want to create a background similar to that image with css. You have several options for doing this. For starters you can use this Gradient Generator to create gradients. They are relatively simple, I'm sure you can figure out the core concepts, the generator is primarily because the different css prefixes must be added for compatibility.
You said not to consider the lines, but I do think I should point you to this CSS triangle generator, incase you need it. Your image has the lower portion cut off at an angle, and the best way to do this is (IMHO) is to create a white triangle and put it over it.
If you need any help applying these concepts, feel free to ask.
Update: See: http://jsfiddle.net/6uMzc/6/
You need two seperate divs to create this effect. One div is the gradient, the other is the triangle that removes the lower corner. I'll explain what the different parts of the css do:
The background attributes are all defining the gradient for different browsers.
position:relative;
position:absolute;
left:0;
bottom:0;
These lines are postioning the divs. This can get quite complex, but it is incredibly useful for web developers, so you should probably do further research. I actually think the w3schools page on this is pretty good. Elements with relative, fixed, and absolute positions can be modified by the left, right, bottom, and top attributes, which can move them around in different directions. Elements with relative positioning are moved "relative to their normal position." Elements with absolute positioning are moved relative of the next containing element with a relative or absolute positioning, or the body. Since the containing div is positioned relative, the position:absolute moves the triangle relative to the gradient. Left:0 and bottom:0 put the element in the bottom left corner.
border-width: 50px 0 0 500px;
Change the first number to change height and the last one to change width. 500px is the width of the containing div, so this covers up the entire bottom of the containing div.
I'm trying to disable some portions of my html pages.
I read that you can use a transparent div with absolute position on top of your page to prevent clicking on elements beyond it, but is there a way to accomplish this only on a portion of a page (let's assume this portion is all contained in a div) without the use of absolute position?
Put position: relative on the div you want to disable, then add the transparent blocking div as a child of this div with position: absolute and top, bottom, left, right equal to 0.
If you are unable to put position: relative on the div you want to disable then it will be a bit more difficult as you need to compute it's dimensions and offset and then position the transparent mask as a child of the body and at the exact same position as the element you need to disable. JS frameworks (as jQuery) usually provide you with ways to determine a box's offset relative to the document.
Make a little 1px x 1px transparent image and save it as a .png file.
In the CSS for your DIV, use this code
background:transparent url('/images/transparent-bg.png') repeat center top;
Remember to change the file path to your transperant image.
I think this solution works in all browsers, maybe except for IE 6, but I haven't tested it.
jsfiddle link: http://jsfiddle.net/djDWF/84/
The problem is, the inner container (text-padding) margin/width for the text/images is affecting the center background image. The repeated image that touches the footer does not extend to full height, and cuts off so the center and footer images do not match up (it is kind of hard to tell, but if you add or remove text in my jfiddle example you can see the center image change where it meets the footer.).
This is for a school project, and though I did not need to actually do this type of image background, I got this far so might as well continue. I don't want to use javaScript if possible because that is not part of the course yet.
I tried removing the text wrapper and styling each p tag individually but the same effect occurs.
I also tried mathematical combinations using line-height and margins. If I set the line-height to equal the right and bottom margins, and the left margin to equal the height of the footer then the effect works, but because my footer image is so large this is not a workable solution.
Mathematically I tried to keep the same ratios with the footer height but this did not work either (or else I did this wrong. I tried dividing each by the same amount.)
Is there any way to do this using only CSS and and not having to resort to tables?
So in short the problem is: You can see a line showing up at the footer separation because the repeated centre background isn't fully showing it's last repeat as the container isn't big enough.
The solution: If it doesn't need to be variable and you know how much content you will be putting in you can just set a height: Live example - http://jsfiddle.net/djDWF/85.
div#background-center{
background:url(http://i.imgur.com/gsNFa.png) repeat-y;
float:left;
width:700px;
height: 1604px; /* add this */
}
Obviously, pick whatever height is right to fit your final text.
With your current images there is no way to do this automatically without using JavaScript.
Is there a way to let the repeating background 'start' fixed at the bottom of the div and overflow on the top? (the other way round than default)
Small example to show what I'm trying to accomplish. The red lines on the image below are the (horizontal) borders between several divs. The middle div is used as a transition between the upper and lower one, with a single non-repeating image. The background of the lower div consists of a repeating darker image with a paper hole, resulting in the paper border seen below.
Now the upper div (with sizable content) should have a repeating background image starting at the bottom and repeating upwards. This for maintaining the correct distance ratio between the holes instead of ending up with a smaller distance or even a partial hole in the 'paper border'.
Preferably it should be compatible with method 4 of equal height columns, but I don't see a problem there.
Just make sure you set background-position: right bottom; to the top div, and the background image should start from the bottom and "overflow" out the top.
Example in shorthand:
.my-div {
background:#fff url(my-bg.png) right bottom repeat;
}
Some reference in case you'd like to explore this further:
http://reference.sitepoint.com/css/background-position