Why is the text in a button always centered vertically?
TL;DR: It is not possible to recreate the layout created by a <button>
element using CSS. Or at least it is hidden really well.
I tested this by changing the display value to flex on a <button>
and it affected the vertical alignment. Other values like display:block
did not have an effect. It seems that there is no css value for this, it just is the default behavior and there is no way to do it for a div.
Like you, I was looking for an answer to this question. I was trying to find css property which centers vertically content in button. When I almost gave up I decided to leaf through html specification and this is what I found:
If the element is an input element, or if it is a button element and
its computed value for 'display' is not 'inline-grid', 'grid',
'inline-flex', or 'flex', then the element's box has a child anonymous
button content box with the following behaviors:
- The box is a block-level block container that establishes a new
block formatting context (i.e., 'display' is 'flow-root').- If the box does not overflow in the horizontal axis, then it is
centered horizontally.- If the box does not overflow in the vertical axis, then it is
centered vertically. Otherwise, there is no anonymous button content box.
https://html.spec.whatwg.org/multipage/rendering.html#button-layout
How do I center text vertically and horizontally in Flutter?
Text alignment center property setting only horizontal alignment.
I used below code to set text vertically and horizontally center.
Code:
child: Center(
child: Text(
"Hello World",
textAlign: TextAlign.center,
),
),
Vertically align text to the bottom of the box?
2020 Update
You can use CSS grid, flexbox or the original method with line-height
:
body { display: flex } /* just to prettify */
div { margin: .5em; width: 6.25em; height: 6.25em; background: #eee; color: #333; text-align: center}
.grid { display: grid; align-content: end;}
.flex { display: flex; align-items: flex-end; justify-content: center}
.lh { line-height: 11.5 /* 6.25*2 - 1.5 */ }
<div class='grid'>Hello</div>
<div class='flex'>Hello</div>
<div class='lh'>Hello</div>
Vertically center text that has no descenders?
After some testing, it seems I was wrong about how text is vertically centered. It turns out that most browsers do exactly what I want—they don't take descenders into account.
For those who were unclear about what I was asking: The "descender" is the part of text that goes below the baseline. For example, the lowercase characters "g" and "y" have parts that go below the baseline. I wanted to vertically center text that does not have characters like these (in my case, only uppercase characters like "X").
Here is a jsfiddle that demonstrates what I found (which sets line-height
to the height of the div
). Notice that the "X" is centered properly, which makes the "g" overflow outside the div. But that's okay for me, because the text is all caps.
Vertically center rotated text with CSS
The key is to set position top and left to 50% and then transformX and transformY to -50%.
.inner {
position: absolute;
top: 50%;
left: 50%;
}
.rotate {
transform: translateX(-50%) translateY(-50%) rotate(-90deg);
}
see: http://jsfiddle.net/CCMyf/79/
Android TextView align text vertically within the bounding box
Here's your solution:
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/item_card"
android:layout_width="96dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
The layout_height of your CardView
wasn't high enough and the bottom of the TextView
was getting cut off. Setting it to wrap_content
does the trick.
Related Topics
How to Remove a CSS Class from a Jqgrid Cell
Safari Page-Break-Inside:Avoid Not Working
A:Visited Links - Opacity Not Working
Jquery Mobile Page Transition Without Jquery Mobile
How to Create Cube Window with CSS
What's the Best Way to Test Cross-Browser Compatibility
How to Vertically Centralize a Bootstrap V4 Modal with CSS
Differencebetween :Where() and :Is()
Primefaces 5 Rotated Axis Tick Labels
Vue Cli 3 - Use Background Image in Style Tag
Article Push/Pull Alignment Using Bootstrap 3
Using Attribute Value of a Parent in a Child Using CSS3 Attr Function
How to Span to the Last Column in an Implicit Grid
Less CSS - Accessing Classes Further Up the Dom Tree from Within a Nested Class
CSS Div Id Used Only Once Per Page
How to Create a Sidebar and Content Area Using Flexbox
How to Set the Transform Origin to a Specific Point on the Element