Make sure the elements of the responsive and fluid grid stay within the boundaries of the viewport's size consistently

Can you help me figure out how to create a responsive/fluid grid with 2 rows of content, one for ".tier1" and the other for ".tier2"? I want the contents to always fit inside the viewport dimensions, with ".tier1" taking up 60% of the height and ".tier2" taking up the remaining 40%. I'm not sure what the best approach is at the moment. I could use JavaScript to monitor the viewport dimensions and set heights accordingly, but I'm wondering if there's a way to achieve this using just CSS.

CSS

.content1 {
    height: 60%;
}
.content2 {
    height: 40%;
}

HTML

<div class="container">
    <div class="tier">
        <div class="content content1">
            <img src="http://dummyimage.com/600x400/000/fff">
        </div>
    </div>
    <div class="tier">
        <div class="content content2">
            <img src="http://dummyimage.com/600x400/dedede/fff">
        </div>
    </div>
</div>

Fiddle http://jsfiddle.net/EK6QT/1/

Answer №1

To cater to the needs of different web browsers you are supporting, consider implementing CSS3 Viewport Relative Lengths, as suggested on CanIUse.

Viewport-percentage lengths adjust according to the size of the initial containing block. Changes in height or width of the initial block will scale these lengths accordingly.

In your scenario, instead of using %, try utilizing vh:

.content1 {
    height: 60vh;
}
.content2 {
    height: 40vh;
}

Check out this JSFiddle demonstration.

For a more comprehensive explanation on viewport relative lengths, refer to my other answer here.

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

Issues arise when attempting to insert data into an HTML file using PHP

Good evening, I am having a problem where when I attempt to use PHP to replace "hello world" in an HTML file, it ends up completely empty each time. Can anyone point out what mistake I might be making? Here are the HTML and PHP files: <!DOCTYPE html&g ...

Transform Asp:CheckBox Appearance with CSS Styling

Is there a way to customize the appearance of the standard "3D" look for asp.net checkboxes to have a solid 1px style? When applying styling to the Border property, it simply adds a border around the checkbox itself. I'm looking to change the styling ...

Is there a way to launch QTP from JavaScript without relying on ActiveXObject?

Is there a way to call QTP from JavaScript without relying on ActiveXObject? I would appreciate any guidance on how to accomplish this task. Thanks in advance, Ramya. ...

Understanding the concept of nested children JSON within Angular HTML structures

I am dealing with a JSON structure that contains nested children elements. RowElements can have any number of ColElements, and ColElements can have any number of RowElements within them. Here is an example JSON: "container": { "id" ...

Progress Bar Rating System in Bootstrap 4

Trying to achieve a specific layout using Bootstrap 4: https://i.sstatic.net/Ym6Ov.jpg Disregarding "See All" and "4,327 Ratings" Progress so far has led to: https://i.sstatic.net/hPSRn.png The numerical ratings can be replaced with font icons. Provi ...

A guide on seamlessly incorporating FlotJs functionalities into a ReactJs application

Having trouble integrating Flot charts into React due to a '$.plot' is not a function error. Here's the code I'm using: Script tags Index.html <script src="dist/libs/js/jquery.min.js"></script> <script src="dist/libs/js ...

Secure the data by encrypting it in the frontend before decrypting it in the

How can I achieve the highest level of security in this situation? I have experimented with using the same public key for all users to encrypt data transmitted over HTTPS to my backend automatically. However, individuals could potentially intercept and d ...

Exploring Firebase database with AngularJS to iterate through an array of objects

I'm working on an app that pulls data from a Firebase database, but I'm running into an issue. When I try to loop through the data and display it on the page, nothing shows up. However, if I use console.log to output the data, it's all there ...

What is the best way to transform a Ruby hash into a JavaScript object while maintaining unquoted values?

Is there a way to display a Ruby hash as a JS object without quotes for certain values in the hash, essentially as JS code? For instance, consider the following Ruby code: { foo: proc { 'someJavascriptFn()' } }.to_json How can I have the JS out ...

To avoid displaying an empty drop-down menu, ensure that the options are populated before showing it using AngularJS

I have a situation where there are 3 different drop-downs - Drop-down A, Drop-down B, Drop-down C. Depending on the selection made in Drop-down C, additional dropdowns will be populated. Let's consider another set of 10 drop-downs numbered fro ...

What could be causing this Ajax error to occur when attempting to post HTML using stringify?

When attempting to post HTML in a string using stringify, an Ajax error occurs. Why is this happening? It appears that the characters are being automatically escaped by stringify. Do I need to manually escape them? Thank you for any guidance. var s; // ...

Troubleshooting issue with Highcharts 3D rendering after using setState()

When working on implementing a 3d pie chart in React using react highchart, I encountered an issue. Whenever I utilize this.setState() inside the lifecycle method componentDidMount(), the 3d chart shifts from its original position to the right diagonally. ...

Title vanishes when gradient is added to a div

I have encountered an issue with a recent update on my webpage. Previously, I had multiple divs with the title attribute that displayed information when users hovered over them. However, after adding a gradient background, the titles stopped appearing. Th ...

svg xlink attribute not functioning properly when embedded as svg object within html

Within the SVG, I have multiple A elements linking to various pages. When the SVG is loaded into a browser as a standalone, the links function properly. However, when it is embedded as an object within an HTML page, the links do not work. It seems like th ...

Inject the HTML code into a bash script and perform operations on it

Could someone help me with a bash scripting question? I'm new to this, so I may have missed it if it's already been answered. I want to pass the html source code of a web page to my script, which will then modify or scrape the web page of its HT ...

Utilize WebGL to incorporate a mesh as a background image mask

Currently, I am faced with a challenge in WebGL that involves the following scenario: Picture a mesh positioned in front of the camera. This mesh does not have traditional shading, but its outline, or "silhouette," is utilized to display a background imag ...

Send a response directly from a controller method in Express without using req and res

One of my methods updates data in Mongo, but I am facing a challenge with handling the response as the request never completes. This method will be used multiple times and does not have access to "req" and "res". Any suggestions on how to tackle this issue ...

What is the best way to integrate external APIs into a Next.js application?

I am currently working on a weather app project as part of my resume. My goal is to retrieve current weather data by calling WeatherAPI and using their currentdata.json endpoint. Can anyone guide me on how to implement this in code? This project consists ...

Having trouble receiving a complete response when making an ajax request to fetch an entire webpage

Currently, I am experimenting with J-Query Ajax functionality. My goal is to send a request in order to retrieve the entire HTML data of a page that I have hosted on bitbaysolutions.com. The issue arises when I load this URL and execute document.getElement ...

Guide to submitting a form (adding a new subscriber) using mailchimp-api-v3 with Node.js

I am currently utilizing the mailchimp-api-v3 for form submission purposes. The form I am working with consists of only three fields: FNAME, EMAIL, and COMPANY. const Mailchimp = require('mailchimp-api-v3'); const mailchimp = new Mailchimp(myMa ...