Issue with Flexbox alignment: Second row does not line up with first row

When dealing with a flex container that has 4 items and you want 3 per row while keeping the container centered, it can be tricky. Using justify-content to center the container may result in the fourth item on the second row also being centered. To address this issue, some CSS tricks like using ::after pseudo-element have been attempted, but they may lead to misalignment between rows.

The root of the problem seems to be the width of the flex container surpassing the content items' width, prompting the need to center it with justify-content, causing alignment issues.

Is there an easier way to achieve this layout using CSS grid? If so, how can we create a simple grid layout of iframe YouTube videos, with 3 videos per row and the last row aligned to the left?

Check out this code snippet for reference.

.section-vids ul {
  list-style: none;
  margin:0;
  padding:0;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
}

.section-vids ul li {
  padding: 20px;
}

.section-vids ul:after {
  content: "";
  flex: auto;
}
<div class="container" id="media">
      <section class="section-vids">
        <ul>
          <li><iframe width="560" height="315" src="https://www.youtube.com/embed/G1FFWgzk4vI" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></li>
                    <li><iframe width="560" height="315" src="https://www.youtube.com/embed/G1FFWgzk4vI" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></li>
                    <li><iframe width="560" height="315" src="https://www.youtube.com/embed/G1FFWgzk4vI" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></li>
                    <li><iframe width="560" height="315" src="https://www.youtube.com/embed/G1FFWgzk4vI" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></li>
        </ul>
    </section>
</div>

Answer №1

center-align: middle; doesn't position the container in the center, it actually centers the elements within the container. If you take out that line from your CSS for the ul, you'll see exactly what you're trying to achieve. Another option is to use center-align: space-between; or center-align: space-evenly; for a similar effect.

If you're experiencing horizontal overflow issues (which shouldn't be happening), you can add max-width: 100%; to the ul which should fix the problem. The use of the ::after pseudo element is unnecessary in this case.

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

Tips for making a circle and a bar overlap using CSS

Trying to create a circular image and a horizontal bar of equal height next to it in a user profile. It needs to be responsive and look like the image below, with text in the black bar. Looking for help with CSS to achieve this: I currently have the cod ...

Creating a HTML5 canvas animation of an emoticon winking to add a fun touch to your

Currently, I am facing a challenge in animating an emoticon that was initially sketched on canvas. While following a tutorial to implement draw and clear animations using frames, I haven't been able to achieve the desired outcome. With 6 frames of the ...

What is the best way to create a shape using CSS?

In my chat application, I have a functionality where replies on a chat are initially hidden and represented by a count in a red box. When the user clicks on the red button, the replies from other users are revealed along with an input box for writing a r ...

CSS smoothly scrolls upward within a set height restriction

Is there a way to use CSS to ensure that the scroll bar of a fixed-height message box is always at the bottom, displaying the latest entry? Currently, as more messages populate the chat box main section, everything inside remains static. The only way to v ...

The CORS policy is preventing access to the file "sample.txt" at 'file:///sample.txt' because it is not supported for protocol schemes originating from 'null'

I am a beginner in the world of AJAX and currently exploring its fundamental concepts. In my HTML file, when I click the submit button, my intention is to retrieve and log the text from a text file located in the same directory as the HTML file. However, I ...

Tips on displaying email addresses on your website to prevent them from being spammed

Here is how I display email on my website: <a href="mailto:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7d14131b3d18051c100d1118531e1210">[email protected]</a>">Email</a> After analyzing my website ...

Creating consistent height for elements in different columns using Bootstrap 5

I'm struggling with keeping the ".item" elements equal height in different columns. Does anyone have a solution for this? <div class="container"> <div class="row"> <div class="col-8"> <h3& ...

How to delete a specific section of HTML code while retaining the styling using Python

I need help figuring out how to remove a specific portion of an HTML code that contains a certain string. The section I want to remove includes a person's name, and I want to delete the entire tag to maintain anonymity. Here is the HTML snippet: < ...

How to deactivate the dropdown hover effect in ngx-intl-tel-input

Issue: When hovering over the dropdown items in the country list, I'm encountering a gray background color that I'd like to remove while still maintaining the functionality of the dropdown. Despite attempting to set background-color: transparent ...

Having trouble converting Node.js image to base64 format accurately

While reading a file and saving it into a variable, I create a buffer from the variable and convert it to a base64 string in order to display it within an <img> element. However, after generating it, the output doesn't resemble a base64 string a ...

Creating a Select Directory feature with React - A Step-by-Step Guide

I am in need of uploading all files from a folder to the server. I have been attempting to use a Select Directory window instead of selecting individual files. The standard method: <input type="file" webkitdirectory directory/> has not worked for ...

Building a bespoke search input for the DataTables JQuery extension

As the title indicates, I am in the process of developing a custom search box for the DataTables JQuery plugin. The default options do not suit my needs, and configuring the DOM is also not ideal as I aim to integrate it within a table row for display purp ...

Guide to creating scroll-based animations for a Div element

I've been brainstorming ways to rotate a div as I scroll. My goal is to recreate the effect shown in this codepen. However, I'm struggling to implement scrolling functionality. What I envision is scrolling down causing the word Data to rotate in ...

Manipulate DIV elements with jQuery by selecting them based on their class names and then

Is there a way to use jQuery to eliminate all DIVs on a webpage while preserving their content that have the following specific pattern? <div class="ExternalClass85AC900AB26A481DBDC8ADAE7A02F039">....</div> Please note that these DIVs adhere ...

Utilize Bootstrap rows and columns to extend the column color to the bottom of the page

I'm having trouble getting the background color of a bootstrap col to extend to the bottom of the page without setting a fixed height. I want it to reach all the way to the bottom of the browser screen. HTML: <div class="container"> <div ...

Django: Limit available selections based on preceding model choices made

I am managing multiple models: class Game(models.Model): name = models.CharField('Nom du jeu', max_length=100) logo = models.ImageField('Logo', blank=True, upload_to='game_logos') class League(models.Model): game ...

Incorporate a dynamic form into an ngx-sortable control in Angular 6

Having difficulty with the layout implementation in Angular 6. A form component is dynamically added during runtime. Using ngx-sortable, I aim to have dynamic content within it, but am facing challenges with the setup. Implementing Sortable control: &l ...

Container Floating, Margin Collapse, and Block Formatting Contexts

To truly grasp the function of a block formatting context, I am exploring what happens when one is not created. I found an interesting demo in the article "Everything you Know about Clearfix is Wrong: .wrapper { width: 740px; background: #ccccc ...

The integration of CSS into an HTML file is ineffective

I'm new to learning about html and css files, and I've been struggling with a particular issue for some time. I created these files, but I can't seem to apply the css to the html file: app.py from flask import Flask from flask import rende ...

Guide to uploading a PDF to Google Drive and embedding it on an HTML static website

I manage a static HTML site for a small food shop. They require monthly menu uploads in PDF format. I believe uploading to Google Drive would be a more efficient solution than creating a separate admin view for file uploads. Can anyone advise me on how to ...