Distribute the elements evenly between two separate divs

Hello everyone!

I have a unique challenge that I hope someone can help me with. I have a list of links to various tests, but different sections require these links to be displayed in different ways. One section needs them spread over two columns, while another requires them to be spread over three columns. The number of tests a user takes varies, so we need the elements to distribute evenly without disrupting the overall layout if some items are missing from the list. I hope this all makes sense.

Manually arranging the items could result in uneven distribution across the columns, which is not ideal. A previous query led me to some CSS code snippets like the following:

.menu {text-align:justify;}
.menu > a {display:inline-block}
.menu:after { content:' '; display:inline-block; width: 100%; height: 0 }
<div class="menu">
  <a href="#">0</a>
 <a href="#">1</a>
 <a href="#">2</a>
 <a href="#">3</a>
</div>

I'm looking for a way to control the number of items per row without everything being displayed in a single line. Another CSS snippet I found is:

.test {
  display: flex;/* We first create a flex layout context */
  flex-flow: row wrap; /* Then we define the flow direction and if we allow the items to wrap */
  justify-content: space-around;/* Then we define how is distributed the remaining space */
  border: 1px solid #999999;
}
.test > div {
  margin: 10px;
  padding: 20px;
  background-color: #FF0000;
}
<div class="test">
  <div>Div 1</div>
  <div>Div 2</div>
  <div>Div 3</div>
  <div>Div 4</div>
  <div>Div 5</div>
</div>

This code also controls the distribution of items, but I am still looking for a way to limit the number of items per row. Any insights or feedback would be greatly appreciated.

Thank you!

Answer №1

If you're looking to style elements using CSS selectors, there's a handy one called nth-child(). You can use it like nth-child(even) / nth-child(odd) / nth-child(3) and so on. For example, the code below demonstrates how to evenly space out divs:

menu > a {float:left}
menu > a:nth-child(even) + a { clear: both; }
<div class="menu">
  <a href="#">0</a>
  <a href="#">1</a>
  <a href="#">2</a>
  <a href="#">3</a>
  <a href="#">4</a>
  <a href="#">5</a>
</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

Seeing <br> elements being inserted into a <div> element in the page source

I have encountered an issue where my code does not contain any br tags between the beginning of the div tag and table tag, but when I view the source, several br elements appear. Below is the code snippet from a .php file: <div id='tx_addAccountp ...

Is there a significant time-saving benefit to utilizing nested rules in CSS, particularly when using HAML or LESS?

Here is how I am currently coding my CSS: Regular CSS: #content { color: white; background: black; } #content a { color: yellow; } #content p { margin: 0; } #content (etc...) #contnet (etc...) #content (etc...) (I always include the parent eve ...

Can a Chrome App access a user's files on their hard drive with proper permissions?

I'm currently working on a Chrome Packaged App that includes video playback functionality. My main goal is to enable users to stream online media (such as MP4 videos) while also giving them the option to save the video to a location of their choice. ...

Position of fixed div set relative to its parent fixed div

I'm facing an unusual situation where I need a fixed div to be positioned relative to its parent, which is also a fixed div. When you take a look at my example, everything will become clear. <div class="drawer"> <p>Drawer</p> & ...

The sticky navigation bar becomes fixed at the top prematurely

Having a sticky navbar on my Bootstrap-based website, I've implemented the following jQuery code: $(document).ready(function () { var flag = false; function stickNav() { $(".navbar-default").affix({ o ...

The Ajax contact form is not submitting the necessary POST data to the PHP script

After creating an html form with ajax: function confirmSendMail() { $(".send_contact").click(function(event){ event.preventDefault(); var name = $("#inputName").val(); var email = $("#inputEmail").val(); var pho ...

Sketch a line extending from one element to the next

Is there a way to create a styled line that starts from the center of one <td> element and ends at the center of another? I attempted to use the jQuery Connections plugin but it only connects the edges of the elements, not their centers. The plugin ...

Strange behavior observed while attempting to create a smooth CSS transition effect from the right side

Is there a way to create a hover effect in my navbar where a line appears from the bottom left and top right, without causing any size or alignment issues with the links? * { margin : 0; padding: 0; } nav{ background-color: black; width: 1200px; he ...

I am experiencing a CSS display issue on the mobile version of my map leaflet where only the header and footer are visible

I am experiencing a display issue on Android and iPhone due to conflicting css commands. Here's what's happening: I am using React + React Leaflet with a header, main, and footer all set to width 100% within a parent div with body set to width an ...

The button in the flex container is not wide enough due to center alignment

For my project, I implemented a CSS grid layout consisting of 20 rows and 20 columns (5% of screen for each cell). One particular page includes a button that initially fit within grid columns 5-8 and rows 6-9 without any issues. However, I wanted to center ...

`Erase content from a field once text is typed into a different field`

I have a Currency Converter with two input fields and a button. I enter the amount to be converted in the first field, and the result of the conversion appears in the second field. My question is: How can I automatically clear the second field when I type ...

Ways to broaden the map of a variable in SCSS

Currently, I am utilizing a package known as Buefy, which acts as a Vue.js wrapper for the Bulma CSS framework library. Within Buefy's template components, there is an attribute/property called type (e.g., type="is-warning"). According to the document ...

Implementing a feature to add selected checkboxes and remove unselected checkboxes with PHP and MySQL

I am currently working with 3 tables in my database: product, category, and product_category. I am in the process of creating a form that allows users to edit (insert/delete) categories for a specific product. My challenge lies in inserting an array of che ...

Change occurring within a cell of a table that has a width of 1 pixel

In the code snippet below, there is a transition inside a table cell with a width of 1px to allow it to wrap its content. However, the table layout changes only at the end or beginning of the transition: var animator = document.getElementById("animator" ...

Using JavaScript to create a search bar: Can "no results found" only show after the user has completed typing their search query?

How can I ensure that the "no match" message only appears after the user has finished typing in their search query, rather than seeing it while they are still typing "De" and the list displays "Demi"? usernameInput.addEventListener("keyup",function(){ ...

HTML, JavaScript, and PHP elements combine to create interactive radio buttons that trigger the appearance and disappearance of mod

On my page, I have multiple foreach loops generating divs with different information. These divs are displayed in modals using Bootstrap. However, I am encountering an issue where if I select a radio button and then close the modal and open another one, th ...

Firefox not responding to input select dropdown selection

When creating a jQuery submenu box in Firefox, I encountered an issue with the input select dropdown. While it functions properly in Google Chrome, the select box behaves erratically in Firefox. When attempting to choose an option from the select box, such ...

I've created a logo, but why are there two logos on my website?

There seems to be an issue with two divs stacking on top of each other, which is not the desired layout. This problem occurs when five div boxes are added. The goal is to have all divs in a single row, but it's unclear why this layout is not working ...

React hook dom and material-ui FormControlLabel are causing a checkbox input to return an empty string instead of the expected true/false value

I am currently working on displaying the values returned from a form, specifically focusing on the Checkbox section. <FormControlLabel control={<Checkbox {...register('remember')} name="remember" color="primary" defaultV ...

Can someone explain why line-height can affect the width in CSS?

Can you explain how the line-height property functions in CSS? I have noticed that when I set the line-height to be equal or less than the font size, it causes layout width issues. You can view an example of this problem on a jsFiddle here. Currently, my ...