Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current screen width in CSS?

Tags:

html

css

I use the following CSS code for formatting when screen width is less than 480px, and it works well.

@media screen and (min-width: 480px) {     body {         background-color: lightgreen;     } } 

I would like to get the current width for calculation to use zoom like it follows:

@media screen and (min-width: 480px) {     body {         background-color: lightgreen;         zoom: (current screen width)/(480);     } }  
like image 241
mongmong seesee Avatar asked Feb 14 '16 15:02

mongmong seesee


1 Answers

Use the CSS3 Viewport-percentage feature.

Viewport-Percentage Explanation

Assuming you want the body width size to be a ratio of the browser's view port. I added a border so you can see the body resize as you change your browser width or height. I used a ratio of 90% of the view-port size.

<!DOCTYPE html>  <html lang="en">  <head>      <title>Styles</title>        <style>          @media screen and (min-width: 480px) {              body {                  background-color: skyblue;                  width: 90vw;                  height: 90vh;                  border: groove black;              }                div#main {                  font-size: 3vw;              }          }      </style>    </head>  <body>      <div id="main">          Viewport-Percentage Test      </div>  </body>  </html>
like image 116
Heather92065 Avatar answered Sep 30 '22 11:09

Heather92065