Arranging expandable divs horizontally

Looking for a way to align these blocks so they can expand while remaining in line, I'm struggling with maintaining their individual spaces. The layout I have in mind is as follows:

In this setup, box 2 and 3 should automatically adjust to fill the available space on any screen resolution.

JSFiddle and JSFiddle 2

CSS / HTML:

.container {
  width: 75%;
  min-width: 1005px;
  max-width: 1428px;
  height: 330px;
  margin: 0 auto;
  background-color: gray;
}
.box1 {
  float: left;
  width: 455px;
  height: 250px;
  background-color: rgba(0, 0, 0, 0.75);
  margin-right: 5px;
  margin-bottom: 5px;
}
.box2 {
  float: left;
  width: 75%;
  min-width: 340px;
  height: 250px;
  background-color: rgba(100, 50, 50, 0.75);
  margin-right: 5px;
  margin-bottom: 5px;
}
.box3 {
  float: left;
  width: 25%;
  min-width: 190px;
  height: 250px;
  background-color: rgba(50, 50, 100, 0.75);
  margin-right: 5px;
  margin-bottom: 5px;
}
.box4 {
  display: block;
  width: 100%;
  height: 60px;
  background-color: rgba(50, 100, 50, 0.75);
}
<div class="container">
  <div class="box1">Test</div>
  <div class="box2">Test</div>
  <div class="box3">Test</div>
  <div class="box4">Test</div>
</div>

Answer №1

Discover Three Innovative Techniques

"Click to reveal code snippet" and execute to view the full demonstration.

#1 - Enhance Layout with display: inline-block and calc

Compatibility: Explore this technique on IE 9 + and modern browsers. Workarounds are available for IE8+ compatibility.

  • Avoid percentage calculation discrepancies by utilizing width: calc(50% - 60px) to eliminate margins and fixed columns.

  • Enable dynamic resizing of divs with min-height: 100%, thanks to html,body { height: 100%; }.

  • Eradicate inline gaps by strategically placing closing div tags adjacent to opening tags. For more insights, visit this link.

Sample Implementation

Note: Replace child selectors with class selectors as desired.

html,
body {
  height: 100%;
  margin: 0;
}
div {
  background: #f50057;
  min-height: calc(50% - 5px);
  width: calc(50% - 60px);
  display: inline-block;
  vertical-align: top;
  margin-right: 10px;
}
/*Fix first div*/

div:first-child {
  width: 100px;
}
/*Remove third divs right margin*/

div:nth-child(3) {
  margin: 0;
}
/*Top margin for last div*/

div:last-of-type {
  width: 100%;
  display: block;
  margin: 10px 0 0;
}
<div></div><div></div><div></div><div></div>

#2 - Embrace Unprecedented Style with display: table / display: table-cell

Compatibility: Test this method on IE 8 + and contemporary browsers

  • Nest the top three divs in a container with display: table

  • Assign display: table-cell to the top three divs

  • Specify a fixed width for the left div

  • Use table-layout: fixed on the wrapper for even distribution of "cells"

  • Adopt border property spacing between top divs, adjusting percentages with * { box-sizing: border-box }

  • The bottom div outside the wrapper is set to display: block with a top border creating pseudo-margins

Illustrative Example

* {
  box-sizing: border-box;
}
html,
body {
  height: 100%;
  margin: 0;
  background: #000;
}
.table {
  display: table;
  height: 50%;
  width: 100%;
  table-layout: fixed;
  border-collapse: collapse;
}
.table > div {
  background: #f50057;
  display: table-cell;
  border-left: solid 10px #FFF;
}
.table > div:first-child {
  border-left: none;
  width: 100px;
}
.footer {
  width: 100%;
  display: block;
  background: #f50057;
  height: 50%;
  border-top: solid 10px #FFF;
}
<div class="table">
  <div></div>
  <div></div>
  <div></div>
</div>
<div class="footer"></div>

#3 - Step Into Tomorrow's World with display: flex

Compatibility: Discover the potentials of IE 11, modern browsers, and Safari (with -webkit- prefix)

This final technique stands out as a personal favorite due to its creation in just under 3 minutes!

  • Enclose the top three divs within a flex container employing display: flex

  • Define a fixed pixel width for the initial div along with flex: 0 0 auto to maintain its size

  • Allow flexibility for the other two divs using flex: 1 for adaptable growth and shrinking, independent of the fixed column

  • Position the last div outside the flex container for autonomous behavior

  • Utilize viewport width (vw) and viewport height (vh) units to determine heights and widths of flexible divs. Learn more about these length units here.

For an in-depth guide on flexbox properties, refer to this helpful resource.

Demonstrative Instance

* {
  box-sizing: border-box;
}
body {
  margin: 0;
}
.flex {
  display: flex;
  height: 50vh;
  width: 100vw;
}
.flex > div {
  background: #f50057;
  flex: 1;
  margin-left: 10px;
}
.flex > div:first-child {
  width: 100px;
  flex: 0 0 auto;
  margin: 0;
}
.footer {
  width: 100%;
  display: block;
  background: #f50057;
  height: calc(50vh - 10px);
  margin-top: 10px;
}
<div class="flex">
  <div></div>
  <div></div>
  <div></div>
</div>
<div class="footer"></div>

Answer №2

While it may not be flawless, this code appears to achieve the desired outcome using CSS tables.

<div class="table">

  <div class="trow">
    <div class="tcell">box1</div>
    <div class="tcell">box2</div>
    <div class="tcell">box3</div>
  </div>

</div>   

<div class="table">
    <div class="tcell last">box4</div>
</div> 

.table{display:table; width:100%; text-align:center;}
.tcell{display:table-cell; background:#000; color:#fff; min-height:100px; padding:20px; border:1px solid #fff; }
.trow{display:table-row;  }
.last{ background:red; }
.trow .tcell:first-child{ width:300px; }

http://jsfiddle.net/fjsvnrLp/5/

It turns out that the row element is not necessary in this setup.

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

ng-class in Angular seems to be functioning properly on <td> elements, but is not

When using this HTML inside an ng-repeat, there is a difference in behavior: This part works fine: <tr> <td ng-class="info.lastInMonth">{{ info.date_formatted }}</td> ... But this section does not work as expected: <tr ng ...

What is the best way to add a gradient background image to a container that also has margins?

I am currently working on a push menu that shifts the main content container by adding margin-left to it. I am aiming to achieve a background image effect similar to what is seen on . Here is the progress I have made so far in my codepen: https://codepen ...

Combining Tailwind with Color Schemes for Stylish Text and Text Shadow Effects

tl;dr I have a utility class in my tailwind.config.ts for customizing text shadows' dimensions and colors. However, when using Tailwind Merge, I face conflicts between text-shadow-{size/color} and text-{color}. The Issue In CSS, text shadows are oft ...

Retrieve the Data from Input Fields with Matching Classes and Transmit to a Script Using AJAX Request

I am working on a form that includes multiple input fields: <input type="text" class="date" name="date[]" onkeyup="showHint()" /> <input type="text" class="date" name="date[]" onkeyup="showHint()" /> <input type="text" class="date" name="da ...

Tips for adjusting the header color in materialize framework?

I've been working on a web template and I'm looking to customize the color displayed in the Android browser (maybe using JS?). The default color is blue. How can I go about changing it? https://i.sstatic.net/RxLbS.jpg Appreciate any help! ...

The toggle for hiding and showing, along with changing the button color, is not functioning properly due to

I'm encountering a puzzling issue with a toggle that hides and displays information and changes color on click. The functionality works flawlessly on the page where I initially wrote the code. For instance, the button's background shifts colors w ...

Retrieve data from a MySQL table based on a specific condition in PHP

Can someone assist me with fetching data from a table where the valid_from date is less than a specified date (excluding the current date)? I have a table that looks like this: view my table here For instance, if my date is 02-04-2015, I would want to re ...

AngularJS making a HttpPost request resulting in a 500-Internal Server Error

I'm currently working on an application where I need to include a user in the database which requires a POST operation. However, every time I try to run the application, I encounter a 500-Internal Server Error for the POST API call. Here is a snippe ...

Selecting Child Elements in CSS

Suppose I have the below HTML structure: <div id="div1"> .... <span class="innercontents">...</span> .... </div> Is it possible to target only the child elements of a specific parent ID? For example, could I use this CSS rule? # ...

What could be causing my transform scale to not function properly in Bootstrap?

As a complete beginner, I am currently in the process of learning bootstrap. I have noticed that the animation breaks when I apply it, specifically the scaling part. Interestingly, the animation works perfectly fine without Bootstrap. I am wondering if the ...

What steps can I take to stop a select box within a flex container from shrinking?

Currently working on some code that involves a flex container. Struggling to prevent fields from shrinking too much on smaller screen sizes. var crsdesc; //var for new window function popup(mylink) { if (!window.focus) return true; var href; if (t ...

Passing the initial value from a PHP array to Ajax using an HTML element

I am currently dealing with an HTML form that fetches values from an array list. The form is submitted using Ajax along with a PHP script. However, I am encountering an issue where clicking on different array items only submits the first value in the array ...

"Developing CSS styles that work seamlessly across all

What methods do professional web designers use to develop cross-browser compatible CSS? Is it typically a manual process, or are there specific toolkits available to simplify the task similar to YUI for JavaScript? I am looking to avoid WYSIWYG editors s ...

Delete the "img" and "a" elements from the content within the specified node

Is it possible to only extract text from my HTML content? var sb = new StringBuilder(); doc.LoadHtml(inputHTml); foreach (var node in Doc.DocumentNode.ChildNodes) { if (node.Name == "strong" || node.Name == "#text" || node.Name == "br" || no ...

Can you provide me with a comprehensive list of all the colors available in Twitter Bootstrap 3?

I've integrated the twitter bootstrap sass 3.0.3.0 gem, which is supposed to be the latest version of twitter bootstrap 3.0.3. I'm trying to customize the colors using the 'customize' section of the LESS variables, but I can't seem ...

Having difficulty modifying the width of the BODY element using CSS

For my webpage, I want the body to have a silver background that fills only 75% of the page. This means that only 75% of the page will be painted silver, while the remaining part will be left unused and painted according to the browser's defaults. The ...

Preventing default behavior in a JQuery post request

Encountering an issue with jQuery functionality on a mobile device. function send() { $.post("scripts/post.php", { username: $("input[name=username]").val(), password: $("input[name=password]").val() }, function(data) { if ($(".data div" ...

Using :before and :after in CSS, three dots can be created in a horizontal line

I am currently trying to display three horizontal dots on my webpage. I have created a demonstration on jsfiddle. span { position: relative; background-color: blue; border-radius: 5px; font-size: 0; margin-left: 20px; padding: 5px; ...

Using Bootstrap: adjusting the height of input-group and select elements

How come the select element within an input-group doesn't render similarly to the input element? I would like the height of the select element to be relative to the input-group. <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css ...

Collaborative Vue component: Clients need to manually import the CSS

I recently created a Vue component using TypeScript that resulted in a separate CSS file being generated after the build process. However, I noticed that when the client imports this component, the CSS file is not imported automatically and needs to be exp ...