How to make CSS background-image responsive? [duplicate] - html

This question already has answers here:
Responsive css background images
(19 answers)
Closed 8 years ago.
Ok, so I came across this solution to making background-image responsive:
Responsive css background images
My apologies for re-posting the question, but literally none of their solutions worked for me.
HTML:
<div id="fourohfour_home"></div>
CSS:
#fourohfour_home {
background-image:url('image.png');
height:120px;
width:120px;
}
How exactly would I make this responsive? e.g. When the width of the page is less than the width of the image it scales correctly.

You simply need to define width and height of #fourohfour_home in order for the background image to know in what container to be contained. Something like:
#fourohfour_home{
background-image:url('https://www.example.com/img/404_home.png');
background-repeat:no-repeat;
background-size:contain;
background-position:center;
height: 120px;
width: 20%;
}

You should use media queries as always, and then set the dimensions for the background:
#media all and (max-width: 639px) and (min-width: 320px) {
#fourohfour_home {
background-size: 320px 240px;
}
}
In this example, I changed the size of an image you gave, for the case that the width is few than 640. if it is greater, I use another resolution:
#media all and (max-width: 799px) and (min-width: 640px) {
#fourohfour_home {
background-size: 640px 480px;
}
}
I could even change the image, if I wanted an image with better resolution:
#media all and (max-width: 799px) and (min-width: 640px) {
#fourohfour_home {
background-image: url('my-image-640.png');
background-size: 640px 480px;
}
}
Edit this belongs to the same css definition:
/* for default - too short - size */
#media all and (max-width: 319px) {
#fourohfour_home {
background-image: url('my-image-very-small.png'); /*in the case you have another image for this resolution - usually you'll never have these sizes as for today*/
background-size: 200px 150px;
}
}
#media all and (max-width: 639px) and (min-width: 320px) {
#fourohfour_home {
background-image: url('my-image-320.png'); /*in the case you have another image for this resolution*/
background-size: 320px 240px;
}
}
#media all and (max-width: 799px) and (min-width: 640px) {
#fourohfour_home {
background-image: url('my-image-640.png'); /*in the case you have another image for this resolution*/
background-size: 640px 480px;
}
}
#media all and (max-width: 1023px) and (min-width: 800px) {
#fourohfour_home {
background-image: url('my-image-800.png'); /*in the case you have another image for this resolution*/
background-size: 800px 600px;
}
}
/* this one goes for default images - bigger sizes */
#media all and (min-width: 1024px) {
#fourohfour_home {
background-image: url('my-image-1024.png'); /*in the case you have another image for this resolution*/
background-size: 1024px 768px;
}
}
/* this will have no #media, so will apply for every resolution */
#fourohfour_home {
background-repeat:no-repeat;
background-position:center;
width: 100%; /* assuming you want to expand your div in a screen-dependent way */
}

In order for responsiveness, you often have to use percentages instead of pixel values. So, if you set the height and width for the element to 100% and set all of its parent elements to the full height and width that you want them (including html and body), the code from the other question should work. Try http://goo.gl/2GrwyR

Related

I have an image as background inside a div.I want to make picture fit in responsive

I tried using photoshop and I need to know how much height should be given for image in photoshop for each widths 575.98px ,767.98px,991.98px,1199.98px to get a good image.
#media (max-width: 575.98px) { ... }
#media (max-width: 767.98px) { ... }
#media (max-width: 991.98px) { ... }
#media (max-width: 1199.98px) { ... }
Whatever the image class, id or just the tag is, apply a width of 100%;
img {
width: 100%;
}

Load an image only for mobile device

I want to know if it is possible for my front page to load an image dedicated for mobile users only. I know it is possible with javascript, but I want to know if I can achieve the same using CSS only. I have tried to do some research but can't find what I am looking for. I do not mean resizing the image based on the screen size, but loading a mobile-friendly image.
Make use of media query to change background-image at different screen resolutions as below,
div{
width:100%;
height:400px;
background:url('http://placehold.it/350x150/f22/fff');
background-size:100% 100%;
}
#media screen and (max-width : 480px){
div{
background:url('http://placehold.it/480x150/f12/fff');
background-size:100% 100%;
}
}
#media screen and (max-width : 320px){
div{
background:url('http://placehold.it/320x150/e12/fff');
background-size:100% 100%;
}
}
<div></div>
Check this jsFiddle.
You can use media queries to apply classes depending on the screen size.
#img {
display: none;
}
/* Large Devices, Wide Screens */
#media only screen and (max-width : 1200px) {
}
/* Medium Devices, Desktops */
#media only screen and (max-width : 992px) {
}
/* Small Devices, Tablets */
#media only screen and (max-width : 768px) {
}
/* Extra Small Devices, Phones */
#media only screen and (max-width : 480px) {
#img{
display: block;
}
}
/* Custom, iPhone Retina */
#media only screen and (max-width : 320px) {
}
Yes this is possible. You can use Media Querys. Example CSS:
#yourimage {
display: none;
}
#media (max-width: 500px) {
#yourimage {
display: block;
}
}
This code is for html images tho. Here is a JSFiddle:
https://jsfiddle.net/vqfLokpa/
Just hit run and then start to make your browser window smaller.
Based on your comment to #NullDev, you will need to create a div at the appropriate size and apply the image as a background image on an element in order to condition what is loaded via CSS.
For example:
HTML:
<div id="image"></div>
CSS:
#image {
position: relative;
width: 400px;
height: 400px;
background-image:url('/path/to/image');
background-size: cover;
background-repeat: no-repeat;
}
Then apply the media query:
#media only screen and (max-width : 480px) {
#image{
background-image:url('/path/to/mobile/image');
}
}
I hope this helps.

Loading different sized background images based on browser width using CSS?

I have a page that I would like to have different images load as the background based on the width of the display port. I understand how to do it with the picture tag and the srcset tags for regular images but I'm wondering if there is a way to do it when working with the background image of a tag?
The css that I am currently using is
body{
background-image:url(/Content/Images/CloudGradient_480.jpg);
background-repeat:no-repeat;
background-size:cover;
background-position:center;
}
Use media queries, but give them both an upper and a lower limit. Otherwise all the images are going to be loaded, even if only one is displayed. This way, only one image will be loaded, which saves bandwidth and loading time:
body {
background-repeat:no-repeat;
background-size:cover;
background-position:center;
}
#media screen and (min-width: 480px) and (max-width: 800px) {
body {
background-image:url(/Content/Images/CloudGradient_800.jpg);
}
}
#media screen and (min-width: 801px) and (max-width: 1200px) {
body {
background-image:url(/Content/Images/CloudGradient_1200.jpg);
}
}
#media screen and (min-width: 1200px) {
body {
background-image:url(/Content/Images/CloudGradient_2000.jpg);
}
}
You can use media query
For example:
body{
background-image:url(/Content/Images/CloudGradient_1280.jpg);
background-repeat:no-repeat;
background-size:cover;
background-position:center;
}
#media max-width: 500px {
body {
background-image:url(/Content/Images/CloudGradient_480.jpg);
}
}
#media max-width: 700px {
body {
background-image:url(/Content/Images/CloudGradient_680.jpg);
}
}
try it using CSS media queries, you would have different background image for different screen or port sizes.

Div with background image spilling out of parent div in IE11

I understand there's a gazillion questions on this issue but none of the solutions on any of them seem to work for me. This is what my markup looks like:
<div class="immersion-div">
<div class="immersion-div-image"></div>
</div>
As you see, it's a fairly straightforward setup with one div containing another. The parent div is styled to adapt its height to the device screen resolution using media queries. Here's how the two divs are styled:
Parent div:
#media (min-width: 2000px) { .immersion-div { height: 1307px; } }
#media (max-width: 1999px) and (min-width: 1401px) { .immersion-div { height: 1000px; } }
#media (max-width: 1400px) and (min-width: 750px) { .immersion-div { height: 500px; } }
#media (max-width: 749px) and (min-width: 300px) { .immersion-div { height: 300px; } }
#media (max-width: 299px) { .immersion-div { height: 136px; } }
Child div (with the image background):
.immersion-div-image {
background: url(../../bootstrap/img/homepage/spanish_immersion.jpg) no-repeat center center;
background-size: cover;
height: 100%;
}
The image serving as the background is 2880px by 900px (although that should be inconsequential in this scenario) and the resolution of the screen on which I tested this is 1366px wide. Per the defined media queries, the height of parent div should evaluate to 500px since the screen falls in the 750px-1400px category. However, on Internet Explorer, the div seems to render with a height of 1000px! This issue is only affecting IE whereas all other browsers are rendering the divs fine. What could be the problem? Before anyone suggests using background-size: contain, I must admit I tried it and it messes up the aspect ratio leaving a blank band at the bottom of the div which is why I don't want to go that route. Besides, I want to understand why cover wouldn't work on IE when it does just fine on all other browsers.
Just in case it helps, the site in question is peppyburro.com and the affected divs are on the last green image on the home page.

Fixed background image without a set height

I have a div element that must have a background image that is fixed so when you scroll the content rolls over it. My issue is that I have to set a height for that specific div element in order to see it. That is because there isn't any content in that div.
CSS
#top-banner{
background-image: url(../img/grey.jpg);
background-attachment:fixed;
height:700px;
background-repeat: no-repeat;
background-size: 100% auto;
}
HTML
<div class="container-fluid">
<div class="row" >
<div class="col-sm-12" id="top-banner"></div>
</div>
<div class="row">
<div class="col-sm-12 text-center" >
<h1 class="band-name">Lorem Ipsum</h1>
</div>
</div>
</div>
This gives me what I want for larger screens:
But as you shrink the browser, like you are on a phone or tablet, the height for that div pushes all the content down making it look unappealing:
Is there a way to not give it a specific height so the content is not pushed down on smaller screen but still have the fixed background image?
EDIT
Here is a fiddle to check out. http://jsfiddle.net/0xbfhwnt/
I reiterate: It looks fine at first but when you make the browser smaller the image shrinks like it is supposed to but the height of the div stays keeping the content below the image instead of flush with the background image div.
Have you considered something along the lines of media queries?
Here's a first iteration:
http://jsfiddle.net/0xbfhwnt/2/
#media (min-width: 400px) and (max-width: 700px) {
#top-banner{
height: 200px; }
}
#media (min-width: 200px) and (max-width: 399px) {
#top-banner{
height: 100px; }
}
UPDATE
So, using media queries, you can track the size of the main div all the way down to the smallest screen size.
In this example, all the whitespace is gone.
http://jsfiddle.net/0xbfhwnt/7/
#media (min-width: 500px) and (max-width: 800px) {
#top-banner {
height: 200px;}
}
#media (min-width: 375px) and (max-width: 499px) {
#top-banner {
height: 150px;}
}
#media (min-width: 250px) and (max-width: 374px) {
#top-banner {
height: 100px;}
}
#media (min-width: 50px) and (max-width: 249px) {
#top-banner {
height: 50px;}
}
Of course, the smaller the range between min-width and max-width, the smoother the transition would be.