I’m in the process of learning HTML and CSS and practicing by writing some HTML for a personal page. I am starting by creating a header with my name aligned to the right, but the text is vertically aligned to the top. I’ve seen a few examples of some people trying to achieve similar things, but none appear to solve my problem, or provide a solution that appears to be much more complex than what I’m trying to do (align text on both x and y axes independently)
.header1 {
background-color: #000000;
border: none;
height: 40px;
top: 0px;
left: 0px;
width: 100%;
}
.margin-zero {
margin: 0px;
}
.header1Title {
color: white;
font-size: 25px;
height: 40px;
text-align: right;
vertical-align: middle;
}
<header class="header1 margin-zero">
<h1 class="header1Title margin-zero">Luis Argueta</h1>
</header>
4
Answers
To align right you can use justify-content: flex-end; and to centering it verically use align-items: center;
If you are just starting to learn HTML and CSS, I would suggest learning the flex property. It’s the easiest way to vertically align elements.
In the parent element
.header1
adddisplay: flex;
for setting the container as a flexbox.align-items: center;
will vertically align child elements andjustify-content: end;
will send it to the end of the parent container.I am not sure what you try to achieve still you can independently move element position in X and Y like this using
px
andmargin-top
property to move element from top.You won’t need a lot of the settings you added since they are default settings (top 0 , left 0, width: 100% etc.).
One way to achieve what you want is to use
display: flex
on the container withjustify-content: flex-end;
andalign-items: center;
which does the alignment: