I'm working on a site where we're using the background-image: url('') CSS property.
It works well, but it seems that background-position is calculated differently in Firefox than in Safari/Chrome.
In my code (below) the image to the right (right-floater class) doesn't seem to render the same. Open up the fiddle in both Firefox & Chrome to see what I'm talking about - in Firefox the positioning of the background image is much lower than in Chrome/Safari.
I'm using background-position: 50% 0%; which works well in Chrome/Safari but with Firefox it seems that background-position: 50% 100% gives the same results while 50% 0% end up making the site look pretty bad in FF.
I've tried to play around with a lot of the elements and it seems like this is the culprit...
What is going wrong?
body {
background-color: gray;
}
.left-floater {
height: calc(100vh - 75px);
width: 40vw;
background-image: url(https://cdn.shopify.com/s/files/1/2296/3395/files/TOMO-Website-R3-06.svg?10381558204658209910);
background-size: 50% auto;
background-repeat: no-repeat;
background-position: center;
display: inline-block;
}
.right-floater {
background-image: url('https://cdn.shopify.com/s/files/1/2296/3395/t/2/assets/Sculpture.jpg');
background-size: contain;
background-repeat: no-repeat;
width: 54vw;
height: calc(100vh - 75px);
display: inline-block;
background-position: bottom;
background-size: auto 100vh;
background-position: 50% 0%;
}
<div class="sticky-wrap">
<div class="left-floater"></div>
<div class="right-floater"></div>
</div>
View on JSFiddle
Related
I have HTML and CSS like this:
.item {
width: 100%;
height: 768px;
background-image: url("https://a0.muscache.com/im/pictures/f1502649-e034-40ab-9fed-7992b7d550c6.jpg?im_w=1200");
background-repeat: no-repeat;
background-size: 100% auto;
border-radius: 50px 50px;
}
<div class='item'></div>
When I resize the browser, the bottom border-radius doesn't work.
Change background-size: cover; and you could add background-position: center; to center your image.
.item {
width: 100%;
height: 768px;
background-image: url("https://a0.muscache.com/im/pictures/f1502649-e034-40ab-9fed-7992b7d550c6.jpg?im_w=1200");
background-repeat: no-repeat;
background-position: center;
background-size: cover;
border-radius:50px 50px;
}
<div class='item'></div>
As mentioned in other answers, you might want to use background-size: cover;. It will stretch the image to cover the whole area of the div (so the border-radius will be visible).
However since the image can be cropped after this change (specifically in the case when it does not have the same aspect-ratio like the area of the div), it might be necessary to use also background-position to position the background image as you like (by default it is aligned to the top left corner).
Most probably you will have good results with centering it, but the possibilities are endless ;)
background-size: cover;
background-position: center;
Im using an svg as background for my html site. It works fine on most Desktops and a few broswers like Chrome on android, but in other browsers, there is this weird white blank area beneath the image. Any idea why this is happening? Here's what I mean - https://imgur.com/a/1ui6QeR
This is my styling right now. Any help would be appreciated.
html {
background: url('Bubble.svg') no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
height: 100vh;
overflow: hidden;
-webkit-touch-callout: none;
-webkit-user-select: none;
-webkit-tap-highlight-color: transparent;
}
body{
height: 100vh;
margin: 0;
display: grid;
place-items: center;
font-size: 10px;
}
It works completely fine on Chrome, but is offset to the right and bottom on firefox mobile, and is not displaying properly on edge mobile too.
Try to add 100% 100% instead of center center.
I would suggest to add the background image to a container in body rather to the html. Wrap your contents in a div and add image to that.
.bgImg{
background: url('https://www.w3schools.com/howto/img_forest.jpg') 100% 100% no-repeat;
background-size: cover;
height: 100%;
width: 100%;
min-height: 100vh;
}
<div class="bgImg">
</div>
I am 100% at a loss as to why I cannot get a background-image to display. I have tried multiple different images to rule that out. I have also changed src to url, changed the background-size from cover to auto 100%. Nothing I do will get the image to display.
Does anyone see why my background image will not display?
#home-img {
background-image: url("http://cosmotekcollege.com/wp-content/uploads/2015/08/ban4.png");
background-repeat: no-repeat;
background-size: cover;
background-position: center;
background-position: 50% 50%;
width: 100%;
height: auto;
position: relative;
}
<div id="home-img">
</div>
Here is jsfiddle
If you use backgound-image and you don't have content inside div, you always should set height. Here is example https://jsfiddle.net/5pphkLmt/5/
<div id="home-img">
</div>
#home-img {
background-image: url("http://cosmotekcollege.com/wp-content/uploads/2015/08/ban4.png");
background-repeat: no-repeat;
background-size: cover;
background-position: center;
background-position: 50% 50%;
width: 100%;
height: 540px;
position: relative;
}
Using Chrome Inspect, I changed the #home-img height from "auto" to 537px and then the image appeared. So updating the height is one option to fix this issue.
Another option is covered in this SO Q&A entry.
How to get div height to auto-adjust to background size?
you should add height to your code or put something in this DIV, just test height: 100px; and enjoy it :)
Hi I have an image which should be background image I have written a div and inside div I maintained some style for it and its working fine in Chrome and mozilla firefox , but its not working in IE8+ versions. below is the div that I used in my html.Do someone please respond to this.
<div style= "background:url(../portal/images/AWI_logo.png) no-repeat; background-size:100% 100%;">
You can use background-size: cover;
<h2>background-size: cover;</h2>
<div id="cover"></div>
div {
width: 900px;
height: 400px;
border: 1px solid black;
margin-bottom: 1em;
background-image: url(img.jpg);
background-repeat: no-repeat;
}
#cover { background-size: cover; }
see sample here you can learn more from csstricks here
See Demo. Please check on Chrome and firefox and feel the difference
I have a div element that uses the css classes below. Basically, I'm creating a responsive sprite of images:
.what-wwb-logo, .what-national-geographic-logo, .what-atm-logo, .what-us-ski-logo-large, .what-boart-logo,
.what-comas, .what-left-arrow, .what-right-arrow{
max-width: 100%;
background-size: 100%;
background-image: url('/images/sprites/what_our_client_say_new.png');
}
.what-wwb-logo {
background-position: 0 0%;
background-size: 100%;
padding-bottom: 41%;
}
.what-national-geographic-logo {
background-position: 0 16.588419%;
background-size: 118.648019%;
padding-bottom: 59%;
max-width: 77%;
}
.what-atm-logo {
background-position: 0 42.466823%;
background-size: 146.685879%;
padding-bottom: 94%;
max-width: 100%;
}
.what-us-ski-logo-large {
background-position: 0 65.003723%;
background-size: 181.785714%;
padding-bottom: 65%;
max-width: 70%;
}
.what-boart-logo {
background-position: 0 84.194978%;
background-size: 200.393701%;
padding-bottom: 84%;
max-width: 84%;
}
.what-comas {
background-position: 0 92.206077%;
background-size: 435.042735%;
padding-bottom: 62%;
max-width: 80%;
}
.what-left-arrow {
background-position: 0 96.196003%;
background-size: 820.967742%;
padding-bottom: 93%;
min-width: 7px;
}
.what-right-arrow {
background-position: 0 100%;
background-size: 820.967742%;
padding-bottom: 93%;
min-width: 7px;
}
And in my HTML I have the following code:
<div class="what-right-arrow " />
...
<div class="what-left-arrow " />
...
<div class="what-comas" />
...
<div class="what-boart-logo" /> // and so on and all divs in different position
I ran the HTML in Chrome, IE and Firefox. It ran good in Chrome and IE but not in Firefox.
The problem is that images (ONLY what-right-arrow, what-left-arrow and what-comma) are vibrating (up and down, left and right) all the time.
Is spriting not done properly or is it a Firefox problem ?
I think it's a Firefox problem as there are some bug reports about shaking images in Firefox.
It seems that when I move the background-image style onto the individual selector the vibration stops: See Demo
Example:
// Vibrates
.a, .b {background-image: url(example.png); background-size:50%;}
// Doesn't Vibrate
.a, .b {background-size:50%;}
.a {background-image: url(example.png);}
.b {background-image: url(example.png);}
I had the same problem with the vibrating background images in firefox when resizing the sprite. After adding the background-image to each instruction the problem went away, so the fix is not limited to just your demo.
Personally I'm starting to doubt this approach of resizing the sprite. I'm doing it as a solution for retina devices, but it seems the code is harder to maintain and the sprites are less precise (as not all browsers support fractions of a pixel well). The end result is:
Vibrating bug in firefox and the needed workarounds
The background-position values are just off enough to cut images off in an odd manner
Needing a solution to multiple all values by 50%, which means I can't just copy and paste the generated code from a sprite maker.