CSS media queries and screen resolution - css

I am trying to get these columns to show vaguely under these conditions:
When on a phone -- show one column (at 100%)
When on a tablet -- show two columns (at 50%)
When on a desktop -- show as many as will fit
Here is my CSS code thus far but it's not working as expected:
EDIT: I modified the media queries a bit and the background color is changing as expected...but the widths are not. To re-iterate...on small devices I want DIV's to take up 100% screen width...on tablets I want them to split the screen in half...and on desktop screens I want them to spread across the entire width of screen accordingly.
#media screen and (max-width: 35.5em) { /* 568px or less */
div.listing {
-moz-columns: 100%;
-webkit-columns: 100%;
columns: 100%;
background-color: red;
}
}
#media screen and (min-width: 35.5em) and (max-width: 48em) { /* 568px - 768px */
div.listing {
-moz-columns: 50%;
-webkit-columns: 50%;
columns: 50%;
background-color: orange;
}
}
#media screen and (min-width: 48em) { /* 1024px */
div.listing {
-moz-columns: 350px;
-webkit-columns: 350px;
columns: 350px;
background-color: green;
}
}

Actually, your columns are working exactly as expected. Check out the spec: http://www.w3.org/TR/css3-multicol/#the-number-and-width-of-columns
The issue is that column-width is the minimum width. If you set your columns to be 50% the column will always default to 100% because no matter what, the column will always be at least half of it's container (I probably explained that weird). You basically need to set a desired em or px size for your tablet view. Or, as Adriano66 pointed out, set the number of columns, and not a width.
#media screen and (max-width: 35.5em) { /* 568px or less */
div.listing {
-moz-columns: 100%;
-webkit-columns: 100%;
columns: 100%;
background-color: red;
}
}
#media screen and (min-width: 35.5em) and (max-width: 48em) { /* 568px - 768px */
div.listing {
-moz-columns: 2;
-webkit-columns:2;
columns: 2;
background-color: orange;
}
}
#media screen and (min-width: 48em) { /* 1024px */
div.listing {
-moz-columns: 350px;
-webkit-columns: 350px;
columns: 350px;
background-color: green;
}
}
Fiddle:
http://jsfiddle.net/disinfor/a65pb9re/1/

I don't think u can use percentages this way, try typing 1 instead of 100% which equals to 1 column and 2 instead of 50% which equals to 2 columns
#media screen and (max-width: 35.5em) { /* 568px or less */
div.listing {
-moz-columns: 1;
-webkit-columns: 1;
columns: 1;
background-color: red;
}
}
#media screen and (min-width: 35.5em) and (max-width: 48em) { /* 568px - 768px */
div.listing {
-moz-columns: 2;
-webkit-columns: 2;
columns: 2;
background-color: orange;
}
}
#media screen and (min-width: 48em){ /* 1024px */
div.listing {
-moz-columns: 350px auto;
-webkit-columns: 350px auto;
columns: 350px auto;
background-color: green;
}
}
Fiddle: http://jsfiddle.net/wprdhswr/1/
https://developer.mozilla.org/en-US/docs/Web/CSS/columns

Related

Increase percentage of margins as viewport width widens

How can I increase the percentage of margins as the viewport width also increase? For example if the screen is 1600px wide, the margins are set to 1% and when the screen is 2100px and above the margins are 10%.
How do I set this in CSS?
Here is a working example of what you are looking for: https://codepen.io/colinah/pen/VwQombR
The Idea is to use a media query to tell if the device is larger than 1600px then set the margin to 10vw
body{
margin: 1vw
}
#media(min-width:1600px){
body {
margin: 10vw
}
}
.wrapper{
border: 1 px solid black;
background: blue;
}
.my-list {
border: 1 px solid;
}
.my-list {
background: red;
}
/* On screens that are 1600px wide or more, make margin 1%; */
#media screen and (max-width: 1600px) {
.my-list {
margin: 1%;
}
}
/* On screens that are 1600px wide or more, make margin 1%; */
#media screen and (min-width: 1600px) and (max-width: 1700px){
.my-list {
margin: 1%;
}
}
/* On screens that are 1700px wide or more, make margin 3%; */
#media screen and (min-width: 1700px) and (max-width: 1800px){
.my-list {
margin: 3%;
}
}
/* On screens that are 1800px wide or more, make margin 4%; */
#media screen and (min-width: 1800px) and (max-width: 1900px){
.my-list {
margin: 4%;
}
}
/* On screens that are 1900px wide or more, make margin 6%; */
#media screen and (min-width: 1900px) and (max-width: 2000px){
.my-list {
margin: 6%;
}
}
/* On screens that are 2000px wide or more, make margin 8%; */
#media screen and (min-width: 2000px) and (max-width: 2100px){
.my-list {
margin: 8%;
}
}
/* On screens that are 2100px wide or more, make margin:10%; */
#media screen and (min-width: 2100px) {
.my-list {
margin: 10%;
}
}
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<div class="wrapper">
<div class="my-list">
test message
</div>
</div>
</body>
</html>

when i set #media between 600~767,100~599 there is missing on 599.xx

Now I'm learning about CSS
when I type like this
.pc{
color: red;
font-size: 50px;
background-color: pink;
}
#media (min-width: 600px) and (max-width: 767px) {
.pc {
color: blue;
font-size: 20px;
background-color: yellow;
}
}
#media (min-width: 100px) and (max-width: 599px) {
.pc {
color: green;
font-size: 10px;
background-color: gray;
}
}
for example
in the 599.XXXpx (599.123, 599.284)
At this point
the color is go back to red and pink
How can I solve this?
Most of browsers will not display fraction pixel. A pixel is a smallest unit to display. So you do need to be worry about the breakpoint you mentioned. It is not phisically happens.
In essence, I would recommend you to use the same number for both media queries, and then order the rules so the one that you want to win goes later.
If you would like to keep it blue and yellow, then you will have to change the order of the rules:
#media (min-width: 100px) and (max-width: 600px) { /* … */ }
#media (min-width: 600px) and (max-width: 767px) { /* … */ }
But if you'd like to keep the green and gray colors, keep the current order:
#media (min-width: 600px) and (max-width: 767px) { /* … */ }
#media (min-width: 100px) and (max-width: 600px) { /* … */ }

Is there a way we can club browser detection and media queries in CSS?

I am trying to write CSS for IE 11 and IE Edge browser detection and browser width media queries both together.
With regular media query for Chrome, I am achieving element width without any issue.
But, if the browser is IE11 and the width is 1360px, then I want to apply red color and the element width should be 800px with green background and if the browser width is 1920px the the element width should be 1200px with red background.
If the browser is Edge and width is 1360px, then I want to apply red color and the element width should be 900px with green background and if the browser width is 1920px the the element width should be 1300px with red background.
And this should not be disturbing my regular classes that are working smoothly for Chrome and Firefox.
Below are the two classes I tried to use to achieve my desired outcome, but which fail to dos o:
Regular Media Query for Chrome
#media only screen and (max-width: 1920px) {
.test-class {
width: 100%;
background-color: red;
}
}
#media only screen and (max-width: 1360px) {
.test-class {
width: 80%;
background-color: green;
}
}
For IE:
#media only screen and (max-width: 1920px) {
.test-class {
width: 1200px;
background-color: red;
}
}
#media screen and (-ms-high-contrast: active),
screen and (-ms-high-contrast: none) {
.test-class {
width: 1200px;
background-color: red;
}
}
#media only screen and (max-width: 1360px) {
.test-class {
width: 800px;
background-color: green;
}
}
#media screen and (-ms-high-contrast: active),
screen and (-ms-high-contrast: none) {
.test-class {
width: 800px;
background-color: green;
}
}
For MS Edge
#media screen and (max-width: 1920px) {
#supports (-ms-ime-align: auto) {
.test-class {
width: 1200px;
background-color: red;
}
}
}
#media screen and (max-width: 1360px) {
#supports (-ms-ime-align: auto) {
.test-class {
width: 800px;
background-color: green;
}
}
}
When I write the CSS like the above, all the elements are taking 100% for 1920px width browser and 80% width for 1360px browser.

CSS Media Query won't work unless I add "!important"

I am using SCSS trying to set up some media queries for max-width: 320px and max-width: 768px
I have a div with class item-content inside an Owl Carousel slide with width set to 600px but I want to reduce the width for smaller devices. I want to use 200px for resolution 320px wide and 300px for resolution that is anywhere between 320 and 768.
This is what I'm using in my _app-responsive.scss but as you can see, I have !important set to the width of item-content because otherwise it is getting overridden even though I am on resolution of the iPhone 5 which is 320px wide.
#media (max-width: 320px) {
.home {
section#top {
.owl-theme {
.owl-dots {
top: -10%;
}
.owl-stage-outer {
.owl-stage {
.owl-item {
.item {
.item-content {
width: 200px !important;
}
}
}
}
}
}
}
}
}
#media (max-width: 768px) {
.home {
section#top {
.owl-theme {
.owl-stage-outer {
.owl-stage {
.owl-item {
.item {
h2 {
font-size: 16px;
}
.item-content {
width: 300px;
}
}
}
}
}
}
}
}
}
This is my overall app.scss
I am importing the file with the media queries last, I tried importing it before the _app-custom.scss but that didn't change anything.
#import 'app-variables';
#import '../../vendor/bootstrap-sass/stylesheets/bootstrap';
#import '../../vendor/bootstrap-sass/stylesheets/bootstrap-compass';
#import '../../vendor/font-awesome/scss/font-awesome';
#import 'app-custom';
#import 'app-responsive';
The two media queries would make width: 200px when window is between 0px and 320px but also width: 300px when window is between 0px and 768px.
To solve this you can apply width: 200px without using a media query and only when windows size is greater than 320px use width: 300px:
.item-content {
width: 200px;
}
#media (min-width: 321px) {
...
.item-content {
width: 300px;
}
}
And:
#media (max-width: 320px) {
...
.item-content {
width: 200px;
}
}
#media (min-width: 321px) and (max-width: 768px) {
...
.item-content {
width: 300px;
}
}
Should also works because your issue is that the query with the max-width: 768px is always overwriting the media query with max-width: 320px

CSS responsive vertical scrollbar issue

I'm currently playing with bootstraps v2.3.2. media querys (I'm not using bootstraps grid, just those 4 media queries) to test them on mobile and tablet devices, and I notice that I keep getting a horizontal scrollbar and I don't understand why?
Basically I have one div and this CSS:
html {
box-sizing: border-box;
}
*, *:before, *:after {
box-sizing: inherit;
}
body{
margin:0;
/* height: 3000px; */ /* forced vertical scrollbar */
height: 300px;
}
div{
padding: 0 10px;
margin: 0 auto;
background: aqua;
width: 980px;
border: 1px solid black;
height: 100%;
}
/* Large desktop */
#media (min-width: 1200px) {
div{
background: red;
width: 1200px;
}
}
/* Portrait tablet to landscape and desktop */
#media (min-width: 768px) and (max-width: 979px) {
div{
background: yellow;
width: 768px;
}
}
/* Landscape phone to portrait tablet */
#media (max-width: 767px) {
div{
background: blue;
width: 100%;
}
}
/* Landscape phones and down */
#media (max-width: 480px) {
div{
background: green;
}
}
Situation when I force vertical scrollbar: JSBin
But when I don't force vertical scrollbar, I get the wanted result: JSBin
So it's obviously due the vertical scrollbar. I found this article about scrollbar issue in Responsive Web Design, but I get the same result in both Chrome and FF.
Update: as looking the source of bootstrap v3.3.2 I've noticed that they have new media queries, however, they don't use the minimal possible width for the .container. This are their media queries:
#media (min-width: 768px) {
.container {
width: 750px; /* 18px difference */
}
}
#media (min-width: 992px) {
.container {
width: 970px; /* 22px difference */
}
}
#media (min-width: 1200px) {
.container {
width: 1170px; /* 30px difference */
}
}
And here's the JSBin. Even when I forced the vertical scrollbar to appear, this won't trigger the horizontal scrollbar.
But if I want to use the minimal possible width for the media queries, like:
#media (min-width: 768px) {
.container {
width: 768px;
}
}
#media (min-width: 992px) {
.container {
width: 992px;
}
}
#media (min-width: 1200px) {
.container {
width: 1200px;
}
}
This will trigger the horizontal scrollbar - JSBin
Did the guys from bootstrap did that on purpose, because of the possibly that there can be the presence of vertical scrollbar?
Question: Why can't I use the minimal possible width in the media query when the vertical scrollbar is present?
I know that this may be a novice question, but I would appreciate if someone clarify this for me.
Bootstrap Media Querys
Setting media query
Bootstrap supports four media sizes:
Phones < 768px (8 inch)
Tablets ≥ 768px
Desktops ≥ 992px (10.33 inch)
Desktops ≥ 1200px (12.5 inch)
These are not fixed sizes!
If you have a screen that has a min-width of 768px the media query should trigger.
However setting a container to 768px will almost allways make that screen overflow
First of all the body element of all modern browser does have a margin to it.
example: Webkit browsers: body {margin: 8px;} so if your element of 768px and a margin-top of 8 and margin-bottom of 8 you get: 784px
so your screen is 768px (or less) and your content is 784px this will make it overflow (as it should). That said bootstrap sets: body {margin:0;}
An other example would be border. Border adds size to your element unless box-sizing isn't default. While outline sets the border inside your element.
Did the guys from bootstrap did that on purpose, because of the possibily that there can be the presence of vertical scrollbar ?
There is a possibility of that but i would think they set it because there is a bunch of css property's that affect size, so they gave a margin of error so to speak to avoid strange behavior like a horizontal scroll bar popping up.
Question: Why can't I use the minimal possible width in the media query when the vertical scrollbar is present?
You can use it: Fiddle!
Just Remember that some browsers will render it with a certain width.
Checkout the fiddle
https://jsfiddle.net/YameenYasin/as4Lmgas/1/
html {
box-sizing: border-box;
}
*, *:before, *:after {
box-sizing: inherit;
}
body{
margin: 0;
}
div {
padding-right: 15px;
padding-left: 15px;
margin-right: auto;
margin-left: auto;
background: blue;
height:auto;
min-height:300px; // For testing purpose only
}
#media (min-width: 768px) {
div {
width: 750px;
background: silver;
}
}
#media (min-width: 992px) {
div {
width: 970px;
background: yellow;
}
}
#media (min-width: 1200px) {
div {
width: 1170px;
background: red;
}
}
<div></div>

Resources