Automatically adapt the height of elements to ensure they are consistently aligned on a single page

I'm working on adjusting the height of elements on my page

https://i.sstatic.net/tqRs9.png

My goal is to dynamically change the height of A and B based on screen height so they always fit within a single screen without requiring scrolling.

Currently, I am using media queries to customize the height of my elements.

@media screen and (min-width: 1920px) and (max-width: 5000px) {
  .A{
    height: 600px;
  }
  .B{
    margin-right: 400px;
  }
}

@media screen and (min-width: 1279px) and (max-width: 1919px) {
  .A{
    height: 550px;
  }
  .B{
    height: 350px;
}

However, this method feels cumbersome as I'd like the heights of A and B to adapt automatically based on current screen size without having to create numerous media queries for each possible dimension.

Is there a more efficient solution for achieving this?

Answer №1

To tackle this issue, we can utilize Flexbox with a vertical direction.

  • Given that the original code used 600px/400px, I assigned 6 and 4 as the flex-grow values for classes .a. and .b. correspondingly.
  • These flex-grow values are proportional, so whether it's written as .6, .4, or 600, 400, the outcome would be the same.
  • The elements header and footer will take up the remaining space in the container after .a and b have claimed their portion.

html, body {
  margin: 0;
}

.container {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.container > * {
  padding: .5em;
}

.a {
  flex-grow: 6;
  background-color: #eee;
}

.b {
  flex-grow: 4;
  background-color: #ff0;
}
<div class="container">
  <header>header</header>
  <div class="a">A</div>
  <div class="b">B</div>
  <footer>footer</footer>
</div>

https://jsfiddle.net/j3kxr50L/2/

Answer №2

  <!-- language: lang-css -->
    give this a shot

    * {
       margin:0;
       padding:0;
       box-sizing:border-box;
     }

     .container {
          height: 100vh; // adjust (.container) height based on the view screen size
      }

     .a{
         height: calc(60vh - 40px);//subtract the header height
       }

      .b{
         height: calc(40vh - 60px); //subtract the footer height
        }



<!-- language: lang-html -->

    <div class="container">
      <header>header</header>
      <div class="a">A</div>
      <div class="b">B</div>
      <footer>footer</footer>
    </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

"Is there a way to target an element within another element that has inline styles using

What is the best CSS strategy for targeting elements with inline styles? How can I focus on the first span but exclude the second using only CSS? If that's not achievable, is it possible to accomplish this using jQuery? http://jsfiddle.net/TYCNE/ & ...

Using Jquery to toggle classes between "display block" and "display none

Hello friends, I'm in need of some help with my code. $(document).ready(function() { var circle = $('.circle'); $(".send a").click(function(e) { e.preventDefault(); $('.wrap').css('display', 'bl ...

Creating a radiant button with HTML and CSS that gradually glows

I've been experimenting with different codes in an attempt to create a glowing button for my website. Here are some of the code snippets I have tried: .button { background-color: #1c87c9; -webkit-border-radius: 60px; border-r ...

Could images be positioned in this manner without using the float property?

Looking to arrange images in a left-to-right flow while keeping them centered on the screen: https://i.stack.imgur.com/WHBv9.png However, I'm struggling to achieve this as the last image always ends up centered horizontally as shown below: https://i ...

Having trouble getting the jQuery toggle function to work properly with div elements

I'm struggling with some divs that are not behaving as expected. When I click the button, it opens and changes the text to "Collapse", but when I click it again, it doesn't change the text back to "Find Support". Additionally, if I click on a dif ...

The hamburger icon menu is malfunctioning

Having some trouble with adding a hamburger button using Bootstrap classes. The button is not showing up at all. In fact, none of the Glyphicons seem to be working for me. Did I miss something? Maybe forgot to import something? I would really appreciate ...

What could be causing the background image on my Jumbotron to fail to load?

Having some trouble with setting my image as the background of the Jumbotron. I've tried a few different approaches, but none seem to be working. It's not an issue with static loading because using img src directly into the Jumbotron section does ...

When you hover over the image, a thin white line materializes along the bottom edge

Can anyone help me troubleshoot an issue I am facing with a white pixel line that appears at the bottom of every image within the "div class gallery" element? The white line seems to only show up when the mouse hovers over the images, especially since I ha ...

Creating separation between a dropdown menu and its corresponding button

I'm dealing with a situation where I have a button that reveals a hidden droplist upon hover. The problem is that there is no space between the button and the droplist, causing interference with the functionality. My attempt to create some distance b ...

What is the best way to prioritize .py-md-6 over .p-3 in Bootstrap 4?

I have set the padding for the class .py-md-6 as follows: .py-md-6 { padding-top: 6rem !important; padding-bottom: 6rem !important; } Despite this, there seems to be a conflict with the .p-3 class in my HAML element: #id.p-3.py-md-6 It appears th ...

Tips for including numerous hyperlinks in a single row for a website's navigation menu

I need assistance in creating a navigation bar for my website that includes multiple headers with links. I am not sure if the issue lies within my CSS, HTML, or both. Can someone please help me troubleshoot? index.html <!DOCTYPE html> <html> ...

LFT Etica font showcases digits with varying heights

In the project I am working on, we are required to use the font LFT Etica. However, there is an issue with the height of certain digits when displayed. Specifically, some numbers appear taller than others. Is there a way to make all digits have equal heigh ...

The rendered typeface may vary from the specified font family

I've encountered an issue with a font on my website. I'm using DIN Regular True Type Font and declaring it as follows: @font-face { font-family: 'DINRegular'; src: url('../fonts/DINBek-Regular.ttf') format('truetype ...

Using CSS to style the last element in a set of dynamic content

I am currently working on targeting the last element in a dynamically generated content set. For instance, here is an example of how the generated content appears: <div class="block"> <div class="block-inner"> <div class="box"> ...

Creating a personalized version of Material UI with unique styles, and uploading it to my GitHub repository for universal reuse across all my projects

Seeking input on the best solution for customizing MUI themes in my React projects. Currently, I manually copy theme files from previous projects to maintain consistency in styling between MUI and my UI design. Looking to create a custom library based on M ...

"Adjusting the height of a div element while considering the

I have 2 elements with different heights and I want to make them equal: var highestEl = $('#secondElement').height(); $('.element').first().height(highestEl); I understand that the second element is always taller than the first one. W ...

Parallax scrolling and Swiper slider, a match made in digital design

Currently facing an issue with Swiper Slider and Simple Parallax Scrolling. Whenever I switch slides, the same image is displayed repeatedly. I suspect the problem lies in the fixed position of the image within the parallax effect. Attempted to resolve b ...

Having issues with animation performance in IE11

I've encountered an issue with an animation that is functioning correctly on all browsers except for IE11. $(document).ready(function() { $('.activate').click(function() { $('.eight-box').toggleClass('animate') ...

Troubleshooting: Unable to fetch CSS file in Node.js Express framework

I'm trying to access my .html file with localhost using node.js, but I'm facing an issue with loading the CSS that is included in the file. I am using the express framework for this purpose. Code: var express = require('express'); var ...

Tips for keeping the footer at the bottom center of the page with Bootstrap?

I'm in the early stages of learning how to build a webpage using HTML & CSS, and I've been utilizing the Bootstrap 4.5 framework. One challenge I'm facing is trying to keep my footer with social media icons centered at the bottom of the page ...