Tips for organizing three flexbox divs in a horizontal row

Within the content div, I have three separate divs that resize when the browser window is adjusted.

  • The blue and red divs should maintain a fixed width
  • The green div should resize to fill any available space on the left

I attempted to accomplish this using CSS with the following code:

.yellow{
    height: 100%;
    width: 100%;
}
.red{
    height: 100%;
    width:200px;
    display:inline-block;
    background-color: red;
}
.green{
    height: 100%;
    min-width:400px;
    display:inline-block;
    background-color:green;
}
.blue{
    height: 100%;
    width:400px;
    display:inline-block;
    background-color: blue;
}

Unfortunately, this code did not properly resize the green div, and in some browsers, the red panel was not visible.

I also experimented with float: left as well as:

display: -webkit-flex;
    display: flex;

However, these attempts were unsuccessful. Any suggestions on how to achieve this effect?

Answer №1

To achieve the desired layout, utilize the flex-grow property. Assign a value of 0 for the blue and red containers, while setting a larger value for the green container:

.red{
    height: 100%;
    width:200px;
    flex-grow: 0;
    display:inline-block;
    background-color: red;
}
.green{
    height: 100%;
    min-width:400px;
    flex-grow: 1000;
    display:inline-block;
    background-color:green;
}
.blue{
    height: 100%;
    width:400px;
    flex-grow: 0;
    display:inline-block;
    background-color: blue;
}

For a comprehensive guide on using flexbox, refer to this useful cheat sheet: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

Remember to include other essential properties such as display: flex;and justify-content: space-between. Detailed explanations can be found in the provided link.

It's worth noting that while flexbox offers a modern solution, you can achieve a similar layout using float for compatibility with older browsers. Simply add display: block; along with float: left for the blue div, and float: right; for the red one.

Answer №2

To achieve flexible layout behavior for an item, you can utilize the flex shorthand property:

.yellow { display: flex } /* Begin magic */
.red, .green, .blue { min-width: 0 } /* Refer to http://stackoverflow.com/q/26895349/ */
.red { flex: 0 200px } /* Initial width set to 200px, non-growing and shrinkable if needed */
.green { flex: 400px } /* Initial width set to 400px, expands to fill available space, shrinkable if needed */
.blue { flex: 0 400px } /* Initial width of 400, non-growing and shrinkable if necessary */

html, body {
  height: 100%;
  margin: 0;
}
.yellow {
  display: flex;
  height: 100%;
}
.red, .green, .blue {
  min-width: 0;
}
.red {
  flex: 0 200px;
  background-color: red;
}
.green {
  flex: 400px;
  background-color:green;
}
.blue {
  flex: 0 400px;
  background-color: blue;
}
<div class="yellow">
  <div class="blue"></div>
  <div class="green"></div>
  <div class="red"></div>
</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

Deactivate event handling for viewport widths below a specified threshold

Currently, I am attempting to deactivate a script when the width of the window falls below 700px. Despite reviewing suggestions from various sources, I have not been successful so far. window.onresize = function () { if(window.innerWidth < 700) { ...

Tips on how to properly format a DateTime String

I need help with formatting a DateTime string retrieved from an API where it is in the format of YYYY-MM-DDTHH:MM:SS +08:00 and I want to change it to DD-MM-YY HH:MM getDataFromApi(res) { this.timestamp = this.timestamp.items[0].timestamp; console ...

Ways to guarantee consistent font appearance across all browsers/platforms for PDF display (Cufon, Images, sIFR)

After countless hours delving into various @font-face issues for RTL languages, font-rendering disparities on different browsers, PDF compatibility, and more. My main goal is to achieve CSS-compatible Arabic text (an RTL language) in an HTML document that ...

Retrieve the selected date from the date picker widget

Welcome to my custom datepicker! Here is the HTML code: <div class="field-birthday field-return" id="birthday-edit" style="display:none;"> <div class="birthdaypicker"></div> <input class="hidden" name="birthday" type="hidden" ...

Gliding over the problem with the Azure line

https://i.sstatic.net/7N3hO.jpg Is there a way to hide the opacity and font on hover? I have tried using outline: 0; in my CSS but there is still a blue line lingering. Any ideas on how to remove it? If you need to see the code and the issue I'm des ...

CSS for two columns with a width of 50% each

Below is a table for reference: <table width="100%"> <tr> <td id="td1"></td> <td id="td2"></td> <td id="td3"></td> <td id="td4"></td> </tr> </table> Is there a wa ...

Instead of accessing the HTML page directly, you can view the source page by using the F12

I need to extract both data and IPs from VirusTotal.com using selenium and Beautiful Soup in Python. When I make a get request, I only receive the view-source HTML instead of the complete data-rich HTML shown in Inspect mode (F12). Does anyone have any su ...

What is the best way to design unconventional grids using Bootstrap 4?

Is it possible to achieve unique grid layouts in Bootstrap 4? For instance, An irregular grid where the first column covers two rows, followed by two rows with three columns each |||||||||||||||| | | | | | | |_ |_ |_ | | | | | | |_____| ...

Saving Information from an HTML Form Input?

I am currently working on a form that collects information about employees including their name, age, position, and details. When the "Add Record" button is pressed, this information should be added to a designated div. Although I have set up the Object C ...

How come I'm encountering issues when trying to click on the "register-selection" button in my Bootstrap and JS setup?

I am facing a challenge while developing my website. I want to trigger an alert when the "register-selection" is clicked, but despite trying both Jquery and vanilla Javascript, I have not been successful. Even after searching online resources and ChatGPT f ...

What is the process for retrieving tags and attributes from a website using nodejs?

In my project, I am looking to extract the metatag and specifically check for the presence of 'viewport' within the metadata using nodejs. Despite my efforts in searching, I have not been able to find a highly effective solution. Does anyone have ...

Tips for achieving printing with the "Fit sheet on one page" option in JavaScript

Is there a way to print a large table of data with 200 or 300 rows all on one page, similar to the Online MS Excel print option 'Fit Sheet on One Page'? I attempted the code below: var tble = document.createElement("table"); tble.id = "tble"; d ...

Transferring a unique Python object back and forth between Jinja2 and Flask

As a newcomer to Flask, Jinja2, and HTML templates, I am working on understanding how to transfer data between the controller and views. Currently, I have a calendar of events where each event is a hyperlink that uses url_for to navigate to a view with add ...

Customizing event colors in Full Calendar

My interactive calendar is created using : $('#calendar').fullCalendar({ height: 300, //............. events: jsonData, month: firstMonth }) I am looking to dynamically change the color of an event based on certain conditions ...

Duplicate the canvas onto a new canvas

One of the functions I am using involves copying an old canvas to a new canvas. Here is the code snippet for this functionality: function cloneCanvas(oldCanvas) { //create a new canvas var newCanvas = document.createElement('canvas&a ...

Learn how to showcase a predetermined option in an HTML select element using Vue.js and Minimalect

I am currently utilizing Vue.js along with the Mininmalect HTML select plugin to showcase a list of countries by their names and values, where the value represents the 2 digit country code. Successfully, I have managed to implement the plugin for selectin ...

"Position the input file with Bootstrap's input text for a seamless alignment

After experimenting with the design of my input file using this code snippet, I am facing difficulties aligning the button within the text input of a bootstrap element. Below is the code I have used to style my input field, but unfortunately, the bootstra ...

Incorporate a variable from an HTML form into PHP for FPDF functionality

How can I retrieve a variable from an "input" element in another HTML file? <input type="text" id="client" name="clientTest" required minlength="1" maxlength="30" size="30"> I need to pass this variable to my PHP file for FPDF. It seems like I hav ...

The background-repeat property in CSS appears to cease functioning when the container becomes too wide

Exploring various combinations of space and round values for background-repeat, I've discovered a way to utilize radial gradients to create a visually appealing dotted border on an element without having the dots cut off. .test { background-repeat: ...

AngularJS - How to Deactivate List Items

Currently, I am working on developing a breadcrumb navigation system using AngularJS. However, I am facing an issue where some of the links need to be disabled based on certain user requirements not being met. After researching the Angular documentation, ...