positioning in having multiple background images in css - html

I had an image which I had used as background in my css. I now want to have two images, one after the other. Think, earlier my website had one sponsor, now there are two sponsors, and so, two logos.
I was able to add two background images by googling around a bit, but the position of the second image is right on top of the first. When I give pixel values, it goes off as well.
This is my code so far
.app-header-logo {
background-image: url("../images/image1.png"),url("../images/image2.png");
background-position: center center, 200px center;
background-repeat: no-repeat;
float: left;
height: 50px;
vertical-align: middle;
width: 265px;
}
.app-header-logo a {
float:left;
width:190px;
height:50px;
text-indent:-999px;
}
How can I have image2 right after image1?

This can be accomplished by using CSS Sprites, with sprites you'll be able to combine multiple images into one single .png or .jpg file. There's a great online tool which will help you combine this images into one single file and to use them you just need to call a class for example .sponsor-1 which will contain the background-position for the Sponsor numer 1 image, plus it builds your CSS automatically so you don't have to worry about finding the right position of the background for each Sponsor image. Here's an example of how to use them:
.sponsor-1, .sponsor-2{
background: url(sprites.png) no-repeat;
}
.sponsor-1{
background-position: 0 0;
width: 80px;
height: 50px;
}
.sponsor-2{
background-position: -26px 0;
width: 80px;
height: 50px;
}

Related

Css background image different on different width

What I was trying to do is have 2 divs with the same class side by side, one big in width and one small but both with the same background image.
The problem is when I use this code:
.div {
padding: 10px 0;
font-weight: bold;
margin-bottom: -40px;
color: #fefefe;
width: 100%;
position: relative;
background: url(/images/title_background.png);
background-position: center;
background-size: 100%;
}
The result is:
The first div looks good but the seccond one the background repeats, and if I change the backgroud-size to fill the seccond one the first one looks stretched and weird.
is it possible to have both with nicely fit background image withou having 2 diffent classes?
You can use background-size: cover; in order for the background to scale to the container.
MDN is a great resource for these kind of problems.

howto crop *AND* scale a background image in css

I have a "main-image" containing lots of small images which I "clip" into divs of fixed size by setting the background-position to some negative offsets. This works great!
Now I have a div with a size that changes during the lifetime of the web-page.
The old code had its own backgound-image with the background-size set to "contain". Something like this:
.dump {
display: inline-block;
background-image: url("/some/image.png");
background-repeat: no-repeat;
background-size: contain;
}
And that worked great too.
Now I'm trying to clip that background image from my "main-image".
E.g. My "main-image" has a size 1800px128px
The sub-image I like as background starts #1200px,10px with a size of 200px x 80px.
Is there a way to clip this rectangle and than scale to the dimensions of the containing div (which are unknown at the time of programming)
Thanks for the hint. However, I tried but can't get anything to work:
My problem is, that the div image should follow the height the containing div, so I can't tell size, or scale or zoom or whatever at the time of coding. I give an example:
<div style="width:100%; height:30%; text-align: center">
<div class="dump"></div>
</div>
Now, as I said: The image I want to appear as the background of div.dump is the 200x80px area from the main-image #origin(1200,10) AND I want that resulting image scaled to fit the hight of the container. So, I have a known translation, followed by an unknown zoom. Maybe it's just over my head.
I believe the best way to do this is using css transforms, I found this page for further reference on how to transform a background image and made this fiddle based on it.
The idea is that you will use the classes "icon" and "icon:before" to configure your sprite to fit in an element and use other classes like "smaller" and "bigger" to set the actual size of the element.
.icon
{
font-size: 2em;
text-align: center;
line-height: 3em;
border: 2px solid #666;
border-radius: 7px;
position: relative;
overflow: hidden;
}
.icon:before
{
content: "";
position: absolute;
width: 100%;
height: 100%;
left: 0%;
top: 0%;
z-index: -1;
background: url(http://blogs.sitepointstatic.com/examples/tech/background-transform/background.png) 0 0 repeat;
transform: translate(-50%, -50%) scale(1.5, 1.5);
background-repeat: no-repeat;
background-size: contain;
}
.smaller{
float:left;
width: 120px;
height: 120px;
}
.bigger{
float:left;
width: 200px;
height: 200px;
}
Because css transforms support percentage, the background will be clipped and scaled correctly, according to the size defined in "smaller" and "bigger"

Position Background Image without inside content

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.

Why CSS Background position changes on sprite height change?

Something interesting happens in my website. It is in active development and I keep adding and adding stuff to the website's sprite PNG file. Sometimes I add so many icons and blocks that I NEED to change the height of the image, but when I do this, some (NOT ALL!) elements appear on different locations.
For example I have a PNG image with size 900x900 pixels. I mapped the CSS styles to the proper coordinates, they I added 200 pixels of transparent space at the bottom of the image and some styles report different positions :< breaking stuff around the website. So each time I increase the sprite file, I have to open various CSS files and add X pixels (the amount of height I've added). I even added a 1px baseline on the top of the sprite so that I would be certain that I am not changing any positions but just the height.
I read the specification even in the RFCs and the coordinate system start is at x=0,y=0 which is the top left corner of the image. It doesn't make sense to me :(
UPDATE: Some of the containers that give me bugs are made with positive coordinates rather than negative. I still can't explain it to myself, why stuff like that happens.
UPDATE: So the sprite is located at this URL http://lucho.hoolwars.com/img/sprites.png
and here are few styles that change coordinates if the height of the sprite changes
.job-summary {
width: 330px;
height: 45px;
background: transparent url(/img/sprites.png) -15px 435px;
cursor: default;
}
.popup-title {
background: url('../img/sprites.png') -425px -1077px transparent;
width: 275px;
color: black;
font-weight: normal;
}
.popup-close {
position: absolute;
background: url('../img/sprites.png') -771px -972px transparent;
right: -9px;
top: -22px;
width: 38px;
height: 38px;
z-index: 2;
cursor: pointer;
}
Each time I change the height of "sprites.png" those coordinates are no longer valid :|
I think I know whats going on. Hard to explain - but I'll give it a shot.
I believe you have configured some of your sprite images based on the repeating background, because you are not using no-repeat. Every time you add more images to the sprite any icons that were configured on a repeated image will shift.
You will need to:
1) Add no-repeat to your background (any icons that were using a repeated image will probably now be blank)
2) Reconfigure all your classes to use negative values (always use negative values for your image sprites)
I would suggest you setup your sprites similar to this:
CSS
.sprite-map {
background: url(sprites.png) no-repeat;
}
.job-summary {
background-position: -80px 0;
width: 100px;
height: 80px;
}
.popup-title {
background-position: -15px -100px;
width: 200px;
height: 100px;
}
.popup-close {
background-position: -15px -472px;
width: 50px;
height: 50px;
}
HTML
<div class="job-summary sprite-map"></div>
<div class="popup-title sprite-map"></div>
<div class="popup-close sprite-map"></div>
That way you only need to specify the URL to the sprite once. Now when you add images to the bottom or the right of the sprite - nothing else will be affected.
Also if you're familiar with Sass - Compass makes image sprites incredibly easy. It might be worth taking a look at if you're interested: http://compass-style.org/help/tutorials/spriting/
Hope this helps!

Is it possible to achieve this flexible layout without using JS?

What I'm trying to achieve without using JS can be seen on jsfiddle.net/k2h5b/.
Basically I would like to display two images, both centered, one in background and one in foreground:
Background Image: Should cover the whole window without affecting the aspect ratio, which means that the image will always touch two opposite edges of the window, but the image will be cropped.
Forground Image: Should be inside the window without affecting the aspect ratio, which means the image will be always touch two opposite edges of the window, but the image will not be cropped.
It doesn't matter if it's a <div> or an <img> tag, as long as they are displaying the images.
Asume also that the image sizes are known upfront and can be used in CSS or HTML part.
So my question is: is it possible using only CSS or CSS3?
If it's not possible I will accept the answer that will be as close as possible to my goal.
Examples:
When the background image is cropped from the top and bottom:
When the background image when it's cropped from left and right:
After looking at #Kent Brewster's answer, I think I could achieve all the requirements of OP.
This doesn't have the problem of foreground image being cropped and you can also specify constant margin around the foreground image. Also div is being used instead of img tag, because we are using background images. Here is the link and here is the code:
<div id='bg'></div>
<div id='fg'></div>
#bg {
position: absolute;
height: 100%;
width: 100%;
background-image: url(http://i.imgur.com/iOvxJ.jpg);
background-repeat: no-repeat;
background-position: 50% 50%;
background-size: cover;
}
#fg {
position: absolute;
top: 10px;
left: 10px;
bottom: 10px;
right: 10px;
opacity: .7;
background-image: url(http://i.imgur.com/HP9tp.jpg);
background-repeat: no-repeat;
background-position: 50% 50%;
background-size: contain;
}
Try this:
<html>
<style>
body {
margin: 0;
}
#bg {
position: absolute;
height: 100%;
width: 100%;
background: transparent url(bg.jpg) 50% 50% no-repeat;
background-size: cover;
}
#fg {
position: absolute;
height: 90%;
width: 90%;
top: 5%;
left: 5%;
background: transparent url(fg.jpg) 50% 50% no-repeat;
background-size: cover;
opacity: .7;
}
</style>
<body>
<div id="bg"></div>
<div id="fg"></div>
</body>
</html>
If the scaling requirement is flexible, it might work. See http://jsfiddle.net/k2h5b/5/ to see it run.
Yes, it's possible.
Basically I just made the background image the background for the <body> (doesn't have to be the body of course), and then put the image inside that with a small margin.
<body>
<img id='fg' src='http://3.bp.blogspot.com/-OYlUbWqyqog/TeL-gXGx3MI/AAAAAAAAHRc/bdqvvvaeC7c/s1600/bald-eagle3.jpg'></img>
</body>
css:
body {
margin: 0; padding: 0;
overflow: hidden;
background: url('http://wallpaper.zoda.ru/bd/2006/07/21/2c7b4306fd22f049f331d43adb74a5f7.jpg') no-repeat left top;
}
#fg {
margin: 20px 20px;
opacity: 0.7;
}
obviously if the window is too big, there'd be issues. You could (I guess) use media queries to pull in different image sizes based on window size.
edit — OK, well for the image, if you do want it to crop and retain the right aspect ratio, then I think you'll have to know the image size ahead of time to do it so that it works out. Lacking that, here's another revision.
<body>
<div id='fg'> </div>
</body>
css:
body {
margin: 0; padding: 0;
overflow: hidden;
background: url('http://wallpaper.zoda.ru/bd/2006/07/21/2c7b4306fd22f049f331d43adb74a5f7.jpg') no-repeat left top;
}
body, html { width: 100%; height: 100%; }
#fg {
margin: 2%; width: 96%; height: 96%;
opacity: 0.7;
background: url('http://3.bp.blogspot.com/-OYlUbWqyqog/TeL-gXGx3MI/AAAAAAAAHRc/bdqvvvaeC7c/s1600/bald-eagle3.jpg') no-repeat center center;
}
If you know the image dimensions, you could then set max-height and max-width. (I'll try that too :-)
edit again To get the background to crop in a centered way, you'd need to set the position to "center center" instead of "left top". (Or "center top" if you just want it centered horizontally.)
Vertically centering elements with CSS without cutting-edge non-standard features (flexible box layout) is hard. That may be something to do with JavaScript. I'll say that one problem with any JavaScript solution like that is that it really slows the browser down. If you must do it, I would suggest introducing a little time lag so that you don't try to recompute the layout on every resize event. Instead, set a timer for like 200 milliseconds in the future where the work will get done, and each time you do so cancel the previous timer. That way, while a person is dragging the window corner it won't burn up their CPU.
edit even more ooh ooh yes #Kent Brewster's answer with the vertical centering is good - I always forget that trick :-)
There is no way to achieve this effect using only CSS, for two main reasons:
Because you are trying to resize your image, you cannot use the background property and must instead use an <img> tag. Your image will always try to take up as much room as it can if the width and height are not set. Thus, the aspect ratio will not be maintained, or your image will be cropped.
The other caveat of resizing the image is that you will not be able to vertically-align it to the center of your page without knowing its dimensions.