Media Queries breakpoints clarification - html

I've setup these breakpoints
X-Small devices (portrait phones, less than 576px)
#media (max-width: 576px) {
body {
background-color: red;
}
}
Small devices (landscape phones, less than 768px)
#media (max-width: 768px) {
body {
background-color: blue;
}
}
Medium devices (tablets, less than 992px)
#media (max-width: 992px) {
body {
background-color: green;
}
}
Large devices (desktops, less than 1200px)
#media (max-width: 1200px) {
body {
background-color: purple;
}
}
X-Large devices (large desktops, less than 1400px)
#media (max-width: 1400px) {
body {
background-color: yellow;
}
}
Why if I try to resize the screen the colour is always yellow and doesn't change according with the breakpoints?

Because max-width: 1400px is also true for smaller screens. Either place the larger screens at top so that the queires for smaller screens overwrite the value or use #media (min-width: value) and (max-width: value) { ... }
Starting with the largest to the smallest screen:
#media (max-width: 1400px) {
body {
background-color: yellow;
}
}
#media (max-width: 1200px) {
body {
background-color: purple;
}
}
#media (max-width: 992px) {
body {
background-color: green;
}
}
#media (max-width: 768px) {
body {
background-color: blue;
}
}
#media (max-width: 576px) {
body {
background-color: red;
}
}
Using the and-keyword:
#media (max-width: 576px) {
body {
background-color: red;
}
}
#media (min-width: 577px)
and (max-width: 768px) {
body {
background-color: blue;
}
}
#media (min-width: 769px)
and (max-width: 992px) {
body {
background-color: green;
}
}
#media (min-width: 993px)
and (max-width: 1200px) {
body {
background-color: purple;
}
}
#media (min-width: 1201px)
and (max-width: 1400px) {
body {
background-color: yellow;
}
}

Related

Media Query works in the first screen but doesn't work in the next

I am currently working on making my website responsive. It has been designed in a 14inch laptop which has a screen size of around 1100px. To make it responsive I have started from small screens:
#media only screen and (max-width: 399px)
Now when I move onto the next media query, no code actually works(but it works below 400px).
These are the media query I am planning to work on:
#media only screen and (min-width: 400px) and (max-width: 767px)
#media only screen and (min-width: 768px) and (max-width: 991px)
#media only screen and (min-width: 992px) and (max-width: 1199px)
#media only screen and (min-width: 1200px) and (max-width: 1399px)
Is there anything wrong with writing those statements? Or is the order wrong?
#media screen and (min-width: 400px) and (max-width: 767px) {
.content {
right: 0px !important;
}
}
#media only screen and (min-width: 768px) and (max-width: 991px) {}
#media only screen and (min-width: 992px) and (max-width: 1199px) {}
#media only screen and (min-width: 1200px) and (max-width: 1399px) {}
#media only screen and (min-width: 1400px) and (max-width: 1599px) {}
#media only screen and (min-width: 1600px) and (max-width: 1799px) {}
#media only screen and (min-width: 1800px) and (max-width: 1999px) {
/*.content {
position: absolute;
right: 310px;
top: 234px;
width: 880px;
}
.background-overlay {
height: 600px;
}
#about h1{
font-size: 80px;
}
.content .aboutUs {
padding: 60px 45px 0px 45px;
}
.content .aboutUs div {
margin-top: 35px;
height: 382px;
}
.content .aboutUs p {
margin-right: 32px;
font-size: 22px;
line-height: 25px;
font-weight:400;
-webkit-font-smoothing: antialiased;
font-smoothing: antialiased;
color: #777;
}
}
*/
}
An improvements I suggest is that you have 3 media queries like so:
(max-width: 640px)
(min-width: 641px) and (max-width: 800px)
(min-width: 801px) and (max-width: 1024px)
(min-width: 1025px)

Media query does not work with pixels defined

<div class="col-md-4 saldos">
<div class="saldo">
<p>Saldo</p>
<h1>R$ 713,00</h1>
</div>
</div>
#media (max-width: 800px) {
.saldo {
width: 150px;
}
}
I defined 800px, but it just works on 530px. Does anyone know why this happens?
TRY:
#media only screen and (max-width: 800px) {
.saldo {
width: 150px;
}
.saldo h1 {
font-size: 25px;
}
}

reduce repetitive media query head in sass

I use mobile first principle for my sass (css), but I found quite a pattern which is quite disturbing.
.innerWrap {
display: flex;
align-items: center;
&:first-child {
margin-right: 20px;
#media only screen and (min-width: 640px) {
margin-right: 0;
}
}
#media only screen and (min-width: 640px) {
flex: 1;
}
}
Imagine there will be more screen size, there will be
#media only screen and (min-width: 1200px)
#media only screen and (min-width: 1200px)
#media only screen and (min-width: 1200px)
everywhere, how to solve this?
You can also set a map with all you breakpoints (you can call them as you want) and use it in a mixin.
// your variables.scss file
$breakpoints:(
sm: 640px,
md: 1200px,
lg: 1400px,
xl: 1900px
);
// your mixins.scss file
#mixin min-width($breakpoint){
#media only screen and (min-width:#{map-get($breakpoints, $breakpoint)}) {
#content;
}
}
// your module.scss file
.innerWrap {
display: flex;
align-items: center;
&:first-child {
margin-right: 20px;
#include min-width(sm){
margin-right: 0;
}
}
#include min-width(sm){
flex: 1;
}
}
you can use #mixin to manage it
https://jsfiddle.net/wyd6pxnh/2/
#mixin mediaQuery($point) {
$mq-device1: "(min-width: 640px)";
$mq-device2: "(min-width: 1200px)";
#if $point=="device1" {
#media #{$mq-device1} {
#content;
}
}
#else if $point=="device2" {
#media #{$mq-device2} {
#content;
}
}
}
.innerWrap {
display: flex;
align-items: center;
&:first-child {
margin-right: 20px;
#include mediaQuery('device1') {
margin-right: 0;
}
}
#include mediaQuery('device1') {
flex: 1;
}
}
Output
.innerWrap {
display: flex;
align-items: center;
}
.innerWrap:first-child {
margin-right: 20px;
}
#media (min-width: 640px) {
.innerWrap:first-child {
margin-right: 0;
}
}
#media (min-width: 640px) {
.innerWrap {
flex: 1;
}
}

Multiple media queries on one item

Is it possible to have multiple media queries for one item in code?
Let me explain,
I'm trying to have a video background work on multiple devices, so far so good, it works, but when on mobile the majority of the video is cut off, same for tablet. So I have 3 videos, one full screen, one 960 x 540 and another 480 x 270. I want to set up a media query that will allow me to switch between these videos depending on the size of the screen. So far I have:
HTML:
<video autoplay="autoplay" loop="loop" class="bgvideo">
<source src="videos/videohd.mp4" type="video/mp4">
</video>
<video autoplay="autoplay" loop="loop" class="bgvideo-2">
<source src="videos/video960x540.mp4" type="video/mp4">
</video>
<video autoplay="autoplay" loop="loop" class="bgvideo-3">
<source src="videos/video480x270.mp4" type="video/mp4">
</video>
CSS:
#media (max-width: 960px) {
.bgvideo {
display: none;
}
}
#media (min-width: 960px) {
.bgvideo {
display: block;
}
}
#media (max-width: 960px) {
.bgvideo-2 {
display: block;
}
}
#media (min-width: 960px) {
.bgvideo-2 {
display: none;
}
}
#media (max-width: 480px) {
.bgvideo-2 {
display: none;
}
}
#media (min-width: 480px) {
.bgvideo-2 {
display: block;
}
}
#media (max-width: 480px) {
.bgvideo-3 {
display: block;
}
}
#media (min-width: 480px) {
.bgvideo-3 {
display: none;
}
}
I'm assuming there is an easier way to do this than what I am doing - as it doesn't work too well!
Thanks! ^^
you can simplify a bit your mediaqueries like this:
#media (min-width: 480px) {
.bgvideo-2 {
display: block;
}
.bgvideo-3 {
display: none;
}
}
#media (min-width: 960px) {
.bgvideo {
display: block;
}
.bgvideo-2 {
display: none;
}
}
#media (max-width: 960px) {
.bgvideo {
display: none;
}
.bgvideo-2 {
display: block;
}
}
#media (max-width: 480px) {
.bgvideo-2 {
display: none;
}
.bgvideo-3 {
display: block;
}
}

Making font size relative to screen width without repetition in CSS

In my webpage I want my fonts to have font sizes according to the width of the page. Currently I'm using #media screen and (max-width: blah px) to adjust to the screen size, but when I use it for every 100px it gets really long, and it seems like there must be a better way to do this.
#media screen and (max-width: 1500px) {
#todayDate, #cosmicDate, #cosmicdatelabel{
font-size: 70px;
}
}
#media screen and (max-width: 1400px) {
#todayDate, #cosmicDate, #cosmicdatelabel{
font-size: 65px;
}
}
#media screen and (max-width: 1300px) {
#todayDate, #cosmicDate, #cosmicdatelabel{
font-size: 60px;
}
}
#media screen and (max-width: 1200px) {
#todayDate, #cosmicDate, #cosmicdatelabel{
font-size: 55px;
}
}
#media screen and (max-width: 1100px) {
#todayDate, #cosmicDate, #cosmicdatelabel{
font-size: 50px;
}
}
#media screen and (max-width: 1000px) {
#todayDate, #cosmicDate, #cosmicdatelabel{
font-size: 45px;
}
}
#media screen and (max-width: 900px) {
#todayDate, #cosmicDate, #cosmicdatelabel{
font-size: 40px;
}
}
#media screen and (max-width: 800px) {
#todayDate, #cosmicDate, #cosmicdatelabel{
font-size: 35px;
}
}
#media screen and (max-width: 700px) {
#todayDate, #cosmicDate, #cosmicdatelabel{
font-size: 30px;
}
}
#media screen and (max-width: 600px) {
#todayDate, #cosmicDate, #cosmicdatelabel{
font-size: 35px;
}
}
#media screen and (max-width: 500px) {
#todayDate, #cosmicDate, #cosmicdatelabel{
font-size: 20px;
}
}
#media screen and (max-width: 400px) {
#todayDate, #cosmicDate, #cosmicdatelabel{
font-size: 15px;
}
}
Is there a better way to do this? Or do I have to use this many repetitions to achieve this??