HTML: Align DIV contents next to each other without any overlap

Below are three boxes displayed. Boxes 1 and 3 appear fine, but in box 2 the text content is overlapping.

The issue lies with the <div> having the class .vertical_center.grade_info which has a specific margin-left of 100px, causing the overlap. I need to adjust the margin for each box individually to prevent this problem. How can I accomplish this?

Here is the current CSS code:

.three_separation {
  width: 100%;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-gap: 60px;
}

.grades_dashboard_box {
  height: 130px;
  width: 320px;
  background-color: #0d0d0d;
  color: #ffffff;
  margin: 0 auto;
  position: relative;
}

.grade_display {
  float: left;
  font-size: 60px;
}

.vertical_center {
  margin: 0;
  position: absolute;
  top: 50%;
  -ms-transform: translateY(-50%);
  transform: translateY(-50%);
}
<div class="three_separation">
  <div class='grades_dashboard_box'>
    <div class="vertical_center">
      <h1 class="grade_display" id="grade_display_best">2.3</h1>
      <div class="vertical_center grade_info" style="margin-left: 100px;">
        <p style="font-size: 15px;">Best Grade Average</p>
        <p id="grade_display_best_sub" style="font-size: 20px;">Biology</p>
      </div>
    </div>
  </div>
  <div class='grades_dashboard_box'>
    <div class="vertical_center">
      <h1 class="grade_display" id="grade_display_average">13.70</h1>
      <div class="vertical_center grade_info" style="margin-left: 100px;">
        <p style="font-size: 15px;">Average Grade</p>
      </div>
    </div>
  </div>
  <div class='grades_dashboard_box'>
    <div class="vertical_center">
      <h1 class="grade_display" id="grade_display_worst">3.4</h1>
      <div class="vertical_center grade_info" style="margin-left: 100px;">
        <p style="font-size: 15px;">Worst Grade Average</p>
        <p id="grade_display_worst_sub" style="font-size: 20px;">German</p>
      </div>
    </div>
  </div>
</div>

Answer №1

If you're looking to create a consistent layout, consider utilizing flex-box in your CSS code. By making the following updates, you can achieve the desired result:

.grade_display {
  font-size: 60px;
}

/* Add this */
.grades_dashboard_box>div {
  /* Implement flex box on the first div of .grades_dashboard_box */
  display: flex;
  /* Set flex layout axis to horizontal */
  flex-direction: row;
  /* Center children vertically */
  align-items: center;
}

/* Include this (replaces inline margin-left style) */
.grades_dashboard_box .grade_info {
  /* Provide extra space to the left of .grade_info */
  margin-left: 40px;
  /* Limit width to allow small text to break into two lines */
  width: 100px;
}


/* Add this */
.grades_dashboard_box h1 {
  /* Adjust h1's default margin for vertical centering */
  margin: 0;
}

.three_separation {
  width: 100%;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-gap: 60px;
}

.grades_dashboard_box {
  height: 130px;
  width: 320px;
  background-color: #0d0d0d;
  color: #ffffff;
  margin: 0 auto;
  position: relative;
  /* Include this */
  display: flex;
  align-items: center;
}
<!-- remove margin-left:100px throughout -->
<div class="three_separation">
  <div class='grades_dashboard_box'>
    <div class="vertical_center">
      <h1 class="grade_display" id="grade_display_best">2.3</h1>
      <div class="vertical_center grade_info">
        <p style="font-size: 15px;">Best Average Grade</p>
        <p id="grade_display_best_sub" style="font-size: 20px;">Biology</p>
      </div>
    </div>
  </div>

  <div class='grades_dashboard_box'>
    <div class="vertical_center">
      <h1 class="grade_display" id="grade_display_average">13.70</h1>
      <div class="vertical_center grade_info">
        <p style="font-size: 15px;">Average Grade</p>
      </div>
    </div>
  </div>

  <div class='grades_dashboard_box'>
    <div class="vertical_center">
      <h1 class="grade_display" id="grade_display_worst">3.4</h1>
      <div class="vertical_center grade_info">
        <p style="font-size: 15px;">Worst Average Grade</p>
        <p id="grade_display_worst_sub" style="font-size: 20px;">German</p>
      </div>
    </div>
  </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

Error: Papa is not defined. The file was loaded from the CDN in the header section

I have integrated the cdn hosted lib for PapaParse in my HTML header. However, when I execute my JavaScript file and it reaches the function where I call Papa.unparse(data); It throws an error stating that Papa is undefined. This has left me puzzled as I h ...

Problem with Bootstrap 5.3 navbar dropdown functionality not functioning as expected

I'm facing an issue with my code. I attempted to use both bootstrap 5.2 and 5.1, but neither seems to be working for me. I've tried opening it in both Chrome and Firefox, but the dropdown functionality is not functioning in either browser. Does a ...

Obtaining data from a hyperlink's attribute within HTML

I have a question that builds upon my previous inquiry: Finding the text of an href tag from an HTML file My current query involves extracting the number within seekVideo(number). For instance: <a href="#" class="transcriptLink" onclick="seekVideo(200 ...

Manipulate the CSS of a single div within a group of divs that have the same class using jQuery

I'm attempting to modify the CSS of a specific DIV that shares its class with other divs. I've searched for a solution but haven't had any luck so far. Here is the code I've been using without success: $(".singleOffer").click(functio ...

Tips for setting up the image as the header background in HTML5 and CSS3

I am currently designing a website. I'm wondering if there is a way to create a div with a curved bottom using HTML5, CSS3 and JavaScript. It should resemble the design of the curved header bottom ...

HTML5 - Adding Space to Main Section with a Two-column Layout

After spending 3 and a half hours racking my brain, I still can't figure out how to complete two seemingly simple tasks. First, I need to pad the outside of paragraph div, and secondly, I want to split the main section into two columns to add some lin ...

Learn the step-by-step process of cropping and zooming an image using the react-image-crop

I am looking to implement zooming and cropping functionality for an image using react-image-crop instead of react-easy-crop. Currently, the cropping does not take into account the zoom level set by ReactCrop's scale property. I want to be able to zo ...

Mysterious gap found between image and table

Hey there! I've been struggling with an unusual gap between a top image and a table on my website (www.tradecaptaincoaching.com). The table was created using . Despite spending hours on it, I can't seem to get rid of the huge 200 pixel gap that& ...

Tips for adding images using v-for in Vue 3

Having some trouble creating a set of images using a v-for loop, as it seems to be not recognizing the variables inside the tag. The picture is broken and the alt attribute just shows me {{ item.alt }}. Here is my HTML: <section class="our-technolo ...

Printing pages with Bootstrap without disrupting cards

My goal is to print a basic bootstrap page using window.print(). Here is what the page looks like (with multiple div.col-md-12 & cards) : <div class="overview"> <!-- Takes up entire screen width --> <div class="row"> <div ...

The method window.getComputedStyle retrieves the CSS max-height property containing an unresolved calc() function

Has anyone encountered a challenge with a function related to Angular in their project? Here is a snippet of the code causing issues: console.log(window.getComputedStyle(this.frontLayer.nativeElement).maxHeight); And within the component template: <di ...

Using the clip-path property to determine the content padding within a div

I am encountering an obstacle while attempting to insert icons into a polygon-shaped div. The problem lies with the padding, as the icons get cut off by the borders of the polygon. #container { width: 200px; height: 200px; border: 2px solid blac ...

What is the best method for saving and accessing a user-entered date using session storage?

https://i.sstatic.net/I8t3k.png function saveDateOfBirth( dob ) { sessionStorage.dateOfBirth = dob; } function getDateOfBirth() { document.getElementById("confirm_dob").textContent = sessionStorage.dateOfBirth; } function pr ...

Ensuring Input Accuracy with jQuery Form Validation

I have implemented a form validation code that is currently functioning correctly. Below is the snippet of the code I used: <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type="text/javascript"></script> <script ...

The items in the footer are not all aligned on a single line

Trying to create a footer with two linked images, I used justify-content: center, but it seems to be centering the images from the start rather than their actual centers. This causes them to appear slightly off to the left. Additionally, the images are sta ...

Discovering the clicked element using JavaScript: A complete guide

Let me start by saying that I have come across similar posts about tracking event listeners, but in my specific case, I am struggling to figure it out. While I am familiar with the event.target property, I just can't seem to make it work. Here is a s ...

Borders appear to be missing in the card-like div layout

Despite using the same color for the outer border and inner borders, I am having trouble getting the inner borders to show up. Can anyone provide guidance on how to display both the inside and outside borders? Note: I have attempted changing the color, bu ...

Modifying the URL of an image based on screen size with the @media property

Currently working on developing a responsive webpage. An interesting challenge is presenting two distinct images for the desktop and mobile views. Attempted to switch between images by utilizing the @media feature within CSS, as shown below. #login-top-s ...

What is the process of importing an HTML template into a Vue.js project?

I downloaded a template from the internet that I want to import into my vue project. This template includes HTML, CSS, and Javascript files of its own. How can I utilize vue-router to access the index.html file in this template without converting them into ...

What is the best way to distribute components with varying cell heights?

I am currently working on creating a user interface layout with components that need to be arranged in a specific order, as depicted in the accompanying image. My task involves developing a Material UI Dialog within a React application based on the design ...