Exploring ways to distribute the height of parent div among child divs

I am looking to dynamically adjust the height of child divs within a fixed-height parent div. The parent div's height is fixed, but the number of child divs can vary. I want each child div to have a height equal to the parent div's height divided by the total number of child divs present. Although there are currently two child divs in my example, the number may change.

Below is the HTML structure:

<div class="calendar-default">
   <div class="calendar-plage" style="background-color: red;">&nbsp;</div>
   <div class="calendar-plage" style="background-color: green;">&nbsp;</div>
</div>

Here is the corresponding CSS:

.calendar-default{
    background-color: black;
    position: relative;
    height: 100px;
    width: 100px;
}

.calendar-plage{
    height: auto; /* ??? */
}

You can see a working example in this Fiddle: https://jsfiddle.net/z2anpsy7/

While I have achieved this using Javascript, I am curious if there is a way to accomplish it using CSS only. Is this possible?

Additionally, since this is within an AngularJS app, if you have an elegant AngularJS solution to this problem, I would greatly appreciate it!

Answer №1

To achieve this layout, utilize flexbox along with the flex-direction: column; property.

.calendar-default {
  background-color: black;
  position: relative;
  height: 100px;
  display: flex;
  flex-flow: column wrap;
}
.calendar-plage {
  flex: 1 0 auto;
}
<h2>2 children</h2>
<div class="calendar-default">
  <div class="calendar-plage" style="background-color: red;">&nbsp;</div>
  <div class="calendar-plage" style="background-color: green;">&nbsp;</div>
</div>

<h2>3 children</h2>
<div class="calendar-default">
  <div class="calendar-plage" style="background-color: red;">&nbsp;</div>
  <div class="calendar-plage" style="background-color: green;">&nbsp;</div>
  <div class="calendar-plage" style="background-color: blue;">&nbsp;</div>
</div>

Answer №2

When it comes to spreading the children horizontally, we utilize the CSS property display: table-cell. For vertical distribution, on the other hand, we can employ display: table-row. However, it's important to note that display: table-row requires some content within it, which can be provided through a pseudo-element as illustrated in the following example.

Feel free to add more children, as they will spread out and adjust themselves within the parent container automatically.

.parent {
    height: 100px;
    width: 100px;
    display: table;
    border: 1px solid black;
}
.child {
    display: table-row;
    width: 100px;
    background-color: tomato;
}
.child:nth-child(2n) {
    background-color: beige;
}
.child::after {
    content:"";
}

See the Working Fiddle

Answer №3

.default-calendar{
    position: relative;
    height: 100px;
    width: 100px;
}

.calendar-range{
    height:50%
}
    <div class="default-calendar">
       <div class="calendar-range" style="background-color: red;">&nbsp;</div>
       <div class="calendar-range" style="background-color: green;">&nbsp;</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

Executing a script that has been inserted into the page post-loading

I'm facing an issue where the script tag at the bottom of my project is not being executed after adding new pages later on. Main const fetch = (url, call) => { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if ...

angular setting the scroll offset in ng-infinite scroll

Looking for a way to maintain my offset value in order to fetch the next set of data whenever I scroll or click on 'Load more'. This is essential for enhancing performance. Currently, I am experimenting with setting an offset and limit, passing t ...

What makes the <aside> tag in HTML a sectioning content, while the <main> tag is not?

I've been delving into the HTML specifications provided by whatwg and MDN. They outline the 4 sectioning elements: article, aside, nav, and section, and mention that they establish the scope of footers and headers. Now, I have a couple of queries: ...

Guide on serving static HTML files using vanilla JavaScript and incorporating submodules

Is it possible to serve a static html file with elements defined in a javascript file using imports from submodules using vanilla JS? Or do I need bundling tools and/or other frameworks for this task? Here's an example code structure to showcase what ...

The website functions well in responsive design mode on an iPad screen, but encounters issues when viewed on an actual

The website is functioning well in responsive design mode on an iPad screen, however it seems to be having some issues specifically on iPad devices. I have tested it on all other devices and it works perfectly fine, but for some reason not on iPads. Feel ...

Creating a dynamic webpage where multiple images are displayed on a single canvas using

I'm currently working on a chess project where I'm using a canvas to create the game board and draw 32 chess pieces on it. To update the screen when a piece moves, I've implemented requestAnimFrame. However, I'm facing an issue where th ...

Transferring the values of JavaScript objects to HTML as strings

My goal is to generate HTML elements based on the values of specific JavaScript objects that are not global variables. However, when attempting to execute the code below, I encounter an error stating "params is not defined." What I actually aim to achieve ...

Is it feasible to create a full-page text gradient clip without it repeating on each individual element?

.gradient { background-color: #00ccaa; background: linear-gradient(0deg, #00ccaa, #f530a9); background-size: cover; background-clip: text; box-decoration-break:slice; -webkit-background-clip: text; -webkit-text-fill-color: transparent; -web ...

Changing states in Ionic Nav View triggers a controller change, however, the view remains the same

Having some trouble with routing in Ionic and Ui-Router. I'm noticing that when I click on the link to change the view (using an anchor tag with ui-sref), the controller changes as expected (I can see output in the console), but the view remains uncha ...

Angular material raised button with the file input feature for versions 4 and 5

I am currently developing an Angular application and for file upload, I am using the following code snippet: <label class="btn btn-default"> <input type="file" (change)="selectFile($event)"> </label> <button class="btn btn-success ...

The static files for the icon CSS (404 Error) from Flaticon and Font-mfizz are failing to load properly

When I was designing a website, I needed an icon of Python, so I turned to Flaticon and found what I was looking for. This snippet shows the HTML code I used: {% load static %} <!DOCTYPE html> <html lang="en"> <head> <li ...

Refine your search with Angular filter for nested properties

Trying to implement a filter on a specific element within my JSON object. The filter works effectively, but when I remove it, the properties without the specified sub element are not shown. Any suggestions on how to overcome this issue? Important note: ...

I struggle with generating a transition effect in the input box through label transformation

Why is the input box not using the specified CSS styles for the input and label tags? The transform property doesn't seem to be working as expected. I've highlighted the areas where I'm facing issues with bold text, and I've included bo ...

Ways to display a different div when clicking on a div?

Good afternoon, I've noticed that this question has been asked numerous times before, but none of the solutions provided seem to work for my specific issue. My problem involves a div with the class .title3. I want another div with the class .Content ...

Using injected services within static methods may seem tricky at first, but once you

I am exploring the integration of angularjs and typescript in my project. Currently, I am working on creating an Orm factory using typescript but have encountered some challenges. The structure of my factory class is as follows: class OrmModel implements ...

Odd behavior of dropdown list on Internet Explorer 8

Thank you for taking the time to review this. I have a dynamic dropdown list that changes its content based on a specific value (for example, "51") inputted into the JavaScript code on each page. Different pages display different lists depending on this va ...

What is the best way to place an image above a step progress bar?

I need assistance with adding an image or icon above each step in the progress bar. I attempted to insert an image tag, but it ended up next to 'step 1', which is not the desired outcome. .progressbar { counter-reset: step; } .progressbar li ...

Having trouble displaying database model information in the template

My challenge is displaying information from the Django third model (class Jenkinsjobsinformation) in the template. I've been successful in showcasing data from the first and second models (Projectname and Jenkinsjobsname). See below for a snippet of m ...

Transitioning the color of links in Firefox causes flickering when switching between the hover and visited state

I have been working on testing this code snippet in Firefox 49.0 on Linux Mint: a{ font-size:50px; color:gray; font-weight:1000; transition:color 1s; } a:hover{ color:black; } a:visited{ color:lightgray; } <a href="">VISITED LINK</a>&l ...

The refreshment of an Angular JS scope variable is contingent upon the value of another scope variable creating a

My directive has scope variables that rely on other variables within the scope. I expected that if the variable on the right side of the equation changed, it would automatically update the left side as well. However, that doesn't seem to be the case. ...