This SO question may be of some help.
Technically, you can't access the screen's dimensions directly from CSS, nor can an element know about its parent's absolute dimensions without the help of JavaScript. Instead you usually express an element's dimensions relative to its parent, such as
#logo {
width: 500px;
}
#logo img {
width: 33%;
}
If an image's CSS height is not explicitly set or inherited, it'll scale with the width; the reverse is also true.
You can set an image to fill its parent container using width: 100%
or height: 100%
, but this may cause the image to overflow the container. The properties max-width
and max-height
can account for this. Try:
img {
max-height: 100%;
width: 100%;
}
but be aware that most browsers by default allow a document to grow in height as necessary, so a max-height: 100%
property will do very little if its parent container doesn't have some sort of restriction on its own height.
That said, Javascript can access the browser's dimensions directly with window.innerWidth
and window.innerHeight
. However, these properties are only set when the page initially loads, and they won't change if the browser window is resized. document.documentElement.clientWidth
and document.documentElement.clientHeight
are more reliable.
As always, UX libraries like jQuery and Bootstrap.js make this kind of task much easier.