Making #media queries more specific and more focused - css

Hello: I am trying to have my CSS (wordpress) change on different devices (responsive). I am using #media queries in my child theme css file. But I am finding that my queries are "overlapping" for iPad and iPhone.
here is what I am using for iPad:
#media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : portrait)
and here is what I am using for iPhone:
#media only screen
and (min-device-width : 320px)
and (max-device-width : 480px)
I have found that on my iPhone - it is using both of the styles within both the queries…
I also have a plugin I use called "mobile" which allows me to use "is_mobile" and "is_tablet" etc which are very helpful…
so what I am trying to do is ensure that my CSS only applies on a mobile phone, or only applies on a tablet, and it seems these media queries are not as reliable as I thought…
the mobile plugin works good in separating them out; but i cannot use PHP if statements in the "style.css" file
So here are my questions:
is there a better way to write the two #media queries so they more clearly only work on the devices that I want?
thanks so much...

This is what I use for it to change css depending on the width of the screen. Hope it helps.
#media only screen and (max-width: 768px) {
body
{
margin:0px;
}
#wrapper
{
min-width: 0px;
width:auto;
}
#content
{
margin-left: 0px;
}
#nav
{
float:none;
width: auto;
padding: 0px;
}
#nav ul
{
text-align: center;
}
#nav li
{
display: inline;
padding-top: 0px;
padding-bottom: 0px;
padding-left: 0.75em;
padding-right: 0.75em;
}
footer
{
margin-left: 0;
}
}
#media only screen and (max-width: 480px) {
body
{
margin: 0px;
}
label
{
float: none;
text-align: left;
}
#wrapper
{
width: auto;
min-width: 0;
margin: 0;
}
#content
{
padding-top: 0.1em;
padding-bottom: 0.1em;
padding-right: 1em;
padding-left: 1em;
}
h1
{
margin: 0;
font-size: 1.5em;
padding-left: 0.3em;
}
#nav ul
{
padding: 0px;
}
#nav li
{
display: block;
margin: 0px;
border-style: solid;
border-width: 2px;
border-color: #330000;
}
#nav
{
display: block;
}
#content img
{
float: none;
padding: 0px;
margin: 0.1em;
}
#mobile
{
display: inline;
}
#desktop
{
display: none;
}
}

/* Smartphones (portrait and landscape) ----------- */
#media only screen
and (min-device-width : 320px)
and (max-device-width : 480px) {
/* Styles */
}
/* Smartphones (landscape) ----------- */
#media only screen
and (min-width : 321px) {
/* Styles */
}
/* Smartphones (portrait) ----------- */
#media only screen
and (max-width : 320px) {
/* Styles */
}
/* iPads (portrait and landscape) ----------- */
#media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px) {
/* Styles */
}
/* iPads (landscape) ----------- */
#media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : landscape) {
/* Styles */
}
/* iPads (portrait) ----------- */
#media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : portrait) {
/* Styles */
}
/* Desktops and laptops ----------- */
#media only screen
and (min-width : 1224px) {
/* Styles */
}
/* Large screens ----------- */
#media only screen
and (min-width : 1824px) {
/* Styles */
}
/* iPhone 4 ----------- */
#media
only screen and (-webkit-min-device-pixel-ratio : 1.5),
only screen and (min-device-pixel-ratio : 1.5) {
/* Styles */
}

Related

Media query conflicts

My goal is to resolve site-content overlapping the background slider (Plugin) in WordPress. Below the media queries are conflicting between device. If it works on Mobile devices then it creates an issue on ipads or any landscape mode.
Here is my CSS media queries.
/*------ MEDIA QUERIES------ */
/* Iphone 6,7 Portrait */
#media only screen and (min-device-width: 375px) and (max-device-width: 667px) and (orientation: portrait) and (-webkit-min-device-pixel-ratio: 2) {
.HomePageBody {
margin-top: -970px !important;
}
.nivoSlider {
top: 40px;
/* position:absolute; */
min-height: 500px !important;
width: 100%;
overflow: hidden;
}
/* .site-content {
margin-top:-320px
} */
}
/* Iphone 6,7 Landscape */
#media only screen and (min-device-width: 375px) and (max-device-width: 667px) and (orientation: landscape) and (-webkit-min-device-pixel-ratio: 2) {
.HomePageBody {
margin-top: -110px !important;
}
.nivoSlider {
top: 40px;
/* position:absolute; */
min-height: 500px !important;
width: 100%;
overflow: hidden;
}
.site-content {
margin-top: -320px !important;
}
}
Since min/max-device-width has deprecated, you can use the orientation queries for portrait and landscape.
So your code will be like that:
#media (orientation: landscape) {
...
}
#media (orientation: portrait) {
...
}

#media not working in different browsers

I am a beginner to coding and currently making an app for a uni assignment. I am currently only using html and css until I get it right and then I will add JavaScript. The problem I am having is that my code is responsive on chrome for the set devices I need, but when I preview it in any other browser and on macs, the #media rules don't seem to be working.
This is my current css
body {
background-color:#F4BAAE;
background-image:url("Images/Pattern Landscape.png");
background-size:cover;
}
#logo {
background-image: url("Images/Logo.png");
background-repeat:no-repeat;
background-size 400px 400px;
width:100vw;
height:100vh;
background-position:top;
position:fixed;
margin:-10px;
}
#start {
background-image:url("Images/Start.png");
background-repeat:no-repeat;
position:fixed;
border-radius:15px;
background-position:bottom;
width:600px;
height:300px;
}
/* ----------- Webpages ----------- */
#media only screen and (min-device-width:1024px) {
#start {
background-size: 600px 300px;
top: 80%;
left: 50%;
transform: translate(-50%, -20%);
}
#logo {
background-size:500px;
top:80px;
}
}
/* ----------- Iphone 6 ----------- */
/* Portrait and Landscape */
#media only screen and (min-device-width: 375px) and (max-device-width: 667px) and (-webkit-min-device-pixel-ratio: 2) {
#start {
background-size:180px 80px;
width:180px;
height:80px;
}
body {
background-size:667px auto;
}
}
/* Portrait */
#media only screen and (min-device-width: 375px) and (max-device-width: 667px) and (-webkit-min-device-pixel-ratio: 2)and (orientation: portrait) {
#start {
top: 90%;
left: 50%;
transform: translate(-50%, -80%);
}
#logo {
background-size: 150px;
top:54px;
}
}
/* Landscape */
#media only screen and (min-device-width: 375px) and (max-device-width: 667px) and (-webkit-min-device-pixel-ratio: 2)and (orientation: landscape) {
#start {
top: 90%;
left: 50%;
transform: translate(-50%, -80%);
}
#logo {
background-size:130px;
top:40px
}
}
/* ----------- Ipad ----------- */
/* Portrait and Landscape */
#media only screen and (min-device-width: 768px) and (max-device-width: 1024px) and (-webkit-min-device-pixel-ratio: 2) {
body {
background-size:1024px auto;
}
}
/* Portrait */
#media only screen and (min-device-width: 768px) and (max-device-width: 1024px) and (orientation: portrait) and (-webkit-min-device-pixel-ratio: 2) {
#start {
background-size: 400px 180px;
top: 90%;
left: 50%;
transform: translate(-50%, -80%);
}
#logo {
background-size:300px;
top:50px
}
}
/* Landscape */
#media only screen and (min-device-width: 768px) and (max-device-width: 1024px) and (orientation: landscape) and (-webkit-min-device-pixel-ratio: 2) {
#start {
top: 90%;
left: 50%;
transform: translate(-50%, -90%);
background-size: 340px 130px;
}
#logo {
background-size:320px;
top:50px;
}
}
/* ----------- Galaxy S4 ----------- */
/* Portrait and Landscape */
#media only screen and (min-device-width: 360px) and (max-device-height: 640px) and (-webkit-device-pixel-ratio: 3) {
#start {
background-size:180px 80px;
width:180px;
height:80px;
}
body {
background-size:640px auto;
}
}
/* Portrait */
#media only screen and (min-device-width: 360px) and (max-device-height: 640px) and (orientation: portrait) and (-webkit-device-pixel-ratio: 3) {
#start {
top: 90%;
left: 50%;
transform: translate(-50%, -90%);
}
#logo {
background-size:150px;
top:54px;
left:10px;
}
/* Landscape */
#media only screen and (min-device-width: 360px) and (max-device-height: 640px) and (orientation: landscape) and (-webkit-device-pixel-ratio: 3) {
#start {
top: 90%;
left: 50%;
transform: translate(-50%, -90%);
}
#logo {
background-size:130px;
top:40px;
}
}
Lose the 'device' #media (min-width: 600px) { ... }
On another note... I applaud you're hard work, but your letting things get too complex.
body {
background: red;
}
#media (min-width: 400px) {
background: blue;
}
#media (min-width: 700px) {
background: green;
}
Small-screen-first... no device specific targeting... create a break when things get ugly... repeat...
(you can use 'only screen and' if you research it and think it's important)
(i'd ditch the ids for styling... use classes)
You have -webkit-device-pixel-ratio, -webkit-min-device-pixel-ratio, etc in the media queries. -webkit-* is a browser prefix and will only apply to safari/chrome. Unless you only want the media query to fire for a webkit based browser, you likely want to use the non-prefixed versions of those instead. Like device-pixel-ratio, min-device-pixel-ratio, etc.

cant get media query to work

I created a website that is mostly responsive, but it needed a little help when viewed on smaller screens. I tried to add an #media query to the css page, but it doesn't seem to be making any difference.
I only added different style rules in my main css page, and did not add anything to my html. I am confused on how media queries work exactly, so maybe that's my problem?
Here is my css
/*Alt styles for smaller screens*/
#media only screen and (max-width: 780px) {
div.price-area1 {
width: 100%;
text-align: center;
}
div.price-area2 {
width: 100%;
text-align: center;
}
div.price-area3 {
width: 100%;
}
input.submit {
width: 60%;
margin-left: 20%;
}
nav {
width: 100%;
}
nav ul li {
position: relative;
float: left;
width: 50%;
font-family: 'Poiret One', cursive;
margin-bottom: 15px;
text-align: center;
font-size: 24px;
}
}
The easiest way to understand media queries is to read about it here and here:
With that said, you may want to use a framework such as bootstrap which is a great way to start a site. Otherwise you can use this template for media queries:
/* Smartphones (portrait and landscape) ----------- */
#media only screen and (min-device-width : 320px) and (max-device-width : 480px) {
/* Styles */
}
/* Smartphones (landscape) ----------- */
#media only screen and (min-width : 321px) {
/* Styles */
}
/* Smartphones (portrait) ----------- */
#media only screen and (max-width : 320px) {
/* Styles */
}
/* iPads (portrait and landscape) ----------- */
#media only screen and (min-device-width : 768px) and (max-device-width : 1024px) {
/* Styles */
}
/* iPads (landscape) ----------- */
#media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : landscape) {
/* Styles */
}
/* iPads (portrait) ----------- */
#media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : portrait) {
/* Styles */
}
/* Desktops and laptops ----------- */
#media only screen and (min-width : 1224px) {
/* Styles */
}
/* Large screens ----------- */
#media only screen and (min-width : 1824px) {
/* Styles */
}
/* iPhone 4 ----------- */
#media only screen and (-webkit-min-device-pixel-ratio:1.5), only screen and (min-device-pixel-ratio:1.5) {
/* Styles */
}
This is a good starting point if you want to do it all yourself.

Boostrap 3: How to give different images for (mobile/tablet) versions?

I want to keep different background image for mobile version.I applied media queries but still its not taking.
CSS:
#banner{
margin:0 0 0 0;
padding:0 0 0 0;
background-image:url(images/bg.jpg);
background-repeat:no-repeat;
background-position:center;
position:absolute;
width:100%;
height:2816px;
}
#media (max-width: 480px) {
#banner{
background-image:url(images/cooper.png);
/*display:none; Doesnt shows anything*/
}
}
Any suggestions?
Its not working because you have not given size for bg-images as well as div#banner :
working fiddle
div, html, body {
width:100%;
height:100%;
}
#banner {
margin:0 0 0 0;
padding:0 0 0 0;
background-image:url(http://www.mpa-garching.mpg.de/galform/cr/CR_TCDM_dump40_400_170000_12000_100_blue.gif);
background-repeat:no-repeat;
background-size:100% 100%;
background-position:center;
position:absolute;
width:100%; /* u have set this here, so this is fine */
height:2816px;
}
#media only screen and (max-width: 480px) {
#banner {
width:100%; /* u have no set this here, so this is not working for you */
height:100%;/* u have no set this here, so this is not working for you */
background: red url('http://bingbangstudios.com/press/fork/fork_pixie_400x400.jpg');
background-size:100% 100%; /* notice this here too*/
/*display:none; Doesnt shows anything*/
}
}
Try this meta tag and media queries..
<header>
<meta name="viewport" content="width=device-width">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
</header>
/* Smartphones (portrait and landscape) ----------- */
#media only screen
and (min-width : 320px)
and (max-width : 480px) {
/* Styles */
}
/* Smartphones (landscape) ----------- */
#media only screen
and (min-width : 321px) {
/* Styles */
}
/* Smartphones (portrait) ----------- */
#media only screen
and (max-width : 320px) {
/* Styles */
}
/* iPads (portrait and landscape) ----------- */
#media only screen
and (min-width : 768px)
and (max-width : 1024px) {
/* Styles */
}
/* iPads (landscape) ----------- */
#media only screen
and (min-width : 768px)
and (max-width : 1024px)
and (orientation : landscape) {
/* Styles */
}
/* iPads (portrait) ----------- */
#media only screen
and (min-width : 768px)
and (max-width : 1024px)
and (orientation : portrait) {
/* Styles */
}
/* Desktops and laptops ----------- */
#media only screen
and (min-width : 1224px) {
/* Styles */
}
/* Large screens ----------- */
#media only screen
and (min-width : 1824px) {
/* Styles */
}
/* iPhone 4 ----------- */
#media
only screen and (-webkit-min-device-pixel-ratio : 1.5),
only screen and (min-device-pixel-ratio : 1.5) {
/* Styles */
}
You don't need too much coding. Just code as follow-
#banner{
margin:0 0 0 0;
padding:0 0 0 0;
background-repeat:no-repeat;
background-position:center;
position:absolute;
width:100%;
height:2816px;
}
#media (min-width: 480px) {
#banner{
background-image:url(images/bg.jpg);
}
}
#media (max-width: 480px) {
#banner{
background-image:url(images/cooper.png);
}
}

Responsive Design CSS fills entire screen when using small browser

I'm trying to use responsive css media queries to hide my sidebar unless the screen is large, or is a tablet big enough and in landscape mode. It seems to be working based on resizing my browser, until I get to a certain size it fills the entire screen. I'm using Twitter Bootstrap styles as well, but not the responsive styles so I don't see how that could be a problem.
Is there another media query that I should use? I also tried a min-width of 0 and max-width of 320, that did not work.
Example:
HTML:
<div class="row">
<div class="span2 sidebar">
<a href="#Url.Action("Index", "Home")">
<h3>Link Home</h3>
</a>
</div>
<div class="span10">
#RenderBody()
</div>
</div>
CSS
html {
height: 100%;
}
.sidebar {
background: #333;
margin: 0;
padding-left: 1.5em;
height: 100%;
-moz-box-shadow: 0 0 5px #888;
-webkit-box-shadow: 0 0 5px#888;
box-shadow: 0 0 5px #888;
min-height: 100%;
position: absolute;
display: none;
}
h1, h2, h3, h4, h5, h6 {
font-family: 'Fenix', serif;
font-weight: 400;
}
/* Smartphones (portrait and landscape) ----------- */
#media only screen and (min-device-width : 320px) and (max-device-width : 480px) {
/* Styles */
.sidebar {
display: none;
}
}
/* Smartphones (landscape) ----------- */
#media only screen and (min-width : 321px) {
/* Styles */
.sidebar {
display: none;
}
}
/* Smartphones (portrait) ----------- */
#media only screen and (max-width : 320px) {
/* Styles */
.sidebar {
display: none;
}
}
/* iPads (portrait and landscape) ----------- */
#media only screen and (min-device-width : 768px) and (max-device-width : 1024px) {
/* Styles */
.sidebar {
display: none;
}
}
/* iPads (landscape) ----------- */
#media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : landscape) {
/* Styles */
.sidebar {
display: block;
}
}
/* iPads (portrait) ----------- */
#media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : portrait) {
/* Styles */
.sidebar {
display: none;
}
}
/* Desktops and laptops ----------- */
#media only screen and (min-width : 1224px) {
/* Styles */
.sidebar {
display: block;
}
}
/* Large screens ----------- */
#media only screen and (min-width : 1824px) {
/* Styles */
.sidebar {
display: block;
}
}
/* iPhone 4 ----------- */
#media only screen and (-webkit-min-device-pixel-ratio : 1.5), only screen and (min-device-pixel-ratio : 1.5) {
/* Styles */
.sidebar {
display: none;
}
}
Simply use
// Landscape phone to portrait tablet
#media (max-width: 767px) {
.sidebar {
display: none;
}
}
And why did you not use the responsive css when using twitter bootstrap? There is a class called .hidden-phone, very useful.
The most used media querys are
// Large desktop
#media (min-width: 1200px) {
}
// Portrait tablet to landscape and desktop
#media (min-width: 768px) and (max-width: 979px) {
}
// Everything below 1024px
#media (max-width: 979px) {
}
// Landscape phone to portrait tablet
#media (max-width: 767px) {
}
// Landscape phones and down
#media (max-width: 480px) {
}
Bootstrap has responsive utility classes to help you do this easily. Eg .hidden-phone, .visible-desktop
Instead of complicating your life and creating the custom css classes, try something as simple as:
<div class="row">
<div class="span2 sidebar hidden-phone">
<!-- sidebar details -->
</a>
</div>
<div class="span10">
<!-- main body -->
</div>
</div>
Good luck!

Resources