Load an image only for mobile device - html

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.

Related

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.

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

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

How to remove a footer for smaller screen size

I am using a wordpress responsive theme, however I need the footer to not appear on any screen size smaller than an ipad. When viewed on an iphone 5 size screen the footer is too bulky and on some pages covers the content. In this instance it would be much neater to remove this for mobile phone size screens. Is there a CSS command, or any alternate method, to remove the footer below a certain screen size?
Many thanks in advance, Phil
I would say :
#media (min-width: 0px) and (max-width: 480px){
#footer {
display : none;
visibility : hidden;
}
}
Or shorter :
#media (max-width: 480px){
#footer {
display : none;
visibility : hidden;
}
}
A CSS media query should work
#media only screen and (max-width: enter breakpoint) {
#footer {
display: none;
}
}

Show one picture on tablet and different picture on pc

I have this problem...I am running page http://exploreprague.cz. In the right upper corner I have ribbon. But when I am looking on it on my tablet, its's overlapping my menu. So I figured that if there is way to show different picture(different kind of ribbon, not just differently styled) it could work. But I don't know if there is some kind of HTML/CSS/JS trick which can do it. Thanks
One of the better ways to achieve what you want would be to use CSS3 Media queries.
In the CSS file targeted at tablet-sized resolutions, you could set display:none on that particular image, and replace it with a new image that fits in with your smaller resolution better if you prefer.
For example (iPad portrait/landscape resolution):
#media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px) {
#oldImg { display:none; }
#newImg { display:block; }
}
Here is an example of how to use a responsive css:
Large desktop
#media (min-width: 1200px) {
#largeImage{
display: inline;
}
#smallImage{
display: none;
}
}
Portrait tablet to landscape and desktop
#media (min-width: 768px) and (max-width: 979px) {
#largeImage{
display: none;
}
#smallImage{
display: inline;
}
}
Landscape phone to portrait tablet
#media (max-width: 767px) {
/* do the same as tablets or include this width range in the tablet style*/
}
Landscape phones and down
#media (max-width: 480px) {
/* do the same as tablets or include this width range in the tablet style*/}
Just set the image display property according to the width of the screen.
use 2 images one with
display: none;
and the other with:
display: inline;
and switch between them on a narrower screen

Using css 'important' in both the media query cases

I am creating a mobile application in which I am getting some error.
here my core style is for desktop:
.abc{
width:1001px;
}
#media only screen and (max-width : 320px) {
.abc{
width:320px!important;
}
}
#media only screen and (max-width : 480px) {
.abc{
width:480px!important;
}
}
Here from the above styles only the style of 480px is applying for both the 320px and 480px.
Is there any alternate suggestion to come over this problem.
This is because max-width:480px; still targets 320px too. Change the last one to:
#media only screen and (min-width: 321px) and (max-width: 480px) {
.abc {
width: 480px !important;
}
}
and this will stop that query affecting anything below 321px.
It doesn't look like you need !important This fix has nothing to do with that so I would remove that if I were you, it may mess things up in the future
An alternative solution would be to move the 320px query below the 480px. They both have the same specificity so the one that comes last in the cascade would take precedence.
set a min-width
.abc {
width: 1001px;
}
#media only screen and (max-width: 320px) {
.abc {
width: 320px;
}
}
/* set a min-width here, so these rules don't apply for screens smaller than 321px */
#media only screen and (min-width: 321px) and (max-width: 480px) {
.abc{
width: 480px;
}
}
If I'm right you should be able to remove the !important syntax too...