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

AInspector WCAG evaluation found that mat-select does not meet level A compliance standards

As I work on making my website WCAG level A compliant, I've encountered an issue with the mat-select component in Angular material. After running checks with the AInspector extension for Firefox, it appears that the mat-select component lacks aria-con ...

How can I modify the color scheme of radio buttons to include a variety of different hues?

Is there a way to customize the colors of my radio buttons with three different options? I want numbers 1, 2, and 3 to be red, number 4 to be orange, and number 5 to be green... Here is the code I am using: /* Option 1 */ input[type='radio'] { ...

A guide on automating the html5 color picker using input type="color" in selenium webdriver

When interacting with an HTML color input element, a color picker window pops up that prevents viewing the DOM. I am looking for a solution to either select a color and close the window or enter a hex code in the popup's text box. Can anyone provide m ...

Developing an interactive URL in an HTML page for a Flask web application

I'm attempting to create dynamic URLs in HTML and route all "orders/" to a Flask app. The hyperlink on the browser should display as a link to name, but it should actually route as "orders/". I've tried numerous approaches ...

My goal is to have the "show more" button reveal extra information without having to reload the entire page

I’m trying to figure out how to make the “more” button show additional information without reloading the entire page. I’ve done some research online and found that AJAX can help with this, but since I’m just starting to learn JavaScript, I have n ...

What is the best way to save JavaScript array information in a database?

Currently working on developing an ecommerce website. My goal is to have two select tags, where the options in the second tag are dynamically populated based on the selection made in the first tag. For example, if "Electronics" is chosen in the first tag f ...

Why won't my Java servlet code execute?

included the directory structure To facilitate adding two numbers and displaying the result upon clicking a submit button, I developed a straightforward Java servlet. This servlet retrieves the necessary information when called. The development environmen ...

Troubleshooting a text alignment problem with form-group in the Bootstrap framework

Currently, I am in the process of building a form that consists of several form-group elements. My main issue arises when dealing with long text as it causes alignment problems. Could you kindly take a look at the problem through this link: http://jsfidd ...

"Looking for a way to eliminate the background of an image on NextJS? Learn

https://i.stack.imgur.com/zAsrJ.png When this image is incorporated into a Nextjs rendering, it unexpectedly includes additional content. <div className="flex flex-col items-start justify-start rounded-3xl p-4 bg-boxi w-1/3"> <div cla ...

css boxShadow merger

Looking to create a sleek horizontal navigation bar using an unordered list with list items representing each element: To ensure the elements fit perfectly within the bar, I set the width of each at 25% and added a box-shadow for a border. Using a traditi ...

align items center with respect to my central element

Describing my issue might be a bit complex, but I'll give it a shot. I'm working on a website and have 3 div elements in the header for my navigation bar (burger menu / logo / social networks). I've used flex display with justify-content: be ...

`Make adjustments to the visual representation of crispy forms`

When using crispy forms, the labels for CheckboxSelectMultiple() fields are populated from the default string representation of objects defined in a list. To change this behavior, I am seeking assistance. Is it possible to create a custom string represent ...

Tips for resolving conflicts between CSS files

My index.html file contains multiple css and js files, including MaterializeCSS and a style.css file. However, when both are included simultaneously, using elements from Materialize such as tabs results in them not appearing correctly. Despite initializing ...

What is the best way to make a multi-column image responsive in amp-html code?

Here is the concept I was referring to, you can see it in action by following this link <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> * { box-sizing: border-bo ...

displaying the identical image from various perspectives

I have an arrow image that I need to display in different angles - top, left, bottom, right. I was considering what would be the best approach: 1: Changing the image direction via CSS and loading the main image <img src="a.png"> <img src="a.png" ...

Counting words with JavaScript without using an input tag is a useful skill

Is there a way to count the words in a text using only p and span tags, without using input tags? How can this be achieved? <span class="word" id="word">Words Number</span> <p class="any" id="any"> ...

Remove the boundaries from the grid and only retain the box

I am not skilled in css, but I simply want to eliminate the interior edges of the box and retain only the outer edges. .grid-container { display: grid; grid-template-columns: auto auto auto; background-color: #2196F3; padding: 10px; } .grid-ite ...

How do I use CSS to organize data by row and column within a table?

I'm looking to format the table data in a column-row layout for mobile devices. Can anyone provide an example or guidance on how to achieve this? I've browsed through some discussions but haven't found a solution that works with tables. The ...

When examining two arrays for similarities

I am dealing with two arrays within my component arr1 = ["one", "two"] arr2 = ["one", "two"] Within my HTML, I am utilizing ngIf in the following manner *ngIf="!isEnabled && arr1 != arr2" The isEnabled condition functions as expected, however ...

The active link color for navigation in Bootstrap 4

I am trying to update the active menu links to display in green on my website. Despite various attempts, I have not been successful in changing the color. Can anyone provide guidance on how to proceed? My website is built with Bootstrap 4 and mdbootstrap. ...