What is the best way to evenly distribute dynamic divs in a vertical layout?

Essentially, my goal is to have the child divs evenly spread out within the parent div. If one of the child divs is removed, I want the remaining divs to resize and fill the space equally again.

For example:

 ---parent---------------
|  --------------------  |
| |              child | |
|  --------------------  |
|  --------------------  |
| |              child | |
|  --------------------  |
|  --------------------  |
| |              child | |
|  --------------------  |
 ------------------------   

If a child div is removed, the layout should adjust as follows:

 ---parent---------------
|  --------------------  |
| |              child | |
| |                    | |
| |                    | |
|  --------------------  |
|  --------------------  |
| |              child | |
| |                    | |
| |                    | |
|  --------------------  |
 ------------------------   

Currently, I am trying to achieve this using only pure HTML/CSS, but I haven't found a solution yet.

Other options I'm considering include:

  • Using JavaScript to calculate heights dynamically
  • Creating different classes for various scenarios (e.g., parent with 2, 3, or 4 child divs) and loading them accordingly with PHP

Does anyone have a better idea or alternative approach?

Answer №1

Utilize css tables with table-layout: fixed - no need for javascript.

CHECK OUT THE FIDDLE HERE

To witness this in action, simply click on 'inspect element' on one of the li elements in the provided fiddle, right-click and select 'delete node' - you'll see exactly what you're looking for.

Markup

<ul>
    <li><span>Item1</span></li>
    <li><span>Item1</span></li>
    <li><span>Item1</span></li>
    <li><span>Item1</span></li>
</ul>

CSS

ul
{
    display: table;
    table-layout: fixed;
    width: 100%;
    border: 1px solid #c2c2c2;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}

li
{
    text-align: center;
    display: table-row;
    background: pink;
}
span
{
    display: table-cell;
    vertical-align: middle;
    border-top: 1px solid green;
}

Answer №2

Using CSS3 for styling? Consider implementing the flexbox layout for a modern and efficient solution!

.container{
  height:200px;
  width:100%;
  display:flex;
  flex-direction:column;
}
.item{
  width:100%;
  height:100%;
}

Updated JSFiddle Link

Answer №4

Mastering Flexbox

Imagine you have a group of elements residing inside the mainContainer. If this mainContainer is nested within another container (let's call it superParentContainer?), it's crucial to ensure that the mainContainer occupies the maximum space available.

Suppose the superParentContainer extends across the entire window. In that case, we need to define the CSS properties for the mainContainer as follows:

.mainContainer {
  height: 100%;
  width: 100%;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

Refer to the top-notch flexbox guide online for different options under justify-content.

By setting both the height and width to 100%, the container will fill its parent (superParentContainer). We designate it as a flexbox with a column direction, aligning its children vertically.

To enhance the appearance, consider adding a margin of 1rem to the child elements.

.element {
  margin: 1rem;
}

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

Illuminate a corresponding regular expression within a text input

I currently have a functional code for testing regex, which highlights matching patterns. However, my challenge lies in highlighting the result within the same input as the test string. Below you will see the HTML and JavaScript snippets along with two ima ...

Is there a way for me to determine the quality of a video and learn how to adjust it?

(function(){ var url = "http://dash.edgesuite.net/envivio/Envivio-dash2/manifest.mpd"; var player = dashjs.MediaPlayer().create(); player.initialize(document.querySelector("#videoPlayer"), url, })(); var bitrates = player.getBitrateInfoListFor("vid ...

Browser comparison: Chrome and Firefox - peculiar ID name causing table width difference without CSS. Suspected AdBlocking interference

There seems to be an issue with a table I have in Chrome. Whenever I change the ID name, it either sets the width to zero or doesn't display at all. You can view it here: http://jsfiddle.net/kdubs/W6GTE/ The specific line causing trouble is: <ta ...

Embracing right-to-left (RTL) languages

I am looking to create a website that can support both left-to-right (LTR) and right-to-left (RTL) languages seamlessly. My goal is to have the text direction switch automatically to RTL if the content is in an RTL language. Additionally, I want the input ...

I am unable to extract the text content from an element

Check out this website: driver.get("https://www.drk-kv-calw.de/kurse/erste-hilfe-eh/rotkreuzkurs-erste-hilfe.html"); WebElement bir=driver.findElement(By.xpath("(//a[text()='Link zur Karte'])[4]")); WebElement iki=driver.find ...

What is the best way to bridge the distance between a shrunken iframe and the following element?

https://i.sstatic.net/AZ7T4.png My iframe was resized to 65%, but the containing "div" still occupies the original iframe size, creating a large gap between the iframe and the next element. I'm not sure how to remove this gap. I want the next element ...

Background images are displayed correctly in Chrome, but are not appearing in Internet Explorer or Firefox

I'm having an issue with the CSS for the main content section of my website. The background image displays correctly in Chrome, but not in IE11 or Firefox. Can anyone provide insight into why this may be happening? #maincontent { position: relati ...

Retrieve data from the server as effortlessly as accessing an <img> tag

Retrieving data from a server (potentially on another domain) is straightforward when it comes to images: <img src="http://www.example.com/test.jpg" /> This process does not result in any CrossOrigin issues. However, when attempting to fetch text ...

What steps should be followed to effectively incorporate Google Fonts into a Material UI custom theme for typography in a React/TypeScript project

Hey there, I'm currently working on incorporating Google fonts into a custom Material UI theme as the global font. However, I'm facing an issue where the font-weight setting is not being applied. It seems to only display the normal weight of 400. ...

Having trouble getting CSS headers to work smoothly with iPhone?

After creating a web application with CSS, the initial GUI was exactly how I wanted it to be. However, within 3 seconds of loading the page, the GUI would change. The HTML code for my header is as follows: <div id="Slideshow"> <img src="image/s1 ...

Utilizing arrow keys as a means of setting focus (HTML & JavaScript)

Is it possible to program the left arrow key to function like the tab button (moving focus to the next focusable item) and the right arrow key to function as a shift+tab (moving focus to the previous focusable item)? I've made some progress with the ...

Only IE7 seems to be experiencing a z-index problem with CSS that is not present on any

I’ve encountered a perplexing problem with CSS z-index in IE7 that I just can’t seem to solve. #screen { display: none; background-image: url('/images/bg.png'); background-repeat: repeat; position: fixed; top: 0px; le ...

Creating a CSS dropdown menu with absolute positioning and setting the left position to auto

Just laying out my usual HTML menu structure: <div id="page"> <h1>Home</h1> <ul id="nav"> <li><a href="http://example.com/">Home</a> <ul> <li><a href="http://examp ...

Looking to preserve the "ALL" selection in a JavaScript-CSS filter?

On the front page, there are approximately 16 posts displayed along with a filter featuring 4 drop-down menus showcasing post categories. I have assigned category names as classes to each post div and am currently using javascript to hide them. The code s ...

Creating a web page with CSS that completely fills the screen and includes a lengthy list within the design

I am looking to create a webpage that takes up the entire browser window. However, I also need to display a long list on this page that requires a scrollbar for navigation. Currently, with my existing HTML code, the list is causing the page to stretch in ...

Arrangement of content in three columns with a flex direction set to column

In my current project, I am working on creating a view of items that are mapped using React. The main objective is to set up a 3-column layout with items arranged vertically within each column. Each item panel contains a hidden div element which expands wh ...

Is there a way to effortlessly launch a new window with just a single click?

I'm encountering an issue with a function that is supposed to open a new window when a button is clicked. Strangely, on the first click, nothing happens, but on the second click, the window finally opens. Here's my code: Script <script src ...

using javascript to retrieve php variables

After creating a webpage, setting up Apache2 on an Ubuntu server to access it over the internet, and installing PHP5 and MySQL, I encountered issues with accessing database information on my page through a .php file. Despite having a file named test.php th ...

Tips for retrieving multiple div elements by their id using the querySelectorAll method

Looking to incorporate a new feature for a click event. I'm trying to hide four div elements but the code seems to be ineffective. Can you identify what's wrong with it? <a class="lightbox-close" href="#" onclick="document.querySelectorAll(&a ...

A guide on extracting checkbox value from a database in PHP through the use of the explode function

I have a form with three checkboxes where some people select all subjects and others select only two. I need to retrieve the checkbox values from the database. Below is my current code. If anyone knows the solution, please share. <?php $connection = ...