Is it possible to employ a dynamic size in media queries based on width?

Delving into the world of media queries as a novice, I stumbled upon an interesting condition while developing a Desktop application using electron. Although the CSS concept remains consistent with normal web development, a new challenge presented itself.

Typically, I utilize media queries in the following manner (illustrated here with examples for mobile, tablet, and desktop devices):

// default style for desktop
.my-default-css-code-is-here

// tablet
@media screen and (min-width: 768px) and (max-width: 1024px) {
   .my-specific-css-code-for-tablet-is-here
}

// mobile
@media screen and (max-width: 767px) {
   .my-specific-css-code-for-mobile-is-here
}

However, a friend posed a unique challenge:

"In this desktop application, we aim to achieve a layout where the window size is greater than 60% for Normal Size, but transitions to Tablet Size (window-size less than or equal to 60% of desktop resolution) when applicable. How can we accomplish this effectively?"

Accustomed to fixed sizes, I found myself uncertain about employing @media screen for this task. Can anyone shed light on this dilemma? Would utilizing a Ratio technique as described in this article be beneficial? Or perhaps implementing it like so?

// Desktop
@media screen and (min-width: 60% from A full Resolution) and (max-width: 100% from A full Resolution) {

}

// Tablet
@media screen and (max-width: 59% from A full Resolution) {

}

Alternatively,

// default css
.my-code-is-here {}

// Tablet
@media screen and (max-width: 59% from A full Resolution) {
   .my-code-for-tablet-is-here {}
}

For further clarification, let's consider two laptop types:

Laptop A :

Real resolution = 1368px, meaning 59% from real is 807px

Laptop B :

Real resolution = 2560px, equating to 59% from real being 1510px

Answer №1

To create dynamic media screens, consider using the npm package postcss-media-variables. This package enables you to incorporate CSS variables into your media queries. For example:

:root {
  --min-width: 1000px;
}
@media (min-width: var(--min-width)) {}

By setting a CSS variable like --min-width in JavaScript, you can easily adjust your media screen.

Answer №2

It seems like you may be a bit confused about the concepts of vw (viewport width) and vh (viewport height). These units are based on the size of the browser window, not the device itself. So whether you're viewing a website on a desktop monitor or a tablet, the values will adjust to fit that specific viewport.

Instead of relying solely on vw and vh, it might be wise to use fixed values in your media queries to ensure a consistent design across different screen sizes. This way, you can customize your layout for each device without solely depending on relative units. More information can be found here: https://www.w3schools.com/cssref/css_units.asp

If you need further clarification or guidance, feel free to reach out!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

jQuery .click() only triggering upon page load

I've been searching all over the place and I just can't seem to find a solution to my specific situation. But here's what I'm dealing with: Instead of using inline HTML onclick, I'm trying to use jQuery click() like this $(docume ...

Parsing JSON data repeatedly using JavaScript within an HTML environment

The following code I found on a popular web development website works perfectly: <!DOCTYPE html> <html> <body> <h1>Customers</h1> <div id="id01"></div> <script> var xmlhttp = new XMLHttpRequest(); var url ...

Implementing a splash screen upon selecting the second exit option

Check out the code snippet here: https://jsfiddle.net/wust7gdy/ Once the curtain is raised, the exit button will appear. How can I introduce a splash screen after clicking the 2nd exit button? Currently, there is a splash screen that appears after click ...

Leveraging basscss through NPM/Webpack installation

As a newcomer to the webpack/react app environment, I am attempting to incorporate the Basscss CSS framework into my project. After successfully running 'npm i basscss' and adding require('basscss/css/basscss.css'); to my app entry poin ...

Adjusting color schemes for Twitter Bootstrap Tooltips according to their placement

I have been attempting to customize the colors of tooltips (specifically from Twitter Bootstrap), but I am encountering some difficulties. While changing the default color was straightforward, altering the colors for .tooltip and its related definitions ha ...

To reveal more options, simply click on the button to open up a dropdown

I am currently utilizing the Bootstrap 5 CDN and I am looking for a way to automatically open the dropdown menu without having to physically click on it. Within the navbar, I want to display the "Dropdown Link": <nav class="navbar navbar-expand-sm ...

Perfecting the Radio Button Selection (because why not?)

Struggling to round the outer corners of three inline squared radio buttons with unique IDs. Need help fixing my CSS syntax - I can get each button to individually round its corners, but not just radio1 and radio3's outer corners. Check out the issue ...

Having trouble with input event listeners in JavaScript?

I've been attempting to trigger the keyup event using EventListener for an input tag, but it doesn't seem to be working and I'm unsure why. Here is the code I have: document.getElementById("ajax").addEventListener("keyup", function() { ...

Is there a way to display a loader when an item is selected from a dropdown menu? For example, while data is being fetched from the backend, can we

This is the HTML form division I have created: <div class="col-lg-2 "> <select name="limitCount" id="limitCount" ng-model="limitCount" class="form-control col-md-2 panel-refresh" ...

An exciting tutorial on creating a animated dropdown using vueJS

I've set up a navigation menu with mouseover and mouse leave events to toggle a dropdown by changing a boolean value. Now, I'm trying to apply different animations to the list items in the dropdown compared to the surrounding box, but I'm f ...

Elements on the page are being truncated beyond the visible viewport

Encountering an issue with a web page I'm currently developing that is proving challenging to explain. The problem occurs when the page is viewed in a small browser window and cuts off when scrolling horizontally to content outside of the viewport. Wh ...

The dimensions of the image are determined by its orientation

I am working with two different types of images on my website: vertical and horizontal. Currently, I have a JavaScript function that adjusts the height of all images to fit the screen size. However, I would like to modify this function so that it also adap ...

Incorporating CSS into an HTML page created by a Python CGI script

Last week, I was handed a Python-based monitoring and notification tool to work on. My task is to develop a proof of concept dashboard using the data collected by this tool over the years. The catch is that I'm fairly new to Python and have never dabb ...

What is the procedure for making the menu background color transparent?

I need help changing the background color to transparent on my menu in desktop view. ul.dropdown-menu li { background-color: transparent !important; } <div class="collapse navbar-collapse navbar-ex1-collapse"> <ul id="menu" class="menu"> ...

choosing from dropdown menu items

Learn HTML <table> <tr> <td>ENTER PRINCIPLE AMOUNT</td> <td><input type="text" name="principle" size="7"></td> </tr> <tr> <td>ENTER RATE OF INTEREST</td> <td><input type="text" na ...

Is there a CSS Grid that supports IE6+ with percentage-based layouts?

Seeking a sleek CSS grid system that is percentage-based with 12 columns and works well on IE6+ as well as all modern browsers. Support for nested columns would be a bonus but not mandatory. Many grids out there lack compatibility with IE6 or are based sol ...

The fonts appear differently when working on a Mac but show up as Times New Roman when displayed on Windows in development

Currently, I am utilizing react combined with nextjs for server-side rendering and bootstrap. Within my component, I have integrated bootstrap via a CDN: <Head> <title>{title}</title> <meta charSet="utf-8" /> < ...

Is it possible to implement smooth scrolling in HTML without using anchor animation?

Is it feasible to implement a more seamless scrolling experience for a website? I'm referring to the smooth scrolling effect seen in MS Word 2013, but I haven't come across any other instances of this. I've heard that AJAX can make such th ...

Customizing screen dimensions for a single page using jQuery mobile

I am currently facing an issue with adjusting the size of a specific page within my application. Even though I am using <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, target-den ...

How can you ensure that divs stay the same size and appear next to each other without resorting to using

I need assistance with organizing my website layout as shown below: The image div will display an image of variable size, which needs to be vertically and horizontally centered. The text div will contain a large amount of text. While this could easily be ...