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

JavaScript for rotating an element upon button click

My webpage design <div id="yabanner"> <img src="img/ok.jpg"> </div> <button>Click Me</button> <button>Click Me</button> <button>Click Me</button> My script code var button = document.getElementsBy ...

Looking to trigger a PHP page by clicking on a div?

Is there a way to trigger a PHP page call when a user clicks on a <DIV> with AJAX? Additionally, can the text of the DIV be changed to display "LOADING....." simultaneously? I lack knowledge about AJAX. Could you please provide me with more details ...

Updating div visibility based on form content in PHP

I am struggling to find a way to filter a page filled with divs based on form input to determine whether they should be displayed or not. The layout is akin to a collection of articles, where each div showcases an image and some text that links to the comp ...

On smaller screens in portrait mode, CSS automatically incorporates margin adjustments

Here is the code I am working with: <html> <head> <style> body { margin: auto; width: 720px; } </style> </head> <body> <p>Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ip ...

Methods for smoothly animating an image to move up and down using CSS

I'm new to CSS and I was wondering if it's possible to create a smooth vertical movement effect for an image using CSS. Something like shown in this link: .... ...

Grab Text Class - Python Selenium Automation

I am attempting to extract an element from a webpage using Selenium in Python <span class="_2-1_O"><span dir="auto" class="_1jlxc _3Whw5">Ana Smith</span></span> My goal is to extract the name Ana Smith using Python: contact_name ...

Issue with positioning in IE11 when using `top:0` and `bottom:0` inside a table

Expanding on a previous inquiry of mine, I have created this interactive demo The demo features two columns on top of one column in smaller views, and 3 columns in a row in larger views. While the layout functions as intended, I noticed a lack of spacing ...

Resolving Problem of Jumping When 'invalid-feedback' Appeared in Bootstrap 4 Input

There is an input with an add-on and a customized focus that includes a '%' at the end of the field, which is working fine. However, there is a small issue where the error message causes the input to jump when clicked in or out. To see the issue ...

Using PHP to nest a table within HTML and populate it with numbers ranging from 1 to 100

I am struggling with a practice question that involves creating a table using PHP nested in HTML. I need some help fixing it and understanding where I went wrong. The task is simple: generate numbers from 1 to 100 in a table, with each row containing 10 n ...

Validating a form using HTML5 in a modal designed with Bootstrap

As a newcomer to jquery and javascript, I am facing a challenge. I want the form to open when the "Open Modal" button in the modal is clicked. After that, upon clicking 'Save' in the modal, I need it to validate the HTML5 form and if validation ...

Migrating a few PHP scripts to ColdFusion

Hey there, I'm currently in the process of converting some really basic PHP code to be compatible with a development server that only supports CF. This is completely new territory for me, so I'm looking for some guidance on how to port a few thin ...

What is the process for including icons on buttons?

I have been researching how to use Font Awesome software to incorporate icons into my HTML elements, but I am uncertain about the implementation process for my specific case. Currently, I have created a basic webpage with buttons and I would like to includ ...

Triggering a targeted action upon the user's click on any HTML element

Currently, I am in the process of developing navigation buttons that trigger a dropdown sub-menu when clicked by utilizing checkbox inputs. Upon clicking the label, the input is checked, causing the menu to drop down, and when the label is clicked again, t ...

Error encountered while generating PDF using xhtml2pdf Pisa.CreatePDF() due to CSS parsing issue

Currently, I am referencing the xhtml2pdf instructions. I opted to utilize one of the provided sample html files and saved it as test.html: <html> <head> <style> @page { size: a4 portrait; @frame header_frame { ...

Attempting to determine the smallest and largest dimensions of two values using CSS3

Trying to make sure the image on my phone adjusts its size based on orientation, I attempted using Ionic4. When in landscape mode, I want it to adapt according to height instead of width. Essentially, I need it to use the minimum size and also require the ...

Dynamic stylesheet in Vue component

Currently, I am utilizing a Vue component (cli .vue) and facing the challenge of selectively displaying my stylesheet based on a boolean value. To put it simply: When myVar==false, the component should not load its styles. <style v-if="myVar" lang="sc ...

What is a way to include a ":hover" effect in Elm without relying on signals?

I'm looking to apply a :hover style to an element using Elm's HTML library. I've considered using Signals and Sets to manage selected nodes, but it feels like overkill for such a simple task. Another option is adding an external stylesheet, ...

Refreshing a specific div without refreshing the entire page is currently not functioning as expected

Currently, I am involved in Java Portlets and Websphereportal development On a specific page, there is an option to delete or update a profile picture. The layout of the page consists of two divisions within my JSP file: <div id="fotoDefault" style="d ...

Minimizing Unused Space After Media Query Adjustment

Using the bootstrap grid system, I am facing some uncertainties. I would like to create a header where there is a logo on the left and links on the right. Below this header, I want a menu that spans the same width as the content above. In my current setu ...

PHP - Unable to store the input content

I've encountered an issue with my website where I need to create a form for users to submit new information or news. Below is the code snippet that I have: <?php include "0begin.php"; $title=$_POST["title"]; isset($title) or $title=$_GET["tit ...