Restricting the number of items in each row using Flexbox while maintaining the original width of the child

Recently, I experimented with creating a layout using Flexbox. My goal was to have a parent element set to display:flex with a maximum of 7 child elements arranged in rows of 4 and 3 respectively, all centered. Here's an overview of what I attempted:

.parent {
  display:flex;
  flex-wrap:wrap;
  justify-content:center;
}
  .child {
    width:65px;
    height:65px;
    border-radius:50%;
    overflow:hidden;
    flex:0 0 25%;
  }
<div class="container">
  <div class="parent">
    <div class="child"><img src="http://placehold.it/200x200" /></div>
    <div class="child"><img src="http://placehold.it/200x200" /></div>
    <div class="child"><img src="http://placehold.it/200x200" /></div>
    <div class="child"><img src="http://placehold.it/200x200" /></div>
    <div class="child"><img src="http://placehold.it/200x200" /></div>
    <div class="child"><img src="http://placehold.it/200x200" /></div>
    <div class="child"><img src="http://placehold.it/200x200" /></div>
  </div>
</div>

While my code is almost functional, there seems to be an issue where the specified width of 65px for each child element is being disregarded. Additionally, I am seeking advice on how I can maintain consistent spacing between the child elements in both rows without exceeding their defined width.

Answer №1

To ensure proper styling for the images inside the .child divs, a separate CSS rule is necessary. This new rule will house properties specifically targeting the image:

.child img {
  border-radius: 50%;
  width: 65px;
  height: 65px;
}

Additional flexbox rules have been applied to the .child divs in order to center the images within each .child container. As a result, the .child divs now focus solely on organizing the layout while the image styling remains independent.

.parent {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
}

.child {
  flex: 0 0 25%;
  
  /*for aligning image inside .child */
  display: flex;
  justify-content: center;
}

.child img {
  border-radius: 50%;
  width: 65px;
  height: 65px;
}
<div class="container">
  <div class="parent">
    <div class="child"><img src="http://placehold.it/200x200" /></div>
    <div class="child"><img src="http://placehold.it/200x200" /></div>
    <div class="child"><img src="http://placehold.it/200x200" /></div>
    <div class="child"><img src="http://placehold.it/200x200" /></div>
    <div class="child"><img src="http://placehold.it/200x200" /></div>
    <div class="child"><img src="http://placehold.it/200x200" /></div>
    <div class="child"><img src="http://placehold.it/200x200" /></div>
  </div>
</div>

Answer №2

If you want to create a distinct line between the first and second row in your layout, consider using a hidden element with width:100%. You can then apply either space-around or space-evenly for spacing.

.parent {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-evenly; /*or space-around*/
}

.child {
  width: 65px;
  height: 65px;
  border-radius: 50%;
  overflow: hidden;
}

.parent:before {
  content: "";
  width:100%; /*hidden element to separate rows*/
  order: 1;
}

.child:nth-child(n + 5) {
  order: 2; /*adjust order for second row items*/
}
<div class="container">
  <div class="parent">
    <div class="child"><img src="http://placehold.it/200x200" /></div>
    <div class="child"><img src="http://placehold.it/200x200" /></div>
    <div class="child"><img src="http://placehold.it/200x200" /></div>
    <div class="child"><img src="http://placehold.it/200x200" /></div>
    <div class="child"><img src="http://placehold.it/200x200" /></div>
    <div class="child"><img src="http://placehold.it/200x200" /></div>
    <div class="child"><img src="http://placehold.it/200x200" /></div>
  </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

Eliminate the excess space at the bottom of rows in Materialize CSS

Having difficulty adjusting the padding between two rows in materializecss Troubles with Padding https://i.sstatic.net/KFe1w.png Seeking to eliminate the bottom padding. Managed to remove left and right padding with the following code: <div class="c ...

When a td element is clicked, the Textbox will also be clicked

Similar Question: Dealing with jQuery on() when clicking a div but not its child $(oTrPlanning).prev().children('td').each(function () { this.onclick = setCountClick; }); When a TD element is clicked, the function setCountClick() i ...

The clingy navigation bar hinders smooth scrolling to the content

My website has an image where users can click on elements to scroll down to text. However, I'm facing a problem because there is a sticky menu at the top of my website. How can I ensure that the scrolling works correctly with the sticky menu included? ...

Enhance Material UI BottomNavigationAction by adding a pseudo-element

Trying to enhance a selected item with an additional UI element using a pseudo-element. Specifically, looking to add a line above the menu item. https://i.stack.imgur.com/MZnmw.png The code I've implemented is not displaying the desired line: const ...

What is the method for customizing the default dropdown color of a bootstrap 4 selectpicker?

In my form, there are single and multiple dropdown selects. To keep things simple, I use the selectpicker class for both types. However, I want to customize the default grey color for Bootstrap 4 Dropdown with the selectpicker class to a clear color. Which ...

After clicking, I would like the text from the list item to have a style of "background-color: aqua" when displayed in the textarea

Is there a way to apply a highlight with a style of "background-color: aqua" in the textarea only to the word selected from the list above after clicking on it? See below for my HTML: <head> <script src="https://ajax.googleapis.com/ajax/libs/ ...

Python Selenium tutorial: Navigating inner scrollbars on websites

Struggling to implement scrolling within a container with its own scrollbar. Various methods have been attempted, but none have succeeded or met the desired standard. Here is the HTML code snippet of the scrollbar: <div id="mCSB_2_dragger_vertical ...

Ensure that the Left and Right menu are fixed in position, while allowing the middle content to be scrollable

I'm in the process of creating a web page with a fixed left and right menu, while the middle content should be scrollable and positioned behind the menus. I've attempted to implement this using the following HTML and CSS, but encountered an error ...

Tips for positioning a 404 message at the center of a webpage

Struggling with centering a 404 error message on your Bootstrap 5 page without messing up the footer layout? When the viewport is small, the 404 message may end up getting covered by the footer. Feel free to check out the code snippet in full view to see ...

What is the best way to position a Radio group next to a TextField in Material UI

I've been through the documentation, but I'm struggling to understand how styling works in Material UI. Currently, I have a radio-group component set up like this: import React from 'react' import Radio from '@material-ui/core/Rad ...

Utilize JQuery to implement fading effects for clicked elements in a webpage

I've been using a rollover JavaScript plugin to create smooth transitional effects when users hover over clickable page elements. Everything was going well until I decided to switch to making ajax calls instead of page loads for dynamic content. The p ...

Using Zurb Foundation to enable horizontal scrolling on an iPad

This is my first experience building a website with the Zurb Foundation framework, and so far, it's been a pretty smooth process. However, I'm encountering an issue when testing the site on an iPad in portrait orientation - there seems to be som ...

What is the best way to toggle between different sections of a webpage using HTML, CSS, and Javascript?

I am looking to display unique content based on the user's selection of different month/year options on the same page. For example, if the user chooses March 2021, I would like to show them events specific to that month. Here is a screenshot for refer ...

Avoid Scrolling within an iFrame in HTML with the Use of #

I have a menu loading in an iframe with links using # like http://<location>/page.html#part1. When I click inside the iframe, the entire page outside of the iframe scrolls to the location of #. How can I stop this from happening? I only want the me ...

Struggling to locate the src/site/globals/site.variables file to customize the font-family in Semantic UI React. Is there an alternative method to achieve this?

I've been attempting to modify the overall font-family for my application, but so far, I haven't had any luck. Since I'm working with Semantic-UI-React, I initially thought that could be the issue. However, when I tried to locate src/site/g ...

Display the list at the top using <ul> tag

I am struggling to create a <ul> list with a specific width of 50%. When the text spans more than one line, it is causing an unwanted margin at the top of the second LI element. Please refer to the screenshot linked below for visual clarity. https:/ ...

Adjusting the hue of each fifth div

There are multiple divs in a row, with 4 divs in each row. To adjust the padding of every 4th div, I'm using the nth-child selector. Now, I am looking to modify the rollover property every 5th time. Here is an example of what I have in mind: .cont ...

Leveraging the :checked state in CSS to trigger click actions

Is there a way to revert back to the unchecked or normal state when clicking elsewhere in the window, after using the :checked state to define the action for the clicked event? ...

Steps for placing a menu link at the far right edge of the menu

I'm looking to customize my NAVBAR by adding a Donate Link Menu on the right side, next to the search icon. Can anyone help with this simple task? Thank you in advance! Here is the link to the website: www.clihelp.org Below is the menu code for my N ...

Separate the two divs with some added spacing

I am facing a challenge in creating a navbar with two regions split by white space. I attempted to use float:right and justify-content: space-between, but it doesn't seem to work as expected. My goal is to have site-header-right on the right side and ...