What is the best way to position an element to be alongside multiple other elements within the same parent container?

I have a bunch of elements with an unknown number, and I need them to always be accompanied by another element next to them in this pattern:

╔═══════╦═══╗
║       ║   ║
╠═══════╣   ║
║       ║   ║
╠═══════╣   ║
║       ║   ║
╠═══════╣   ║
║       ║   ║
╚═══════╩═══╝

They must remain within the same container, as my CSS is already intricate and I am collaborating with others. Changing it all would be complex.

I attempted setting grid-row on the right element to 1 / -1, but negative values require grid-template-rows to be explicitly set, which is not feasible since the number of columns is unknown.

I also tried using a CSS counter to dynamically set either grid-row-end: counter(count) or

grid-template-rows: repeat(counter(count), auto)
, but it did not display a numerical output.

What is the best solution? Should I resort to having two separate containers? Using JavaScript is not ideal either.

Edit: A potential workaround, although not foolproof, involves setting grid-column: 1 / span 99. Initially, I tried 1 / 99, but the former is more effective and does not introduce additional rows.

Answer №1

Method 1: Utilizing two containers

To achieve the desired layout, you can employ the flexbox method. By dividing your container into two columns - left and right, then further segmenting the left column into four smaller containers. Below is a sample code snippet with fixed dimensions provided for reference purposes. I hope this explanation helps.

.container {
  max-width: 500px;
  width: 100%;
  height: 1000px;
  border: 1px solid black;
  display: flex;
  align-items: center;
  padding: 20px;
}

.left, .right {
  height: 100%;
  border: 1px solid red;
  padding: 10px;
}

.right {
    width: 30%;
}

.left {
  width: 70%;
  display: flex;
  flex-direction: column;
}

.row {
  width: 100%;
  height: 250px;
  margin: 5px 0;
}

.first-row {
  border: 1px solid blue;
}

.second-row {
  border: 1px solid green;
}

.third-row {
  border: 1px solid purple;
}
.fourth-row {
  border: 1px solid orange;
}
<div class="container">
  <div class="left">
    <div class="row first-row"></div>
    <div class="row second-row"></div>
    <div class="row third-row"></div>
    <div class="row fourth-row"></div>
  </div>
  <div class="right"></div>
</div>

Method 2: Single container approach (Not recommended)

It is also possible to achieve the desired layout using a single container, but it is not advised due to the dynamic nature of the boxes on the left. Here is an example with fixed elements and heights included for illustrative purposes.

I recommend opting for the dual container setup as it offers a clearer layout for accomplishing your objective.

.container { 
    max-width: 500px;
    width: 100%;
    height: 1200px;
    border: 1px solid black;
    display: flex;
    align-items: center;
    padding: 20px;
    flex-direction: column;
    flex-wrap: wrap;
}

.row {
    width: 70%;
    height: 240px;
    margin: 5px 0;
    float: left;
}

.fifth-row {
    margin: 5px;
    height: 100%;
    width: 30%;
    border: 1px solid red;
}

.first-row {
    border: 1px solid blue;
 }

.second-row {
    border: 1px solid green;
}

.third-row {
    border: 1px solid purple;
}

.fourth-row {
    border: 1px solid orange;
}
<div class="container">
    <div class="row first-row"></div>
    <div class="row second-row"></div>
    <div class="row third-row"></div>
    <div class="row fourth-row"></div>
    <div class="fifth-row"></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

The way images appear can vary between desktop and mobile devices

I am in the process of creating a photography website with a simple goal of displaying images down the page one after another. However, I am encountering issues with uniform display across various desktop and mobile platforms. The site appears perfect on i ...

The Bootstrap card is elegantly framed by a crisp white border surrounding the

After referencing a tutorial, I implemented the following code: <div class="card" style="width:400px"> <img class="card-img-top" src="image.png" alt="Card image"> <div class="card-body"> <h4 class="card-title">John Doe</ ...

Adjust the dimensions of the HTML button to match that of its background

Typically, buttons are created with specific dimensions like this: <button style="width: 120px; height: 40px;"> Mememe <button> Afterwards, a background is added that matches the size of the button: button { background-size: 100% 100%; } ...

Tips for aligning text to the right and keeping it in line with another section

I have a code snippet and I'm aiming to align the caption for my "DigDug" game directly below the game itself, perfectly lined up horizontally. The id for the caption is "#DigCaption" and the id for the game is "#DigDug". Could anyone provide guidance ...

How can I adjust the height and width of a checkbox input specifically for mobile screens?

Check out this code snippet: <div class="container-fluid mt-5"> <div class="row"> <div class="col-10"> Hello <br> Hello </div> <div class="col-2"> <input type="checkbox" class= ...

Ensure that the text remains in the forefront while applying a scaling effect to the image

My image gallery features a hover effect that utilizes a CSS transform when the user hovers over the images. transform: scale(1.1); Before the hover effect, the image appears like this: https://i.sstatic.net/fiP5e.jpg And with the effect applied, it loo ...

How to perfectly align text vertically using Bootstrapstylesheet

Previously attempted solutions from: here for vertical-align with Bootstrap 3 and also from: here for vertical-align: middle with Bootstrap 2 but unfortunately, neither worked. If you want to view the page, click here: Check out the code snippet: <di ...

Instructions for showing a "read more" and "read less" link when the text goes beyond a

I am pulling paragraph content from the database and displaying only 300 characters on the screen, with a "read more" option for the user to see the rest of the content. However, I am facing an issue where I also need to include a "less" option for the us ...

Navbar-toggle divider shade

I am having trouble changing the color of the line break on the Twitter Bootstrap 3 navbar-toggle function. Upon viewing this image, you can see that there is a line break using my background color. https://i.sstatic.net/qgVAm.jpg I have tried several o ...

Angular 2's innovative approach to implementing a sticky footer concept

Is there a way to make my footer sticky without being fixed? I attempted using the CSS negative margin trick, but it did not work as expected. In the provided Plunker code, I tried to replicate the issue in my Angular 2 app. The goal is for the footer to s ...

Is there a way to add a disappearing large space that vanishes when a line break occurs?

I am currently working on a website where I aim to have the name and airport code displayed in a centered title above an image of the airport. https://i.sstatic.net/Vwi2V.png The name and code will be separated by an em space: div.terminal-silhouette- ...

Which option is more effective: using an id or using !important?

While some say to avoid using the !important rule and others argue that since ids are unique, there is no need to target like #main > #child and you can simply use #child. In my particular case: #main > div{ color: red; } #child{ color: blue;/ ...

Using Next.js in conjunction with Tailwind CSS and the shadcn/ui library to tackle the issue of preventing overscroll from creating

As I delve into a Next.js project, my goal is to use Tailwind CSS to craft an interface featuring a sidebar on the left, a header at the top, and a main content area. However, an issue has surfaced where users can over-scroll, leading to a blank space appe ...

Creating text that adapts to various screen sizes when displayed over a video background

I am experimenting with having text displayed over a video background that adjusts to different screen sizes. I want the text to fill up the entire screen regardless of the browser size. What would be the most effective way to achieve this? Currently, I h ...

Find out if the visible area contains an open dropdown menu and take action if it is not

I am currently working on a way to determine if an open dropdown menu is within the user's view or has moved out of sight. I've been experimenting with the getBoundingClientRect() method to compare coordinates and viewport dimensions, but it seem ...

Creating a List Item with Equal Height as Its Parent Container

<nav> <ul id="navUl"> <li> <div class="settingsDiv"> hey </div> </li> </ul> </nav> I am trying to adjust the height of the div element to match the height of the nav. My g ...

Creating a Jquery method to handle multi-line ellipsis for long text

Check out this fiddle I made: http://jsfiddle.net/mupersan82/JYuSY/ This snippet handles multiline ellipsis: $(function() { var ellipsisText = $('.multilineEllipseText'); var textContainer = $('.incidentCellBottomRight').height(); whi ...

Step-by-step guide on making a cascading dropdown menu using HTML

I am trying to create a file with two dropdown menus, where the options in the second dropdown are based on the selection made in the first dropdown. Although I have never worked with HTML before and feel a bit lost, I attempted to modify code from a tutor ...

CSS: Targeting a particular instance of a specific element type within an HTML document

My goal is to target specific occurrences of a certain type of element in CSS for styling purposes. Consider the following example: <span>Some random stuff</span> <p>Occurrence 1 of p</p> <ul> <li>Random stuff</li&g ...

Bootstrap - A collapsed navigation button that remains fixed without the rest of the navbar

I'm currently working on a responsive drag and drop game that utilizes Bootstrap and jQuery UI. Within the game, I have implemented a collapsible navbar at the top of the page. To optimize space for buttons at the bottom, I am looking to hide the nav ...