I read a lot about this topic the last days, but I didn't find a solution. I have two sprite sets, one with low quality for normal displays and one with high quality for retina displays.
My problem is, that my site has to be responsive, graphics should resize depending on the browser window. But with the background-size property, it seems that I can't tell the browser to resize the sprite. Here is what I got so far:
#logo-img {
max-width: 100%;
text-indent: -9999px;
height: 85px;
width: 360px;
background-image: url('/images/sprites-sa026cef942.png');
background-position: 0 -2609px;
background-repeat: no-repeat;
overflow: hidden;
outline: 0 none !important;
display: block;
white-space: nowrap;
}
#media (-webkit-min-device-pixel-ratio: 1.5), (min--moz-device-pixel-ratio: 1.5), (-o-min-device-pixel-ratio: 3 / 2), (min-device-pixel-ratio: 1.5) {
#logo-img {
background-image: url('/images/sprites-retina-se61e802cf4.png');
background-position: 0 -2601px;
-webkit-background-size: 360px, auto;
-moz-background-size: 360px, auto;
-o-background-size: 360px, auto;
background-size: 360px, auto;
}
}
The HTML is
<a href="#" id="logo-img">
<h1>My logo text</h1>
</a>
Everything works fine, except on mobile devices, or when resizing the browser window. The logo gets cutted, and doesn't resize to the browsers window. I was playing with max-width: 100%; and height: auto; but this only effects the outlines, not the sprites within.
Hope anyone knows a solution. It doesn't have to be IE8 compatible, but it would be nice ;)
EDIT
Before using sprites, I used background-size: contain; to make sure, that the background image gets resized to the size of the element:
#logo-img {
background:url(../images/sprites-retina/logo.png) no-repeat center center;
background-size: contain;
max-width: 100%;
text-indent:-9999px;
height: 85px;
width: 360px;
overflow: hidden;
outline: 0 none !important;
display: block;
white-space: nowrap;
}
This worked well, but then I can't use a spritesheet. Maybe that makes it clear what I mean.
EDIT2
I made a fiddle http://jsfiddle.net/euvRD/2/
I think you need to remove the comma from the background-size lines and you might want to set the height to 85px as well:
-webkit-background-size: 360px 85px;
-moz-background-size: 360px 85px;
-o-background-size: 360px 85px;
background-size: 360px 85px;
But the comma is the main problem here, I think. Did that help you?
Related
I'd like to have a background image on my homepage that is full width on desktop, but as the screen size shrinks, less and less of the right hand side of the image should disappear (but always shows the left hand edge and the objects in the left hand of the image).
The CSS code I have for this is as follows;
.sm-img-bg-fullscr {
background-position: 0% 0px;
background-size:auto;
width: 100%;
display: block;
position: relative;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
overflow: hidden;
background-repeat: no-repeat;
}
and the html where this class is called is as follows;
<div class="sm-img-bg-fullscr parallax-section" style="background-image: url(assets/system/backgroundimage.jpg)" data-stellar-background-ratio="0.5">
I have tried adjusting the background-size variables, and the background position variable, but with no luck. The image being used is 1920 x 1080.
I would like to know what combination of CSS parameters will allow the full image to be shown when the screen width is large enough, and have it remove more and more of the right hand side of the image as the screen size is narrowed, making only the left hand side of the image visible.
Why don't you use media queries to adjust the image background on certain screen sizes?
All we are doing is adjusting the background-position.
body {
margin: 0; padding: 0}
*{
box-sizing:border-box}
.image {
width:100vw;
height:100vw;
background-image:url("https://upload.wikimedia.org/wikipedia/commons/9/94/Cirrus_clouds_mar08.jpg");
background-repeat:no-repeat;
background-position:center center;
background-size: cover
}
#media only screen and (min-width:1px) and (max-width:700px) {
.image {
background-position:left center;
}
}
<div class="image">
</div>
Di you tried this?
background-position: center;
Try this in your CSS
{background-size: auto;}
or try inline style using max-width: ??.px
/* Responsive Full Background Image Using CSS
* Tutorial URL: http://sixrevisions.com/css/responsive-background-image/
*/
body {
/* Location of the image */
background-image: url(images/background-photo.jpg);
/* Image is centered vertically and horizontally at all times */
background-position: center center;
/* Image doesn't repeat */
background-repeat: no-repeat;
/* Makes the image fixed in the viewport so that it doesn't move when
the content height is greater than the image height */
background-attachment: fixed;
/* This is what makes the background image rescale based on its container's size */
background-size: cover;
/* Pick a solid background color that will be displayed while the background image is loading */
background-color: #464646;
/* SHORTHAND CSS NOTATION
* background: url(background-photo.jpg) center center cover no-repeat fixed;
*/
min-width: 100%;
max-width: 100%;
min-height: 100%;
max-height: 100%;
}
/* For mobile devices */
#media only all and (max-width: 768px) {
.logo {
/* The file size of this background image is 93% smaller
* to improve page load speed on mobile internet connections */
background-image: url(images/background-photo-mobile-devices.jpg);
height: 300px;
width: 100%;
background-size: contain;
background-repeat: no-repeat;
background-position: center;
}
}
The background image shows up fine on a desktop browser but when I view it on my iphone 6 plus or any other mobile phone the image doesn't scale correctly and is partially cut off. Can anyone help? Thanks.
There are two ways to work on the background and make it work well:
You should delete the onset:
min-width: 100%;
max-width: 100%;
min-height: 100%;
max-height: 100%;
And
height: 300px;
width: 100%;
The first way to make the length or width is supported on the other by adding the following code:
background-size: 100% auto;
The second way to make the rear rubber with any size by adding the following code:
background-size: 100% 100%;
Thank you
Also you can use or add for more optimization:
html{
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
it will cover the all html or body base on the tag you want the image to cover.
Hi guys I'm using stellar.js and it is working quiet well for desktop devices. But on mobile devices the background-image will be displayed only a part of it. It is not showing the whole image but the left corner of the image. The problem must be the css code. But I don't see it. Thanks for the help guys!
My css looks like:
.intro-section {
padding: 150px 0px;
text-align: center;
background-attachment: fixed;
width:100%;
height:100%;
position: relative;
background-position: center center;
background-size: cover;
background-image: url(../images/frederick_meseck_wood_logo.png);
background-repeat: no-repeat;
}
#media(min-width:768px) {
.intro-section {
min-height: 100%;
}
}
Try this in your media query:
#media(min-width:768px) {
.intro-section {
background-attachment: inherit !important;
background-size: 100% 100%;
background-repeat: no-repeat;
}
}
The first background-size parameter is the width, you can increase this value when going to smaller devices (I recommend this)
I have an image background for a div that I want to show on different devices, The problem is that I have to give height of the image in order to fit it correctly. Now on different phones, I have to adjust the height with different px. forexample on iphones 65px works for portrait mode but not for landscape and etc. Is there a way that the div just gets resized in height to cover 100% of the background image?
here is my code
<style>
div.testing {
height: 95px;
background-image: url(/myimageurl);
background-repeat: no-repeat;
background-size: 100%;
}
#media screen and (max-width: 320px) {
/* iphone portrait */
div.testing {
height: 65px;
}
}
#media screen and (min-width: 321px) and (max-width: 480px) {
/* iphone portrait */
div.testing {
height: 80px;
}
}
</style>
<div class="testing"> </div>
You could use background-size: cover;
.thing {
background: url(images/bg.jpg) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
but why are you using a background-image? if you can use a regular image, you could do it like this:
.thing {
width: 100%;
max-width: [your biggest width];
}
.thing img {
display: inline-block;
width: 100%;
height: auto;
}
ALSO
I would recommend flipping your mindset on the max-width and start small screen first, using min-width and getting bigger.
And you don't really need div.testing - it can just be .testing
And if you are using a background image for a good reason... you should investigate making the div -
.thing {
height: 0;
padding-bottom: 30%; /* play with this */
}
This will keep the proportions... but it's only useful in specific cases.
A complete jsfiddle with an actual image would be useful.
Good luck!
Nest your div inside the background div and set the height to 100%
Trying to get full size background image with:
html {
height: 100%;
background:url('../img/bg.jpg') no-repeat center center fixed;
background-size: cover;
}
It's showing the background image with correct width but height gets stretched.
I tried various options like
html {
background:url('../img/bg.jpg') no-repeat center center fixed;
background-size: 100% auto;
-webkit-background-size: 100% auto;
-moz-background-size: 100% auto;
-o-background-size: 100% auto;
}
removing height: 100%
but none of them worked.
Your screen is obviously a different shape to your image. This is not uncommon, and you should always cater to this case even if your screen is the same shape (aspect ratio), because other people's screens will not always be. To deal with this, you have only one of three options:
You can choose to completely cover the screen with your image, but not distort the image. In this case, you will need to cope with the fact that edges of your image will be cut off. For this case, use: background-size: cover
You can choose to make sure the entire image is visible, and not distorted. In this case, you'll need to cop with the fact that some of the background will not be covered by your image. The best way to deal with this is to give the page a solid background, and design your image to fade into the solid (although this is not always possible). For this case, use: background-size: contain
You can choose to cover the entire screen with your background, and distort it to fit. For this case, use: background-size: 100% 100%
try with contain instead of cover and then center it:
background-size: contain;
background-position: center;
background: url(images/bg.jpg) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
A better solution might be to use a lightweight jQuery plugin to dynamically size the background to the browser site. One I really like is backstretch.js. They're incredibly simple to implement.
I have same problem I use this CSS on body
background: url(image.jpg);
background-color: rgba(0, 0, 0, 0);
background-position-x: 0%;
background-position-y: 0%;
background-size: auto auto;
background-color: #0a769d;
height: 100%;
min-height: 100%;
max-height: 100%;
width: 100%;
background-size: 100% 100%;
background-position: center;
max-width: 100%;
min-width: 100%;
top: 0;
left: 0;
padding: 0;
margin: 0;
You should use the body as the selector and not html, this will cause issues with your markup. Your code is below:
html {
height: 100%;
background:url('../img/bg.jpg') no-repeat center center fixed;
background-size: cover;
}
I would try something like:
body {
background:url('../img/bg.jpg') no-repeat 50% 0 fixed;
margin: 0 auto;
}
You should not have to specify the dimensions for the image.