I’m trying to wrap my title at the top of my webpage with a border.
But, whenever I do, the text and the border around it shift all the way to the left of the webpage.
I’m trying to do this with an h1 element. I also don’t want to just put a margin value to center it because it then will not scale with different webpage sizes.
#title {
color: rgb(226, 35, 35);
display: inline;
}
.borderedTitle {
border: 2px solid black;
display: inline-block;
border-radius: 10px;
padding: 10px;
}
.lato-bold {
font-family: "Lato", sans-serif;
font-weight: 700;
font-style: normal;
}
<span class="borderedTitle">
<h1 id="title" class="lato-bold">Title</h1>
</span>
I’ve tried different things like text-align: center
, align-content: center
, etc.
3
Answers
Try this:
Use a flexbox layout with
justify-content: center
This centres the elements within the flex container and also shrink-wraps them to their content size.
You don’t even need a wrapper. You can horizontally center a block-level element by giving it a
margin-left
andmargin-right
of0
such as usingmargin: 0 auto
. All you need to do is limit the width of the block-level element to the content by using:width: max-content
If you want to use a container you can even make use of
text-align
: