Similar to aspect ratio, orientation is something CSS spec authors recognize as being useful for styling, but are only support on the <html>
element itself via Media Queries, and not something that you can use to apply styles to other elements in the document.
The solution is to use JavaScript to determine an element's orientation by measuring the width and height of the element.
Portrait:
this.offsetWidth < this.offsetHeight
Square:
this.offsetWidth == this.offsetHeight
Landscape:
this.offsetHeight < this.offsetWidth
<html>
element only)@media (orientation: portrait) {
body {
background: lime;
}
}
@media (orientation: landscape) {
body {
background: hotpink;
}
}
@element div and (orientation: square) {
$this {
background: orchid;
}
}
@element div and (orientation: portrait) {
$this {
background: darkturquoise;
}
}
@element div and (orientation: landscape) {
$this {
background: greenyellow;
}
}
/* portrait */
.orientation[test="this.offsetWidth < this.offsetHeight"] {
background: orchid;
}
/* square */
.orientation[test="this.offsetWidth == this.offsetHeight"] {
background: darkturquoise;
}
/* landscape */
.orientation[test="this.offsetWidth > this.offsetHeight"] {
background: greenyellow;
}