Skip to main content
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
VerticalAlignment Enum
- Reference
Definition
Describes how a child element is vertically positioned or stretched within a parent's layout slot.
In this article
public enum class VerticalAlignment[System.Windows.Localizability(System.Windows.LocalizationCategory.None, Readability=System.Windows.Readability.Unreadable)] public enum VerticalAlignment[<System.Windows.Localizability(System.Windows.LocalizationCategory.None, Readability=System.Windows.Readability.Unreadable)>] type VerticalAlignment = Public Enum VerticalAlignmentInheritanceAttributesFields
Bottom | 2 | The child element is aligned to the bottom of the parent's layout slot. |
Center | 1 | The child element is aligned to the center of the parent's layout slot. |
Stretch | 3 | The child element stretches to fill the parent's layout slot. |
Top | 0 | The child element is aligned to the top of the parent's layout slot. |
Examples
The following example demonstrates how to use the VerticalAlignment enumeration in code and Extensible Application Markup Language (XAML).
TextBlock myTextBlock = new TextBlock(); myTextBlock.FontSize = 18; myTextBlock.HorizontalAlignment = HorizontalAlignment.Center; myTextBlock.Text = "VerticalAlignment Sample"; Grid.SetRow(myTextBlock, 0); Button myButton1 = new Button(); myButton1.VerticalAlignment = VerticalAlignment.Top; myButton1.Content = "Button 1 (Top)"; Grid.SetRow(myButton1, 1); Button myButton2 = new Button(); myButton2.VerticalAlignment = VerticalAlignment.Bottom; myButton2.Content = "Button 2 (Bottom)"; Grid.SetRow(myButton2, 2); Button myButton3 = new Button(); myButton3.VerticalAlignment = VerticalAlignment.Center; myButton3.Content = "Button 3 (Center)"; Grid.SetRow(myButton3, 3); Button myButton4 = new Button(); myButton4.VerticalAlignment = VerticalAlignment.Stretch; myButton4.Content = "Button 4 (Stretch)"; Grid.SetRow(myButton4, 4); Dim myTextBlock As New TextBlock() myTextBlock.FontSize = 18 myTextBlock.HorizontalAlignment = Windows.HorizontalAlignment.Center myTextBlock.Text = "VerticalAlignment Sample" Grid.SetRow(myTextBlock, 0) Dim myButton1 As New Button() myButton1.VerticalAlignment = Windows.VerticalAlignment.Top myButton1.Content = "Button 1 (Top)" Grid.SetRow(myButton1, 1) Dim myButton2 As New Button() myButton2.VerticalAlignment = Windows.VerticalAlignment.Bottom myButton2.Content = "Button 2 (Bottom)" Grid.SetRow(myButton2, 2) Dim myButton3 As New Button() myButton3.VerticalAlignment = Windows.VerticalAlignment.Center myButton3.Content = "Button 3 (Center)" Grid.SetRow(myButton3, 3) Dim myButton4 As New Button() myButton4.VerticalAlignment = Windows.VerticalAlignment.Stretch myButton4.Content = "Button 4 (Stretch)" Grid.SetRow(myButton4, 4) <Page xmlns="//schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="//schemas.microsoft.com/winfx/2006/xaml" WindowTitle="VerticalAlignment Sample"> <Border Background="LightBlue" BorderBrush="Black" BorderThickness="2" Padding="15"> <Grid Background="White" ShowGridLines="True"> <Grid.RowDefinitions> <RowDefinition Height="25"/> <RowDefinition Height="50"/> <RowDefinition Height="50"/> <RowDefinition Height="50"/> <RowDefinition Height="50"/> </Grid.RowDefinitions> <TextBlock Grid.Row="0" Grid.Column="0" FontSize="18" HorizontalAlignment="Center">VerticalAlignment Sample</TextBlock> <Button Grid.Row="1" Grid.Column="0" VerticalAlignment="Top">Button 1 (Top)</Button> <Button Grid.Row="2" Grid.Column="0" VerticalAlignment="Bottom">Button 2 (Bottom)</Button> <Button Grid.Row="3" Grid.Column="0" VerticalAlignment="Center">Button 3 (Center)</Button> <Button Grid.Row="4" Grid.Column="0" VerticalAlignment="Stretch">Button 4 (Stretch)</Button> </Grid> </Border> </Page>Remarks
Height and Width properties that are explicitly set on an element take precedence over the Stretch property value.
Applies to
Additional resources
In this article
The vertical-align CSS property sets vertical alignment of an inline, inline-block or table-cell box. The vertical-align property can be used in two contexts: Note that vertical-align only applies to
inline, inline-block and table-cell elements: you can't use it to vertically align block-level elements. /* Keyword values */
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;
/* <length> values */
vertical-align: 10em;
vertical-align: 4px;
/* <percentage> values */
vertical-align: 20%;
/* Global values */
vertical-align: inherit;
vertical-align: initial;
vertical-align: revert;
vertical-align: revert-layer;
vertical-align: unset;
The vertical-align property is specified as one of the values listed below. These values vertically align the element relative to its parent element: Aligns the baseline of the element with the baseline of its
parent. The baseline of some replaced elements, like <textarea>, is not specified by the HTML specification, meaning that their behavior with this keyword may vary between browsers. Aligns the baseline of the element with the subscript-baseline of its parent. Aligns the baseline of the element with the superscript-baseline of its parent.Try it
Syntax
Values for inline elements
Parent-relative values
Aligns the top of the element with the top of the parent element's font.
text-bottomAligns the bottom of the element with the bottom of the parent element's font.
middleAligns the middle of the element with the baseline plus half the x-height of the parent.
<length>Aligns the baseline of the element to the given length above the baseline of its parent. A negative value is allowed.
<percentage>Aligns the baseline of the element to the given percentage above the baseline of its parent, with the value being a percentage of the line-height property. A negative value is allowed.
Line-relative values
The following values vertically align the element relative to the entire line:
topAligns the top of the element and its descendants with the top of the entire line.
bottomAligns the bottom of the element and its descendants with the bottom of the entire line.
For elements that do not have a baseline, the bottom margin edge is used instead.
Values for table cells
baseline (and sub, super, text-top, text-bottom, <length>, and <percentage>)
Aligns the baseline of the cell with the baseline of all other cells in the row that are baseline-aligned.
topAligns the top padding edge of the cell with the top of the row.
middleCenters the padding box of the cell within the row.
bottomAligns the bottom padding edge of the cell with the bottom of the row.
Negative values are allowed.
Formal definition
baseline |
inline-level and table-cell elements. It also applies to ::first-letter and ::first-line. |
no |
refer to the line-height of the element itself |
for percentage and length values, the absolute length, otherwise the keyword as specified |
a length |
Formal syntax
vertical-align =
[ first | last ] ||
<'alignment-baseline'> ||
<'baseline-shift'>
Examples
Basic example
HTML
<div> An <img src="frame_image.svg" alt="link" width="32" height="32" /> image with a default alignment. </div> <div> An <img class="top" src="frame_image.svg" alt="link" width="32" height="32" /> image with a text-top alignment. </div> <div> An <img class="bottom" src="frame_image.svg" alt="link" width="32" height="32" /> image with a text-bottom alignment. </div> <div> An <img class="middle" src="frame_image.svg" alt="link" width="32" height="32" /> image with a middle alignment. </div>
CSS
img.top { vertical-align: text-top; } img.bottom { vertical-align: text-bottom; } img.middle { vertical-align: middle; }
Result
Vertical alignment in a line box
HTML
<p> top: <img style="vertical-align: top" src="star.png"/> middle: <img style="vertical-align: middle" src="star.png"/> bottom: <img style="vertical-align: bottom" src="star.png"/> super: <img style="vertical-align: super" src="star.png"/> sub: <img style="vertical-align: sub" src="star.png"/> </p> <p> text-top: <img style="vertical-align: text-top" src="star.png"/> text-bottom: <img style="vertical-align: text-bottom" src="star.png"/> 0.2em: <img style="vertical-align: 0.2em" src="star.png"/> -1em: <img style="vertical-align: -1em" src="star.png"/> 20%: <img style="vertical-align: 20%" src="star.png"/> -100%: <img style="vertical-align: -100%" src="star.png"/> </p>
#* { box-sizing: border-box; } img { margin-right: 0.5em; } p { height: 3em; padding: 0 0.5em; font-family: monospace; text-decoration: underline overline; margin-left: auto; margin-right: auto; width: 80%; }
Result
Vertical alignment in a table cell
HTML
<table> <tr> <td style="vertical-align: baseline">baseline</td> <td style="vertical-align: top">top</td> <td style="vertical-align: middle">middle</td> <td style="vertical-align: bottom">bottom</td> <td> <p> There is a theory which states that if ever anyone discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable. </p> <p> There is another theory which states that this has already happened. </p> </td> </tr> </table>
CSS
table { margin-left: auto; margin-right: auto; width: 80%; } table, th, td { border: 1px solid black; } td { padding: 0.5em; font-family: monospace; }
Result
Specifications
Cascading Style Sheets Level 2 Revision 1 (CSS 2.1) Specification # propdef-vertical-align |
Browser compatibility
BCD tables only load in the browser