Is there a method for adjusting the dimensions of an empty div similar to how image sizes are adjusted

Is there a way to make a div responsive like an image in CSS? For example, if I have an image sized at 800px x 400px and set its width to 100%, it will automatically resize to fit the full width of the screen on a desktop (width: 1400px) to become 1400px x 700px. However, for a div element I must set both the width and height specifically. Is there a method to set an empty div with a width of 800px and height of 400px, that will then resize proportionally depending on the screen size, while still maintaining its ratio like an image?

Any insights would be appreciated.

Answer №1

To ensure compatibility with your browser, consider using viewport units to establish the aspect ratio for your div element. The browser will handle the rest of the calculations.

You can assign class names for various sizes, such as the following one designed for 16:9 images:

width: 100vw;
height: 77.78vw;

For a 16:9 aspect ratio (1.777777777777778), you would need to apply 77.78% height of the viewport to match 100% width of the viewport on the element.

You have the option to pre-define classes for different aspect ratios or utilize simple JavaScript to compute these values dynamically.

Check out this example on Codepen: https://codepen.io/Mchaov/pen/vJrgYa

html, body {
  height: 100%;
  margin: 0;
  padding: 0;
}

div {
  margin: 0;
  padding: 0;
  border: 1px solid red;
}

.autoScale-16-9 {
  width: 100vw;
  height: 77.78vw;
}
<div class="autoScale-16-9">auto scale div</div>

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

How to store information in a database without the need to select and view images

I need help saving my data using a form without selecting an image because it's not necessary at the moment. However, I keep encountering the error Image source not readable. My form includes 'enctype' => 'multipart/form-data' ...

Modify the colors of <a> elements with JavaScript

I am brand new to the world of UI design. I am encountering an issue with a static HTML page. (Please note that we are not utilizing any JavaScript frameworks in my project. Please provide assistance using pure JavaScript code) What I would like to achie ...

The background image does not appear to be displaying correctly on GitHub pages

I've encountered an issue where my svgs are not displaying as background-image or background on GitHub pages, even though they work fine locally. I utilized sass as a preprocessor for css, in case that detail is helpful. If anyone has insights on how ...

Preventing event duplication through clone() method

I've successfully duplicated my HTML elements using this JavaScript code, but I'm having trouble copying the events associated with the code. Can anyone provide a solution? <script type = "text/javascript" language = "javascript"> f ...

Failure to capture spaces in onChange events for <input> elements

I'm having an issue where the onChange event is not being called on my HTML input element when pressing the space bar. Here's a snippet of what I've tried: class FilterDropDown extends React.PureComponent { state = { query: '&ap ...

Using HTML to load an image is not possible, as it can only be done using CSS

I am currently dealing with a CSS issue in my project. Here is the code snippet from my stylesheet: .Img{ background: url('../assets/Trump.png'); } Now, let's take a look at the corresponding HTML code: <div class="Img"> However, ...

What is the best way to ensure that two divs have aligned bottom edges?

I have a set of div containers positioned in different columns within a grid layout. I am looking for a way to identify the containers where the bottom edge is less than 50px higher than any other container, and adjust their heights so they align perfectly ...

Confirming the information in two sections of a form

Could someone assist me with validating my form? Specifically, I need help ensuring that the house number field only accepts two numbers and the postcode field only allows 5 characters. I have implemented a pattern attribute for validation and I am curiou ...

Creating a Basic Slideshow using Javascript

I've been attempting to create a slideshow using only JavaScript, but I've run into an issue. When I set the divs to change on click, it works perfectly fine. However, when I try to set it to change at specific intervals, it doesn't work. ...

`Can you provide instructions on modifying CSS using JavaScript once the window size reaches a specified threshold?`

Is there a way to use JavaScript to automatically adjust the font size when the screen reaches 1050px? ...

The dropdown menu is being truncated

I am having an issue with a drop-down menu being cut off by its parent div. When I increase the height of the parent div, the drop-down menu becomes visible. Can someone please assist me? Below is my code snippet: MarkUp <div id="main-navigation" clas ...

Is it possible to detect inline elements that have been wrapped?

I am facing a challenge with displaying an indefinite amount of in-line elements. Depending on the width of the browser, some elements may shift to a new line. I am curious to know if it is possible to identify and isolate these rows of elements, or if the ...

What is causing col-xs-0 to occupy the entire width of the row?

Here is the structure of my Bootstrap container: <main > <div class="row co-lg-12 col-md-12 col-xs-12"> <div class="col-lg-1 col-md-1 col-xs-1" style="background-color:red; height: 100vh;"></div> <div class="col-lg-4 col-m ...

What is the best way to align a tweet in the middle of

When using Twitter to embed a tweet, the code provided is necessary. An example of this can be seen below: <blockquote class="twitter-tweet"><p>NoSQL space gradually becoming SlowSQL space.</p>&mdash; Big Data Borat (@BigDataBorat) & ...

Detecting a click on the background div in Angular without capturing clicks on child divs

Over at ollynural.github.io, I've been working on creating a pop-up div in the portfolio page that provides additional information about the project you select. To close the pop-up, I have implemented an ng-click feature so that clicking on the main p ...

Transition smoothly between two images with CSS or jQuery

I am working on a project where I need to implement a hover effect that fades in a second image above the initial image. The challenge is to ensure that the second image remains hidden initially and smoothly fades in without causing the initial image to fa ...

My code written using Visual Studio Code is not displaying properly when I view it in my browser

I have been following along with a tutorial series that can be found at this link. This link will take you to the third video in the series, but I have also followed and programmed alongside the first and second videos. After installing Visual Studio Code ...

How come this relative URL is functioning properly?

Recently, I've been experimenting with web crawlers and testing them on various websites. However, I've encountered some sites where the relative URLs don't seem to work as expected. When the browser resolves them, they end up pointing to a ...

CSS Techniques for Smooth Transitions

When the hamburger icon is pressed, a menu appears over the current page with CSS from Glamor. The menu slides in perfectly from the right side of the screen, but I am having trouble getting it to slide out when any part of the menu is clicked. I have def ...

Cropping and resizing images

Within my angular application, I have successfully implemented image upload and preview functionality using the following code: HTML: <input type='file' (change)="readUrl($event)"> <img [src]="url"> TS: readUrl(event:any) { if ...