Design a dynamic div element for banners that adjusts to different screen

Is there a way to create a responsive div similar to the img-responsive class in Bootstrap, but for a div element that changes its height relative to its width when the window size is adjusted?

For example, if you look at the slider on this website "", you will notice that as you resize the window, the slider maintains the ratio of height to width.

Do I need to use JavaScript to achieve this effect, or can it be done with CSS or Bootstrap alone?

    <div class="responsive-banner">
        <!-- height/width === k -->
    </div>

Answer №1

If you want to achieve this without worrying about legacy IE, simply utilize @media queries.

To do so, set the minimum and maximum width / height of the viewport and then assign the appropriate height property based on specified break points.

For example:

@media only screen
and (min-width: 320px) /* Set your preferred minimum width */
and (max-width: 480px){ /* Set your preferred maximum width */
.responsive-banner{
     height:200px; /* Input desired height value */
}
}

You can incorporate various queries like the one above to tailor different break points or ranges accordingly.

Answer №2

To achieve this layout, you can utilize the Bootstrap Grid System.

For example:

<div class="row">
  <div class="col-md-3">
    This is text 1
    </div>
  <div class="col-md-3">
    This is text 2
    </div>
  <div class="col-md-3">
    This is text 3 
    </div>
  <div class="col-md-3">
    This is text 4
    </div>
  </div>

Include the bootstrap CSS (you can download and reference locally or use a CDN path).

Essentially, I have created a row using Bootstrap and divided it into four evenly sized columns. Whatever content you place in each column will automatically adjust to fit the column width by default.

By adding custom divs within these column classes, your layout will be responsive to changes in browser window size.

Answer №3

After doing some research and pondering, I have come up with a few ways to achieve this:

Utilizing CSS units :

We can utilize CSS units for relative sizing:
- em: Relative to the font-size of the element (2em equals 2 times the size of the current font)
- ex: Relative to the x-height of the current font (not commonly used)
- ch: Relative to the width of the "0" (zero)
- rem: Relative to the font-size of the root element
- vw : Relative to 1% of the width of the viewport*

We can use the vw unit to establish the height of the div element as shown below:

.responsive-banner {
    height: 30vw;
} 

Using jQuery :

Firstly, we assign a favorite ratio to a variable and then adjust the height of the element based on its width.

var ratio = 700/1920;
$(document).ready(function() {
  $('.responsive-banner').height(ratio * $('.responsive-banner').width());
});

$(window).resize(function() {
  $('.responsive-banner').height(ratio * $('.responsive-banner').width());
})

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

The left margin class in Bootstrap seems to be having trouble functioning properly within a React

Navbar <div className="collapse navbar-collapse" id="navbarSupportedContent"> <ul className="navbar-nav ml-auto"> <li className=&quo ...

Having trouble appending array elements to table rows using jQuery

I'm currently working on a project that involves creating a table using jQuery. I've encountered an issue when trying to populate the table rows with integer elements from an array. Unfortunately, I'm receiving the following error message ...

Utilize tag helpers to choose an option within a select element

I am facing a challenge in my ASP.NET Core application where I need to create a specific HTML structure in a Razor view based on a model instance. The HTML code I need to generate is a dropdown list with multiple options, each having custom attributes like ...

Experience the auditory bliss with Javascript/Jquery Sound Play

I am creating an Ajax-driven application specifically for our local intranet network. After each response from my Ajax requests, I need to trigger a sound in the client's browser. I plan to store a sound file (mp3/wav) on our web server (Tomcat) for ...

The concealed [hidden] attribute in Angular2 triggers the display of the element after a brief delay

I am currently utilizing the [hidden] attribute within my application to conceal certain elements based on a specific condition. The situation is such that I have two divs - one for displaying results and another for showing the text "No results". Both t ...

A collection of items that mysteriously affix themselves to the top of the page as

Unique Context In my webpage, I have a specific div element with the CSS property of overflow: auto. Within this scrolling div, there is structured content like this: <h3>Group A</h3> <ul> <li>Item 1</li> <li>I ...

What advantages does using ImageMagick to resize images offer compared to using CSS?

My Ruby on Rails application has a feature that allows users to upload images. I have discovered that Active Storage can utilize ImageMagick to resize images using this code: model.image.variant(resize: "100X100") However, resizing images can also be ach ...

Using Bootstrap to Position DIVs at the Top, Middle, and Bottom

I'm looking to create a layout with three DIVs inside a container DIV, all centered horizontally. The first DIV should be aligned at the top of the container, the second in the vertical center, and the third at the bottom. While I found some helpful t ...

Clicking on the images will display full page versions

Apologies for not making an attempt just yet, but I'm struggling to locate what I need. Here's the menu structure in question: <div class="overlay-navigation"> <nav role="navigation"> <ul class="test"> & ...

Is there a way to refresh a webpage without the need to reload it

Imagine a scenario where a tab on a website triggers the loading of a specific part of the webpage placed within a div. For example, clicking on a tab may trigger the loading of a file named "hive/index.php". Now, if a user selects an option from an auto ...

Error in RatingBar component: Vue.js 3 and Tailwind CSS not displaying dynamic width correctly

I have a component called RatingBar in Vue.js 3 with Tailwind CSS. My goal is to dynamically adjust the width of the parent element's class based on a value, but even though I see the desired width in DevTools, it always renders as 100%. Below is the ...

Guide to highlighting input field text using Angular

I've set up an angular page that includes an input field within its template. My goal is to highlight the text in the input field when a specific function is triggered. When I refer to "highlighting the text," I mean (check out the image below) https ...

Sliced Text Transformation

Recently, I inquired about a pesky text effect that was bothering me. Thanks to the assistance of fellow users, I discovered that the culprit behind the effect was the background color's edge. Eliminating this problem should be simple, but even with ...

Developing dynamic progress indicators in Django - A guide

I'm in the process of figuring out how to create a real-time progress bar for updating. The idea is that the server will update the user periodically on the current progress. Fortunately, I am familiar with making an Ajax call using Django and jQuery ...

Attempting to conceal image previews while incorporating pagination in Jquery

I'm working on implementing pagination at the bottom of a gallery page, allowing users to navigate between groups of thumbnail images. On this page, users can click on thumbnails on the left to view corresponding slideshows on the right. HTML <di ...

Press on any two table cells to select their content, highlight it, and save their values in variables

I have a table retrieved from a database that looks like this (the number of rows may vary): |Player 1|Player 2| ------------------- |Danny |Danny | |John |John | |Mary |Mary | My goal is to select one name from each Player column and sto ...

Step-by-step guide on transferring an HTML5 sqlite result set to a server using AJAX

Imagine I have a scenario where I receive a result set as shown below: db.transaction( function(transaction) { transaction.executeSql( 'SELECT col1, col2, col3 FROM table;', [],function(transaction, result){ //need to find a ...

What is the best way to utilize the features of component A within component B when they exist as separate entities

Component A has all the necessary functionalities, and I want to use it in Component B. The code for ComponentA.ts is extensive, but it's not written in a service. How can I utilize the logic from Component A without using a service, considering both ...

Retrieve the character count of the text within a dynamically created div using AJAX

I have a .php file containing an array of strings. One of these strings is fetched by my page and inserted into an empty div named post. However, when I try to determine the length of the string in the post, I always receive the error: Uncaught TypeErro ...

How can one transform a web-based application into a seamless full-screen desktop experience on a Mac?

"Which software can be utilized to enable a web application to display an icon on the desktop of a Mac computer, while also opening up the web application in a fully immersive full-screen mode that supports all the touch and gesture functionalities provi ...