I am trying to make a homepage of some sorts and I can’t find a way to make a div center, I can’t really explain it good so the picture might help
The piece of code I’m trying to fix
I tried putting text-align: center; for the div but it only centered the text in the div, I wanted it to center the div just like the text above.
HTML:
<script src="https://kit.fontawesome.com/0c86b13d40.js" crossorigin="anonymous"></script> <div> <p class="text title">My Info</p> </div> <div id="buttons"> <i class="fa-solid fa-house"> H o m e </i> </div>
CSS:
.title {
font-size: 350%;
margin-top: 100px;
}
.text {
text-align: center;
}
#buttons {
text-align: center;
width: 130px;
border: 2px solid;
padding: 10px;
border-radius: 25px;
}
4
Answers
You can use the
flex
display property.Wrap the buttons inside a div set to flex:
Then use this CSS:
FYI
text-align
only works on content within the div itself.You used the
text-align
propoerty which centers text only;You should use the follow css to center elements:
it’ll center your elements both vertically and horizontally.
CSS property margin: 0 auto; can be used to center a div horizontally. By setting the left and right margins to auto, the div will be centered horizontally within its parent container.
It might be because they are not inside the same div?
It would be helpful if you could upload some code here but this code works too.