Having a difficult time getting the CSS to work on different devices. I have a desktop, an iPad 4 and a Sony S8. With the following CSS media queries:
#media screen and (min-width: 1200px){
body{
background: red;
}
}
#media screen and (min-width: 768px) and (max-width: 979px){
body{
background: green;
}
}
#media screen and (max-width: 767px){
body{
background: purple;
}
}
#media screen and (max-width: 480px){
body{
background: tomato;
}
}
I have honestly tested MANY variations if this from examples from many different internet sources and articles and still cannot get it to work properly. With the configuration above, my desktop has a red browser background - the Sony and the iPad both have white. Can someone please provide me with some direction? Thank you.
UPDATE:
Changed my CSS to look like this:
/* smartphones, portrait iPhone, portrait 480x320 phones (Android) */
#media screen and (min-width:320px) {
body{
background: tomato;
}
}
/* smartphones, Android phones, landscape iPhone */
#media screen and (min-width:480px) {
body{
background: red;
}
}
/* portrait tablets, portrait iPad, e-readers (Nook/Kindle), landscape 800x480 phones (Android) */
#media screen and (min-width:600px) {
body{
background: blue;
}
}
/* tablet, landscape iPad, lo-res laptops ands desktops */
#media screen and (min-width:801px) {
body{
background: yellow;
}
}
/* big landscape tablets, laptops, and desktops */
#media screen and (min-width:1025px) {
body{
background: green;
}
}
/* hi-res laptops and desktops */
#media screen and (min-width:1281px) {
body{
background: purple;
}
}
Desktop is now purple, iPad is now yellow and the S8 is YELLOW??
This code works for me. You must have something overriding it.
/* smartphones, portrait iPhone, portrait 480x320 phones (Android) */
#media screen and (min-width:320px) {
body{
background: tomato;
}
}
/* smartphones, Android phones, landscape iPhone */
#media screen and (min-width:480px) {
body{
background: red;
}
}
/* portrait tablets, portrait iPad, e-readers (Nook/Kindle), landscape 800x480 phones (Android) */
#media screen and (min-width:600px) {
body{
background: blue;
}
}
/* tablet, landscape iPad, lo-res laptops ands desktops */
#media screen and (min-width:801px) {
body{
background: yellow;
}
}
/* big landscape tablets, laptops, and desktops */
#media screen and (min-width:1025px) {
body{
background: green;
}
}
/* hi-res laptops and desktops */
#media screen and (min-width:1281px) {
body{
background: purple;
}
}
Related
I am looking to find a way to create a sass function that alters the color of my font from white (on desktop) to black (on tablet and mobile). The reason being is that I am overlaying text on a video on desktop, but then on mobile the overlayed text switches to blocked text placed underneath the video, so the font color needs to change to black at that time.
I am relatively new to sass, but so far have tried this as a mixin (that did not work)
** I know this can be done with css but am looking to make this a bit more dynamic and reusable **
$color-media-sizes: (
"max1024": #000 or #fff,
null: #000 or #fff
);
with this function
#function color($mobile-color, $desktop-color){
#return ($mobile-color $desktop-color)
}
I don't think you really need to use SASS for this, CSS will do the trick.
Just put media queries and colors based on your device screen
(Source : https://gist.github.com/gokulkrishh/242e68d1ee94ad05f488)
Read this doc, it'll help you to understand media queries : https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries
/*
##Device = Desktops
##Screen = 1281px to higher resolution desktops
*/
#media (min-width: 1281px) {
/* CSS */
}
/*
##Device = Laptops, Desktops
##Screen = B/w 1025px to 1280px
*/
#media (min-width: 1025px) and (max-width: 1280px) {
/* CSS */
}
/*
##Device = Tablets, Ipads (portrait)
##Screen = B/w 768px to 1024px
*/
#media (min-width: 768px) and (max-width: 1024px) {
/* CSS */
}
/*
##Device = Tablets, Ipads (landscape)
##Screen = B/w 768px to 1024px
*/
#media (min-width: 768px) and (max-width: 1024px) and (orientation: landscape) {
/* CSS */
}
/*
##Device = Low Resolution Tablets, Mobiles (Landscape)
##Screen = B/w 481px to 767px
*/
#media (min-width: 481px) and (max-width: 767px) {
/* CSS */
}
/*
##Device = Most of the Smartphones Mobiles (Portrait)
##Screen = B/w 320px to 479px
*/
#media (min-width: 320px) and (max-width: 480px) {
/* CSS */
}
Mixin in SASS is like to create a "template" of a component. E.g. : A button
#mixin button($text, $background) {
background: $background;
border-radius: 10px;
color: $text;
padding: 0 15px;
text-decoration: none;
text-transform: uppercase;
}
// Then you can call it this way :
.success-button {
#include button("#FFF", "#0F0");
}
.error-button {
#include button("#FFF", "#F00");
}
Hope I could help
Maybe you can do that using just css media queries :
#media screen and (min-width: 980px) {
body {
color: red;
}
}
#media screen and (max-width: 979px) {
body {
color: blue;
}
}
#media screen and (max-width: 500px) {
body {
color: green;
}
}
Can anybody tell me why exactly this works
/* small desktop */
#media all and (max-width: 1200px) {
}
/* tablet */
#media all and (max-width: 1024px) {
}
/* mobile phone */
#media all and (max-width: 768px) {
}
but this not:
/* mobile phone */
#media all and (max-width: 768px) {
}
/* tablet */
#media all and (max-width: 1024px) {
}
/* small desktop */
#media all and (max-width: 1200px) {
}
since the last style always overwrite the previous style like :
[class=foo]{
background:red;
background:yellow;
}
output:
.foo background yellow
Simply: stylesheets cascade, so if the condition is true, it will override any previous. Your second example is a mobile-first approach, so you would need to use min-width.
/* mobile phone */
#media all and (min-width: 768px) {
}
/* tablet */
#media all and (min-width: 1024px) {
}
/* small desktop */
#media all and (min-width: 1200px) {
}
I am using some media queries for responsive versions, but with the smallest screen media query it breaks the whole code.
This is the structure of my media query!
/* Landscape phone to portrait tablet */*1
#media (min-width: 480px) and (max-width: 767px) {
/* All Smartphones in portrait and landscape ----------- */*2
#media only screen and (min-width: 321px) and (max-width: 479px) {
/* Styles */
/***** For HTC Mobile *******/*3
#media only screen and (min-width: 0px) and (max-width: 320px) {
With the above structure, the 3rd one media query isn't good at all.
I wrote following code in my style sheet with 3rd one media query.
/***** For HTC Mobile *******/*3
#media only screen and (min-width: 0px) and (max-width: 320px) {
.module-title {
font-size: 25px !important;
line-height: 25px;
}
}
And this code is making title of all versions into font-size 25.
Why is this not specific only for small screens and why it's taking effect on all versions?
And also, should I use "!important" on all versions for all classes?
like:
/* Landscape phone to portrait tablet */*1
#media (min-width: 480px) and (max-width: 767px) {
.module-title: 30px !important;
}
}
/* All Smartphones in portrait and landscape ----------- */*2
#media only screen and (min-width: 321px) and (max-width: 479px) {
/* Styles */
.module-title: 27px !important;
}
}
/***** For HTC Mobile *******/*3
#media only screen and (min-width: 0px) and (max-width: 320px) {
.module-title: 30px !important;
}
}
Any idea?
Remove the !important from the non-responsive class. and make sure you're closing media queries properly.
Example:
#media (max-width: 300px {
/*styles goes here*/
.tag {
} This is tag closing
} this is query closing
This syntax is very wrong:
/* Landscape phone to portrait tablet */*1
#media only screen and (min-width: 321px) and (max-width: 479px) {
/* Styles */
.module-title: 27px !important;
}
}
...because you can't just give a property to a selector!
The *1 after the comment above the code is outside the comment.
So the problem is that and the double braces. The !important below would only break other query if any of the conditions were met in other media-queries (only screen, min-width: 321px or max-width: 479).
#media only screen and (min-width: 321px) and (max-width: 479px) {
.module-title { font-size: 27px !important; }
}
It would not influence the media-query below, for instance:
#media only print and (min-width: 480px) {
.module-title { font-size: 27px; }
}
The syntax above would be the correct one.
I have these media queries to apply different styles for iPhone 4 and iPhone 5
/* iPhone 4 (landscape) */
#media only screen and (min-width:320px) and (max-width:480px) and (orientation:landscape) {
.background img {
height: 5px;
}
}
/* iPhone 4 (portrait) */
#media only screen and (min-width:320px) and (max-width:480px) and (orientation:portrait) {
.background img {
height: 10px;
}
}
/* iPhone 5 (landscape) */
#media only screen and (min-width:320px) and (max-width:568px) and (orientation:landscape) {
.background img {
height: 245px;
}
.logo img {
height: 205px;
width: 205px;
}
}
/* iPhone 5 (portrait) */
#media only screen and (min-width:320px) and (max-width:568px) and (orientation:portrait) {
.background img {
height: 210px;
}
.logo img {
height: 170px;
width: 170px;
}
.top-content h2 {
font-size: 1.8em;
line-height: 120%;
margin-bottom: 20px;
}
.main-container {
margin-top: 30px;
margin-left: 15px;
margin-right: 15px;
}
}
The problem is that on iPhone 5, the styles for iPhone 4 are applied too. How can I prevent this?
Another useful media feature is device-aspect-ratio.
Note that the iPhone 5 does not have a 16:9 aspect ratio. It is in fact 40:71.
iPhone < 5:
#media screen and (device-aspect-ratio: 2/3) {}
iPhone 5:
#media screen and (device-aspect-ratio: 40/71) {}
iPad:
#media screen and (device-aspect-ratio: 3/4) {}
Reference: Media Queries # W3C
In addition to Adam's helpful answer I've expanded this further to try and push my CSS to just the iPhone and iPad for both orientations in my case. The below may be useful for anyone looking at this question:
/* iPhone 5/5S Retina Display Portrait */
#media screen and (device-aspect-ratio: 40/71) and (max-device-width: 640px) and (orientation:portrait) {}
/* iPhone 5/5S Retina Display Landscape */
#media screen and (device-aspect-ratio: 40/71) and (max-device-width: 640px) and (orientation:landscape) {}
/* iPad 3/4/Air Retina Display Portrait */
#media screen and (device-aspect-ratio: 3/4) and (max-device-width: 1536px) and (orientation:portrait) {}
/* iPad 3/4/Air Retina Display Landscape */
#media screen and (device-aspect-ratio: 3/4) and (max-device-width: 1536px) and (orientation:landscape) {}
Media screen for the particular iPhone 4 is as follows:
#media only screen and (min-device-width: 320px) and (max-device-width: 480px) and (-webkit-device-pixel-ratio: 2) and (device-aspect-ratio: 2/3)
{
...
}
Here's the style.css where I've implement media queries:
body {
background: url("http://voxie.co.uk/assets/img/infr/bgartL.png"), url("http://voxie.co.uk/assets/img/infr/bgart.png");
background-repeat: no-repeat;
background-position:bottom left, bottom right;
}
/* Large desktop */
#media (min-width: 1200px)
body {
background: url("http://voxie.co.uk/assets/img/infr/bgartL.png"), url("http://voxie.co.uk/assets/img/infr/bgart.png");
background-repeat: no-repeat;
background-position:bottom left, bottom right;
}
/* Portrait tablet to landscape and desktop */
#media (min-width: 768px) and (max-width: 979px)
body {
background: #b5ebf9 !important;
}
/* Landscape phone to portrait tablet */
#media (max-width: 767px)
body {
background: #b5ebf9 !important;
}
/* Landscape phones and down */
#media (max-width: 480px)
body {
background: #b5ebf9 !important;
}
And I've added this in the HTML, for it to detect (I think this is right):
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
And obviously, linked to the above CSS file in the header.
But for some reason, the background image ALWAYS displays, instead of the colour #b5ebf9.
What am I doing wrong?! I've spent 1-2 days on and off trying to fix this! I tried...
Making separate CSS sheets for each device , linking to those style sheets with the media tag included (no reaction whatsoever)
Also tried removing the first body query from the above CSS - which just keeps the backgrounds in that color, across all devices, no background image at all! (I want the background image to show up on desktop screens.)
Please help!
You need those {} to regroup media queries:
body {
background: url("http://voxie.co.uk/assets/img/infr/bgartL.png"), url("http://voxie.co.uk/assets/img/infr/bgart.png");
background-repeat: no-repeat;
background-position:bottom left, bottom right;
}
/* Large desktop */
#media (min-width: 1200px){
body {
background: url("http://voxie.co.uk/assets/img/infr/bgartL.png"), url("http://voxie.co.uk/assets/img/infr/bgart.png");
background-repeat: no-repeat;
background-position:bottom left, bottom right;
}
}
/* Portrait tablet to landscape and desktop */
#media (min-width: 768px) and (max-width: 979px) {
body {
background: #b5ebf9 !important;
}
}
/* Landscape phone to portrait tablet */
#media (max-width: 767px){
body {
background: #b5ebf9 !important;
}
}
/* Landscape phones and down */
#media (max-width: 480px){
body {
background: #b5ebf9 !important;
}
}
Can you try this:
#media only screen and (max-width: 979px) and (min-width:768px) {}
#media only screen and (max-width: 767px) and (min-width:481px) {}
instead of just #meadia