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

What is the best way to load my CSS file using express.static?

How do I properly load my CSS file using express.static in Node.js? I have attempted various methods to link my stylesheet to my HTML file through Express, and I'm also interested in learning how to include images, JavaScript, and other assets to crea ...

Tips for swapping out a sticky element as you scroll

Context As I work on developing a blog website, I aim to integrate a sticky element that dynamically updates according to the current year and month as users scroll through the content. This would provide a visual indication of the timeline for the listed ...

Is there an advantage to pre-rendering a circle on an HTML5 canvas for improved performance?

Is it more efficient to pre-render graphics for a basic shape like a circle on an off-screen canvas? Would there be noticeable improvements in rendering performance when dealing with 100 circles at game-like framerates? What about 50 circles or just 25? ...

Organize object properties based on shared values using JavaScript

Check out the JavaScript code snippet below by visiting this fiddle. var name = ["Ted", "Sarah", "Nancy", "Ted", "Sarah", "Nancy"]; var prodID = [111, 222, 222, 222, 222, 222]; var prodName = ["milk", "juice", "juice", "juice", "juice", "juice ...

When using nextjs, there is an issue that arises when attempting to define the property 'id' using context.params.id. This results in

Attempting to retrieve data from a JSON file (response.json) hosted on localhost:8000/response.json. Below is the code snippet from pages/test.js: import React from "react"; import Link from "next/link"; import Image from "next/ima ...

Adjustable / consistent box height

For hours, I've been attempting to make some divs the same height. Check out my JSfiddle here: https://jsfiddle.net/4cj5LbLs/15/ I experimented with using display: table; in the parent element and either display: table-cell; or display: table-colum ...

Constantly centered div

I'm dealing with a situation like this: .center_position_div{ width:100%; background-color:green; display: flex; flex-wrap: wrap; } .show_running_exam { width: 22%; background-color:aliceblue; margin-left: 1%; margi ...

Expanding the `div` element to visually occupy the entire vertical space

I'm facing a design challenge with my webpage layout. I have a fixed header and footer, but I need the content section to dynamically adjust its height between the two. The goal is to fill the remaining vertical space with a background-image in the co ...

Changing pages in Django with a single click of a line: A step-by-step guide

As a beginner in Django, I am looking to create views that change when a line in an HTML tag is clicked. The idea is to open a new view named after the hash inside the <code>Hash tag. {% load staticfiles %} <!DOCTYPE html> <html lang="fr"&g ...

Deliver real-time notifications to linked users by leveraging socket.io and node.js

Is there a solution for sending real-time updates to connected clients every second without using the setInterval() function in nodejs and socket.io? Please provide an alternative method that fits my specific scenario. ...

Error: Unable to locate module 'next/font/google/target.css'

After setting up Next.js version 14.0.1 on Node.Js v20.9.0 and Ubuntu 20.04, I encountered an error right from the start. Failed to compile /var/www/html/C#/nextApp/app/layout.js Module not found: Can't resolve 'next/font/google/target.css?{&quo ...

Unable to display content when the button is triggered

I am facing an issue with hiding a div (class="login-form") and displaying it only after clicking the Login button on my HTML page using jQuery. However, despite clicking the button, the login form does not appear. Can anyone help me understand why this ha ...

Is there a way to dynamically alter the background style of a div by clicking on it multiple times using Javascript?

I am attempting to create a calendar where each day changes its background color between blue and green when clicked, using JavaScript and CSS. It should function similar to a toggle feature. The default color is blue, and I have successfully made the days ...

typescript is failing to update CSSRule with the newly assigned background-color value

I am currently working on dynamically changing the CSS style (specifically background-color) for certain text on a webpage. To achieve this, I have been attempting to modify CSSRule by accessing the desired CSSRule based on selectedText. Here is the code s ...

Add a Page to Your Domain Name using the Text Input Box

I'm looking to create an input field that allows users to enter a text string, which will be added to a domain name when submitted, redirecting the user to a specific page. Here's how the process works: The user enters 'foo' into the ...

How can I combine an ordinary image and a CSS2D image in THREE.js CSS2DRenderer and save them together as a single .jpg file?

After implementing the following code... (1) I successfully saved regular rendered THREE.js scenes as .jpg files; (2) Additionally, I managed to utilize CSS2DRenderer to display CSS2D labels on top of the canvas; (3) Now, my goal is to save the image wi ...

Incorporating outside content into HTML

Currently, I am working on a project at my job which requires me to create a comprehensive help file with multiple chapters and extensive text all within one large HTML file. However, as the text will need to be translated into different languages, I am lo ...

What is the best way to implement colorful opening hours in code?

I found this code on a different platform and decided to tweak it to display only Monday - Friday, Saturday, and Sunday. However, I'm stuck trying to update the Javascript code to match my modifications. Any help would be appreciated! You can view th ...

Guide to aligning several texts to the right using Bootstrap 4 breadcrumbs

I am looking to implement a breadcrumb using Bootstrap 4.1 that displays the sitemap path on the left and login links on the right. Below is a visual representation of what I aim to achieve: Home/Blog/post Re ...

Is there a way to specifically modify the color of the last digits in a number?

Seeking guidance on changing the color of the last digits of a number if it ends in a zero or more. For instance, transform 0.50 to 0.50, 10.00 to 10.00, 10,000.00 to 10,000.00, 100,050.00 to 100,050.00, and the like. Any assistance in achieving this would ...