What's the Best Way to Align a Floating Element in the Center

I am struggling to align three h3 tags horizontally within a div tag. The first h3 should be left aligned, the second centered, and the third right aligned. Despite finding solutions here, I have not been able to execute them successfully. Appreciate any help!

Check out the HTML5 code below:

<div class="education">                         
    <h3 class="date"> April 2013 – Present</h3> 
    <h3 class="place"> Sutton ON, Canada </h3> 
    <h3 class="company" > All Reasons Party Rentals </h3>
</div>

Also, here's the corresponding CSS3:

h3.date {
    text-align:left; 
    display:inline-block; 
    float:left;
}   
h3.company {
    text-align:center; 
    display:inline-block; 
    margin-left: auto; 
    margin-right: auto; 
}   
h3.place {
    text-align:right; 
    display:inline-block; 
    float:right;
}

Answer №1

It appears that the h3.company and h3.place selectors were reversed, causing a conflict in the layout due to the HTML order.

However, I believe this revised code will resolve the issue:

.education h3 {float:left;padding:0;margin:0;width:33.333%;}

h3.date {text-align:left;}
h3.place {text-align:center;}
h3.company {text-align:right;}

You can see the corrected layout with colored backgrounds on the headings by clicking on this link: http://jsfiddle.net/QfAeA/

Here's an explanation of how this solution works:

  • All three headings are floated and given equal width of one-third of the available space for balanced distribution
  • The padding and margin settings prevent the headings from exceeding 33.333% width
  • Text alignment is adjusted individually for each heading

I hope this clarifies the issue for you.

Answer №2

To achieve perfect alignment, I recommend using text-align:center on the container element (in this case, body) and floating the two outer elements. This approach accommodates varying widths of the elements while keeping them aligned. Additionally, on smaller screens, the layout automatically adjusts to a vertical orientation.

body { text-align:center; }
h3.date {
    text-align:left; 
    float:left;
}   
h3.company {
    margin:20px auto;
    display:inline-block;
}   
h3.place {
    text-align:right; 
    float:right;
}

Check out the demo here

If you prefer to keep the company name at the top on smaller screens, make sure to position it before the floated elements in your HTML markup.

Answer №3

To view the code, please refer below:

HTML

 <div class="container">
    <div class="tabletContainer">


        <div class="left">
            <h3>April 2013 – Present</h3>
        </div>
        <div class="middle">
            <h3>All Reasons Party Rentals</h3> 
        </div>

    </div>
    <div class="right">
        <h3>Sutton ON, Canada </h3>
    </div>
</div>

plus

CSS

   /* reset browser styles */
    html, body, div, span, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend,
    table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed,
    figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary,
    time, mark, audio, video {
        margin: 0;
        padding: 0;
        border: 0;
        font-size: 100%;
        vertical-align: baseline;
    }

    article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section {
        display: block;
    }

    body {
        line-height: 1.2;
    }

    ol {
        padding-left: 1.4em;
        list-style: decimal;
    }

    ul {
        padding-left: 1.4em;
        list-style: square;
    }

    table {
        border-collapse: collapse;
        border-spacing: 0;
    }
    /* end reset browser styles */

    /*Fill all available spaces*/

    * {
        -webkit-box-sizing: border-box;
        -moz-box-sizing: border-box;
        box-sizing: border-box;

    }


    .container {
        width: auto;
    }

        .container:after {
            content: " ";
            clear: both;
            display: table;
        }


    .tabletContainer {
        /*The total width for the first two column*/
        width: 67%;
        float: left;
        display: block;
    }



    .left {

        float: left;
        /*Each column takes have of the container size, so their width =67/2 = 33.5%*/
        width: 50%;
    }

    .middle {

        float: right;
        /*Each column takes have of the container size, so their width =67/2 = 33.5%*/
        width: 50%;
    }

    .right {
        float: right;
        width: 33%;

    }

    .right h3 {
        float: right;
    }

    /*For tablet devices, show only the two columns in a row and a column underneath*/

    @media (min-width: 481px) and (max-width: 768px) {
        .tabletContainer, .right {
            float: none;
            width: auto;
        }

      .right
      {
          clear: both;
          width: 50%;


      }

        .right h3 {
            float: left;
        }
    }


    /*For mobile phones, show only one column*/
    @media (max-width: 480px) {
       .tabletContainer, .left, .right, .middle {
            float: none;
            width: auto;
           display: block;
        }
        .right h3 {
            float: left;
        }



    }

JSFiddle:

http://jsfiddle.net/KxryX/

EDIT: OK as Jason and Zach suggested that the original solution was not responsive. I have updated to make it responsive.

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

Numerous passcodes needed for easy access to secured webpage

I am currently working on a basic webpage that uses a PHP password script to grant users access. It's functioning perfectly for one user, but now I need to add a second password for another user. If anyone has any suggestions or solutions to this issu ...

Incorporating font options for a PDF generator package within the Symfony framework

I'm currently working on a Symfony project where I need to export PDF code that includes images and text stored in the database. Since I can't install anything on the web servers, I've opted for using the Spread PDF Generator Bundle instead ...

Increment count using jQuery upon clicking

I've got an HTML snippet that looks like this: var useful, cool, funny; '<ul data-component-bound="true" class="voteset'+data[i]['review_id']+'">'+ '<li class="useful ufc-btn" id="1">'+ & ...

If the checkbox is selected, retrieve the product name and price and store them in an array

If the checkbox is checked, I want to retrieve the service name and its price in an array. To get the values of the selected items (i.e. service name and its price in an array), please explain how I can achieve this. $(document).on('click', &apos ...

How do you prevent overlapping divs from being clickable in order to access content below?

Currently, I am utilizing a JPG overlay with decreased opacity for a particular effect. However, I am interested in keeping it solely as an aesthetic effect and making the content beneath that division clickable. Do you think this is achievable? Thanks! I ...

I haven't quite mastered the art of styling and JavaScript, but I'm currently focused on creating an HTML form with

I attempted to create a button, but it's not functioning correctly. It should display 'Submit', but instead, it just shows a square. Since I haven't learned CSS yet and my online video instructor hasn't covered it either, I need as ...

What causes the discrepancy between 1px and 0px padding on floating divs in various web browsers?

Take a look at this HTML example - the only variation is padding-top:0px; vs padding-top:1px; However, in the second example, the div is shifted by a completely different amount? <div style="clear: both; margin:0px; padding-top:0px; border: 0px"> ...

The Bootstrap navigation bar is experiencing functionality issues when viewed on Chrome mobile browsers

I'm currently testing out the bootstrap 3 navbar feature on my meteor application, but I'm encountering some issues specifically on mobile devices. The problem seems to be that the toggle button is not showing up as expected. Interestingly, it wo ...

generate an abundance of results exceeding the set limits from a form submission

I'm currently working with a script that submits search terms into a form and pulls back results. Here's the code I'm using: import mechanize url = "http://www.taliesin-arlein.net/names/search.php" br = mechanize.Browser() br.set_handle_ro ...

Animated CSS Checkmark Design

How can I create an animated Check-mark using CSS with SVG animation? I attempted the following code but it doesn't seem to be working. Any suggestions? DEMO: https://jsfiddle.net/b7Ln0jns/ CSS: @import "bourbon"; $color--green: #7ac142; $curve: c ...

No results found in the search query

I am currently developing a page where I need to search within an address book. The address book is located on another page with the URL http://example.com/start.php. This address book comes with its own search field, which I plan to use for my page. I hav ...

"Unable to get 'vertical-align middle' to function properly in a table

Can someone please review my code at http://jsfiddle.net/4Ly4B/? The vertical alignment using 'vertical-align: middle' on a 'table-cell' is not centering as expected. ...

The .css() method does not apply any styles to the elements in the document

Having trouble with a jQuery function: .css() This is the HTML code in question: <div class="..." id="about"> // OTHER HTML CODE </div> And here's the corresponding jQuery code: (function($, window, document) { $(function() { ...

JavaScript library for creating animated car movements on a map using JPG images

Looking for a way to animate a car's movement on a map? I have a map image in jpg format (not svg) and a sequence of (x,y) points ready to go! If you could recommend a JavaScript library that can help me easily create an HTML page with this animation ...

Showing a vertical arrangement of lists containing updated information

I am struggling to create lists that seamlessly flow together to showcase data. I am using the following website as a reference: I have examined the style sheet thoroughly but I cannot figure out how they are achieving this effect. I am hoping someone can ...

CSS navbar with a unique design: Issue with List Hover not Persisting

I'm facing a common issue with a pure CSS navbar. I have constructed a navbar using ul and li elements, but I am unable to make the menus stay visible when I hover over them. The problem lies in the fact that the menu opens only when I hover over the ...

Bootstrap navbar problem: struggling to adjust padding for image and navigation links

Can anyone help me understand why my navbar isn't displaying padding on the left and right as specified in my CSS? I've been trying to figure it out for a while now, but I'm not sure if I need to remove the div-container fluid or if I'm ...

Ways to automatically update a page once the form response is received

After scouring various websites, I still haven't found a solution to my issue. What I need help with is how to refresh a page once a successful deletion message is received for a specific row. My admin2.php page displays information about the admin fr ...

What is the best way to modify a Bootstrap class' SASS attribute within a React component?

Just to clarify, I am utilizing react-bootstrap and attempting to incorporate bootstrap. Here is the code I have: import React from 'react' // bootstrap import { Button } from 'react-bootstrap'; const MainDisplay = () => { return ...

Unable to interact with the page while executing printing functionality in

component: <div class="container" id="customComponent1"> New Content </div> <div class="container" id="customComponent2"> different content </div> ...