Design a div element with a responsive aspect ratio of 16:9

Hey everyone, I've been working on creating a responsive div with a 16:9 ratio. However, I've noticed that when I resize the window, the container3 remains the same size. Can anyone spot an issue in my code and offer some help? Thanks!

$(document).ready(function(){
rp_config();

});
$(window).resize(function(){
rp_config();

});
function rp_config(){
var windowwidth = $(window).width();
var windowheight = $(window).height();
var bar = 64;
var bottom = 45;
var chat = 250;
var container2_width = windowwidth - chat;
var available_height = windowheight - bar - bottom;

$(".container").width(windowwidth);
$(".container2").width(container2_width);
$(".container3").width(available_height / 9 * 16);

$(".container").height(available_height);
$(".container2").height(available_height);
$(".container3").height(available_height);



}
rp_config();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container" style="background-color:#000;">
<div class="container2" style="background-color:#333;">
    <div class="container3" style="background-color:#ccc;">
        
        </div>
    </div>
</div>

Answer №1

To determine if container3 is wider than container2, you must make a decision.

$(document).ready(function(){
rp_config();

});
$(window).resize(function(){
rp_config();

});

function rp_config(){
  var windowwidth = $(window).width();
  var windowheight = $(window).height();
  var bar = 64;
  var bottom = 45;
  var chat = 250;
  var container2_width = windowwidth - chat;
  var available_height = windowheight - bar - bottom;
  

  var altWidth = available_height / 9 * 16
      ,altHeight = available_height;
  if (altWidth > container2_width) {
    altWidth = container2_width;
    altHeight = altWidth / 16 * 9;
  }   
  
  $(".container").width(windowwidth);
  $(".container2").width(container2_width);
  $(".container3").width(altWidth);
  
  $(".container").height(available_height);
  $(".container2").height(available_height);
  $(".container3").height(altHeight);
}
rp_config();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='case1' class="container" style="background-color:#000;">
<div class="container2" style="background-color:#333;">
    <div class="container3" style="background-color:#ccc;">
        </div>
    </div>
</div>
</div>

altHeight is used here to maintain a 16:9 ratio for container3, but you can delete it if you prefer to fill the entire container2 space.

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

Angular JS function is returning before the completion of the http.get request

I'm encountering an issue where this block of code is returning before it has received all the necessary information. Here's my current implementation: function (){ .... var promise = $http.get(...) promise.then (...){ //get info nee ...

Reactjs Invariant Violation caused by the npm package (react-loader)

I'm currently attempting to integrate react-loader into my react component. This is the code snippet I'm using: /** @jsx React.DOM */ var Loader = require('react-loader'); var DisplayController = React.createClass({ // etc ...

Learn how to use jQuery to load a text file containing arrays and then format them as

I'm attempting to load a .txt file containing multidimensional arrays using AJAX, and then sort through the data to display it on my website. However, I'm facing an issue where the data is only returning as plain text, even after trying to use JS ...

Ensuring elements are correctly positioned

I am struggling to make the images in the following code align horizontally across the top and also need to align the h1's in the same way. I have tried using the vertical-align property, but it seems to behave oddly to me. HTML: <div class=&apos ...

How can PHP be used to replace the div html() function?

I am working with multiple product elements that are dynamically assigned their class and ID using PHP: $product1["codename"] = "product-1"; $product1["short"] = "Great Product 1"; $product2["codename"] = "product-2"; $product2["short"] = "Great Product ...

Why is the responseText from XMLHttpRequest always stripped of tags in AJAX?

Whenever the server sends the XML string to the client using the XMLHttpRequest object, I noticed that when I insert the text inside the div tags, it appears without any tags. However, I actually need the XML tags to be present so that I can parse the cont ...

Resolving Unrecognized Vue Variable in PhpStorm

I'm encountering an issue with my Vue script in PhpStorm. Despite setting the variable redirect_to, I am getting an Unresolved variable syntax error during debugging. Please refer to the image below for further information. How can I resolve this prob ...

Attempting to abbreviate repetitive Javascript instructions

I have this functional javascript code, but I feel like there might be a more efficient way to write it. Any suggestions on simplifying this? let searchTerm = 'Some search text'; const isMatch = entry.title.toLowerCase().includes(searchTer ...

Issue with rendering initial item from API in Nativescript-vue TabView

Issue: Currently, I am developing a NativeScript-Vue application in which I am utilizing TabView Items to generate tabs. The tab title and content are populated from a JSON data file through a loop. Challenge: The first tabView item is not displaying an ...

Organizing JSON objects into groups of x items

I am working with dynamically generated JSON data that needs to be grouped by a specific number. I have a method for generating the variable representing the grouping number, but now I need to organize the items based on that number. Here is the original J ...

Having trouble targeting multiple dropdowns using jQuery

I am encountering an issue with two separate dropdowns where the hover states cannot be targeted by CSS based on the HTML markup. After attempting to solve this using jQuery, I have made some progress but one problem persists. Whenever I hover over the lin ...

Data connections in React Firebase provide essential links between multiple pieces of information

In my React application, there is an Administration area where the Admin user can add items to a large catalog stored in Firebase under "catalogue". When a regular user logs into their account, they can see the list of items added by the Admin and choose ...

Maintain the previous state in AngularJS using ui-router

My goal is to preserve the current state of the view, not just the URL and parameters. I am looking to save the entire view along with its scopes. Specifically, I am working on creating a search system similar to Facebook's, but with a unique twist. I ...

"Encountered the following error message: "Error [ERR_HTTP_HEADERS_SENT]: Unable to modify headers once they have been sent to the client" while attempting

My attempts to set a cookie when someone inputs the correct key (1234) are resulting in an error message: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client. I'm confused on what steps to take next. I've tried r ...

Why does jQuery function properly in jsfiddle, but not in an HTML file?

I have been troubleshooting repeatedly, but I am unable to figure out why the jQuery code is not functioning as expected. Strangely enough, it works perfectly in jsfiddle! Here is the HTML code: <!doctype html> <html> <head> <meta ch ...

How can I eliminate the black outline added to text by IE CSS filter?

Is there a way to add text-shadows to elements in IE without having a black outline around the text when it is not black? I know IE doesn't support text-shadow property but using filter: property gets pretty close. If anyone has any suggestions or so ...

When scrolling, the body content covers up the navbar

Whenever I try to scroll the page, the body contents start overlapping with the navbar. I have content over the navbar that should disappear when scrolling, and it is working correctly. The navbar should be fixed while scrolling, and this functionality ...

typescript warns that an object may be potentially null

interface IRoleAddProps { roles: Array<IRole> } interface IRoleAddState { current: IRole | null } class RoleAdd extends React.Component<IRoleAddProps, IRoleAddState> { state = { current: null, } renderNoneSelect = () ...

What is the best way to distinguish between an IPFS URL or path (content-addressed) and a regular URL (location-addressed)?

I need to retrieve nft-metadata for the nfts that have been added to my platform. Some of these Nfts have ipfs-urls as their tokenURIs and I am looking for a way to distinguish them from regular urls (location-addressed urls). Previously, I used is-ipfs, ...

Tips on integrating Angular's ui-grid with an array of strings

I have utilized Angular ui-grid to display an array of json objects received in the response. However, my current scenario involves getting an array of string values instead. How should I configure the grid for this situation? $http.get(url) .success(func ...