I have a HTML site and that has some images that I do not want to be seen on a mobile devices or on desktop.
for example:
HTML Code: (index.html)
<link rel="stylesheet" type="text/css" href="css/style.css" />
//Image for desktop
<img class="desktop" border="0" src="images/logo.png">
//Image for mobile device
<img class="mobile" border="0" src="images/logo-mobile.png">
CSS Code: (style.css)
/* #Desktop
================================================== */
.mobile {
display: none;
}
/* #Mobile
================================================== */
#media only screen and (max-device-width: 480px) {
.desktop {
display: none;
}
}
I use this CSS code but does not work.
Use CSS3 Media Queries
#media (min-width: 1100px) {
h1:after {
content: 'Large screen';
}
}
#media (max-width: 1100px) {
h1:after {
content: 'Medium screen';
}
}
#media (max-width: 480px) {
h1:after {
content: 'Small screen';
}
}
-DEMO-
The answer is in media queries itself...i used this and it seems to work for me
#media screen and (max-device-width:480px)
The problem is that you are not resetting the display: none on the .mobile class on mobile screen, and your .mobile class elements will remain hidden. You have to add the following to your media query in order to make the .mobile class visible:
.mobile {
display: block;
}
Final coode:
/* #Desktop
================================================== */
.mobile {
display: none;
}
/* #Mobile
================================================== */
#media only screen and (max-device-width: 480px) {
.desktop {
display: none;
}
.mobile {
display: block;
}
}
EDIT
As I can see, you will have problems to detect which device is a mobile device based on resolution, so I will recommend you to detect it by using the User Agent string, it will have bigger success in your case.
Here is what you should do:
1) Include this in a javascript file loaded on the page:
$(document.body).ready(function(){
var isMobile = (/android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test(navigator.userAgent.toLowerCase()));
if(isMobile) {
$("html").addClass("mobile-device");
} else {
$("html").addClass("desktop-device");
}
});
2) Change your css to:
/* #Desktop
================================================== */
.desktop-device .mobile {
display: none;
}
/* #Mobile
================================================== */
.mobile-device .desktop {
display: none;
}
This should work for most(maybe 99.9%) of the mobile devices that cand be bought. Hope this helps you.
use media-queries-for-standard-devices
use it like this
/* #Desktop
================================================== */
.mobile {
display: none;
}
.desktop {
display: block;
}
/* #Mobile
================================================== */
#media only screen and (max-width: 480px) {
.desktop {
display: none;
}
.mobile {
display: block;
}
}
For HTC One try
#media screen and (device-width: 360px) and (device-height: 640px) and (-webkit-device-pixel-ratio: 3){
}
NOTE Don't forget to add this inside tag
<meta name="viewport" content="width=device-width, initial-scale=1">
css:
#media screen and (max-device-width: 480px) {
.desktop {
display: none;
}
.mobile {
display: block;
}
}
IT will work. see link http://cssmediaqueries.com/what-are-css-media-queries.html
Related
I am trying to make a simple code for desktop and mobile display.
<html>
<head>
<style>
#media (min-device-width: 770px) {
#containermobile {display:none;}
}
body {
background-color: #000000;
}
#media (max-device-width: 769px) {
#containerPC {display:none;}
}
body {
background-color: #ffffff;
}
</style>
</head>
<body>
<div id="containerPC">pc</div>
<div id="containermobile">mobile</div>
</body>
</html>
Yet the background color is not displaying. What am I doing wrong ?
You didn't put the body blocks inside the #media blocks, so the second one just overrides the first one, and you get white background.
Also, for the purpose of testing, you should probably avoid using #000000 (black) and #ffffff (white). The former will hide the text, and the latter is the default background color so you can't be sure whether your code worked.
The following is an example of what will work correctly:
#media (min-device-width: 770px) {
#containermobile {display:none;}
body {
background-color: #444444;
}
}
#media (max-device-width: 769px) {
#containerPC {display:none;}
body {
background-color: #cccccc;
}
}
I have this real code to show and hide two divs depending on device type:
Problem: in Android the #div-desktop is shown.
I need to show only div#div-mobile on mobile devices
I need to show only div#div-desktop on desktop devices
CSS
#media screen and (min-width: 0px) and (max-width: 700px) {
#div-mobile { display: block; }
#div-desktop { display: none; }
}
#media screen and (min-width: 701px) and (max-width: 3000px) {
#div-mobile { display: none; }
#div-desktop { display: block; }
}
HTML
<div id="div-mobile">m<img width="100%" height="auto" src="http://uggafood.com/wp-content/uploads/2017/03/ugga-food_mobile.jpg" alt="" width="600" height="1067" /></div>
<div id="div-desktop">d<img width="100%" height="auto" src="http://uggafood.com/wp-content/uploads/2017/03/ugga-food_desktop.jpg" alt="" width="1920" height="1280" /></div>
I have just checked the live link.
Meta tag is missing for responsive devices.
Add the below code for detecting mobile devices.
<meta name="viewport" content="width=device-width, initial-scale=1.0">
EDIT
After seeing your site, you need to add:
<meta name="viewport" content="width=device-width, initial-scale=1">
You can just use min-width
Also, don't use width/height html tags in img use CSS instead
img {
max-width: 100%
}
#div-desktop {
display: none;
}
#media screen and (min-width: 701px) {
#div-mobile {
display: none;
}
#div-desktop {
display: block;
}
}
<div id="div-mobile">m<img src="http://uggafood.com/wp-content/uploads/2017/03/ugga-food_mobile.jpg" alt="" /></div>
<div id="div-desktop">d<img src="http://uggafood.com/wp-content/uploads/2017/03/ugga-food_desktop.jpg" alt="" /></div>
Change your media queries to the following.
Just change the widths to whatever you'd like. The top media query says if the min width is above standard mobile sizes show xyz, then the second one says if it's below do abc.
#media screen and (min-width: 769px) {
#div-mobile { display: none; }
#div-desktop { display: block; }
}
#media screen and (max-width: 768px) {
#div-mobile { display: block; }
#div-desktop { display: none; }
}
this line only find the size resolution of the user system and gives to your css code
<meta name="viewport" content="width=device-width, initial-scale=1.0">
If you use sass, this is a very efficient way of effecting media queries and writing appropriate css per devise: https://eduardoboucas.github.io/include-media
include-media is a Sass library for writing CSS media queries in an easy and maintainable way, using a natural and simplistic syntax.
The CSS is in a .scss file and being minified with Gulp which works without any issues. I'm testing the responsiveness is Chrome(resizing the webpage). I have added
<meta name="viewport" content="width=device-width, initial-scale=1">
HTML
<div class="container text-center" >
<h1 class="banner-title">Welcome Refugees</h1>
</div>
.SCSS
.banner-title {
margin-top: 275px;
font-size: 700%;
}
#media only screen and (min-width: 400px)
and (max-width: 736px)
and (-webkit-min-device-pixel-ratio: 3) {
.banner-title {
font-size: 500%;
}
}
Is this a common issue?
I've been using $sm, $md, $lg, $xl as SASS variables to represent different sizes.
$sm = 600; // #media screen queries don't compile correctly
$sm = 600px; // #media screen queries work as expected
Hopefully this helps someone
i found this on a blog
http://thesassway.com/intermediate/responsive-web-design-in-sass-using-media-queries-in-sass-32
profile-pic {
float: left;
width: 250px;
}
#media screen and (max-width: 320px) {
.profile-pic {
width: 100px;
float: none;
}
}
#media screen and (min-width: 1200px) {
.profile-pic {
float: right;
}
}
this is my first post on this site so I'll try doing things right but sorry if I format things wrong or do anything stupid.
I'm doing an assignment for school due on Thursday June 2nd so I'm really looking for a quick answer as kind of desperate. I need my website to be able to re size for different screen resolutions, I've looked at other answers on this site but most are in reference to making mobile websites. I've read about the media queries rule but everything I read about it is just about making mobile websites, but that looks like a good solution. Really appreciate any help I can get :), here's my code for anyone to look at:
HTML:
<!DOCTYPE html>
<html>
<link href='https://fonts.googleapis.com/css?family=Raleway:300' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Roboto:300' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="https://code.getmdl.io/1.1.3/material.indigo-pink.min.css">
<head>
<meta charset="utf-8">
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ann Mockett</title>
<script src="code.js" type="text/javascript" charset="utf-8"></script>
<link href="style.css" rel="stylesheet" type="text/css">
<!__NEEDS DESCRIPTION__>
<meta name="description" content="Description of Site">
<!__NEEDS KEYWORDS__>
<meta name="keywords" content="Selection of Search Terms">
</head>
<body>
<nav>
<ul>
<li><div id="Ann-Nav"><a class="active" href="index.html">Ann Mockett</a></div></li>
<li class="dropdown">
Work
<div class="dropdown-content">
TV
Film
Other
</div></li>
<li>Contact</li>
<li>About</li>
</ul>
</nav>
<div class="bodyimg">
<img src="images/placeholder.png">
</div>
<div class="box"><h3>TV Shows</h3><img src="images/placeholder.png"></div>
<div class="box"><h3>Movies</h3><img src="images/placeholder.png"></div>
<div class="box"><h3>Other Projects</h3><img src="images/placeholder.png"></div>
</body>
CSS:
html{
background-color: #fafafa;
}
h3 {
margin: 0;
font-size: 25px;
font-family: font-family: 'Roboto', sans-serif;
}
body{
width: auto;
margin: auto;
text-align: center;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #009688;
box-shadow: 0px 3px 9px 0px rgba(0,0,0,0.67);
}
li {
float: left;
}
li a, .dropbtn {
display: block;
color: black;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover, .dropdown:hover .dropbtn {
background-color: #004D40;
}
li.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
.dropdown-content a {
color: black;
background-color: #80CBC4;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #4DB6AC;
}
.dropdown:hover .dropdown-content {
display: block;
}
#Ann-Nav {
font-family: 'Raleway', sans-serif;
font-size: 30px;
}
.bodyimg img{
height: 390px;
width: 100%;
display: block;
margin-bottom: 16px;
}
.box {
background-color: #A7FFEB;
height: 230px;
width: 32%;
float: left;
margin-left: 15px;
margin-bottom: 12px;
display: block;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
.box img{
height: 175px;
width: 95%;
display: block;
margin: auto;
position: relative;
top: 50%;
transform: translateY(-63%);
}
Thanks for any help.
You might want to use media queries.
#media screen and (max-width: 480px) {
body {
background-color: lightgreen;
}
}
As for this example, when the screen width gets smaller than 480px, the background-color changes to lightgreen. You can find more on W3Schools!
You could also use bootstrap for a responsive design and pre-made CSS. It's less work and a great solution.
Since you have asked a HW question here, I want to reply in a line : use bootstrap
As Nitsan, Tawfiq, and Danooned said, you should look into media queries. They allow you to design certain aspects of your page differently for different device specifications. Here's a link to get you started:
https://developers.google.com/web/fundamentals/design-and-ui/responsive/fundamentals/use-media-queries?hl=en
** If your teacher/professor allows you to use bootstrap, I would definitely start there, as it does most of the heavy lifting for you.
I hope this can be an answer for what you need. I use it to create html template on just 20 minutes from zero to about 5 responsive pages.
http://tools.qixstudio.com/reycss.css
example of web : http://www.supersukses.net/, http://microsite.detik.com/display/kabar-dpd/, http://microsite.detik.com/display/preview-telkom/.
It's not a perfect css yet, but hopely can be a shortcut to build your responsive website.
Below are the media queries for all devices. You can use them to make your website responsive.
/* Smartphones (portrait and landscape) ----------- */
#media only screen
and (min-device-width : 320px)
and (max-device-width : 480px) {
/* STYLES GO HERE */
}
/* Smartphones (landscape) ----------- */
#media only screen
and (min-width : 321px) {
/* STYLES GO HERE */
}
/* Smartphones (portrait) ----------- */
#media only screen
and (max-width : 320px) {
/* STYLES GO HERE */
}
/* iPads (portrait and landscape) ----------- */
#media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px) {
/* STYLES GO HERE */
}
/* iPads (landscape) ----------- */
#media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : landscape) {
/* STYLES GO HERE */
}
/* iPads (portrait) ----------- */
#media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : portrait) {
/* STYLES GO HERE */
}
/* Desktops and laptops ----------- */
#media only screen
and (min-width : 1224px) {
/* STYLES GO HERE */
}
/* Large screens ----------- */
#media only screen
and (min-width : 1824px) {
/* STYLES GO HERE */
}
/* iPhone 5 (portrait & landscape)----------- */
#media only screen
and (min-device-width : 320px)
and (max-device-width : 568px) {
/* STYLES GO HERE */
}
/* iPhone 5 (landscape)----------- */
#media only screen
and (min-device-width : 320px)
and (max-device-width : 568px)
and (orientation : landscape) {
/* STYLES GO HERE */
}
/* iPhone 5 (portrait)----------- */
#media only screen
and (min-device-width : 320px)
and (max-device-width : 568px)
and (orientation : portrait) {
/* STYLES GO HERE */
}
Also You can visit https://css-tricks.com/css-media-queries/
I have below script, it's doing fine in firefox and chrome. ie9 doesn't show the change of colors.
<style type="text/css">
body {
background-color: #FF77BB;
}
#media screen and (max-width: 960px) {
body {
background-color: #8A2BE2;
}
}
#media screen and (max-width: 768px) {
body {
background-color: #FF8C00;
}
}
#media screen and (max-width: 550px) {
body {
background-color: #7FFF00;
}
}
#media screen and (max-width: 320px) {
body {
background-color: #CC0011;
}
}
}
</style>
Works fine for me, just get rid of the extra } at the very end, the other browsers may not make it an issue but IE9 is very picky, if one thing is off it breaks the whole thing.