CSS - Layering new DIVs over existing DIVs

My goal is to implement "vertical-align: bottom;" in order for the DIVs to align from the bottom of the wrapper/parent DIV to the top. However, I am facing an issue where I want the first DIVs to be displayed at the bottom, rather than at the default top position. This preference stems from my desire to always have the bottom row filled completely, even if it means adding new boxes on top.

The specific order of the DIVs is not crucial, but what I really aim for is having the very bottom "row" fully occupied, with the top row sometimes only containing 1 or 2 boxes. By introducing new boxes at the top rather than at the bottom, I believe this layout can be achieved. I hope this explanation makes sense.

In addition, I've noticed gaps between each "column" in the code provided below that I've been attempting to eliminate. While resolving the initial issue may require a complete code change, any insights into why these gaps are present would be greatly appreciated :)

Thank you!

Here's the current state of the code:

div.wrapper {
display: table-cell; 
vertical-align: bottom; 
height: 500px;
width: 640px;
background-color: lightgrey;
}
div.wrapper div {
width: 200px; 
height: 50px;
display: inline-block;
}
div.wrapper div p {
margin: 0px;
padding: 0px;
}
<div class="wrapper">
<div style="background-color: #7E7ECC;">
<p>1</p>
</div>
<div style="background-color: #FFA347;">
<p>2</p>
</div>
<div style="background-color: #80E680;">
<p>3</p>
</div>
<div style="background-color: #FF99C2;">
<p>4</p>
</div>
<div style="background-color: #A6DBEE;">
<p>5</p>
</div>
</div>

And here is the desired outcome I'm aiming for:

https://i.sstatic.net/jfLOC.jpg

This alternative design would also work: https://i.sstatic.net/I1p4O.jpg

Answer №1

To achieve this layout, utilize CSS3 flexbox with the flex-wrap: wrap-reverse property to wrap items in the opposite direction of flex-direction: row.

Instead of using *-end, opt for align-content: flex-start due to the reversed wrap order.

div.wrapper {
  /* New Properties */
  display: flex;
  flex-direction: row;
  flex-wrap: wrap-reverse;  
  align-content: flex-start;
  /* ^ New Properties */
  background-color: lightgrey;
  height: 150px;
  vertical-align: bottom;
  width: 640px;
}
div.wrapper div {
  width: 200px;
  height: 50px;
  display: inline-block;
}
div.wrapper div p {
  margin: 0px;
  padding: 0px;
}
<div class="wrapper">
  <div style="background-color: #7E7ECC;">
    <p>1</p>
  </div>
  <div style="background-color: #FFA347;">
    <p>2</p>
  </div>
  <div style="background-color: #80E680;">
    <p>3</p>
  </div>
  <div style="background-color: #FF99C2;">
    <p>4</p>
  </div>
  <div style="background-color: #A6DBEE;">
    <p>5</p>
  </div>
</div>

Answer №2

To achieve the desired stacking effect, you can use flex layout on the wrapper and arrange items in a row. Here's the CSS code snippet:

div.wrapper {
    display: table-cell; 
    vertical-align: bottom; 
    height: 500px;
    width: 640px;
    background-color: lightgrey;
    display: flex; /* added */
    flex-direction: row; /* added */
    flex-wrap: wrap-reverse; /* added */
    align-content: flex-start; /* added */
}
div.wrapper div {
    width: 200px; 
    height: 50px;
    display: inline-block;  
}

div.wrapper div p {
    margin: 0px;
    padding: 0px;
}

For a live example, check out this FIDDLE

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

Default file is still generated by Sass even when variables are modified

Currently, I am in the process of modifying some variables for my customized version of Bootstrap. At the moment, I have the Bootstrap SCSS files along with my own variables file. This is how my file appears: @import "/path/to/bootstrap"; $blue: #42a5f ...

Using a pug template to render a dynamically generated SVG code

I am attempting to include an SVG in my pug template, but I am encountering issues. I am using r6operators from marcopixel, which provides a function operator.toSVG() that returns the XML string of an SVG. When I try this: p some text #{operator.name} ...

Spooky 'outline' emerges with rounded corners in Internet Explorer 11 and Microsoft Edge

I encountered an unusual issue in IE11 and Edge (on Windows 10) where a strange, transparent border appears in the HTML/CSS code. <!DOCTYPE html><html> <head> <style> body { background-color:red; ...

What are the specifications for the opacity, width, and height of the background image in Bootstrap's

My current project has specific requirements that I need assistance with: I need the background image of the jumbotron to fit the width of the page and have its height scaled proportionally. The opacity of the jumbotron's background image should be ...

The art of replacing material-ui styles with styled components

As a newcomer to UI material design, I am eager to create my own customized Button Component using styled-components. I am facing a challenge in overriding the CSS based on different button variations such as "primary" or "secondary". You can find my cod ...

Steps to halt webkit animation within a div located inside a circle:

I have a series of nested circle divs, and I want to give them a pulse animation. The issue is that the text container is within one of these circles, causing the animation to apply to the text as well. I am unable to move the text container due to potenti ...

step-by-step guide on showcasing and concealing textbox borders

Within a table, I have a column with a text box for users to edit the text as needed. The edited text is properly being saved using the onkeyup event. However, I want to hide the border of the text box after editing, so only the edited text is visible. If ...

Tips for customizing the appearance of popup windows

I want to enhance the appearance of my popup window by applying a different format for opening it. How can I style it so that it looks visually appealing when the popup window opens? You can find below the source code I am working with: HTML: <div onM ...

Tips on creating a repeating background image with CSS for a sprite

I'm facing a challenge that I can't seem to solve - I need to set a sprite as the background for a container. The issue is that despite my efforts, I haven't been able to achieve this. I've searched extensively but couldn't find a ...

Extracting values from an *ngFor loop in Angular 8 - Here's how to do

Currently, I am utilizing *ngFor to display some data. I have a requirement to extract a specific value from *ngFor and display it in, for instance, my heading. However, when I attempted to use {{ project }}, it consistently returned undefined. Despite hav ...

display and conceal a division by clicking a hyperlink

How can I make a hidden div become visible when clicking on a specific link without using jQuery? Javascript function show() { var a = document.getElementsByTagName("a"); if (a.id == "link1") { document.getElementByID("content1").style.v ...

How can I assign the output of a function to a variable within a class in Angular?

Is there a way for the Army class to automatically update its CP property with the sum of CP values from all Detachments in the Detachment class? In the Army class, the CP property should reflect the total CP value from all Detachments and be accessible t ...

Change button to an ajax spinner when it is clicked using jQuery

$(".post-btn").html("<img src='../images/loader.gif' />"); Why isn't this code working? I know I have the correct selector because when I tried $(".post-btn").text('test'), it worked. I want the text of the button to change ...

Issue with Firefox: Click event not triggered when clicking on CSS pseudo element

Check out my custom-made button: <button class="btn-close">Close alert</button> Here's the CSS styling for the button: .btn-close{ position: relative; height: 30px; border: none; background-color: #332b2a; color: #ff ...

Disconnecting socket when A-tag with Href is clicked

Currently, I am working on a project that involves using socket.io. In this project, I need to provide users with links to download files. <a href="<path>" >Link Name</a> Interestingly, whenever I click on the link to download the file, ...

Creating a data visualization dashboard using Google Charts and organizing it in a responsive

I am integrating Google charts into a bootstrap theme. The Chart dashboard I am using has a specific structure: <div id="dashboard"> <div id="control1"></div> <div id="chart1"></div> </div> On the other ...

What is the best way to apply a class to the initial div using jquery?

I have the following HTML code: <div class="main"> <div class="sub"></div> <div class="sub"></div> <div class="sub"></div> </div> <div class="main"> <div class="sub"></div> <di ...

Prevent the top of the fifth row from displaying on mobile devices when the first four rows are hidden

My personal website features a collection of user-generated stories, with only the first 4 rows of each story visible in a fading text gradient from black to white. There is a "show more/less" button to reveal or hide the full content. While this layout ...

Tips for incorporating a custom CSS design into a jQueryUI tooltip

I am struggling to apply my custom CSS class on top of the jQueryUI tooltip. Although the tooltip is displaying correctly, my styles are not being applied. Here is the code I'm using: $(document).ready(function () { $("#roles").tooltip({ content: ...

chrome side column must be set to 100% height

Having trouble with setting height to 100%. I have a list of items that need to be distributed to match the height of the side image. It works fine when I use a fixed height, but not with a percentage. The issue arises because the website is responsive a ...