What is the best way to ensure that all rows in flexbox have the same number and dimensions as the first row?

My objective with flexbox was to evenly distribute the text across the width of the page. When I say 'evenly distributed', I mean:

If there are 3 sections, the center of the text in each section should be at fifths of the webpage width.

This is how I managed to accomplish this:

footer {
  display: flex; flex-wrap: wrap;
  color: white;
  padding-left: 100px;
  background-color: grey;
}

footer > * {
  flex-basis: 0px;
  flex-grow: 1;
  padding-right: 50px;
  display: flex; justify-content: center; align-items: center;
}

footer > *:nth-child(1) {
  background-color: red;
}

footer > *:nth-child(2) {
  background-color: green;
}

footer > *:nth-child(3) {
  background-color: blue;
}
<footer>
  <section>
    <div>one.</div>
  </section>
    <section>
    <div>two.</div>
  </section>
    <section>
    <div>three.</div>
  </section>
</footer>

Excellent.

Now, my issue arises when wrapping occurs and the newly wrapped section looks misaligned - the text centers in the middle of the page. (This happens because the flex-grow: 1 property of the section/flex-item causes it to span the page width and applies justify-content center. My current solution?) Introduce a media query, at an estimated vw, to set the flex-item to use justify-content: initial:

footer {
  display: flex; flex-wrap: wrap;
  color: white;
  padding-left: 100px;
  background-color: grey;
}

footer > * {
  flex-basis: 0px;
  flex-grow: 1;
  padding-right: 50px;
  display: flex; justify-content: center; align-items: center;
}

footer > *:nth-child(1) {
  background-color: red;
}

footer > *:nth-child(2) {
  background-color: green;
}

footer > *:nth-child(3) {
  background-color: blue;
}

@media screen and (max-width: 400px) {
  footer > * {
    justify-content: initial;
  }
}
<footer>
  <section>
    <div>one.</div>
  </section>
    <section>
    <div>two.</div>
  </section>
    <section>
    <div>three.</div>
  </section>
</footer>

It's...not ideal. The problem is we lose the justify-content: center property that perfectly distributes text along the width of the page.

But let's get straight to the point: what I really desire is a kind of 'flexible' grid where all rows below the first row have the same number and dimensions of columns as the first row. This would resolve my dilemma.

What have people's experiences been like trying to achieve this?

Answer №1

If you're searching for a solution, I recommend exploring the power of CSS Grid. By defining the structure of a row, you can achieve the desired layout. While I would need more clarity on your specific goals to provide a tailored response, it seems like maintaining consistent row structures is your main query.

Having recently transitioned from using flexbox for webpage design, I urge you to consider adopting CSS Grid for creating the rows and columns you envision, with flexbox serving as a supplementary tool.

Please note that subsequent rows may not inherit the background colors of the initial row due to the CSS targeting only the nth child of the footer.

footer {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  flex-wrap: wrap;
  color: white;
  padding-left: 100px;
  background-color: grey;
}

footer>* {
  flex-basis: 0px;
  flex-grow: 1;
  padding-right: 50px;
  display: flex;
  justify-content: center;
  align-items: center;
}

footer>*:nth-child(1) {
  background-color: red;
}

footer>*:nth-child(2) {
  background-color: green;
}

footer>*:nth-child(3) {
  background-color: blue;
}

@media screen and (max-width: 400px) {
  footer>* {
    justify-content: initial;
  }
}
<footer>
  <section>
    <div>one.</div>
  </section>
  <section>
    <div>two.</div>
  </section>
  <section>
    <div>three.</div>
  </section>
  <section>
    <div>one.</div>
  </section>
  <section>
    <div>two.</div>
  </section>
  <section>
    <div>three.</div>
  </section>
</footer>

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

Experience the captivating AUTOPLAY feature of the mesmerizing FULLSCREEN SLIT SL

I am currently utilizing a slider that is functioning well, however I am encountering an issue with autoplay. Whenever I click on the navigation arrow or Nav dot at the bottom of the slider, the autoplay feature stops working. For more information, please ...

What steps can be taken to implement a code generation feature on our website?

I need to provide the HTML code of my order form to third parties. After they register and pay, they should be able to copy and paste the HTML code from my site onto their own site. The date will then be saved on my site with a reference to the reseller. ...

How can we assign various class names depending on the value of an object?

I have a set of objects stored in my component. I need to dynamically apply different classes based on the value of the ErrorLevel property within each object. If an object has ErrorLevel equal to 1, I want to assign the following classes to various elemen ...

Issues with jQuery functionality occurring when trying to display or hide div contents on dynamically loaded AJAX content

I have encountered an issue while working on a project that involves showing or hiding a div based on the selection of a drop down value. The show/hide functionality works perfectly when implemented on the same page, but it fails when I try to do the same ...

Video with dynamic sizing doesn't adapt properly

I successfully achieved my main goal, which was to have a background video at the top of my page. However, I am facing an issue with making the video responsive. Currently, as I decrease the screen size, the video shrinks in both width and height rather th ...

The issue of nested tabs in Bootstrap 3 causing problems with sliders within the main tab has

Hello, I am utilizing Bootstrap 3 tabs. Within the nav-tabs, there are three tab-panes: the first tab contains text only, the second tab includes a slider, and the third tab has nested tabs. However, I encountered an issue where the slider in the nested t ...

Validating PHP code to ensure that either one of two form fields is filled in

I have been working on a website that utilizes javascript for various functions and PHP-based Captcha code to validate form fields. Everything is functioning well, but the form can still be submitted even if none of the fields are completed. I require at ...

Add space between the first and second rows in the grid gallery display

.gallery{ display: grid; grid-template-columns: repeat( auto-fit, minmax(250px, 1fr) ); grid-template-rows: repeat( auto-fit, minmax(250px, 1fr) ); } view grid image here Could you assist me with identifying the spacing issue between the first and second ...

Dropdown selection returning the text value instead of the designated value

Is it possible to retrieve the text "Region 1" instead of just the value '1' from a dropdown menu in PHP? <select name = 'region' id = 'region'> <option value = '1'>Region 1</option> <select&g ...

Create a centrally located search bar in the header with a stylish button using Bootstrap 5

Struggling with aligning elements on my simple search page. The goal is to center the company name above the search input and have buttons centered under it with some spacing in between. https://i.stack.imgur.com/FdadF.png <div class="backgroundWh ...

Center Button Horizontally and Vertically with CSS Code

I really need some assistance with getting this button I made to be perfectly centered on both the vertical and horizontal axes of the page. So far, I've managed to center it horizontally, but not vertically. Any advice or guidance would be greatly ap ...

What is the method for adjusting the width of a v-card based on its height?

I have a changing number of v-cards that are displayed based on their quantity. My goal is to maintain an aspect ratio of 16/9, but only adjust the width. To achieve this, I believe I need to make the width dependent on the height, although I am unsure ho ...

Unspecified variable in AngularJS data binding with Onsen UI

I am new to Onsen UI and AngularJS, and I have a simple question about data binding. When I use the variable $scope.name with ng-model in an Onsen UI template, it returns as UNDEFINED. Here is my code: <!doctype html> <html lang="en" ng-app="simp ...

Tips on positioning the image to the left of your content instead of above it

Currently, I am following a tutorial on Flask and attempting to include the profile picture in each article displayed on the website. Here is the code snippet used to display an article: <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a ...

Discovering specific keywords within HTML tags and performing replacements using jQuery

I am searching for specific strings within my HTML code, and if I find them, I want to replace them with nothing. For example: HTML: <img src=`javascript:alert("hello ,world!")`> My goal is to locate keywords like javascript, alert, etc, within H ...

Incorporate a local asciinema file into an HTML document

Is there a way to embed a local asciinema session into an html file? Here is how my directory is structured: $ tree . ├── asciinema │ └── demo.cast ├── css │ └── asciinema-player.css ├── index.html ├── js │ ...

Mobile Windows Z-Index

Struggling with the z-index issue on a video tag in Windows Mobile, particularly when it comes to my search box. <div portal:substituteby='common/search::search'></div> The goal is for the search box to appear above the video. ...

The structure becomes disrupted when the Material Ui grid is enclosed within a div container

I currently have a responsive dashboard built with Material Ui's Grid elements. One of the grid items is wrapped in a div element, causing the layout to break. Check out the playground with the div element here: https://codesandbox.io/s/basicgrid-mat ...

Tips for keeping the scroll position of a bootstrap table when reloading the page

Displayed below is a bootstrap table with specific features. <div class="row"> <table id="question-selection" class="table table-sm table-hover table-wrapper"> <tbody> {% for placer in chapter_questions %} ...

The transparency feature using rgba is not supported in the Chrome browser for Android

Have you noticed the transparency effect in certain divs using the rgba CSS property? Strangely, Chrome on my ASUS tablet and Samsung S3 mini Galaxy smartphone does not seem to support this feature. Surprisingly, Internet Explorer does! Any insights on w ...