51

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);
    }
}

sRavioli
  • 27
  • 5
mongmong seesee
  • 987
  • 1
  • 13
  • 24

4 Answers4

77

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>
David Mohundro
  • 11,922
  • 5
  • 40
  • 44
Heather92065
  • 7,333
  • 6
  • 31
  • 39
22

Just as we have the css unit "view-height(vh)", which roughly corresponds to the height of the viewport, you can also use view-width(vw) to dynamically resize your element with respect to the width of the viewport

i.e


.random-div{
    height: 100vh;
    width: calc(100vw - 480px);
}

cheers

Adépòjù Olúwáségun
  • 3,297
  • 1
  • 18
  • 27
8

this can be achieved with the css calc() operator

@media screen and (min-width: 480px) {
    body {
        background-color: lightgreen;
        zoom:calc(100% / 480);
    }
}
Suthura Sudharaka
  • 643
  • 10
  • 25
0

Based on your requirement I think you want to put dynamic fields in CSS file, however that is not possible as CSS is a static language. However you can simulate the behaviour by using Angular.

Please refer to the below example. I'm here showing only one component.

login.component.html

import { Component, OnInit } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
    
    @Component({
      selector: 'app-login',
      templateUrl: './login.component.html',
      styleUrls: ['./login.component.css']
    })
    export class LoginComponent implements OnInit {
    
      cssProperty:any;
      constructor(private sanitizer: DomSanitizer) { 
        console.log(window.innerWidth);
        console.log(window.innerHeight);
        this.cssProperty = 'position:fixed;top:' + Math.floor(window.innerHeight/3.5) + 'px;left:' + Math.floor(window.innerWidth/3) + 'px;';
        this.cssProperty = this.sanitizer.bypassSecurityTrustStyle(this.cssProperty);
      }
    
    ngOnInit() {
    
      }
    
    }

login.component.ts

<div class="home">
    <div class="container" [style]="cssProperty">
        <div class="card">
            <div class="card-header">Login</div>
            <div class="card-body">Please login</div>
            <div class="card-footer">Login</div>
        </div>
    </div>
</div>

login.component.css

.card {
    max-width: 400px;
}
.card .card-body {
    min-height: 150px;
}
.home {
    background-color: rgba(171, 172, 173, 0.575);
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197