I would like to achieve one of the two alignments represented on this image: . 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.
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!
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.
To align text using the alignment buttons: Click the align left, center, align right, or justify button on the Formatting toolbar.
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With