What is the best method for adding space between elements in flexbox layout?

I am struggling to align the child flexboxes within my parent flexbox in a single row, both vertically and horizontally centered. I want to add space between them using the gap property in CSS, but the images and embedded videos inside these child flexboxes don't stretch horizontally as intended.

.latest-news-format {
  background-color: #8ebfa1;
  display: flex;
  justify-content: space-around;
  align-items: stretch;
  gap: 10px;
}

.latest-news-format div {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 30%;
  max-height: 100%;
  border: 1px solid black;
}

.latest-news-format div img {
  /* max-width: 100%; */
  object-fit: cover;
  max-height: 80%;
  max-width: 100%;
  /* aspect-ratio: 16/9; */
}

.latest-news-format div p {
  text-align: center;
  /* This centers the text within each div */
  margin-bottom: auto;
  /* This pushes the text to the bottom of the div */
}

.latest-news-format div iframe {
  /* object-fit: cover; */
  flex-grow: 1;
  flex-shrink: 1;
  aspect-ratio: auto;
  margin: auto;
}
<div style="max-width: 100%; height: 20%; background-color: bisque; display:flex; flex-wrap: wrap; align-content: space-evenly; margin: 20px 0px;">
  <h2>
    Latest News
  </h2>
  <br>

  <div style="margin: 15px 15px;" class="latest-news-format">

    <!--iKON news-->
    <div>
      <a href="https://www.soompi.com/article/1598908wpp/ikons-jinhwan-announces-military-enlistment-date-with-heartfelt-message" title="iKON Jinhwan Enlistment Announcement" target="_blank">
        <img style="object-fit: contain; max-width: 100%;" src="https://picsum.photos/200/300.webp" alt="iKON group photo">
        <p>
          iKON Jinhwan Enlistment Announcement with Heartfelt Letter
        </p>
      </a>
    </div>
    
    <!--SEVENTEEN news-->
    <div>
      <a href="https://www.soompi.com/article/1648474wpp/seventeen-to-headline-lollapalooza-berlin-2024" title="SEVENTEEN Headline at Lollapalooza 2024" target="_blank">
        <img src="https://picsum.photos/200/300.webp" alt="SEVENTEEN group photo">
        <p>SEVENTEEN Perform At Lollapalooza Berlin</p>
      </a>
    </div>
    
    <!--SEVENTEEN watch-->
    <div>
      <iframe src="https://youtu.be/4g6AC6mXShE?si=bxoYJBNyIaxXMNCp"></iframe>
      <a href="https://youtu.be/4g6AC6mXShE?si=bxoYJBNyIaxXMNCp" title="Going SEVENTEEN EP.79" target="_blank">
        <p>
          Watch: Going SEVENTEEN New Episode 79 - Going Vol 2
        </p>
      </a>
    </div>
    
    <!--TAEYONG news-->
    <div>
      <a href="https://www.soompi.com/article/1649391wpp/ncts-taeyong-confirms-military-enlistment-date" title="Taeyong Enlistment Date" target="_blank">
        <img src="https://picsum.photos/200/300.webp" alt="Taeyong picture" title="Taeyong Enlistment Date">
        <p>
          SM Confirms Taeyong Military Enlistment Date
        </p>
      </a>
    </div>

  </div>
</div>

My goal is to have all images/iframes (embedded videos) with equal width, fully stretched horizontally within their respective flexboxes, and equal height. And there should be a visible gap between the child flexboxes.

Answer №1

If you're looking for a more efficient way to create a grid layout, CSS grid is the way to go over flexbox. It will streamline the process of ensuring uniform item sizes and simplify responsive design implementation.

.grid-layout {
  background-color: #8ebfa1;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 1rem;
}

.grid-layout article {
  border: 1px solid black;
}

.grid-layout img,
.grid-layout iframe {
  object-fit: cover;
  width: 100%;
  aspect-ratio: 16/9;
}

.grid-layout p {
  text-align: center;
  padding: 0 1rem;
}
<article>
  <h2>
    Latest Updates
  </h2>
  <div class="grid-layout">
    <!-- Update 1 -->
    <article>
      <a href="#" title="Update 1 Title" target="_blank">
        <img src="#" alt="Update 1 Image">
        <p>
          Update 1 Description
        </p>
      </a>
    </article>
    <article>
      <a href="#" title="Update 2 Title" target="_blank">
        <img src="#" alt="Update 2 Image">
        <p>Update 2 Description</p>
      </a>
    </article>
    <!-- Update 3 -->
    <article>
      <iframe src="#" title="Video Player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture;" allowfullscreen></iframe>
      <a href="#" title="Update 3 Title" target="_blank">
        <p>
          Watch: Update 3 Content
        </p>
      </a>
    </article>
    <!-- Update 4 -->
    <article>
      <a href="#" title="Update 4 Title" target="_blank">
        <img src="#" alt="Update 4 Image" title="Update 4 Title">
        <p>
          Update 4 Description
        </p>
      </a>
    </article>

  </div>
</article>

Responsive Layout:

.grid-layout {
  background-color: #8ebfa1;
  display: grid;
  /* Create responsive grid without media queries */
  grid-template-columns: repeat(auto-fit, minmax(10rem, 1fr));
  gap: 1rem;
}

.grid-layout article {
  border: 1px solid black;
}

.grid-layout img,
.grid-layout iframe {
  object-fit: cover;
  width: 100%;
  aspect-ratio: 16/9;
}

p {
  text-align: center;
  padding: 0 1rem;
}
<article>
  <h2>
    Latest Updates
  </h2>
  <div class="grid-layout">
    <!-- Update 1 -->
    <article>
      <a href="#" title="Update 1 Title" target="_blank">
        <img src="#" alt="Update 1 Image">
        <p>
          Update 1 Description
        </p>
      </a>
    </article>
    <article>
      <a href="#" title="Update 2 Title" target="_blank">
        <img src="#" alt="Update 2 Image">
        <p>Update 2 Description</p>
      </a>
    </article>
    <!-- Update 3 -->
    <article>
      <iframe src="#" title="Video Player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture;" allowfullscreen></iframe>
      <a href="#" title="Update 3 Title" target="_blank">
        <p>
          Watch: Update 3 Content
        </p>
      </a>
    </article>
    <!-- Update 4 -->
    <article>
      <a href="#" title="Update 4 Title" target="_blank">
        <img src="#" alt="Update 4 Image" title="Update 4 Title">
        <p>
          Update 4 Description
        </p>
      </a>
    </article>

  </div>
</article>

Answer №2

Update

I completely agree with @jme11's recommendation to utilize flexbox, although grid would be more effective in this scenario.

To implement the necessary changes in your CSS coding, follow these instructions:

/* Include this new class*/
.latest-news-format div a {
  width: 100%;
}

.latest-news-format div img {
  object-fit: cover;
  height: 240px;
  /* Ensure the p tag accommodates either 2 or 3 lines by specifying a specific height */
  width: 100%;
  /* Adjust width as needed, utilizing width over max-width */
}

.latest-news-format div iframe {
  flex-grow: 1;
  flex-shrink: 1;
  aspect-ratio: auto;
  margin: auto;
  height: 240px;
  width: 100%;
  /* Maintain consistency with img height and width */
}

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

Blog entries alternating between a pair of distinct hues

I want to create a design where each post container has a different color from the one next to it. Essentially, I would like the containers to alternate between two distinct colors. The left side shows how it currently appears, while the right side depict ...

An elementary React project facing compilation issues

I'm currently exploring react hooks, but I encountered an error with the useReducer hook. The console displays the following error message: "Invalid hook call. Hooks can only be called inside of the body of a function component. This could happe ...

Step-by-step guide on inserting an image directly into an HTML file without utilizing LinkedResource or CDO

Let's say we have a scenario: My objective is to put together an HTML file with an embedded image, similar to the structure below: <html> <head> </head> <body> <img src="?" /> </body> </html> The quest ...

Obtaining a background image within a keyframe

I have been experimenting with keyframe animations and am looking to incorporate multiple images into the animation. Specifically, I want to switch out the image every 10% increment to depict someone running. Despite my efforts to use a background-img in ...

Using Conditional Tags for CSS Display

I'm working on implementing Conditional Tags to toggle a CSS class on or off based on the current displayed page. However, I'm running into an issue where the code isn't executing properly. There might be a syntax or logic error causing the ...

Seeking a solution for resizing the Facebook plugin comments (fb-comments) using Angular when the window is resized

Is it possible to dynamically resize a Facebook plugin comment based on the window or browser size? I want the fb-comment div to automatically adjust its size to match the parent element when the browser window is resized. <div id="socialDiv" class="c ...

What is the best method to transfer and incorporate essays and information onto my HTML page?

Here are some unique events and observances for the month of December: DECEMBER 1 World AIDS Day National Pie Day National Eat a Red Apple Day Bifocals at the Monitor Liberation Day Day With(out) Art Day Rosa Parks Day DECEMBER 2 International Day for th ...

New messages are revealed as the chat box scrolls down

Whenever a user opens the chatbox or types a message, I want the scroll bar to automatically move down to show the most recent messages. I came across a solution that seems like it will do the trick: The issue is that despite implementing the provided cod ...

Retrieve Object Key in Angular 7

I have a specific item: this.item = { name:"Michael", age:18 }; I am looking to display it in an HTML format. name: Michael age: 18 <div >{{ item (name) }}</div> --??? should put name <div>{{ item.name }}</div> < ...

Utilizing CSS for a responsive background image

While constructing a website, I encountered an issue where the client is using a mobile device and I want to prevent them from loading a particular background image. For larger screens, my plan is to incorporate approximately 5 images with different resolu ...

Adjusting the height of an element as you scroll will activate the scroll event

One issue I'm facing is with adding a class to my header. The goal is to reduce the height of the top part of the header when the user scrolls down and then remove the class when they scroll back up. While this functionality is working, there is an un ...

How can I implement user-specific changes using Flask?

I am a beginner with Flask and I am working on a project where users can sign up, and if the admin clicks a button next to their name, the user's homepage will change. Below is the Flask code snippet: from flask import Flask, redirect, url_for, render ...

Display an empty string when a value is NULL using jQuery's .append() function

To set an HTML value using the .append() function, I need to include AJAX data values. If the data set contains a null value, it will show as 'null' in the UI. I want to remove that 'null' and display it as blank. However, I can't ...

Bootstrap image with simplistic arrow indicators

I'm facing a minor issue that I need help solving. I want to have two simple arrows (one pointing left and one right) vertically aligned in the center of an image. Additionally, these arrows should resize proportionally when the screen size changes. ...

What is the best way to position an SVG background image at the bottom of a container?

I am facing an issue with using an SVG as a background-image on a pseudo element. The image is not as tall as the container, so I would like to position it at the bottom of the container while having the background color fill up the top portion to create a ...

Preventing AngularJS from Ignoring HTML Elements

Is there a way to calculate HTML content within an AngularJS object (such as {{names}}) that includes an '<a>' element? When I try to display it, the result looks like this: <a href="http://www.example.com">link text</a> I&ap ...

Aligning inline form controls with Bootstrap and Syncfusion widgets

I'm facing a challenge aligning the Syncfusion JavaScript widget - a dropdownlist - to be displayed inline with the label. I am using Bootstrap 3. I haven't been successful in achieving a nice alignment, meaning getting the middle of the label p ...

Using a variable to store the value of the id attribute in HTML code

How can I dynamically add an ID attribute to an HTML element using a variable declared in JavaScript? Using jQuery var table_row = $('table').find('tr#' + pid); var name = table_row.find('td:nth-child(1)').html(); table_ ...

What causes the offsetWidth attribute to be zero in an array of elements within the DOM?

I am encountering an issue with a script that sets widths for certain elements in the Dom. Specifically, when I use the code var elements = document.querySelectorAll("p.caption"), the first 12 elements display the correct offsetWidth, but the remaining hal ...

Start the CSS3 animation in reverse right away

I am trying to achieve a "flashing" effect by adding the .shown class to my #overlay, causing the opacity to fade in for 2 seconds and then immediately reverse, fading out for another 2 seconds. After experimenting with removing the class, I found that it ...