I’m looking for a more elegant way to align the right icons with the left text vertically, as the negative margin might be a little bit of a hack.
div.main {
border: 1px solid black;
padding: 14px;
}
div.icons {
margin-top: -1.65em;
}
div.icon {
font-size: 2rem;
color: blue;
border: 0.1px solid blue;
border-radius: 3px;
margin-left: -1px;
padding: 3px;
float: right;
}
<link rel='stylesheet prefetch' href='https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css'>
<link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css'>
<div class="main">
<div class="summary">Some text</div>
<div class="icons pull-right">
<div class="icon fa fa-line-chart">
</div>
<div class="icon fa fa-bar-chart">
</div>
</div>
</div>
5
Answers
A good way is to set both, the icon and the text, in seperate element and then give them the following css property:
If
flexbox
is an option, just adddisplay: flex
formain
and for theicons
this for horizontal positioning:and
align-items: center
for vertical alignment.See demo below:
You can simply use
flexbox
for doing this.display: flex
to parent which isdiv.main
in your casemargin-left: auto
to child which you want to align right side of window.Something like below.
Hope this will help you in some way (y).
Flex box reference – complete flexbox reference
You can use CSS Flexbox. And use flex’s
align
properties. Have a look at the snippet below:Hope this helps!
You still can use a custom styles when you need:
using your code:
Using Flexbox
support ie10+
Using Table styles
support ie8+
have a look at this flexbox guide or this guide on flexbox without flexbox.
Also consider using more explicit class names that are not dependant to tags i.e. have a look at BEM