Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8225021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:16:24+00:00 2026-06-07T15:16:24+00:00

There are a few questions on Stackoverflow which ask about aligning elements horizontally using

  • 0

There are a few questions on Stackoverflow which ask about aligning elements horizontally using CSS (divs). Generally, this is accomplished using floats, or display: inline-block.

However, I have a pretty simple requirement, and I’m having difficulty using either of these techniques to get it to work perfectly. I simply want to horizontally align a line of text with a DIV element.

So, basically, there are 3 ways of doing this:

Method 1 (Using display: inline-block)

<div style = "display: inline-block;">Foo</div>
<div style = "display: inline-block; padding-left: 19px">
    <div style = "border: 1px solid #000000; height: 1em; width: 100px"></div>
</div>

Method 2 (Using float)

<div style = "width: 150px">
    <div style = "float:left">Foo</div>
    <div style = "float:right">
        <div style = "border: 1px solid #000000; height: 1em; width: 100px"></div>
    </div>
</div>

Method 3 using.. (please don’t kill me) a <table>:

<table style = "width: 150px" cellpadding = "0" cellspacing = "0" border = "0">
    <tr>
        <td style = "width: 40px">
            Foo
        </td>
        <td>
            <div style = "border: 1px solid #000000; height: 1em; width: 100px"></div>
        </td>
    </tr>
</table>

These three methods render like this on Firefox:

enter image description here

Okay, so firstly, the problem with Method 1 (inline-block) is that the text isn’t vertically aligned (centered) with the DIV element, which looks awkward. Instead, the bottom border of the DIV is aligned with the bottom of the text. I tried adjusting padding and margins to fix this, but it didn’t help.

Method 2 (floats) looks good, but the problem with Method 2 is what happens if the text changes to something longer? Well…

enter image description here

So the problem with using floats is that if the text gets longer, the DIV element is knocked down to the next line. So this isn’t acceptable. Note that Method 1 (inline-block) doesn’t have this problem.

The only method which meets all requirements is the one which uses a <table>, but I don’t want to use that, because … well, reasons.

So, what’s the best solution here. How can I use just DIVs with CSS to get the same result as Method 3, which uses a <table>?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T15:16:25+00:00Added an answer on June 7, 2026 at 3:16 pm

    Adding line-height:1em; vertical-align:top; to the text containing div of method 1 seems to solve this (where line-height is the height of the bordered div).

    I, myself, prefer inline-block to float due to the unpredictability of float in earlier versions of IE. It is also hard to prevent the float overflowing to the next line as this is what it was designed for, really!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know there are a few questions and answers on StackOverflow about using regex
First of all, I know there are a few other StackOverflow questions about this
There have been a few questions asked along this line stackoverflow such as What
I know there are a few questions on stack already regarding this, and I
I realize there are a few other questions about why Cygwin is apparently slow,
So, there seems to be a few questions asking about removing files/directories matching certain
There have been several questions over the past few days about the proper use
There's quite a few questions on Stack Overflow about id vs class but these
There are quite a few questions on why local notification is not firing properly
I realise there are a fair few questions on here already with regard to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.