Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

css expanding based on portrait or landscape screen size?

Tags:

I have two divs that are floating next to each other. What i would like is to have it have a width of 100px when you are looking at it in portrait mode and lets say 200 px in landscape. This happens viewing on a mobile device actually.

So the div will expand when in landscape mode and shrink up a bit in portrait.

Any ideas?

like image 545
Xtian Avatar asked Oct 04 '10 19:10

Xtian


People also ask

What is orientation landscape CSS?

portrait. The viewport is in a portrait orientation, i.e., the height is greater than or equal to the width. landscape. The viewport is in a landscape orientation, i.e., the width is greater than the height.

What is orientation landscape in media query?

Media queries can also be used to change layout of a page depending on the orientation of the browser. You can have a set of CSS properties that will only apply when the browser window is wider than its height, a so called "Landscape" orientation.

What is media orientation?

“Media orientation” refers to the attitude of communication professionals towards mass media. It can be used as an indicator of the status of mediatisation within organisations on the individual level of communication professionals (Kohring, Marcinkowski, Lindner, & Karis, 2013.


2 Answers

Well this is not possible with CSS2, but it would be possible to do what you want with Javascript (read out the screen size etc.).

I'm not sure what technology you are looking into, but CSS3 provides exactly what you want with CSS3 Media Queries.

With CSS3 you have fun stuff like this (or you could even specify width e.g. 200px):

/* Portrait */ @media screen and (orientation:portrait) {    /* Portrait styles here */ } /* Landscape */ @media screen and (orientation:landscape) {    /* Landscape styles here */ } 

Check out this example page for more explanation, or this one.

EDIT Just because I found this page today: Hardbroiled CSS3 Media Queries - very good writeup.

like image 187
Dennis G Avatar answered Oct 22 '22 21:10

Dennis G


You can do this by using the css media feature "orientation". This way you can specify styles depending on screen orientation, unrelated from screen size. You can find the official w3.org definition about this media feature here. Combined with the specifications on developer.mozilla.org this will explain how it works.


Quote from w3.org about the ‘orientation’ media feature:

The ‘orientation’ media feature is ‘portrait’ when the value of the ‘height’ media feature is greater than or equal to the value of the ‘width’ media feature. Otherwise ‘orientation’ is ‘landscape’.

A note/quote from developer.mozilla.org about the "orientation" feature:

Note: This value (ie portrait or landscape) does not correspond to actual device orientation. Opening the soft keyboard on most devices in portrait orientation will cause the viewport to become wider than it is tall, thereby causing the browser to use landscape styles instead of portrait.


So to reiterate, it is not actually the screen orientation that triggers portrait or landscape media queries. However it is the ratio between height and width of the screen! Because of this it also makes sense to use the "orientation feature" with non mobile/tactile devices hence I've added a small demo to show the behaviour of these media queries.

JSFIDDLE DEMO (try resizing the view port)

Here are the representative media queries affecting portrait and landscape orientation.

 @media screen and (orientation:portrait) {      /* Portrait styles */      /*set width to 100px */   }  @media screen and (orientation:landscape) {      /* Landscape styles */      /* set width to 200px*/   } 
like image 37
kasper Taeymans Avatar answered Oct 22 '22 21:10

kasper Taeymans