Utilize CSS to ensure divs display inline-block, but stack them only if they overlap

Below are the div elements I have:

.parent {
  padding: 10px;
  background: grey;
}

.child {
  background: green;
  display: block;
  position: relative;
  width: 50px;
}

.stacked {
  left: 0px;
}

.three {
  left: 200px;
}
<div class="parent">
  <div class="child stacked">div1</div>
  <div class="child stacked">div2</div>
  <div class="child three">div3</div>
</div>

This is how it appears: https://i.sstatic.net/iuyjX.png

I want divs 1 and 2 to stack as they currently do. However, since div3 does not overlap with the above divs, I would like it to be vertically aligned with div 1.

If I change the display property to inline or inline-block, div 2 gets pushed to the right of div one. Additionally, the left values do not align accurately with the parent container. https://i.sstatic.net/skJyh.png

The left values of the divs will be dynamically generated, so predicting if there is overlap between them is challenging.

Is achieving this alignment feasible?

Answer №1

Update: If you are looking to align the 3rd div using positioning, here's how you can achieve it:

.parent {
  padding: 10px;
  background: grey;
  position: relative;
}

.child {
  background: green;
  display: block;
  position: relative;
  width: 50px;
}

.stacked {
  position: relative;
  left: 0px;
}

.three {
  position: absolute;
  left: 200px;
  top: 10px;
}
<div class="parent">
  <div class="child stacked">div1</div>
  <div class="child stacked">div2</div>
  <div class="child three">div3</div>
</div>

You can utilize the column-count property as shown below:

.parent {
  padding: 10px;
  background: grey;
  column-count: 2;
}

.child {
  background: green;
  display: block;
  width: 50px;
}
<div class="parent">
  <div class="child">div1</div>
  <div class="child">div2</div>
  <div class="child">div3</div>
</div>

Alternatively, you can employ flexbox for vertical wrapping like this:

.parent {
  padding: 10px;
  background: grey;
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  height: 50px;
}

.child {
  background: green;
  display: block;
  width: 50px;
}
<div class="parent">
  <div class="child">div1</div>
  <div class="child">div2</div>
  <div class="child">div3</div>
</div>

Here is an example using grid:

.parent {
  padding: 10px;
  background: grey;
  display: grid;
  grid-auto-flow: column;
  grid-template-rows: 1fr 1fr;
}

.child {
  background: green;
  display: block;
  width: 50px;
}
<div class="parent">
  <div class="child">div1</div>
  <div class="child">div2</div>
  <div class="child">div3</div>
</div>

Answer №2

If you want to improve the layout, consider placing div3 inside a wrapper div with a class of flex-container.

.flex-container {
  justify-content: flex-start;
}

Make sure that the flex direction is set to horizontal for better alignment.

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

In PHP, special characters in HTML remain unchanged when entered into a text input field

I am currently using the PFBC (PHP form builder class (PFBC)) to generate a TextBox element on a page; However, when dealing with strings containing special characters, it does not properly convert them into normal characters: For example: $razao = "livi ...

Allow the div to break out of the overflow: hidden property without disrupting the overall page layout

Currently, I am attempting to separate a child element from its parent that has overflow: hidden applied. The main reason for restricting the dimensions of the parent is for a JavaScript workaround. I have experimented with positioning the child element, ...

The show more/show less link for a long jQuery paragraph is malfunctioning

I encountered an issue while coding where the "read more" link works correctly, but the "show less" link does not. Despite my efforts, I cannot seem to identify the error. Within this code snippet, there is an anchor tag with class="show-less" that I am u ...

When viewed through the Firefox browser on an Android device, the text on the webpage suddenly enlarg

I currently have the following content displayed on my website: <div style="font-size: 11px; line-height: 17px; text-align: left;"> <p>lorem ipsum etc etc</p> </div> Strangely, despite setting the font size to 11px, the te ...

repeating the identical content in the same setting

I am encountering an issue with jquery as I have 2 different sets of icons, each of which I want to do the same thing: My primary codes are <ul> <li> <a href=""> <i class="fa fa-facebook"></i> ...

Transform Vue.js into static HTML output

Is there a way to convert a Vue.js component into static html without the need for javascript? I am specifically interested in retaining styles. I am searching for a library where I can execute code similar to this: SomeNestedComponent.vue <template> ...

Peek into the Outlook Calendar Event's Source Code

We are interested in analyzing the source code of calendar events in Outlook across all versions from 2010 to 2016 due to its integration with multiple platforms like Google, Exchange, and Exchange Online. Is there a method available to obtain the HTML so ...

Iterate through HTML content and utilize JavaScript along with Regular Expressions to substitute specific strings

In my HTML located in Anki, I have the following structure: <p>[!Quote] Title of callout 1<br>Content of callout 1</p> <p>[!Quote] Title of callout 2<br>Content of callout 2</p> <p>[!Quote] Title of callout 3<br ...

Execute the function when the control becomes visible

Currently, I possess an input control: <input id="someID" class="form-control" type="text" name="SomeData" data-ng-model="vm.SomeData"> I have a requirement to set the initial value of vm.SomeData upon user scrolling down to this control. As a begi ...

In the game of rock paper scissors, the icons become unclickable once you achieve a score of 5

I have created a Rock Paper Scissors game where the score resets if either the user or computer reaches 5 points, but I want to prevent any further play after hitting 5. Currently, the game displays a message and reloads after 5 seconds, during which tim ...

Incorporate a sleek Vueitfy user interface seamlessly into your current website

Recently, I put together a sleek user interface for a web application with the help of Nuxt.js and Vuetify.js (which utilizes Vue.js code). Although my design is top-notch, I now face the challenge of integrating it into an existing website that does not ...

`Certain browsers are experiencing issues with text overlay functionality.`

I have managed to successfully create the necessary code to incorporate an image with a child element containing text overlaying the image. However, this functionality does not appear to be working on Internet Explorer (IE), and I am struggling to find a C ...

Encountering issues with basic login functionality using Node.js and MongoDB - receiving a "Cannot

I'm experiencing some difficulties trying to set up a login for my website. I have a user registered in my database with the email: [email protected] and password 123. The issue arises when I attempt to use the POST method, as it returns the fo ...

Place in the Middle of a Bootstrap Container

Struggling with aligning the elements of a Bootstrap well in the center? The badge is off to the side instead of below the arrows, and the vote buttons are not centered within the well. Check out the code snippet below: HTML: <div class="col-md-1 ...

Elevate Your Designs: A New Perspective on Vertical Alignment

I am struggling to vertically align my text, which spans multiple lines, while implementing the scroll-over effect. I attempted to include vertical-align: middle; in my code, but unfortunately, it did not produce the desired result. Here is my CSS code: ...

An exclusive execution of the JavaScript function is ensured

I have a JavaScript function that I want to execute 12 times in a row. Here's my approach: Below, you'll find a list of 12 images: <img id="img1" src=""> </img> <img id="img2" src=""> </img> <img id="img3" src=""> ...

The layout option is not specified within the ejs-mate package

error boilerplate I am baffled as to why the layout is not being defined. I have installed ejs-mate and ejs, yet it still gives this error. <% layout('layouts/boilerplate') %> <h1>All campgrounds </h1> <div> <a ...

Issue with Internet Explorer 7: Floating elements are incorrectly positioned below their intended placement

Upon visiting using IE7, you may notice that the two middle columns are being pushed down to the bottom of the page. I have attempted to resolve this issue by applying display:inline to both columns without success. Any assistance on this matter would be ...

When a textarea contains a new line, it will automatically add an additional row and expand the size of the textarea

Developed a form with the functionality to move placeholders in a textarea upwards when clicked. The goal is to increment the height of the textarea by 1 row for every line break. However, I am uncertain about what to include in the if statement of my Ja ...

Enhance Your List with Icon Decorations

Is it feasible to utilize a sprite image in place of the default bullet for an ul list? Is this achievable? ...