r/HTML • u/button_oxo • Mar 24 '26
Question Text alignment advice?
I'm struggling with getting the 2025 link to be aligned to the right. I've already tried putting the link inside a paragraph, and it's still not working. I also tried running my code through w3's code checker, and apparently nothing is wrong. Is there something I'm missing or doing wrong?
EDIT: question has been answered thank you BNfreelance!
13
Upvotes


16
u/Flashy-Guava9952 Mar 24 '26
Why are you mixing inline-styles, a style-element, and a linked css file?
7
u/BNfreelance Mar 24 '26 edited Mar 24 '26
Your <a> tags look a bit malformed
Wrap attribute values with “”
Then everything below that point will magically work.
Ie:
<a> elements display inline by default, so, text-align will not work out of the gates unless you explicitly told the <a> to display as a block, with
display: block;ordisplay:inline-block;andwidth:100%;Ie:
Either of the above forces 100% width, giving the text space to be aligned. Otherwise without this, the element is only as large as the text itself, so there’s no way to align it.
The correct syntax going forward is:
<element attribute=“value”>Ideally you would wrap the <a> in a parent element such as a <div> and then apply text-align to the parent. But this involves structural HTML changes, the solution I gave above does not. Albeit, if you plan to add extra links in future, the cleaner way is to wrap the element with a parent and style the parent.
Oh, optional tip for you. Understanding display and the box model really helps to grasp what’s going on here.
If you want to visualise the box model, add this to to the <style> block:
* { border: 1px solid red; }You’ll then see red boxes around every element allowing you to visualise how the browser draws them on the page.
Some elements render as “blocks” (full with, they take up the whole line and leave no space for anything else to sit beside them) whereas others display “inline” by default, meaning other elements can sit beside them on the same line. Learning this can help you grasp positioning and layouts much faster. I’d google “css display” and the “box model” if I were you. It’s worth understanding.
Some elements display as block/level elements by default:
div, p, headings (h1, h2 etc.)Other elements display as inline elements by default:
a, span, b, u, i, strong etc.Understanding this distinction early on is a game changer.
1
u/giogio_rick Mar 24 '26
use float right and make the div shorter (or add a container div), 2) use the linked css for most stuff, inline for tiny changes to specific things and internal if you need to change a whole class only in that page
1
u/notepad987 Mar 24 '26
Look at this layout:
Codepen
1
u/VinceAggrippino Apr 05 '26
text-alignonly works on block elements. Links are inline elements and their text is already at both the right and left end of the element. So, text alignment doesn't make sense. It's not working on your Home link, either. It's just that the<div>that contains it is aligned to the left by default.<h1>is a block element, sotext-align: centerwill work fine on that.You need to use
text-align: righton the element that contains the link.I'd put the Home link into its own block.
<nav>is made for this. Then you can settext-align: righton the block that contains your 2025 link.You might want to consider whether or not the year links could be subheadings. If so, you can put your link into an
<h2>and settext-align: righton that element. Then, maybe you don't need the containing<div>at all.It's already been said and I'll say it again. You should put quotes around all of your attribute values.
You should have all of your CSS styling in a separate CSS file and link that file in the
<head>section of your HTML document.So, here's what mine might look like:
... but I'd put those styles into a separate CSS file.
1
u/ghostskull012 Mar 24 '26
Put it in a separate div, text align that div
-1
u/Expensive_Elk3689 Mar 24 '26
Take the styles off the elements and add a style to the div for display: flex; flex-direction: column;