Guide on adjusting container size according to browser dimension

Is there a way to dynamically scale the height of the black containers from a maximum height of 550px to, for example, a height of 400px simply by adjusting the browser's width? Additionally, I would like the box divs to resize proportionally along with the black container. Instead of using media queries, I want this resizing to happen gradually so that you can see it change pixel by pixel. I prefer not to have the height transition abruptly when reaching a specific width set in media queries.

.container-black {
    height: 550px;
    background-color: black;
    display: flex;
    justify-content: center;
    align-items: center;
    border-bottom: solid 8px grey;
}

.box1 {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 70%;
    width: 30%;
    background-color: red;
}
<body>

    <div class="container-black">
        <div class="box1">
            <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Assumenda libero quidem minima tenetur delectus,
                iusto hic perferendis repudiandae soluta maxime nemo facere rem vero officiis! Illo deserunt eum ullam
                eligendi!</p>
        </div>
        <div class="box1">
            <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Assumenda libero quidem minima tenetur delectus,
                iusto hic perferendis repudiandae soluta maxime nemo facere rem vero officiis! Illo deserunt eum ullam
                eligendi!</p>
        </div>
    </div>
</body>

Answer №1

Utilize dynamic values like vw or viewport width. For instance, you can specify box1 to have a width: 40vw; to maintain black borders. This ensures that box1 will occupy 40% of the viewport size when resized, while container-black retains 10% of viewport width on any device for responsiveness.

The Dynamic Viewport adjusts its size relative to UA interfaces dynamically, responding to changes in shown or hidden UA interface elements.

UPDATE -- introduced two additional container-blacks, nested them in a parent container, and defined their height based on % for responsive resizing.

.container-black {
    height: 33%;
    width: 100vw;
    background-color: black;
    display: flex;
    justify-content: center;
    align-items: center;
    border-bottom: solid 8px grey;
}

.box1 {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 70%;
    width: 40vw;
    background-color: red;
}

.container {
  height: 100vw;
}
<body>
<div class="container">
    <div class="container-black">
        <div class="box1">
            <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Assumenda libero quidem minima tenetur delectus,
                iusto hic perferendis repudiandae soluta maxime nemo facere rem vero officiis! Illo deserunt eum ullam
                eligendi!</p>
        </div>
        <div class="box1">
            <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Assumenda libero quidem minima tenetur deLectus,
                iusto hic perferendis repudiandae soluTa Maxime NEmo facere REM Vero officiis! ILLo deserunT euM uLLam
                ELigendi!</p>
        </div>
        
    </div>
    <div class="container-black">
        <div class="box1">
           <p>Lorem Ipsum Dolor SiT AMeT Consectetur Adipisicing Elit. ASSUmeNDA LibErO QuidEM Minima TenETur DeLeCtus,
                IUSto HIC PerFerendas REpudianDAE SOLUtA MAximE NemO FACERE Rem VeRO OffICIIs! IllO Deserunt EUm UllaM
                elIgeNDI!</p>
        </div>
        <div class="box1">
            <p>LOREM IPSUM DOlOr SIt Amet consectEtur AdIPISciNg eLIT. asSumEnda LIberO QUIdem MINIMa TenUr DElectUs,
                IustO hic PERFeRENDis repuDianaDE SOLUTA MaXIME NEMO FACERe ReM veRo OFFIcis! ilLo deform und zu eine Elle GemELdEn!</p>
        </div>
       
    </div>

</body>

Answer №2

To define the height of an element within a media query, consider utilizing the vw unit, which represents a percentage of the viewport width.

Answer №3

To achieve this, you can utilize the vw unit for the height of the dark-colored box.

Essentially, 1vw is equivalent to 1% of the viewport's width.

Alternatively, you may opt for the CSS aspect-ratio feature. To delve deeper into this topic, click on the provided link below.

https://css-tricks.com/almanac/properties/a/aspect-ratio/

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

Vanilla JavaScript Troubleshooting: Top Scroll Button Issue

Attempting to develop a Scroll To Top Button utilizing Vanilla JS, encountering errors in the dev console. Existing jQuery code that needs conversion to vanilla js Uncaught TypeError: Cannot read property 'addEventListener' of null My Vanilla ...

Remove a row from the database with PHP by utilizing an HTML table

I'm having trouble with the "delete" function in my form. Even after trying this code: while($row = $result->fetch_assoc()) { $ordernr = $row['ordernr']; $klantnaam = $row['klantnaam']; $productnaam = ...

Looking to create a centered 'ul' using Bootstrap

I am looking to center the list with a specific width. body { background-color: rgb(26, 40, 114); } h1 { color: white; text-shadow: 7px 7px 10px black; } li { list-style: none; border-radius: 5px; border: 2px solid white; background-colo ...

Optimal Technique for Arranging ASP.NET Panels in a Side-by-Side Layout

Seeking advice on optimal method to align multiple containers horizontally next to each other rather than stacking them vertically. Some sources recommend using style="float:left;" while others propose utilizing style="display:inline;", which has been inef ...

A pair of large pillars perfectly aligned at the center in both horizontal and vertical position

Can anyone suggest an efficient way to design the following layout with responsiveness in mind? I attempted to divide the elements into sections and applied the transform property, but I encountered difficulties when trying to style the boxes on either si ...

How to effectively use graph paper with both major and minor grids in the background?

Looking to create a 100px by 100px image in Inkscape with major lines every 100px and minor lines every 10px. This will help verify viewport size when used as a repeating background. What's the best approach for this? Thinking of using #888888 for ma ...

Implementing JavaScript in a VB.NET Application

My current project at the office involves developing a VB.NET application to streamline and partially automate our work processes. One task within this project requires downloading a .pdf file from our intranet. To achieve this, we utilize IE through a sc ...

Implement a functionality to retrieve uploaded files by utilizing the onChange event of a

I'm currently working on converting this jQuery code to React: //Js $(".custom-file-input").on("change", function() { var files = Array.from(this.files) var fileName = files.map(f =>{return f.name}).join(", ") $( ...

Steps for eliminating the overlay on top of the padding region

My current code includes an overlay over images, but the overlay extends beyond the image itself and covers the padding area as well. If I switch the padding to margin in order to eliminate this unnecessary overlap, it causes the last image to be pushed on ...

Remove a row from a table by looping through all the rows using jQuery

#exceptions represents an html table. I attempted to use the code below, however it's not successfully removing the table row. $('#exceptions').find('tr').each(function(){ var flag=false; var val = 'excalibur'; ...

Finding the identification of the first element within a nested div using JQuery

Here is the HTML snippet that I am working with: <div id="parent"> <div id="computer"> <div id="items"> <div id="1">hp</div> <div id="2">compaq</div> <div id="3"& ...

Tips for positioning the search form in the navbar header

My brand image and list with 2 glyph-icons & a search form in the navbar header are not aligning properly. The search form button keeps dropping to the line below. Does anyone know a CSS trick to keep it on the same line as the rest of the links? All the ...

Error in Javascript programming | tracking progress bar

After stumbling upon this code snippet at this link, I was eager to delve deeper into the world of JavaScript and jQuery. However, upon implementing these codes, I encountered a perplexing issue. The progress bar and continue buttons seem to be non-funct ...

Encountering difficulties invoking a REST web service using HTML/JavaScript with parameter

I am just starting out with web services and learning about HTML/Javascript. Recently, I created a RESTful web service in Java that takes a username from an HTML form and returns it back to the same page using the alert() function. Below is the code for m ...

Using Angular-Translate to Replace Variables in Title Tags

In my AngularJS project, I am using Angular-Translate version 2.6.1. One of the challenges I am facing is how to include a variable in a translated title attribute within a span element. <span title={{'translationID'|translate:'{username ...

Utilizing knockoutjs to bind JSON data collections

Recently, I began exploring Knockoutjs and encountered an issue that has me puzzled. My situation involves receiving a json object from the server, which is essentially a collection of objects. My goal is to utilize knockoutjs to bind this data to a list. ...

A CSS3 property that does not have a vendor prefix, followed by properties that do have vendor prefixes

As discussed in a response on Stack Overflow, one reason for the necessity of vendor prefixes in CSS properties is to prevent web pages from breaking even if the final specification varies from the implementation by different browsers. In a recent reading ...

Altering the value of a selection form element before sending off the data

Having trouble updating the select value upon submission. The system is rejecting special characters like "$" and ",", so I am cleaning up the data. Using jQuery jQuery("#vfb-53").val( jQuery("#vfb-53").val().replace(/\D/g,'') ); jQuery("# ...

When hovering over the menu, the underline appears, not based on the length of the

Is there a way to make the underline below menu items on hover match the length of the word instead of the element? <nav class="navbar navbar-expand-lg navbar-light"> <a class="navbar-brand active" href="index.html"> <img src= ...

CSS failing to reach the full width of the container

My navigation tab is currently only occupying half of the width that I need it to. I have tried various solutions found on this site and through Google, but none seem to work as expected. The black background of the navigation should fill up 100% of the wi ...