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!
Related
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.
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 */
}
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 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);
}
}
Wondering why it is so difficult to position divs to remain responsive.. they just float where ever they want for some reason.. how can I position these perfectly? Holding their positions while resizing like my social icons http://www.closetvip.com/splash.html
/* Menu Buttons */
#english {
float:right;
position:absolute;
right:570px;
bottom:101px;
clear:right;
}
#spanish {
position:absolute;
left:570px;
bottom:100px;
clear:right;
}
<div id="spanish">
<img src="img/espanol.png">
</div>
<div id="english">
<img src="img/english.png">
</div>
You need to use position: relative; on the parent div of absolute div's otherwise the items are absolute of the window
you have to control this by media queries or use % in your css rule
like
#spanish {
position:absolute;
left:47%;
bottom:20%;
clear:right;
}
Note: this is not final just an example
These are some standard media queries from chris coyier
reference url
/* 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 */
}
use these and set position of your elements for all devices
I don't see the use of bootstrap what so ever, only that it's inclued to the header and that is it...
You are position your element with fixed pixels on right/left and that is taken from the window border, so when you resize your window it's normal that the elements are pushed.
Your social icons are positioned with right 0 so it's normal to stay always at the right when you resize your window, but the 2 other buttons have 570px and they will always keep that space, so when you resize you know what is happening.
The Solution you ask?
You can position them with:
#spanish { bottom: 100px; clear: right; left: 50%; margin-left: -150px; position: absolute;}
#english { bottom: 101px; clear: right; float: right; left: 50%; margin-left: 50px; position: absolute;}