I know I am probably missing something here. I am trying to align md-icon and some text vertically, right now the word "sample text" displays below the icon.
<div>
<md-icon>home</md-icon> Sample Text
</div>
Output:
I did try playing around with vertical align on the Sample Text using a span but couldn't get anything working and felt kind of hacky anyway.
Anyone know how to get this effect?
1 Select the text you want to center between the top and bottom margins. 2 On the Page Layout tab, click the Page Setup Dialog Box Launcher. 3 Select the Layout tab. 4 In the Vertical alignment box, click Center 5 In the Apply to box, click Selected text, and then click OK.
To center both vertically and horizontally, use padding and text-align: center : I am vertically and horizontally centered.
Deprecated as an attribute Occasionally you will see “valign” used on table cells to accomplish vertical alignment. e.g. <td valign=top> . It should be noted that this attribute is deprecated and should not be used.
Text-align is used to align the text in center/left/right/justify horizontally and vertical-align aligns the element in vertically. ... The proper values for text-align are left|right|center|justify as it is horizontal, while the valign is vertical so it's top|middle|bottom|baseline.
This is a common issue when using <md-icon>
. To align the icon and text, you can put your text inside a span and apply style to that:
<div>
<md-icon>home</md-icon><span class="aligned-with-icon">Sample Text</span>
</div>
In your component.css:
.aligned-with-icon{
position: absolute;
margin-top: 5px;
margin-left: 5px; /* optional */
}
You can also use relative
position if you'll be putting multiple icons in the same div. Here is the css for that:
.aligned-with-icon-relative{
position: relative;
top: -5px;
margin-left: 5px; /* optional */
}
Another option is to use flex
display on the outer div and align-items
to center
:
In your html:
<div class="with-icon">
<md-icon>home</md-icon>Sample Text
</div>
In your css:
.with-icon {
display: flex;
align-items: center;
}
Here is a Plunker Demo
I utilize the inline
attribute. This will cause the icon to correctly scale with the size of the button.
<button mat-button>
<mat-icon inline=true>local_movies</mat-icon>
Movies
</button>
<!-- Link button -->
<a mat-flat-button color="accent" routerLink="/create"><mat-icon inline=true>add</mat-icon> Create</a>
I add this to my styles.css
to:
button.mat-button .mat-icon,
a.mat-button .mat-icon,
a.mat-raised-button .mat-icon,
a.mat-flat-button .mat-icon,
a.mat-stroked-button .mat-icon {
vertical-align: top;
font-size: 1.25em;
}
You can use
div {
display: flex;
vertical-align: middle;
}
or
span {
display: inline-flex;
vertical-align: middle;
}
This can be achieved using CSS flexbox
<!-- HTML part -->
<div class="outer-div">
<mat-icon>home</mat-icon>
<span>Home</span>
</div>
<!-- CSS part -->
.outer-div {
display: flex;
flex-direction: column;
align-items: center;
}
.outer-div > span {
font-size: 10px;
}
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