So there are two divs as below:
/*for ipad portrait and landscape*/
#media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px) {
.aclass {
display: block;
background-color: antiquewhite;
}
.bclass {
display: none;
}
}
/*for medium device*/
#media only screen
and (min-width : 992px)
and (max-width : 1200px) {
.aclass {
display: none;
}
.bclass {
display: block;
background-color: aquamarine;
}
}
<div class="hidden-xs aclass">div 1</div>
<div class="hidden-xs bclass">div 2</div>
I want aclass to be applied only in ipad and bclass to be applied in medium devices like desktop. The problem arises in Ipad landscape mode where bclass is applied because of the min-width: 992px, but I want aclass to be applied here. How do I solve this issue?
You can also use max-height/min-height and/or max-device-height/min-device-height in the media queries and combine that with your existing queries.
Have you tried using and (orientation: landscape)? This is a media query constraint that only enforces CSS the rules if the device is in landscape mode. Works the same for orientation: portrait
Related
#media only screen and (min-width:300px) and (max-width:667px) and (orientation: portrait) {Class goes here}
#media only screen and (min-width:667px) and (max-width:768px) and (orientation: landscape) {class goes here}
portrait view working good but landscape not showing any change even media query can't see in inspect code also.
I actually tested the ranges:
#media only screen and (min-width:300px) and (max-width:667px) {
div {
background-color: red;
}
}
#media only screen and (min-width:667px) and (max-width:768px) {
div {
background-color: green;
}
}
They do work. But when I add the orientation, it fails to work on the portrait.
#media only screen and (min-width:300px) and (max-width:667px) and (orientation: portrait) {
div {
background-color: red;
}
}
#media only screen and (min-width:667px) and (max-width:768px) and (orientation: landscape) {
div {
background-color: green;
}
}
Try to remove one of the orientation on the media queries. It could solve the problem. If you still want to leave the orientation condition, test it on a real device instead of a simulating tool.
I'm working with react Bootstrap. I have designed a layout which I want to refactor on small screens. All I want to do is, at certain breakpoints I want my h3 elements and image resized. I've been using the following:
Code from index.css :
// Extra small devices (portrait phones, less than 576px)
#media (max-width: 575.98px) {
.thumbnailImg{
width: 30%;
}
h3{
font-size: 1rem;
}
}
// Small devices (landscape phones, 576px and up)
#media (min-width: 576px) and (max-width: 767.98px) {
.thumbnailImg{
width: 35%;
}
h3{
font-size: 1rem;
}
}
// Medium devices (tablets, 768px and up)
#media (min-width: 768px) and (max-width: 991.98px) {
.thumbnailImg{
width: 40%;
}
h3{
font-size: 1.5rem;
}
}
Now for some reasons none of the breakpoints have been working. h3 remains 1.75 rem throughout the whole time. Not only just the h3 element but the image is also not resizing as defined. Can anyone could tell me what is that I'm doing wrong here.
Here is my component that has been using this style:
import React from 'react';
import { Image } from 'react-bootstrap';
import './index.css';
export const MyIcons = ({source,name}) => (
<div className="col-sm-4 addMargin50 text-center">
<Image className="thumbnailImg" src={ process.env.PUBLIC_URL + source } alt={name} roundedCircle />
<h3 className="font-nunito text-uppercase"> {name} </h3>
</div>
)
I also tried making a separate class to define the font-sizes on different breakpoints and added that class to my image and h3 element however none of them is working either.
Problems not with react, it's the order of your media queries. If you have overlapping rules it will always pick the final rule that was delcared. If you reverse the order of your media queries you should get what you want.
I have edited this answer to be more clear.
You should order your media queries from largest device to smallest device.
/* Medium devices (tablets, 768px and up) */
#media (min-width: 768px) and (max-width: 991.98px) {
.thumbnailImg{
width: 40%;
}
h3{
font-size: 1.5rem;
}
}
/* Small devices (landscape phones, 576px and up)*/
#media (min-width: 576px) and (max-width: 767.98px) {
.thumbnailImg{
width: 35%;
}
h3{
font-size: 1rem;
}
}
/* Extra small devices (portrait phones, less than 576px) */
#media (max-width: 575.98px) {
.thumbnailImg{
width: 30%;
}
h3{
font-size: 1rem;
}
}
https://jsfiddle.net/txL5chk0/2/
Also, I am sure I don't need to tell you that the comments you have in your css file are not valid comments in CSS and should not be included with your code....
Wow hard to get to work...but new to media queries...
Using angular2 btw...
very simple ...
If min-width: 576px do this, else that....
e.g.
Per the below if 576 then show the menu class and hide the toolbar class and vice versa
<ng-container class="menu" >
<i class="material-icons">menu</i>
</ng-container>
<ng-container class="toolbar">
<span color="accent" style="padding-right: 15px">SolarStack</span>
</ng-container>
Here is my css:
/*Small devices (landscape phones, 576px and up)*/
#media (min-width: 576px) {
.toolbar{
visibility: collapse;
}
}
/*Medium devices (tablets, 768px and up)*/
#media (min-width: 768px) {
.menu{
visibility: hidden;
}
}
/*Large devices (desktops, 992px and up)*/
#media (min-width: 992px) {
.menu{
visibility: hidden;
}
}
/*Extra large devices (large desktops, 1200px and up)*/
#media (min-width: 1200px) {
.menu{
visibility: hidden;
}
}
In essence how to I show or hide elements based on screen size using media queries? Menu will be shown with iphone and toolbar on desktop
You either need to set defaults for your class styles which you can then change at a specific screen size, or update your media queries to set styles on any class that changes at that size.
If min-width: 576px do this:
Meaning that for everything including & wider than 576 do:
#media (min-width: 576px) {
.toolbar{
visibility: hidden;
}
.menu {
visibility: visible;
}
}
else that:
Meaning that for everything narrower than 576 do:
#media (max-width: 576px) {
.toolbar{
visibility: visible;
}
.menu {
visibility: hidden;
}
}
In this fiddle, at least, it appears than min-width is inclusive and max-width is not. Try changing the width of the output iFrame between 575 & 576.
How can I set automatic line breaks on mobile devices? At the moment my code looks like:
HTML
<div class="container-fluid bg-1 text-center">
<h2>MessageOfTheDay</br>
</br></h2>
<p style="margin-bottom: 100px;">SOME LOREMIPSUMDOLORSITAMET,CONSECTETUR</p>
<h1 style="margin-bottom: 100px;">XXXXXX</br>
SOME LOREMIPSUMDOLORSITAMET,CONSECTETURSOME LOREMIPSUMDOLORSITAMET,CONSECTETUR</br>
SOME LOREMIPSUMDOLORSITAMET,CONSECTETURSOME LOREMIPSUMDOLORSITAMET,CONSECTETURSOME LOREMIPSUMDOLORSITAMET,CONSECTETUR</h1>
</div>
CSS
.bg-1{
background-color: black;
background-size: cover;
color: #ffffff;
height: auto;
min-height:620px;
padding:10px;
margin-top:0px;}
body {
font: 20px "Montserrat", sans-serif;
color: #f5f6f7;}
p {font-size: 20px;}
.margin {margin-bottom: 10px;}
h1,h2,h3{
margin-top: 0px;
margin-bottom: 0px;}
.container-fluid{
padding-top: 20px;
padding-bottom: 0px;}
h1{font-size: 50px;}
How can I fix truncate text? I want set automatic brake line on mobile.
Image
It would be a bit of a Janky fix, but this should work... Bootstrap allows us to display/hide information based screen size with built in media query's... so if you wanted to add a break at a specific point in the text, you could do something like the following:
<div class="visible-xs"><br /><br /></div>
or maybe this would even work, Not sure on the following so give it a shot and let us know if it worked for you:
<br class="visible-xs" />
the "visible-xs" class in bootstrap should make the content visible only if the screen size is less than 768px... the alternative is "hidden-xs" which hides content on smaller displays. :) Happy coding!
You can use media queries in CSS to do this. You would give the element that would be the higher up element, a class or id and then set its width to 100% and its display to inline-block or block when the screen is less than a certain size (or greater than a certain size).
With this code every .element will be 100% width then the screen size is 600px or less. if you wanted it to be when the screen is greater than or equal to 600px then you would use min-width: 600px instead.
#media (max-width: 600px) {
.element {
display: inline-block;
width: 100%;
}
}
<div class="container-fluid bg-1 text-center">
<h2>MessageOfTheDay</br>
</br></h2>
<p style="margin-bottom: 100px;" class='workdBreak'>SOME LOREMIPSUMDOLORSITAMET,CONSECTETUR</p>
<h1 style="margin-bottom: 100px;">XXXXXX</br>
<span class='workdBreak'>
SOME LOREMIPSUMDOLORSITAMET,CONSECTETURSOME LOREMIPSUMDOLORSITAMET,CONSECTETUR</br>
SOME LOREMIPSUMDOLORSITAMET,CONSECTETURSOME LOREMIPSUMDOLORSITAMET,CONSECTETURSOME LOREMIPSUMDOLORSITAMET,CONSECTETUR</h1>
</div>
</span>
#media only screen and (max-width : 480px) {
.workdBreak{
word-wrap:break-word;
}
}
Used word-wrap css property to break word if it is larger than width of container.
http://www.w3schools.com/cssref/css3_pr_word-wrap.asp
http://www.w3schools.com/cssref/css3_pr_word-break.asp
Bootsrap media query
/========== Mobile First Method ==========/
/* Custom, iPhone Retina */
#media only screen and (min-width : 320px) {
}
/* Extra Small Devices, Phones */
#media only screen and (min-width : 480px) {
}
/* Small Devices, Tablets */
#media only screen and (min-width : 768px) {
}
/* Medium Devices, Desktops */
#media only screen and (min-width : 992px) {
}
/* Large Devices, Wide Screens */
#media only screen and (min-width : 1200px) {
}
/*========== Non-Mobile First Method ==========*/
/* Large Devices, Wide Screens */
#media only screen and (max-width : 1200px) {
}
/* Medium Devices, Desktops */
#media only screen and (max-width : 992px) {
}
/* Small Devices, Tablets */
#media only screen and (max-width : 768px) {
}
/* Extra Small Devices, Phones */
#media only screen and (max-width : 480px) {
}
/* Custom, iPhone Retina */
#media only screen and (max-width : 320px) {
}
I am new to phonegap and facing a problem, I am making a phonegap app which will run on multiple platform devices of different screen size and different screen resolution so I have to load images of different resolution depending on screen resolution.
this can be achieved in android by simply putting your images of different resolution in hdpi, mdpi and ldpi folder and it(android) fetches images automatically depending on devices screen resolution. But how to do this in phonegap.
I have seen lot of articles on responsive web design they all say about positioning the elements on web page but non of them has told about how to place images on the basis of screen resolutions.
thanks i advance.
edited question
i have used following code for html
<div id="header" data-role="header" data-position="fixed">
<img alt="app_icon" src="pictures/app_logo.png" display="inline" class="align-left" />
<img alt="brand_icon" src="pictures/company_logo.png" display="inline" class="align-right" /><h1></h1>
</div>
now I have images inside assets/www/pictures folder. this folder consists of 2 images of same resolution app_logo.png and company_logo.png and 2 images of higher resolution app_logo_big.png and company_logo_big.png now through media queries i will know the screen size and apply the styles but as far as i know i cannot change img src from css. So now how will i use these images of different resolution
Then Dynamically Change Image through jquery:
HTML:
<div id="header" data-role="header" data-position="fixed">
<img id="app-icon" src="pictures/app_logo.png" display="inline" />
</div>
Javascript:
$(document).ready(function () {
if(window.devicePixelRatio == 0.75) {
$("#app-icon").attr('src', '/images/lpdi/app-icon.png');
}
else if(window.devicePixelRatio == 1) {
$("#app-icon").attr('src', '/images/mdi/app-icon.png');
}
else if(window.devicePixelRatio == 1.5) {
$("#app-icon").attr('src', '/images/hpdi/app-icon.png');
}
else if(window.devicePixelRatio == 2) {
$("#app-icon").attr('src', '/images/xpdi/app-icon.png');
}
}
Through CSS: Use Media Queries for Different Resolution :
HTML:
<div id="header" data-role="header" data-position="fixed">
<span id="app-icon"></div>
<span id="brand-icon"></div>
</div>
CSS:
/* Low density (120), mdpi */
#media screen and (-webkit-device-pixel-ratio: 0.75) {
#app-icon { background-image:url(pictures/ldpi/app-icon.png); }
#brand-icon { background-image:url(pictures/ldpi/brand-icon.png); }
}
/* Medium density (160), mdpi */
#media screen and (-webkit-device-pixel-ratio: 1) {
#app-icon { background-image:url(pictures/mpi/app-icon.png); }
#brand-icon { background-image:url(pictures/mdpi/brand-icon.png); }
}
/* High density (240), hdpi */
#media screen and (-webkit-device-pixel-ratio: 1.5) {
#app-icon { background-image:url(pictures/hdpi/app-icon.png); }
#brand-icon { background-image:url(pictures/hdpi/brand-icon.png); }
}
/* Extra high density (320), xhdpi */
#media screen and (-webkit-device-pixel-ratio: 2) {
#app-icon { background-image:url(pictures/xdpi/app-icon.png); }
#brand-icon { background-image:url(pictures/xdpi/brand-icon.png); }
}
If you want to filter through,
ORIENTATION - and (orientation: landscape)
Device WIDTH and (min-device-width : 480px) and (max-device-width : 854px)
Example:
#media screen and (-webkit-device-pixel-ratio: 1.5) and (min-device-width : 640px) and (max-device-width : 960px) and (orientation: landscape) {
/* Your style here */
}
You can also do this using a handlebars helper, which less code intensive and in my opinion the recommended method:
if (screen.width <= 480) {
imgFolder = 'img/low/';
}
else {
imgFolder = 'img/high/';
}
Handlebars.registerHelper('imgFolder', function () {
return imgFolder
});
while img/low/ and img/high contain images in different resolutions with the same name.
Then in your template:
<img src="{{imgFolder}}yourImage.png" />
Of course, you have to set the screen size values according to the devices your app targets.
Appendix:
If you do not have 1:1 pixel mapping in cordova browser (which is NOT recommended - your images will have a blurry/unsharp look) screen.width will differ from browsers width (window.innerWidth) and you have to use $(window).width() or window.innerWidth. You might be able to fix a wrong mapping using media queries.
Creating support for more sizes is a problem, but you can fix it with #media queries in CSS. Check this example code:
/* 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 */
}
With this code you can add support for all devices. Check this link for getting more code for more browsers
Functions which you can use:
Width and height: (min-device-width : 768px) and (max-device-width : 1024px)
Orientation: (orientation: landscape) or (orientation: portrait)
Device pixel ratio: (-webkit-device-pixel-ratio: 1.5)
EDIT:
HTML:
<div id="header" data-role="header" data-position="fixed">
<span id="app_icon" alt="app_icon" src="pictures/app_logo.png" display="inline" class="align-left"></span>
<span id="brand_icon" alt="brand_icon" src="pictures/company_logo.png" display="inline" class="align-right"></span><h1></h1>
</div>
Change img into span and add IDs.
CSS:
#media screen and (-webkit-device-pixel-ratio: 0.75) {
#app_icon {
width: 100px; /* Example size */
height: 100px; /* Example size */
background: url(pictures/app_logo_small.png);
}
}
#media screen and (-webkit-device-pixel-ratio: 1) {
#app_icon {
width: 150px; /* Example size */
height: 150px; /* Example size */
background: url(pictures/app_logo_medium.png);
}
}
#media screen and (-webkit-device-pixel-ratio: 1.5) {
#app_icon {
width: 200px; /* Example size */
height: 200px; /* Example size */
background: url(pictures/app_logo_large.png);
}
}
#media screen and (-webkit-device-pixel-ratio: 2) {
#app_icon {
width: 300px; /* Example size */
height: 300px; /* Example size */
background: url(pictures/app_logo_xlarge.png);
}
}
With this example you can change your code and fix it. Hope this help!
I have found I've had to start adding support for pixel ratios of 0.5, 1, 1.3, 1.5, 2 and 3 using these media queries.
Note I am using -webkit-min-device-pixel-ratio rather than -webkit-device-pixel-ratio.
I had found that on one of my test devices (Galaxy Tab 3 - 8") the pixel ratio was 1.3 and wasn't picking up any of the specific styles I had set in my phonegap app.
#media screen and (-webkit-min-device-pixel-ratio: 0.5) {
#app_icon {
width:64px;
height:64px;
background: url('../images/bigstart.png') no-repeat center bottom;
background-size: 64px 64px;
}
}
#media screen and (-webkit-min-device-pixel-ratio: 1) {
#app_icon {
width:64px;
height:64px;
background: url('../images/bigstart.png') no-repeat center bottom;
background-size: 64px 64px;
}
}
}
#media screen and (-webkit-min-device-pixel-ratio: 1.3) {
#app_icon {
width:64px;
height:64px;
background: url('../images/bigstart#2x.png') no-repeat center bottom;
background-size: 64px 64px;
}
}
#media screen and (-webkit-min-device-pixel-ratio: 1.5) {
#app_icon {
width:64px;
height:64px;
background: url('../images/bigstart#2x.png') no-repeat center bottom;
background-size: 64px 64px;
}
}
#media screen and (-webkit-min-device-pixel-ratio: 2) {
#app_icon {
width:64px;
height:64px;
background: url('../images/bigstart#2x.png') no-repeat center bottom;
background-size: 64px 64px;
}
}
#media screen and (-webkit-min-device-pixel-ratio: 3) {
#app_icon {
width:64px;
height:64px;
background: url('../images/bigstart#3x.png') no-repeat center bottom;
background-size: 64px 64px;
}
}
I think you have to divide the reported screen dimensions by the screen density to get the media query width and height dimensions.