0

i can't align verticaly the text inside the a tag as you can see here.

navigation-item__link {
  color: #000;
  text-decoration: none;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #ccc;
}
<a class="navigation-item__link" href="/auth">Login</a>

also tried with,

vertical-align: middle;

but it also didn't work.
any help is appreciated.

gdsd asgga
  • 77
  • 7

3 Answers3

0

Look at my sample code, i hope you can understand and please provide necessary code.

UPDATE: wrap your a tag with a div and give flex properties to that div.

.wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  background-color: #ccc;
}
<div class="wrapper">
  <a href="/auth">Login</a>
</div>
Robin
  • 4,902
  • 2
  • 27
  • 43
0

As Robin says you must place your inside a div and assign those properties and height and width to the parent, the Robin answer doesn't have a height for that reason you can't see it centered

.wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  background-color: #ccc;
  height: 200px;
}
<div class="wrapper">
  <a href="/auth">Login</a>
</div>

.wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  background-color: #ccc;
}
<div class="wrapper">
  <a href="/auth">Login</a>
</div>
0

In general, for basic button no need to use flexbox.

For horizontally align change the display from display: inline (default value) to display block and add text-align center.

a{
  display: block;
  text-align: center;
  background: lightgray;
}
<a class="navigation-item__link" href="/auth">Login</a>

Sometimes you want to align inline-block buttons - the most simple way to do this is by extra text-align:center wrapper.

a{
  display: inline-block;
  padding: 10px 15px;
  background: lightgray;
  line-height: 1;
  
}

.align-center{
  text-align: center;
}
<div class="align-center">
   <a class="navigation-item__link" href="/auth">Log in</a>
   <a class="navigation-item__link" href="/auth">Sing up</a>
   <br><br>
   <a style="display: block;" class="navigation-item__link" href="/auth">Back</a>
 </div>

For V-align - use padding-top/bottom (Easier to manage space like this and not by flexbox or height/min-height): https://www.w3schools.com/css/css3_buttons.asp

a{
  display: block;
  text-align: center;
  background: lightgray;
  padding: 10px 15px;
}
<a class="navigation-item__link" href="/auth">Login</a>

If you want to align the button to some "card" flexbox is very useful.

a{
  display: block;
  text-align: center;
  background: lightgray;
  padding: 10px 25px;
}

.flex-align-v-h{
  min-height: 100vh;
  background: gray;
  display: flex;
  align-items: center;
  justify-content: center;
}

h1{
  text-align: center;
}
<section class="flex-align-v-h">
  <div>
    <h1>Hello</h1>
    <a class="navigation-item__link" href="/auth">Login</a> 
  </div>
</section>

If the text does not align to the button inspect element (Maybe your styles inherit extra top/bottom margin/padding).

Ezra Siton
  • 6,887
  • 2
  • 25
  • 37