Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Align text baseline with a button in CSS

Tags:

css

alignment

I would like to achieve one of the two alignments represented on this image: problem representation. CSS3 is ok or even better if it makes things simpler.

My main problem is that I managed to align one div containing the text with the button, but the text itself is aligned with the top of the div and not the bottom.

like image 712
Paul Brauner Avatar asked Sep 03 '11 13:09

Paul Brauner


People also ask

How do I center text vertically in a button CSS?

Use line-height to center it vertically. I usually use the same value as its height. Works only when you know the height. I set height to 0 and line-height to zero and this worked!

What is alignment baseline in CSS?

The alignment-baseline attribute specifies how an object is aligned with respect to its parent. This property specifies which baseline of this element is to be aligned with the corresponding baseline of the parent. For example, this allows alphabetic baselines in Roman text to stay aligned across font size changes.

Where is the Align text button?

To align text using the alignment buttons: Click the align left, center, align right, or justify button on the Formatting toolbar.


1 Answers

You can use: line-height!

.box {    color: #fff;    background: #444;    height:      40px;      line-height: 40px; /* Same as height  */  }
<p class="box">some text <input type="button" value="Button" /></p>

set for the button parent,
where, as you can see, line-height matches the element height
and will align both texts at the element's (p) center. Otherwise, the button, being an inline element by default, it's subject to manipulations using the CSS property vertical-align: which basically aligns all *inline** elements vertically inside a block level element using this typography terms:

vertical-align: baseline; vertical-align: sub; vertical-align: super; vertical-align: text-top; vertical-align: text-bottom; vertical-align: middle; vertical-align: top; vertical-align: bottom; vertical-align: 10em;   vertical-align: 4px; vertical-align: 20%; 

*https://developer.mozilla.org/en-US/docs/Web/CSS/vertical-align

exactly, you can even manually adjust the alignment using PX / -PX and %

I've encountered some issues using line-height on Android browsers (), so sometimes the right solution was to play with the parent padding * and vertical-align rather than the inner children's alignments (with line-height).

*(note: padding for block elements is more consistent than (top, bottom) used on inner inline elements.)

like image 97
Roko C. Buljan Avatar answered Oct 01 '22 06:10

Roko C. Buljan