I’ve read the techniques but after an hour and a half I can’t figure out what’s wrong. Am I missing a position? Why won’t the text center vertically?
.subscribe-btn {
display: flex;
margin: 0.5em;
width: 290px;
height: 50px;
background-image: linear-gradient(#4968b0, #3d559e);
border-radius: 20px;
cursor: pointer;
border-style: outset;
justify-content: center;
text-align: center;
}
.subscribe-btn a {
font-weight: bold;
font-size: 16px;
color: white;
text-decoration: none;
}
<div class="subscribe-btn">
<a href="https://www.bridgetodata.org/register">Subscribe to [WEBSITE NAME]</a>
</div>
I’ve been using combinations of aligning in both the parent and child, trying different displays in the parent and child.
5
Answers
You would need
align-items: center;
instead ofjustify-content
.You can get rid of justify-content since you are centrering horizontally using
text-align: center;
You need to add align-items:center;
Ok, so first of all you should not be using a
div
at all for a link you want to look like a button. You only need the tag that you will style to look like a button. Having thediv
be a part of the button in this case is needless and not semantic. So get rid of that by applying the button class to the element.Then you can simply combine the styles and add
align-items: center
to the styles and you will get what you are looking for. Since you are using flex, you can also get rid oftext-align: center
.But honestly unless you need all the buttons the exact same size, it would probably be better to not use flexbox at all and instead set
display: inline-block
and use padding instead of explicit height/width. Also userem
orem
units instead ofpx
units for the font-size and the padding. Like this:Please add:
It looks like you’re using the wrong property. In flexbox,
justify-content
property is used to align items on the main axis (horizontally), while thealign-items
property is used to align items on the cross axis (vertically). So from your styling, you’re only aligning the items on the main axis, and not on the cross axis as well.To fix the issue you’re having, you just need to add the
align-items: center
property to your.subscribe-btn
style class. Your updated code should look like this below:You can read more here MDN documentation on Aligning items in a flexbox container. Let me know if it works.