3

How to use a with-height conditions. Without javascript.

@media (calc(window-width > window-height)) {
    background-color: lightblue;
}
@media (calc(window-width <= window-height)) {
    background-color: lightgray;                
}

I want to page for mobile defices for detecting if mobile is rotated to portrait or landscape.

andreas
  • 16,357
  • 12
  • 72
  • 76
user2301515
  • 4,903
  • 6
  • 30
  • 46

5 Answers5

5

You can use the orientation constraint:

@media (orientation: landscape) {
    /* applies to devices in landscape mode */
}
@media (orientation: portrait) {
    /* applies to devices in portrait mode */
}

Multiple rules are comma separated (OR), otherwise use AND.

See MDN.

andreas
  • 16,357
  • 12
  • 72
  • 76
3

Try this:

.something {
    background-color: lightblue;
}

@media (orientation: landscape) { 
    .something {
        background-color: lightgray;
    }
}
Daniel Rossko Rosa
  • 362
  • 1
  • 2
  • 11
3

You can use media queries with height also -

For example -

@media screen and (max-width: 995px) , screen and (max-height: 700px) {
  ...
}

or you can use with orientation

@media (min-width: 700px) and (orientation: landscape) { ... }

And you can also use with height

@media (max-height: 700px), handheld and (orientation: landscape) { ... }

Some of my friend also post the MDN URL you can get more information from that.

This answer can be useful for you

Community
  • 1
  • 1
Mukul Kant
  • 7,074
  • 5
  • 38
  • 53
2

MDN docs:

orientation

Value: landscape | portrait

Media: visual

Accepts min/max prefixes: no

Indicates whether the viewport is in landscape (the display is wider than it is tall) or portrait (the display is taller than it is wide) mode.

Example

To apply a style sheet only in portrait orientation:

@media all and (orientation: portrait) { ... }

Note: This value 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.

RussAwesome
  • 464
  • 5
  • 18
2

You cannot use calc method as the calc method is css property's value not property.

But you can combine media rule with max-width and min-width (paste the width or height value instead xxx):

@media(max-width:xxx) and (min-width: xxx){}
@media(min-width:xxx) and (max-height: xxx){}

You can also use more advanced media query like this:

@media all and (max-width: xxx) and (min-width: xxx), (min-width: xxx) {}

If you're trying to work with portrait and landscape, then you can do like this:

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

Refer this reference that's all we have.

Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231