Aligning a dynamic amount of divs within a container

Within my Bootstrap container, I have 4 divs that are dynamically rendered and centered on the page. However, there are instances where only 3 divs are rendered, causing the remaining content within the container to float left due to the float tag in the CSS. How can I ensure that these 3 paragraphs remain centered instead of floating left?

<div class="expertise_section text-center">
<div class="row">
<div class="container">
<h2>Centered Headline</h2>
<div class="expertise_section_inner row">
</div>

...

The provided CSS is as follows:

@charset "utf-8";

.expertise_section {
    padding: 85px 0;
}
.expertise_section h2 {
    font-weight:500;
    margin-top:7px;
}
.expertise_div {  
  float: left;
    width: 24.2%;
    margin-right:1.05%;
}

...

You can view a working example on jsfiddle here.

Answer №1

Include the following CSS code:

.container {
    text-align: center;
}

Remove the float: left; property from all elements. Instead, set the display property of divs to inline-block for .expertise_div - this will ensure that they are always centered as inline elements within the container. Additionally, refine the margins of the elements based on different viewport sizes to accommodate varying numbers of elements. Consider using Flex Box Layout as an alternative to simplify handling margins and edge cases.

@charset "utf-8";
.container {
   text-align: center;
}
.expertise_section {
    padding: 85px 0;
}
.expertise_section h2 {
    font-weight:500;
    margin-top:7px;
}
.expertise_div {  
    display: inline-block;
    width: 24.2%;
    margin-right:1.05%;
}
.expertise_div:nth-child(4n+4) {
    margin-right:0px;
}
.expertise_div p {
    font-family: 'Open Sans', sans-serif;
    color: #6c6c6c;
    font-weight: 300;
}
.expertise_content {
    border-bottom: 2px solid #45acba;
    border-top: 2px solid #45acba;
    margin: 18px 0;
    padding: 26px 0;
}
.expertise_content p:last-child {
    margin-bottom:2px;
}
.expertise_div a {
  color: #3a9cab;
  font-size: 14px;
  font-weight: 500;
}
.expertise_section_inner {
    margin-top:36px;
}
.green_div .expertise_content {
    border-color:#8ebb29;
}
.green_div a {
    color:#8ebb29;
}
.maroon_div .expertise_content {
    border-color:#81515d;
}
.maroon_div a {
    color:#81515d;
}
.orange_div .expertise_content {
    border-color:#d86435;
}
.orange_div a {
    color:#d86435;
}
.show_1023 {
    display:none;
}

@media only screen and (max-width: 1199px) {
    .container {
        padding:0px 35px !important;
    }
}
@media only screen and (max-width: 1023px) {
    .expertise_div {
        margin-right: 3.2%;
        width: 48%;
        margin-bottom:53px;
    }
    .expertise_div:nth-child(2n+2) {
        margin-right: 0;
    }
    .expertise_section {
        padding: 85px 0 30px;
    }
    .expertise_section_inner {
        padding: 0 101px;
    }
    header {
        padding: 20px 15px;
    }
}

@media only screen and (max-width: 951px) {
  .container {
        padding: 0 30px !important;
    text-align: center;
    }
}

@media only screen and (max-width: 767px) {
    .container {
        padding: 0 30px !important;
    ...

<div class="row">
<div class="container">
<h2>Centered Headline</h2>
<div class="expertise_section_inner row">
</div>

<!-- ***** if the below is removed, the remaining 3 divs do not center correctly -->
<div class="expertise_div blue_div" >
<div class="expertise_content">
<p>Paragraph 1...</p>
</div>
<a href="https://www.jsfiddle.net" target="_blank">READ MORE</a></div>

<div class="expertise_div green_div" >
<div class="expertise_content">
<p>Paragraph 2...</p>
</div>
<a href="https://www.jsfiddle.net" target="_blank">READ MORE</a></div>

<div class="expertise_div maroon_div">
<div class="expertise_content">
<p>Paragraph 3...</p>
</div>
...

Answer №2

If you're struggling to center elements with the float: left property, there is a workaround.

You can change the display of your elements to inline-block and apply text-align: center to their wrapper.

Check out this example:

HTML:

<div class="wrapper">
  <div class="boxes">
    <div class="box">
    </div>
    <div class="box">
    </div>
    <div class="box">
    </div>
  </div>
</div>

CSS:

.wrapper{
  width: 600px;
  margin: 0 auto;
  font-size: 0;
  text-align: center;
}

.boxes{
  width: 100%;
}

.box{
  font-size: 0;
  width: 250px;
  height: 150px;
  background: #fff;
  border: 1px solid #eee;
  display: inline-block;
}

See Demo Here

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

Contrasting the distinctions between a pair of CSS class declarations

After using my customized CSS class named myclass alongside Bootstrap's built-in class container, I have a question about how to declare a div element with both classes. Should I go with: <div class="myclass container">some code</div> o ...

Switch the alignment of Bootstrap checkboxes from vertical to horizontal

I'm looking to have my checkbox elements aligned horizontally instead of the default vertical alignment in bootstrap. To achieve this, I've added the following CSS code: .checkboxinsameline{ display:inline; } Here's the HTML structure ...

tips on adjusting margins or paddings in option select

How can I use the margin or padding options for my select dropdown? I tried using &nbsp; <select class="select"> <option value="100"></option> <option value="250">$593</option> ...

Can menus in Vue.js be customized differently for various "pages"?

I am new to Vue and facing a challenge with a common component - menu - that needs to be styled differently on the "TOP page" compared to all other "pages". <template> <div> <menu></menu> <transition name="fade" mode="o ...

React Error: Attempting to access the 'style' property of an undefined object

Can anyone help with this issue related to React code? const sidebar = document.getElementsByClassName("pro-sidebar"); The problem occurs when trying to adjust the width using: function openNav() { sidebar.style.width = "250px";} Addi ...

Trouble Integrating JQuery UI DatePicker into Bootstrap Card

I've built a Bootstrap 4 website with various survey questions grouped in Bootstrap Cards. One question requires a Date response, so I've implemented a JQuery-UI DatePicker for that purpose. However, upon selection, the DatePicker doesn't sh ...

Sending a POST request in jQuery to receive a JSONP response

I am currently working on an HTML5 Application and need assistance with implementing a POST request to submit user comments. The page includes a submit button where users can input their {BrandId,ForumId,Title,Description} values that need to be appended t ...

Utilizing PHP for Substituting URLs within HTML

Is there a way to use PHP to eliminate an entire <iframe> tag that contains the link abc.com? If I searched the following code for abc.com: ‹iframe src="http://abc.com/abcd.html"›‹/iframe› ‹iframe src="http://abc.com/lmno.html"›‹/ifr ...

Customizing Material UI Popover CSS classes within a Select component in a React application

I have integrated the Material UI Select component into my React project. I am attempting to customize the CSS classes .MuiPaper-root and/or .MuiMenu-list. This is how my Select component looks: <Select value={selectValue} disableUnderline onCha ...

The "html" element fails to achieve a full height when applying a 100% height setting

I am facing a height problem of 100% <!doctype html> <html> <head> <meta charset="utf-8"> <title>My Dreamweaver Website</title> <link rel="stylesheet" href="style.css"> </head> <body> <section clas ...

Having trouble loading static CSS files in Django

Need help linking the main.css file in my index.html <link rel="stylesheet" href="{% static 'assets/css/main.css' %}"/> I have also included {%load static%} Here is my file structure: signup/static/assets/css/main.css In settings.py S ...

Implement the CSS display flex property for more flexible layouts

After working on some HTML code, I found the following: <section class = "personal_proyects"> <article> <div class = "personal_details"> <h3>Canal YT: Codigo RM</h3> < ...

The Mat-Table does not display any content despite the fact that the data is being recorded in the console

I have been trying to implement a mat-table in my Angular application to display a list of users fetched from a Spring RestAPI database. However, despite following several tutorials, I am facing an issue where only the table headers (column names) are bein ...

Troubleshooting: Issue with fixed positioning in React (Next.js) causing problems with modal window functionality

I'm currently working on implementing a modal window in React, but I've encountered an issue where setting position: fixed results in the modal window's position being relative to the page rather than the browser window. For privacy reasons, ...

What is the reason behind the addition of escape characters to the hidden input value?

<body> <div> <?= $_POST['msg'] ?> </div> <form id="frm" method="post"> <input type="hidden" name='msg' value='{"field0": "Im a string", "field1": 84, "field3": "so am I"}' /> < ...

CSS for Page Header with Scroll-Over Content

Having trouble creating a "collapsing header" design where the page content scrolls over the banner image. I've tried adjusting z-index and positioning properties in CSS, but can't achieve the desired effect. Any help or guidance would be greatly ...

Navigate within the div by scrolling in increments of 100%

I am facing an issue with a div that contains multiple children set to 100% height. My goal is to scroll exactly the height of one child (which is also 100%) on each scroll. However, I am struggling to prevent scrolling multiple steps at a time. I have tri ...

What could possibly be the reason behind jQuery's inability to function properly with HTML content that was dynamically added to the page through an Ajax

The following code snippet is responsible for submitting a form using Ajax and displaying the result from the addNewUser.php script in a popup window. //trigger the signup submit button $('#button_signup_submit').click(function() { document ...

Adjust the size of the text within the div element as needed

Here is my code on jsFiddle where I am dynamically changing the text font to fit in the div. Everything is working perfectly fine. Currently, the text is within a span element, but I would like to remove the span and have the same functionality. However, w ...

What does the term "SPCLICK" signify within an email template?

Recently, I found an email template sample that includes button links with the attribute xt="SPCLICK". Could someone please clarify what this means? Thank you! ...