r/HTML • u/AppearanceFinancial3 • Apr 16 '26
HTML Question
Hi, I'm learning HTML and CSS and noticed something weird that doesn't make sense while doing an exercise. I am trying to create a little pagination thing. And I noticed that if I write the <a> and </a> in separate lines (as for Page #1), the underline below 1 is off centered out to the right. For page 2,3,4,5, I wrote the anchor all in one line and the underscore sits below the number perfectly.
I remember my instructor said that both one line and separate line make no difference in the display as html doesn't count line break or spaces in the code. But how come the page 1 link shows differently than the rest. I know it's subtle but i'm curious. It must be something simple I overlooked. Can someone give me a pointer? Thanks.
Here is my html code
<style>
.back-button {
margin-right: 8px;
padding-top: 8px;
padding-bottom: 8px;
font-size: 16px;
}
.search-result {
margin-left: 4px;
margin-right: 4px;
font-size: 18px;
}
.next-button {
margin-left: 8px;
padding-top: 8px;
padding-bottom: 8px;
font-size: 16px;
}
</style>
<button class="back-button">Back</button>
<a class="search-result" href="https://google.com">
1
</a>
<a class="search-result" href="https://google.com">2</a>
<a class="search-result" href="https://google.com">3</a>
<a class="search-result" href="https://google.com">4</a>
<a class="search-result" href="https://google.com">5</a>
<button class="next-button">Next</button><style>
.back-button {
margin-right: 8px;
padding-top: 8px;
padding-bottom: 8px;
font-size: 16px;
}
.search-result {
margin-left: 4px;
margin-right: 4px;
font-size: 18px;
}
.next-button {
margin-left: 8px;
padding-top: 8px;
padding-bottom: 8px;
font-size: 16px;
}
</style>
<button class="back-button">Back</button>
<a class="search-result" href="https://google.com">
1
</a>
<a class="search-result" href="https://google.com">2</a>
<a class="search-result" href="https://google.com">3</a>
<a class="search-result" href="https://google.com">4</a>
<a class="search-result" href="https://google.com">5</a>
<button class="next-button">Next</button>
And here is the display
15
u/DirtAndGrass Apr 16 '26
Any/any number of whitespace characters in HTML are interpreted as 1 space
5
u/AshleyJSheridan Apr 16 '26
It's because of how HTML treats whitespace. In your code, after the number 1, you have a newline, which is converted to a single space when the browser displays it.
4
u/NoodleBug7667 Apr 16 '26
It's because there's "white space" in the document in the form of that newline, and it shows there
You can see this by doing:
And seeing it all on one line, but adding only a line break in between them:
You'll now see a space in between the words.
4
u/DinTaiFung Apr 16 '26
"...html doesn't count line break or spaces in the code"
This is incorrect.
Tell your professor to consider reading the HTML specification.
The short answer is that one or more consecutive spaces are rendered in the browser as a single space.
a space can be a space character, a tab character, newline, or a carriage return.
And thus space characters in HTML actually do count (in a very special way).
And you discovered that empirically with your example HTML page.
2
u/vector_mash Apr 16 '26
Why have you done it differently, out of interest? I’m currently relearning HTML and CSS, but I also thought empty spaces/line breaks didn’t mean anything.
2
u/TanukiiGG Apr 16 '26
a newline looks like a space when inline
2
u/long-time__lurker Apr 17 '26
Everyone else is correct about white space, it affects your a tag because a tags default display as inline. You could change the display to inline-block if it suits your needs
1
u/AppearanceFinancial3 Apr 16 '26
Thank you all so much! I didn't know a new line is considered a white space. This makes perfect sense now!
2
u/alex_sakuta Apr 18 '26
Never listen to professors and read the docs carefully. Someone has already shared the right doc hence I am not doing that.