What is the best way to ensure uniformity in my boxes (addressing issues with whitespace and box-sizing using flexbox)?

Below is the code I have written:

  body {
  padding-top: 20px;
  text-align: center;
  background-color:black;
}

h1 {
  display: inline-block;
  background: white;
}

h1 {
  font-size: 30px
}

p {
  background: yellow;
}

.container {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
}

.content {
  width: 20%;
  background-color: red;
  padding: 20px; margin:10px;
  }
<div class="container">
<div class="content">
  <h1>Box 1</h1>
  <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</p>
</div>

<div class="content">
  <h1>Box 2</h1>
  <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</p>
</div>

<div class="content">
  <h1>Box 3</h1>
  <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.  
</p>
</div>

<div class="content">
  <h1>Box 4</h1>
  <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. 
</p>
</div>

<div class="content">
  <h1>Box 5</h1>
  <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
</div>

</div>

I am facing two issues with my code:

The height of the boxes is uneven: Why do the box heights on the second row not match those of the first row? I expected the flex-wrap property to make all wrapped rows have equal heights based on the tallest box (i.e., Box 3). How can I modify the code to ensure that all boxes are the same size as the largest box, regardless of wrapping?

The text box heights are inconsistent: To enhance the visual appeal, I want the yellow background of the text to be consistent across all boxes. This means I would like to extend the yellow space below Box 1 and 2 so that the yellow boxes are as tall as Box 3. What changes should I make to achieve this?

Answer №1

Make sure the red boxes are always in a single row as there are 5 of them with a width of 20% each (5 x 20% = 100%). The reason they stack vertically in your design is due to the use of margin. I suggest wrapping each box in a <div class="box"> and applying the following CSS:

.box {
  box-sizing: border-box; // adjusts padding within width
  width: 20%;
  padding: 10px;
  display: flex;
  flex-direction: column;
 }

Utilize flexbox for the remaining layout:

h1 {
  display: inline-block;
  background: white;
  font-size: 30px
}

.text-center {text-align: center;}

.container {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
}

.box {
  box-sizing: border-box;
  width: 20%;
  padding: 10px;
  display: flex;
  flex-direction: column;
}

.content {
  background-color: red;
  display: flex;
  flex-direction: column;
  flex: 1;
}

.box p {
  background: yellow;
  margin: 0;
  display: flex;
  flex: 1;
}

@media (max-width: 640px) {
  .box {
    width: 100%;
  }
}
<div class="container">
  <div class="box">
    <div class="content">
      <div class="text-center">
        <h1>Box 1</h1>
      </div>
      <p>Content inside Box 1.</p>
    </div>
  </div>

  <div class="box">
    <div class="content">
      <div class="text-center">
        <h1>Box 2</h1>
      </div>
      <p>Content inside Box 2.</p>
    </div>
  </div>

  <div class="box">
    <div class="content">
      <div class="text-center">
        <h1>Box 3</h1>
      </div>
      <p>Content inside Box 3.</p>
    </div>
  </div>

  <div class="box">
    <div class="content">
      <div class="text-center">
        <h1>Box 4</h1>
      </div>
      <p>Content inside Box 4.</p>
    </div>
  </div>

  <div class="box">
    <div class="content">
      <div class="text-center">
        <h1>Box 5</h1>
      </div>
      <p>Content inside Box 5.</p>
    <br>

Answer №2

When using flex in your layout, it sets the height row by row.

If you are facing issues with this behavior, you can consider using jquery to solve the problem.

Alternatively, if you prefer not to use jquery, you can disregard this solution.

Below is a jquery solution along with CSS modifications for creating a yellow box effect:

$( document ).ready(function() {
var array = new Array();
    $(".container").children(".content").each(function(){
    array.push($(this).height());
    });
    var maxHeight = Math.max.apply(Math,array);
    $(".content").height(maxHeight + "px");
});
  body {
  padding-top: 20px;
  text-align: center;
  background-color:black;
}

h1 {
  display: inline-block;
  background: white;
}

h1 {
  font-size: 30px
}

p {
  background: yellow;
  height: 80%; // this is making all the yellow containers to be same height
}

.container {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
}

.content {
  width: 20%;
  background-color: red;
  padding: 20px; margin:10px;
  }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
<div class="content">
  <h1>Box 1</h1>
  <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</p>
</div>

<div class="content">
  <h1>Box 2</h1>
  <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</p>
</div>

<div class="content">
  <h1>Box 3</h1>
  <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.  
</p>
</div>

<div class="content">
  <h1>Box 4</h1>
  <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. 
</p>
</div>

<div class="content">
  <h1>Box 5</h1>
  <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
</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

How can I align a button in the center using Bootstrap 4?

<div class="container"> <div class="row"> <div class="col-xs-12 col-sm-12 col-md-8"> <div v-for="job in job"> <div class="text-center"> <h1>{{ job.job_title }}</h1> <p> ...

Incorporating video responses into an HTML player using Node.js with the req.pipe(res) method

I have successfully implemented ytdl-core to play a video and it is working as expected. Below is the code snippet: app.get('/',function(req,res) { var fs = require('fs'); var ytdl = require('ytdl-core'); ...

The method for storing a user's choice in my array and subsequently selecting an element at random

Seeking assistance in developing a program that can play an audio list at various durations. The plan is to have the duration for elements 4 through 8 based on user selection, with the program playing each element within that range during the initial round ...

Can a single volume control be used to manage the audio of multiple sources at once?

I am currently working on a project for my personal interactive video website. I am trying to figure out how to create one volume control that can adjust the audio for multiple tracks at once. Can anyone help me with this? So far, I have successfully crea ...

Is it possible to conceal an arrow in a typical dropdown menu?

Is it possible to conceal the arrow in a standard dropdown select fieldset? Check out this Fiddle for more information I am working on an autocomplete feature where users input an organization number and relevant information is fetched from a database. I ...

Guide for displaying SQL data in an HTML table

I am currently working on transferring my SQL data to an HTML format. The code I have so far is not functioning as expected. I attempted to include HTML within my PHP code, and now I am considering if it would be better to do it the other way around (emb ...

In Safari, the scrollbar appears on top of any overlays, popups, and modals

On my webpage, I have a div with the CSS property overflow-y: scroll. The page also features several popup modals and overlays. Strangely, the scrollbar of the div appears on top of these overlays instead of behind them. I attempted to resolve this issue b ...

Designing an interactive HTML table that adapts to various screen

Currently utilizing Bootstrap to create a responsive HTML table on smaller devices like the iPad, but seeking a more polished and professional alternative. Searching for a JQuery/JavaScript or CSS solution without relying on plugins. Would appreciate any ...

Top technique for mirroring a website

Recently, I created a directory for a client's website. The site is fully operational, but the client would like a replicated version for testing and learning purposes. To create a full replica of his website, it will require hours of work to change ...

position an element at a higher level than the #top-layer

I developed a custom tool and utilized z-index to position it above all other elements: .customTool { position: fixed; z-index: 99999; } However, this functionality stops working when a modal <dialog /> element is opened in modal mode. As a res ...

Checkbox customization that shifts position when clicked

I seem to be missing something as I can't seem to find a solution for this particular issue. I have implemented custom checkboxes on a list of sentences. Initially, they appear fine, but once you check the first checkbox, it shifts its position. I am ...

"Is an Ionic Top Navigation Bar the best choice for your

Creating an Ionic Top Navigation Bar Greetings! I am facing some challenges while trying to implement a Top Navigation Bar in my Ionic Project. Despite the lack of documentation on this specific topic, I am struggling to make it work properly. Your as ...

Failed Ajax request is caused by an incorrect URL

In my current project using Ajax, I encountered a peculiar issue. The main page, Main.html, is accessible locally at 'http://localhost/Main.html', and it is styled with Bootstrap. Upon loading the page, jQuery verifies the existence of a particu ...

Vue.js - Problem with loading image on screen

Although I have experience with React, I am currently facing the challenge of using Vue for a code assessment for the first time. My struggle lies in creating a reusable image component with WAI-ARIA accessibility. Despite my efforts, I cannot get the imag ...

Enhancing Visuals within XSL+XML Documents

Currently, I am working on a project for one of my classes that involves showcasing film information loaded from an XML document. While researching similar questions, I found the answers to be confusing and the solutions did not yield any results. My goal ...

Three divs arranged side by side with responsive design for mobile viewing

This code generates 3 divs for display. However, I am looking to optimize them for mobile devices. What steps can I take to accomplish this? Currently, the divs are oriented to the left and are not visible on a mobile device. I would like them to display ...

Tips for transmitting dropdown selections to a different PHP form using jQuery

After spending a considerable amount of time searching for answers online, I realized that I need some help. Despite finding helpful articles and resources on jQuery, I am still struggling to understand certain concepts. As a beginner in this field, I am w ...

Access an attribute using slashes in Jquery

I've been struggling to use jQuery to select an object based on a unique filename attribute. However, I'm facing issues with escaping slashes when the selector is created using a variable. Despite spending hours trying different combinations, I s ...

Inject the HTML code into a bash script and perform operations on it

Could someone help me with a bash scripting question? I'm new to this, so I may have missed it if it's already been answered. I want to pass the html source code of a web page to my script, which will then modify or scrape the web page of its HT ...

How to apply dynamic styling to a MatHeaderCell using NgStyle?

My goal is to dynamically style a MatHeaderCell instance using the following code: [ngStyle]="styleHeaderCell(c)" Check out my demo here. After examining, I noticed that: styleHeaderCell(c) It receives the column and returns an object, however ...