I'm having trouble figuring out how to perfectly center this div on all types of devices

As someone who is new to css, I have been struggling to center these divs in the middle of the page across all devices despite searching extensively online and trying various solutions without any success.

Check out my code below:

Snippet:

:after,
:before {
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}

h3 {
    font-family: inherit;
    font-weight: 500;
    line-height: 1.1;
    color: inherit;
}

h3 {
    margin-top: 20px;
    margin-bottom: 10px;
}

h3 {
    font-size: 24px;
}

p {
    margin: 0 0 10px;
}

.djfl-box-col-2,
.djfl-box-col {
    position: relative;
    min-height: 1px;
    padding-right: 15px;
    padding-left: 15px;
}

.djfl-box-col {
    float: left;
}

.djfl-box-col {
    width: 50%;
}

h3 {
    font-family: 'Source Sans Pro', sans-serif;
}

.djfl-box {
    border-radius: 2px;
    position: relative;
    display: block;
    margin-bottom: 20px;
    box-shadow: 0 1px 1px rgba(0, 0, 0, 0.1);
}

.djfl-box>.djfl-box-inner {
    padding: 10px;
}

.djfl-box>.djfl-box-header {
    position: relative;
    text-align: center;
    padding: 3px 0;
    color: #fff;
    color: rgba(255, 255, 255, 0.8);
    display: block;
    z-index: 10;
    background: rgba(0, 0, 0, 0.1);
    text-decoration: none;
}

.djfl-box h3 {
    font-size: 38px;
    font-weight: bold;
    margin: 0 0 10px 0;
    white-space: nowrap;
    padding: 0;
}

.djfl-box h3,
.djfl-box p {
    z-index: 5;
}

.djfl-box:hover {
    text-decoration: none;
    color: #f9f9f9;
}

.djfl-box-col {
    padding: 0px 85px;
}

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

.djfl-box p {
    font-size: 12px;
}

.bg-green {
    color: #fff !important;
}

.bg-green {
    background-color: #00a65a !important;
}
<div class="djfl-box-col-2 djfl-box-col">
  <div class="djfl-box bg-green">
      <div class="djfl-box-header">Trees Planted</div>
      <div class="djfl-box-inner">
          <h3>12,690,517</h3>
          <p>Trees</p>
      </div>
  </div>
</div>

I would greatly appreciate any assistance. Thank you.

Answer №1

Adjust the styling of the 'djfl-box-col' class by removing the float left and adding an auto margin:

:after,
:before {
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}

h3 {
    font-family: inherit;
    font-weight: 500;
    line-height: 1.1;
    color: inherit;
}

h3 {
    margin-top: 20px;
    margin-bottom: 10px;
}

h3 {
    font-size: 24px;
}

p {
    margin: 0 0 10px;
}

.djfl-box-col-2,
.djfl-box-col {
    position: relative;
    min-height: 1px;
    padding-right: 15px;
    padding-left: 15px;
}

.djfl-box-col {
    margin: 0 auto;
}

.djfl-box-col {
    width: 50%;
}

h3 {
    font-family: 'Source Sans Pro', sans-serif;
}

.djfl-box {
    border-radius: 2px;
    position: relative;
    display: block;
    margin-bottom: 20px;
    box-shadow: 0 1px 1px rgba(0, 0, 0, 0.1);
}

.djfl-box>.djfl-box-inner {
    padding: 10px;
}

.djfl-box>.djfl-box-header {
    position: relative;
    text-align: center;
    padding: 3px 0;
    color: #fff;
    color: rgba(255, 255, 255, 0.8);
    display: block;
    z-index: 10;
    background: rgba(0, 0, 0, 0.1);
    text-decoration: none;
}

.djfl-box h3 {
    font-size: 38px;
    font-weight: bold;
    margin: 0 0 10px 0;
    white-space: nowrap;
    padding: 0;
}

.djfl-box h3,
.djfl-box p {
    z-index: 5;
}

.djfl-box:hover {
    text-decoration: none;
    color: #f9f9f9;
}

.djfl-box-col {
    padding: 0px 85px;
}

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

.djfl-box p {
    font-size: 12px;
}

.bg-green {
    color: #fff !important;
}

.bg-green {
    background-color: #00a65a !important;
}
<div class="djfl-box-col-2 djfl-box-col">
  <div class="djfl-box bg-green">
      <div class="djfl-box-header">Trees Planted</div>
      <div class="djfl-box-inner">
          <h3>12,690,517</h3>
          <p>Trees</p>
      </div>
  </div>
</div>
Documentation:

Visit this link for more information on CSS alignment.

Answer №2

Consider removing all padding and wrapping everything in an outer div. Eliminate any unnecessary position properties and instead, give the outer div a width in percentage with a margin auto property.

.outer-wrapper{
      width: 50%;
      margin: auto;
   }

You seem to have a lot of unused styles. I've cleaned them up in this code snippet, which may be helpful for you.

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

Developer server experiencing CSS issues in Django admin interface

Experiencing some odd issues with the Django admin application here. I've got everything up and running on the manage.py runserver development server, but for some reason it looks like this: This obviously isn't what I want, so I'm trying t ...

JQuery Menu with Broken UL Formatting on Windows versions of IE and Firefox

Hey there! I've been struggling with a menu design that works perfectly on Mac browsers but is completely broken on Windows. I've spent hours trying to fix it and would really appreciate if a kind programmer could take a look and help me out. The ...

Is it possible to integrate a standard drop-down menu/style into a MUI Select component?

I am currently working on implementing the default drop-down menu style for my MUI Select Component. Here is the desired menu appearance: https://i.stack.imgur.com/GqfSM.png However, this is what my current Select Component looks like: https://i.stack. ...

How to vertically align Bootstrap5 buttons on mobile devices

I'm currently developing a web application and I'm facing an issue with centering two bootstrap buttons next to each other on mobile devices. While it works fine on the computer, when I inspect the page on a mobile device, the buttons stack verti ...

Monochrome Effect Triggered by Cursor Hover

I'm attempting to use Javascript with HTML5 canvas to convert an image to greyscale, but I seem to be missing something in my code. Can anyone spot the error? I feel like I'm very close! function grayscaleConversion(str) { // Access the Canv ...

Steps for disabling automatic formatting in Visual Studio Code Version 1.35.1 with system setup

I am currently utilizing visual studio code for my Node JS application with mustache templating. In one of my HTML files, I have the following code: <script> {{{interface}}} </script> After the automatic formatting on save, it appears like t ...

Image Positioned Outside Border in HTML

I'm working on creating a personalized homepage for my browser and I want to display 3 images with captions evenly spaced within a divider. Although everything seems to be in place, the border of the outermost divider is not covering the images. How c ...

Do custom Bootstrap 4 variables fail to cascade downwards?

Discovering a strange behavior in the custom.scss file of Bootstrap, I realized that setting a custom color and removing the !default flag doesn't cascade down as expected. For example, updating $blue to #000 without !default in _custom.scss should id ...

Creating a sleek grayscale effect when hovering with CSS is both simple and effective

My website features a grayscale logo that smoothly transitions to color upon hover. However, I am experiencing some issues with the smoothness of this transition when using CSS. Here is my current code: <img alt="TT ltd logo" src="./img/tt-logo.png" c ...

CSS flexbox completely collapses all child elements that are empty

Is there a way to collapse the empty "col" divs in this html structure while keeping the content divs equally sized (two columns in this case)? * { box-sizing: border-box; } .container { border: 1px dashed rgba(255, 0, 0, .5); background-color: b ...

Update the content on the webpage to display the SQL data generated by selecting options from various dropdown

My database table is structured like this: Name │ Favorite Color │ Age │ Pet ────────┼────────────────┼───────┼─────── Rupert │ Green │ 21 │ ...

Tips on creating a responsive absolute div

I'm currently working on creating a profile component with Material UI and React.js. I'm having trouble making the Avatar/profile photo and profile name div responsive. Here are some screenshots to illustrate my issue: The specific div that need ...

Is it considered best practice to utilize iframes for the implementation of a header or navbar on a

Is it considered a best practice to utilize iframes for implementing a header/navbar? My website consists of approximately 5 thousand pages, all of which are static HTML (without the use of any content management system, PHP, etc). I am currently in the ...

HTML: Ensure that a user fills out a minimum of one text box before submission

<tr><td> First Name: </td><td><input type="text" name="FirstName" required></td> </tr> <tr><td> Last Name: </td><td><input type="text" name="LastName" required> </td></tr> ...

Having difficulty finding the close button in a pop-up window while using the Selenium library with Python

While using chromedriver to open the website mentioned below in my extract_data() function, I encountered a problem with dismissing a pop-up message by clicking the 'x' button. Surprisingly, it seems to click the wrong button instead. What' ...

Creating equal-sized borders for symbols of varying sizes: a step-by-step guide

Utilizing Font Awesome icons along with their fa-border style: <i class="fa fa-twitter fa-5x fa-border icon-blue"></i> <i class="fa fa-question fa-5x fa-border icon-grey"></i> The border size created varies based on the symbol siz ...

What is the best way to target specific text within the DOM without including text within attributes?

We are currently displaying search results from various posts on our website, and we would like to highlight the search terms in the displayed content. Currently, we are implementing this functionality on the backend using PHP. We iterate through the post ...

The images on my website are not displaying properly on different devices

My attempt at creating a website for fun and testing purposes is facing issues when viewed on mobile devices. The images and specially positioned elements are not displaying correctly, extending beyond the page boundaries. I have tried adding the viewport ...

How can I eliminate the error message "WARNING: no homepage content found in homepage.md"?

Starting my journey with KSS utilizing Keith Grant's CSS in Depth as a guide. In section 10.1.4, the author suggests: To begin, create a new markdown file named homepage.md inside the css directory. This file will serve as an introduction to the patt ...

Create a 2x2 HTML table where the first row remains undivided

I am working with an HTML table that is 2x2. Within this table, I am trying to achieve a layout where the first row consists of only one cell that spans the full width of the table, without being divided into two parts. ------- | | ------- | | | -- ...