Styling divs to be proportionally larger or smaller within a layout

Is there a way to design a layout that automatically scales without relying on the height property? Currently, I'm using a float layout between two divs, as depicted in the image below. The issue arises when the content within these boxes varies in size, causing them to appear uneven. How can I ensure all boxes have equal heights without explicitly setting them?

Answer №1

Check out this informative article on css-tricks.

A great resource I recommend is Paul Irish's blog post on HTML5Rocks. I have also created a JSFiddle based on his code:

CSS

.box {
  /* basic styling */
  width: 100%;
  height: 95px;
  border: 1px solid #555;
  font: 14px Arial;
  /* flexbox setup */
  display: -webkit-box;
  -webkit-box-orient: horizontal;
  display: -moz-box;
  -moz-box-orient: horizontal;
  display: box;
  box-orient: horizontal;
}

.box > div {
  -webkit-box-flex: 1;
  -moz-box-flex: 1;
  box-flex: 1;
}

/* color coding */
.box > div:nth-child(1){ background : #FCC; }
.box > div:nth-child(2){ background : #CFC; }
.box > div:nth-child(3){ background : #CCF; }

HTML

<div class="box">
    <div>A</div>
    <div>B</div>
    <div>C</div>
</div>

Keep in mind that this method may not be compatible with older browsers, so consider using a table layout if needed.

Answer №2

Check out my JsFiddle example.

My approach involves utilizing the CSS properties position:absolute, top, and bottom to make the inner div take up the full height.

Here is the HTML structure:

<div id="top"></div>
<div id="middle"></div>
<div id="bottom"></div>

And here is the corresponding CSS:

body{
    margin: 0px;
    padding: 0px;
    border: 0px;
}

#top{
    width: 100%;
    height: 30px;
    background: blue;
    position: absolute;
}

#bottom{
    width: 100%;
    height: 30px;
    background: yellow;
    position: absolute;
    bottom: 0px;
}

#middle{
    width: 30%;
    position: absolute;
    top: 30px;
    bottom: 30px;
    background: gray;
}

Answer №3

Put the center one in a container and create three inner containers with 100% height

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

"Keep the div locked to the screen when the bottom of the div aligns with the

Hey everyone! I'm currently grappling with a project that requires me to keep a div stuck to the screen (prevent scrolling) when its bottom reaches the bottom of the screen. I have two divs on the page, both with varying heights. My aim is to keep div ...

When a button on a form is disabled using form.submit, it will not be included in the data that is

I have implemented a code on a page that locates every submit button, finds the form it belongs to, and adds a listener. This listener disables the button for a few seconds in order to prevent accidental duplicate submissions of the form. <input type=" ...

Changing the color of MUI Typography when selected

Struggling to modify the styling of the Typography component nested inside a MenuItem. Unable to apply styles to ListItem as intended Check out my code here: https://codesandbox.io/s/dztbc?file=/demo.tsx:1481-1804 Desired behavior: Change styling on sele ...

Managing a fixed footer in an Android webview to prevent it from obstructing an input field when the soft keyboard appears

I am working with a webview that contains a React app, not React Native. The webview features a login page with two input fields and a fixed footer. However, when one of the input fields is focused, the footer moves up and covers part of the input field, ...

Automatic popup updates when submitting a form in a Chrome extension

Looking to develop a chrome extension popup window that, when clicked, will present a form for users to input their name and college. The goal is to save this data in chrome storage and then replace the popup with a new window displaying a greeting message ...

JavaScript functions do not work within the HTML code

I am currently working on my debut website and I'm facing an issue with the JavaScript file not functioning properly. Is there a chance you can help me troubleshoot this? I'm still in the learning stages of programming. This is the HTML content ...

What is the method for incorporating an upward arrow into a select element, while also including a downward arrow, in order to navigate through options exclusively with

I have a select element that I have styled with up and down arrows. However, I am seeking assistance to navigate the options dropdown solely using these arrows and hide the dropdown list. Here is the HTML: <div class="select_wrap"> <d ...

Issue with jQuery's .show method not functioning properly following the .hide method

Is there a way to make it so that only the "pastInvestments" are displayed when the "pastButton" is clicked, and only the "currentInvestments" are displayed when the "currentButton" is clicked? In the HTML code below, there are 2 buttons and 2 divs: < ...

Struggling to retrieve Codemirror textarea values across multiple elements with JavaScript/jQuery

I've been struggling to retrieve the values from my textarea codemirror in order to send them as JSON to the server for saving after editing. Unfortunately, I am unable to select the ELEMENT...I even attempted to grab the element by its id...with no s ...

The issue with the float-left property arises when used within a relative positioning

Why can't I move my Image to the left side of the div? No matter what I try, it stays stuck in the center. I initially used flexbox to achieve this, but had to switch to relative positioning due to a conflict with the Nextjs Image component. The issu ...

Is there a way to retrieve JSON data from a different domain and incorporate it into my own website?

I am attempting to utilize JSON data from "" and display the data on my website. My goal is to extract the JSON field name "Stats" and retrieve the sub-field name '\"v\"'. You can refer to the documentation provided by purpleair here: h ...

Validation for optional fields in asp.net mvc 5

In my asp.net mvc 5 application, I have a form with some fields that are optional and not required to be filled. Despite trying different approaches, I keep getting a warning message stating that these fields are required. [Required(AllowEmptyStrings = t ...

Trigger a notification based on the selected choice

Here is some sample HTML code: <div id="hiddenDiv" style="display: none;"> <h1 id="welcomehowareyou">How are you feeling today?</h1> <select name="mood" id="mood"> <option value="" disabled selected>How are you feeling?</o ...

Creating a structured HTML/PHP page for permanent redirection to another webpage

I'm trying to restructure a part of my website, but I'm struggling to figure out how to do it. This is in the context of a logged-in user: On page 1, the user clicks a button to navigate to page 2 On page 2, the user performs an OPERATION to go ...

The paragraph text should dynamically update upon clicking a button based on JavaScript code, however, the text remains unchanged despite attempts to modify it

I recently started learning JavaScript and wanted to update the content of a paragraph when a button is clicked. However, I encountered an issue where this functionality doesn't seem to work. <body> <p id="paragraph">Change Text on cl ...

I'm currently developing a Javascript quiz, but unfortunately, I am not seeing any outcomes. Can someone identify the issues in my code and provide guidance on what steps I should take next?

There are 3 requirements I must meet: The script should show the number of correct answers and the percentage of correctness at the end of the quiz. If all questions are answered correctly, display 'Well Done', or else display 'Try again&ap ...

Preserve the original color of links in CSS by using the `a

Is it feasible to specify in CSS that a link should not change its color and instead use the default one? For example: If I have text displayed in red, and that text is also a hyperlink. Typically, when clicked on, the text would change to blue because i ...

Generating a new DOM element for each AJAX response received

I am working on a project to build an application that fetches data about restaurants and places of interest from the Yelp API and then generates a materialize "card" for each response. Below is the code I have so far. function callback(results, status) ...

Placing an iframe at the end of a container

I am attempting to use jQuery to insert an iframe into a div. The current code accomplishes this, but it also erases all existing content within the div#off. How can I make the iframe appear at the bottom of the content in the div#off without removing any ...

Resolving Div Alignment Issues in Internet Explorer with jQuery CSS AddClass and RemoveClass

I've set up an HTML table with a hover highlight feature using jQuery and CSS. Here's how my hover event is structured: $('#' + element.id + ' tr:not(:first,[class="summary_row"])').die('mouseover mouseout').live(&a ...