Vertically Align Text Within Input Field of Fixed-Height Without Display: Table or Padding

Vertically align text within input field of fixed-height without display: table or padding?

In Opera 9.62, Mozilla 3.0.4, Safari 3.2 (for Windows) it helps, if you put some text or at least a whitespace within the same line as the input field.

<div style="line-height: 60px; height: 60px; border: 1px solid black;">
<input type="text" value="foo" /> 
</div>

(imagine an   after the input-statement)

IE 7 ignores every CSS hack I tried.
I would recommend using padding for IE only. Should make it easier for you to position it correctly if it only has to work within one specific browser.

How to vertically top-align the text in a INPUT text element?

If you want to have only input element and achieve this, then set padding-bottom and not height.

Once you set height, by default the text will be shown in the middle of the element.

input {
padding-bottom: 462px;
background-color:lightgreen;
}

http://jsfiddle.net/yf5f82vx/1/

How to vertically align text in input type= text ?

Use padding to fake it since vertical-align doesn't work on text inputs.

jsFiddle example

CSS

.date-input {     
width: 145px;
padding-top: 80px;
}​

Vertically align characters inside input

The Problem

In some fonts, characters with descenders, like g, p, q, and y, "overflow" the vertical space defined by the font-size property. Normally, that's not a problem, because the line-height property provides enough extra space to accommodate the descenders. However, if the characters are placed in a container element with a fixed height that's less than the line-height, the descenders may get clipped if that's how the container handles overflow (text inputs being one example of such).

If you were hoping to bump the text up a few notches to avoid the clipping, then you'll be disappointed to know that there is currently no way to reposition text within its own line-height. (vertical-align, in case you were wondering, positions an inline element relative to its parent.) However, there are a few CSS tricks that we can use to achieve the same visual effect...

Solution 1 (Webkit only)

This one works by giving the input a large enough height to fit the font's lower extremities, and then using clip-path to trim it back down to 28px. This is probably the most elegant solution, but unfortunately, clip-path isn't well supported outside of Webkit browsers (Chrome, Safari, Opera).

input {
display: inline-block;
padding: 0;
border: none;
height: 32px;
font-size: 28px;
line-height: 32px;
font-family: arial;
background: #cdcdcd;
vertical-align: baseline;
-webkit-clip-path: inset(4px 0px 0px 0px);
clip-path: inset(4px 0px 0px 0px);
}
input: <input value="asdg">

CSS - Vertical Align with no Height

Add this CSS:

.smallbox p:before{
content:" ";
display:inline-block;
vertical-align:middle;
height:100%;
width:1px;
}
.smallbox p span{
display:inline-block;
vertical-align:middle;
width:99%;
}

Credit goes to Paul.

How to align text in input on top of div

This happens because of how inputs are handled. The textfield inside an input-element is streched to fit the box size. In this case 34px. So the fix is to increase the div's line-height to match the 34px of the input.

.wrapper {
box-sizing: border-box;
padding: 0;
margin: 0;
outline: none;
padding-right: 20px;
}

.box {
height: 34px;
width: 130px;
padding: 6px 12px;
font-family: Verdana!important;
font-size: 24px;
color: #555555;
background: rgba(255, 255, 255, 0) none;
border: 1px solid #cccccc;
border-radius: 4px;
}

.top {
z-index: 2;
position: relative;
}

.underlayer {
top: -48px;
line-height: 34px;
z-index: 1;
position: relative;
background: rgba(255, 255, 255, 1);
color: #c8c8c8;
margin:0;
}
<div class="wrapper">
<input type="text" value="20-123" class="box top">
<div class="box underlayer">20-123-20</div>
</div>

Vertically Align a Header Tag with Fixed Height through CSS

You can try to do something like that:

HTML

<div class="header">
<h1>This would be 2 lines of text</h1>
<h1>This is 1 line</h1>
<h1>This would be 2 lines of text</h1>
<h1>This is 1 line</h1>
<h1>This would be 2 lines of text</h1>
<h1>This is 1 line</h1>
<h1>This would be 2 lines of text</h1>
<h1>This is 1 line</h1>
</div>

CSS

.header {
display: inline-table; /* IE8+, Chrome, FF3+, Opera7+ */
}

h1 {
display: table-cell;
margin:5px;
padding:5px;
width:100px;
height:50px;
vertical-align: middle;
text-align:center;
background:#336699;
color:#fff;
}

In this way you can use tables without break the website semantic. Look at MDN for further details about display and table styling.

However, usually only one <h1> element per page is used to define the website's page title, then all the other combinations of <h*> elements.

If you don't need cross-browser compatibility (for example, if you're developing some Chrome/Firefox extension/webapp), even the new flexbox could be an interesting alternative.


Another way could be to change vertical-align in your code with line-height set to the height of your <h1> element, but in this case you will have limited to a single line of text.

CSS: Vertically align div when no fixed size of the div is known

This is a pure CSS2 solution for horizontally and vertically centering without known sizes of either container nor child. No hacks are involved. I discovered it for this answer and I also demonstrated it in this answer.

The solution is based on vertical-align: middle in conjunction with line-height: 0, which parent has a fixed line-height.

The HTML:

<span id="center">
<span id="wrap">
<img src="http://lorempixum.com/300/250/abstract" alt="Sample Image" />
</span>
</span>

And the CSS:

html,
body {
height: 100%;
width: 100%;
padding: 0;
margin: 0;
overflow: hidden;
}
#center {
position: relative;
display: block;
top: 50%;
margin-top: -1000px;
height: 2000px;
text-align: center;
line-height: 2000px;
}
#wrap {
line-height: 0;
}
#wrap img {
vertical-align: middle;
}

Tested on Win7 in IE8, IE9, Opera 11.51, Safari 5.0.5, FF 6.0, Chrome 13.0.

The only caveat is IE7, for which the two innermost elements have to declared at one line, as demonstrated in this fiddle:

<span id="center">
<span id="wrap"><img src="http://lorempixum.com/300/250/abstract" alt="Sample Image" /></span>
</span>

Note that the span's are also required for IE7. In every other browser, the span's may be div's.



Related Topics



Leave a reply



Submit