expandable div or cell panels

Greetings,

I'm facing a challenge with my code:

<div id="left"></div>
<div id="center"></div>
<div id="right"></div>

My goal is to ensure that #center always displays at a minimum size, while the other two divs will collapse or even disappear if the window is resized or viewed on a smaller resolution.

Here's the CSS I currently have:

div {display:table-cell;}
#left, #right {width:auto;}
#center {width:1460px;}

This setup works well on a 1080p screen, but not on a smaller one like 1024 width where a scroll bar appears. In such cases, I want #center to cover the entire width and the other two divs to collapse completely to accommodate. How can I achieve this using only HTML and CSS?

Appreciate your assistance.

Answer №1

It would be more effective to demonstrate this concept by showcasing a narrower center column. To achieve this, you can easily adjust the CSS to operate at a width of 1460px.

/* 
  1. Ensure the center column maintains a fixed size
  2. If there is horizontal space exceeding 460px,
     evenly distribute it between the left and right divisions
*/

.container {
  display: flex;
}

.container > div {
  height: 40px;
}

#left, #right {
  flex: auto;
  background: rgba(255, 0, 0, 0.5);
}

#center {
  flex-basis: 460px;
  background: black;  
}
<div class="container">
  <div id="left"></div>
  <div id="center"></div>
  <div id="right"></div>  
</div>

Answer №2

To implement a flexible layout using CSS, you can utilize the power of flexbox like shown below:

#container {
  display: flex;
}

#left, #right {
  background-color: gray;
  flex-grow: 1;
  min-width: 0;
  overflow: hidden;
}

#center {
  background-color: yellow;
  min-width: 500px;
}
<div id="container">
  <div id="left">Left</div>
  <div id="center">Center</div>
  <div id="right">Right</div>
</div>
  • flex-grow:1 will allow the left and right divs to expand and occupy any available space.
  • min-width:0 ensures that the left and right divs can completely disappear if needed, even with content inside.
  • overflow:hidden hides any overflowing content within the left and right divs as they shrink in size.
  • min-width:500px sets a minimum width of 500px for the center div, preventing it from shrinking below this value.
  • The center div cannot exceed 500px either, as the left and right divs will grow to fill any excess space.

If you also want to limit the maximum width of the left and right divs while allowing the center div to expand, include the following:

  • Add flex-grow:1 to #center to enable its growth.
  • Set max-width:50px on #left, #right to cap their widths.

For a live demonstration, check out this working example.

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

Accordion A and B both require the first category of accordion A to be opened first in order for the first category of accordion B to also be

Greetings, this is my initial inquiry, please excuse any errors as English is not my first language. For my project, I am utilizing Angular 6, jQuery, and BS. I have two accordions, each with different categories: A1 and A2. My goal is for clicking on A ...

The "label for" functionality does not activate the control

<form> <label for="male">Male</label> <input type="radio" name="sex" id="male" /> <br /> <label for="female">Female</label> <input type="radio" name="sex" id="female" /> </form> I am cu ...

Complete the form by following a hyperlink located on a different webpage

I recently developed three PHP pages. The first one consists of the following form: <form method="POST" name="go" action="search_form_all.php" > <input name="value" type="text" id="search_form_1" size="65" placeholder="enter name"/> <i ...

Exploring the attributes of div elements with XPath

Currently, I am in the process of familiarizing myself with xpath through the creation of a basic program that will display the list of Premier League fixtures in football from a specific webpage. The page in question can be found at the following link: A ...

100% width is applied to the table cell within the div element

Below is the code I am working with: <div style='display: table'> <div style='height:200px; width:100%; text-align: center; display: table-cell; vertical-align: middle'>No result found</div> </div> I'm ...

What is the strategy used by jQuery to select elements by class name?

Is there a "getElementsByClass" function in JavaScript? If not, how does jQuery manage to do it without looping through all elements which would be very inefficient? Also, how can I specify CSS for elements with two or more classes? <a class="one two ...

Jquery form extension

I've implemented the Jquery form plugin for submitting login form data and it's working well. However, I am facing an issue with redirecting a successfully logged-in user to the client panel from the login page in my PHP script. Whenever a user s ...

Attempting to eliminate an HTML element using JavaScript

Currently, I am working on creating an image rotator in JavaScript. The CSS fade animation I have applied only seems to work during element creation, so I am forced to remove the element on each loop iteration. The main issue I am encountering is related ...

Position two div elements and an hr tag to create a step progress bar

Check out my progress bar design https://i.sstatic.net/u9SRM.png How can I center the 'created' and 'assigned' divs below the circle while ensuring the 'hr' line touches the circle, and it is responsive across different scre ...

Selecting an option from a dropdown menu and entering data into a text box

I am currently facing an issue with inserting a value using a text box. The process starts with the admin selecting a category, such as a cellphone brand, from a drop down list. Once the brand is chosen, they will input the cellphone model using a text bo ...

Iterating through the Bootstrap grid

https://i.sstatic.net/XMIzT.jpg I'm attempting to create a layout similar to the image provided. Currently, I am utilizing WordPress and WooCommerce and aiming to showcase products in this manner. The following HTML code is what I have been working ...

The alignment of SVG is not in sync with the aspect ratio

I've been struggling with this issue for some time now, trying to align the SVGs correctly for different width and height values. My viewBox has an aspect ratio of 16:9, and I want to scale the SVG accordingly. It's working fine for certain combi ...

The submit button remains unresponsive, yet upon removing its JavaScript code, it functions smoothly

This is the content of my index.html file. It contains JavaScript code. However, there seems to be a problem with the JavaScript validation as the Submit button does not perform any action when clicked. Surprisingly, when I remove the JavaScript code, the ...

Design a custom icon for uploading multiple files

Looking to create a bootstrap-4 icon for multi-file upload. I've got the code reference for single file upload, but now I need to know how to create an icon for multi file upload. <!DOCTYPE html> <html> <head> & ...

Creating a CSS rollover effect for your logo on a WordPress website

Struggling to implement a CSS image rollover on a Wordpress header logo. The solution may be simple for you experts, but I can't find it anywhere on Stackoverflow. Could it have something to do with the position? Adding :relative or :absolute doesn&ap ...

Looking for a JavaScript or jQuery library that can automatically handle input direction?

Seeking a jQuery library for bidirectionality handling. Google offers one in the closure library, but it seems excessive to include the entire library just for bidirectional input support (unless you suggest otherwise) The Google closure library contains ...

Can you please explain the purpose of the white space in the HTML code?

Currently, I am working on developing a mobile application, and as I start adding styles, I notice a mysterious blank space that seems to be appearing out of nowhere. Can anyone provide any insights or suggestions to help me troubleshoot this issue? https ...

Initiating a file download using HTML

When I try to initiate a download by specifying the filename, instead of downloading, it opens in a new tab. The code snippet below shows what I am currently using. Additionally, I am working on Chrome Browser. <!DOCTYPE html> <html> < ...

Assertion error: 1 does not match the expected value of 6

I am faced with a challenging test that requires me to write code that will pass successfully. However, no matter what I try, all I get is the following error messages: "Failed asserting that 1 matches expected 6" when I return 6 and "Too few arguments t ...

Transforming CSS shorthand background properties into longhand representation

I have been working on a function to convert shorthand CSS background declarations into longhand. The function I created is functional, but it does not handle cases where the background-color property includes color values like black or yellow. It also doe ...