How do I keep a responsive background image from clipping? - css

I have a responsive background image here and it works well in most screen sizes, however, on the iPad/tablet size it clips the edge of the windmill blades.
Here is the CSS I am using for the space:
background: url('/image/background-img.jpg') no-repeat;
width: 100%;
background-position: 0% 25%;
background-size: cover;
background-position:center;
background-repeat: no-repeat;
How can I continue to fill the background but not clip the image?

It's the expected behavior of background-size:cover. It displays the image as small as possible, making sure it covers all the element and cutting off (cropping) the excess.
You'll want to adjust the min-height of your element until you are happy with the result. To keep it from being cut (too much). To keep it from interfering with how it looks on different screens, you might want to wrap your special rules for tablets in a specific #media query. Example:
#media (max-width: 991px) and (orientation: landscape) {
.jumbotron {
min-height: 62vw;
}
}

Related

Background only covers half of the page on mobile

It is all fine when i open it through my pc browser
I have changed the height to 350%, it works, covered all of the screen, but the image turns to be so stretched out. I have changed the background-size to contain, it doesnt work.
{background-image: url(bg.jpg) ;
background-repeat: no-repeat;
background-position: center center;
background-attachment: fixed;
background-size: 100% 100%
this it what it looks like
Image ends here
it even looks worse, when i switch to desktop mode
Image ends here
Try adding min-height to 100vh..Maybe it helps
Add this css code to your div
min-height : 100vh;
Your best option here would be to use a media query for mobile sizes and either another picture with an aspect ratio set for mobiles, or to have the background-positions and size changed to suit for phone. Code below to get you started.
/*--For Desktop--*/
body{
background-image: url(bg.jpg) ;
background-repeat: no-repeat;
background-position: center center;
background-attachment: fixed;
background-size: 100% 100%
}
#media only screen and (max-width: 600px) {
body{
background-image: url(theotherimage-withnewaspectratio.jpg);
}
}
Also - max-width i'd change based on when your image starts to look dodgy on desktop rather than the 600px (just shrink your screen in developer mode to tell the pixel width)
Change background-size to cover instead of 100% 100% i think this will help you some how

How to make background image responsive for mobile devices

I am making a site using HTML and CSS but my background image is not responsive
for small devices like mobile its not resizing as I decrease the size of windows
my CSS code for the image is like this:
body{
background-image: url(image.jpg);
background-size: cover;
border-image-repeat: no-repeat;
}
You need to look at using media queries, they are used in css to define certain screen sizes.
I recommend having a look here: https://www.w3schools.com/css/css_rwd_mediaqueries.asp
I would also recommend for images to have them in multiple sizes, you don't want to squash a large image onto a mobile size canvas, you should separate them into mobile/tablet/desktop
#media only screen and (max-width: 768px) {
/* For mobile phones: */
body {
background-image: url(mobile_image.jpg);
background-size: cover;
border-image-repeat: no-repeat;
}
}
You can use the same image to scale based on the size of the browser window:
background-image:url('../images/bg.png');
background-repeat:no-repeat;
background-size:contain;
background-position:center;
/* it is used when the browser or screen size is below 767px */
#media only screen and (max-width: 767px) {
body{
background: url(image.jpg)no-repeat;
background-size: cover;
background-position: center;
}
}

Stop scaling background relatively at a certain value?

Apologies if the title is hard to follow.
Essentially I want the background image to scale relative to the screen size, but only above a certain point, so if you shrink the screen small enough, the image will not shrink with it.
I've done it with my divs with this:
.row{
width: 100%;
min-height: 70px;
min-width: 1000px;
}
Is there something similar I can do with the background?
This is what I have at the moment.
.body{
background-image: url('../static/banner.jpg');
background-repeat: no-repeat;
background-size: 100% 100%;
}
Media queries can do this for you, just use a breakpoint where the background has stops to scale.
For example, this CSS will apply styles only if your browser's
viewport width is equal to or narrower than 12450px:
#media (max-width: 12450px) { ... }
Take a look at:
https://developer.mozilla.org/en/docs/Web/CSS/Media_Queries/Using_media_queries

Dealing with CSS background images on screens with different aspect ratios. (16:9 -> 9:16)

I have webpage with a background image:
body {
background-image : url("https://example.com/image.png");
background-size : cover;
background-repeat : no-repeat;
}
This works fine for 16:9 screen but for a mobile phone(9:16), the image covers (kind of) only half the screen!
How to specify different images based on aspect ratio?
You can try this. It may be problem with your image position
body{
background: url(http://www.planwallpaper.com/static/images/nature-wallpapers-hd.jpg) no-repeat top center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
Show this, It may help you to understand background-size
CSS background image to fit width, height should auto-scale in proportion
That will always happen as with background-size: cover the browser tries to fill the element with the same background. You can still do some adjustments with background-position, but it's not really helpful in these circumstances.
What could be much better is that you use media queries to use different backgrounds depending on the screen size.
Something like
#media only screen and (max-width: 640px) {
background: url(../images/mobile-background.jpg) no-repeat center center;
background-size: cover;
}
#media only screen and (min-width: 641px) {
background: url(../images/large-background.jpg) no-repeat center center;
background-size: cover;
}
This way you can select specific images for both cases and some of the benefits include more adequate image size which leads to faster loads and less bandwidth consumption for the user. That also leads to better Page Speed results.
This deals with aspect ratios in CSS : https://developer.mozilla.org/en-US/docs/Web/CSS/#media/aspect-ratio. Thanks #GabyakaG.Petrioli
/*background image to load for 9:16 display*/
#media (min-aspect-ratio: 9/16) {
body {
background-image : url("https://example.com/image1.png");
}
}
/*background image to load for 16:9 display*/
#media (min-aspect-ratio: 16/9) {
body {
background-image : url("https://example.com/image2.png");
}
}
/*Common properties can go here*/
body {
background-size : cover;
background-repeat : no-repeat;
}

Is it possible to make a responsive div with a background-image that maintains the ratio of the background-image like with an <img>?

Not a a native english speaker so there's probably a better way to shape the question...anyway:
What I want to create is similar to the header here: http://thegreatdiscontent.com/adam-lisagor
The header image is shown fully in all screensizes, and the aspect-ratio of the image is of course always correct.
This is made using an and getting the text to appear on the using position: absolute.
But if you use css for the background-image instead of an , you'll get something like this header: http://elegantthemes.com/preview/Harmony/
Resize browser to see parts of the background being left out.
Is it possible to make a a div look and behave like the first link, using the background-image css property like on the second link?
Or do I have to change how my entire header works and use the for the background for it to show fully in all screensizes?
I would like to have a header background that doesn't leavy anything out, but is fixed like this http://getflywheel.com/
Only idea so far is to make a transparent png that has the correct ratio of the image, and then use background-image that has background-attachment:fixed. But this doesn't seem very smart.
Hopefully I was clear enough that I'll get understood. Thank you all very much in advance!
Here is a nice and simple tip with only css/html:
Ingredients
Transparent PNG image with the desired ratio
(transparent-ratio-conserver.png)
tag
Different images for different view-ports (retina.jpg, desktop.jpg,
tablet.jpg...)
The idea is to open an tag and to assign to it a transparent image (with our desired ratio). We also add class="responsive-image" that's all in HTML.
<img src="img/transparent-ratio-conserver.png" class="responsive-image">
In the CSS, we set background-size to fit the and we choose the width of our image.
.responsive-image{
width: 100%;
background-size: 100% 100%;
}
and finally, we serve for every view-port the right image:
/* Retina display */
#media screen and (min-width: 1024px){
.responsive-image{
background-image: url('../img/retina.jpg');
}
}
/* Desktop */
#media screen and (min-width: 980px) and (max-width: 1024px){
.responsive-image{
background-image: url('../img/desktop.jpg');
}
}
/* Tablet */
#media screen and (min-width: 760px) and (max-width: 980px){
.responsive-image{
background-image: url('../img/tablet.jpg');
}
}
/* Mobile HD */
#media screen and (min-width: 350px) and (max-width: 760px){
.responsive-image{
background-image: url('../img/mobile-hd.jpg');
}
}
/* Mobile LD */
#media screen and (max-width: 350px){
.responsive-image{
background-image: url('../img/mobile-ld.jpg');
}
}
You can download the demo from here.
This is done with the background-size property:
background-size: cover;
Cover will make the image as small as it can be, whilst still covering the entirety of its parent, and maintaining its aspect ratio.
You may also want to try contain, which makes the image as big as it can be whilst still fitting inside the parent.
Source(s)
MDN - background-size CSS property
I think theres a better solution than contain or cover (which dind't work for me, btw).
Here's an example I recently used for a logo:
#logo{
max-width: 600px;
min-height: 250px;
margin: 0 auto;
background: url(../images/logo.png) no-repeat center;
background-size: 100%;
}
So now we have a responsive div with a backgound image, which size is set to the full width of the div.
Although there are other solutions.
% will scale the div to image size or the aspect ratio.
.responsive-image{
width: 100%;
background-image: url(x.jpg);
background-repeat:no-repeat;
background-size: 100% 100%;
}
You just need to pass the height to width ratio to the element.
For an image 1400x600;
1400:600 = 98:42
span( style="padding-bottom:42%;
width:98%;
background:url('/images/img1.jpg');
background-size:contain;
display:inline-block;")
would display the same as
img(src="/images/img.jpg" style="width:98%;")

Resources