Steps for aligning floating DIVs in the center of a parent DIV

Why is the text alignment not working as expected when I have three divs within a parent div that need to be center aligned?

If you'd like to see the issue in action, check out this Demo fiddle

<div class="container_alt">

    <div class="pricing_options_col">

        <div class="pck1">pck1</div>

        <div class="pck2">pck2</div>

        <div class="pck3">pck3</div>

    </div>

</div> 

.container_alt{
    max-width: 1000px;
    margin: 0 auto;
}

.pricing_options{
    width: 100%;
    background-color: #fff;
    height: auto;
    overflow:auto;
    text-align:center;  
    display:inline-block;
}

.pricing_options_col{
    width: 100%;
    max-width:1000px;
    margin: 0 auto;
    text-align:center;
    padding:100px 0;
    display:inline-block;
}


.pck1{
    float: left;    
    margin: 0 auto;
    width: 200px;
    background-color: green;    
    border-radius: 6px;
    box-sizing:border-box;
    padding: 20px;          
}

.pck2{
    float: left;    
    margin: 0 auto;
    width: 400px;
    background-color: red;
    border-radius: 6px;
    box-sizing:border-box;
    padding: 20px;  
    -webkit-box-shadow: 0px 0px 20px 0px rgba(50, 50, 50, 0.75);
    -moz-box-shadow:    0px 0px 20px 0px rgba(50, 50, 50, 0.75);
    box-shadow:         0px 0px 20px 0px rgba(50, 50, 50, 0.75);
    z-index: 999999;    
}

.pck3{
    float: left;    
    margin: 0 auto;
    width: 200px;
    background-color: pink; 
    border-radius: 6px;
    box-sizing:border-box;
    padding: 20px;                  
} 

Answer №1

exclude float: left; in the CSS for .pck1 .pck2 .pck3

update: I believe this is the solution you are seeking:

.pricing_options_col{
    width: 800px;
    margin-left:auto;
    margin-right:auto;
    max-width:1000px;
    margin: 0 auto;
    text-align:center;
    padding:100px 0;
    display:inline-block;
}

Answer №2

To style elements with classes .pck1, pck2, and pck3, it is recommended to remove the property float:left and instead add display:inline-block.

When you float an element, it positions it to one side or the other, which can affect the stacking behavior. By default, divs have a display property of block, causing them to stack vertically. Changing this to display:inline-block will make them display in-line horizontally.

You can view a demonstration here.

Answer №3

Give this a try. The reason for the issue is that both the parent and child elements have a width set to 100%

.pricing_options_col{
    width: 100%; <-- Remove
    max-width:1000px;
    margin: 0 auto;
    text-align:center;
    padding:100px 0;
    display:inline-block;
}

DEMO

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

Attempting to display divs at the bottom when hovering over menu options

I need help troubleshooting my HTML and CSS code. I want to be able to hover over a menu item and have the corresponding div appear at the bottom. Can you point out what's wrong with my code? <!DOCTYPE html> <html> <head> &l ...

Arrange an element to appear hidden behind a text

I have a stylish horizontal navigation bar with links set up like this: <div class="blackbar"> <span class="blackbar-text"><a href="news.php">NEWS</a></span> <span class="blackbar-text"><a href="foo.php">F ...

Unable to conduct this search with Python using Selenium on a website

I have written a script to search for books on a webpage using Selenium: from selenium import webdriver from selenium.webdriver.common.keys import Keys import time PATH = "C:\Program Files (x86)\chromedriver.exe" driver = webdriver.Chrome(PATH) ...

How do I utilize the Material-UI slider's activated (CSS API) feature to conceal the shadows?

Can someone please explain to me how I can use the activated class to change the style of the thumb and hide its shadows? According to the official website, the activated class is applied to the track and thumb elements to trigger JSS nested styles when a ...

Is it possible to implement the SCSS CSS preprocessor in Angular 1.6.6, even though it is already utilizing LESS as the current CSS preprocessor?

Is it necessary to compile SCSS and Stylus files into CSS before importing them into my Angular version 1 app? Are there any other alternatives available? Context: I have two applications, one using Angular 4 with SCSS and the other using Angular 1 with L ...

jQuery not refreshing properly

I'm currently in the process of creating a script to switch the language on a website using PHP and Ajax/jQuery. I would like the page content to refresh without having to reload the entire page. So far, this is what I have come up with: $( "a[data-r ...

Struggling to get the Hover feature on Link from Material-UI to function properly

I've been attempting to hover over Link from Material UI, but unfortunately, it's not working as expected. The CSS styles are not being applied for some unknown reason, and I can't seem to figure out why. Additionally, the Button class is al ...

Exploring the Canvas with Full Element Panning, Minimap Included

Currently, I am working on incorporating a mini map onto my canvas that mirrors what is displayed on the main canvas. The main canvas includes zoom and pan functions. I have created a rectangular shape for the minimap to display the content of the canvas. ...

Creating a color icon for StackExchange using HTML

I am currently working on building my personal website. I am looking to include a link to my Stack Exchange profile on my site using the Stack Exchange icon. However, the icons available in Font Awesome are grayscale and not colored like other icons such a ...

How to dynamically reduce the number of columns in a textarea using jQuery according to its content

Does anyone know how to make a textarea shrinkwrap the text inside on blur? The default number of columns is 10 or 15 and I want the textarea to adjust its width based on the text content. I have tried the following code: $('textarea').on(&apos ...

How can we begin to create this dynamic animation?

Can someone help me figure out how to create an animation effect similar to the one seen in this link? I have some basic knowledge of css and html5, but I'm not sure where to start. Is this done with css, html5, plugins, or something else? I am looki ...

Can Three.js be used to create a compact canvas?

I've successfully implemented a three.js scene on my website where users can drag to rotate an object. However, I don't want the scene to take up the entire webpage. I tried adjusting the field parameters with the following code: renderer.setSiz ...

The touch event doesn't seem to be functioning properly on the div element, but it works perfectly on the window element

I have a dilemma that's been puzzling me lately. I'm trying to append a touchevent to a div, but my current method doesn't seem to be working. Here's what I've tried: $("#superContainer").bind('touchstart', function(even ...

Utilize CSS to style active buttons with JavaScript

In an HTML document, the dynamic generation of divs and buttons using JavaScript can be seen in the code snippet below. <div style="background: rgb(247, 247, 247);"> <button class ="xyz" disabled="disabled">Button1</button> </div> ...

Using multiple header tags in HTML

I have been tasked with developing a webpage featuring a prominent header at the top of the page. This header will include the navigation bar, logo, and website title. As the user begins to scroll, I want the header to transform into a compact version that ...

Delaying CSS Keyframe animations

I'm having trouble getting the color squares in my table to appear one by one. I want each color to show up, then disappear, followed by the next color appearing and disappearing, creating a light sequence effect. Any advice or assistance would be gre ...

What is the proper way to define a class attribute for an HTML element within a PHP file?

I am having trouble writing the class attribute in a PHP file for an HTML element. Here is my code: echo '<div><p class="'.($value->getIsRequire() == 1) ? 'required' : ''.'"> </p> <input type="tex ...

"Uncovering the ways iOS disrupts background-size functionality

I am currently working on creating several marked images that will adjust in size to fit the length of their content. This functionality works well on most browsers, except for those on iOS devices like iPhone and iPad. Take a look at the image-link below ...

Utilizing Bootstrap to arrange table cells in a shifted manner

I'm new to utilizing bootstrap in web development. I've been exploring various examples of bootstrap templates that include tables, and I noticed that when I resize my browser window, the table adjusts accordingly. Now, I'm curious to know ...

What is the best way to adjust column sizes, setting one with a minimum width while allowing the other to expand to accommodate its content?

In the process of creating a user interface, I have included clickable cards that reveal a detailed panel when clicked. The detail panel is set to a minimum width of 900px and should adjust to the remaining space between the cards and itself. The column ...