0

I have a textbox container div (div.tb) with position:relative, containing the input and placeholder div, these both have position:absolute.

The problem is that the input text is vertically centered, but the placeholder text isn't vertically centered.

So I need the placeholder text to be vertically centered, just like the input.

HTML:

<div class="tb">
    <div class="placeholder">username</div>
    <input type="text" name="tb-username" />
</div>

CSS:

.tb{
    position: relative;
    height: 28px;
    border: 1px solid #CCCCCC;
    background-color: #FFFFFF;
}

.tb input, .tb .placeholder{
    position: absolute;
    top: 0px;
    left: 0px;
    right: 0px;
    bottom: 0px;
    height: 28px;
}

Tested with latest Chrome for Mac Os X

Thanks for your time.

4
  • 1
    And you're trying to achieve...what? Commented May 8, 2012 at 21:03
  • @Madbreaks vertical align, not horizontal alignment, but the input and placeholder equal centered vertically. Commented May 8, 2012 at 21:05
  • Try setting the line-height property of your .placeholder and make it equal to your input height. Commented May 8, 2012 at 21:10
  • So is the input on top of the div or vice-versa? Since they're both positioned absolutely I can't tell which one you want to show on top of the other. Commented May 8, 2012 at 21:13

2 Answers 2

1

Try changing line-height for your placeholder text. I'm not seeing what you're doing but that should fix the problem. (set a line-height equal to your input height. Hope it helps!

Sign up to request clarification or add additional context in comments.

3 Comments

Do you have a link? And btw, why don't you try using the placeholder="placeholder text" inside your input tag?
because I can't style the placeholder and it won't work crossbrowser.
ok, your input height is 28px, not 18px... you can add a line-height: 28px to your .placeholder and it will work just fine! Edit: and I would use html5 placeholder anyway, and use that just as a fallback.
0

if you only have to support Chrome & FF & IE 8+ then

following css will help:

display: table-cell; vertical-align: middle;

also take a look at following for diff ways to achieve the same

How to vertically center a div for all browsers?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.