Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Reza Sorasti
491 PointsWhy the text in my <a> element is not center aligning?
Do you guys know why the text in my <a> element is not center aligning? In other words, why the text "Visit our HTML tutorial" is not center aligning?
<!DOCTYPE html> <html> <head> <style> .colortext { color: red; } "#MyLink{ text-align: center; }"
</style> </head> <body>
<h1>This is a Heading</h1> <p>This is a paragraph.</p> <p class="colortext">This is another paragraph.</p> <p class="colortext">This is also a paragraph.</p> <h1 class="colortext"> hi </hr> <br> <a id = "MyLink" class="colortext" href="https://www.w3schools.com/html/">Visit our HTML tutorial</a>
</body> </html>
4 Answers
Steven Parker
243,266 PointsThe link element is the same size as the text, so "centering" has no effect on it.
1) First of all I like to know why my code ... does not center the text in the <a> element.
It actually does, but by itself, a link ("a") is an inline element and would be no wider than the text it contains. So there's no visual difference between being centered and not. So to make your link centered on the page, you need a context that spans across the whole page. One way to do that would be to change the link into a block element:
#MyLink {
display: block;
text-align: center;
}
Or you could apply the style to a container of the element instead of the element itself:
<div id="MyLink">
<a class="colortext" href="https://www.w3schools.com/html/">Visit our HTML tutorial</a>
</div>
2) Can you please give me an example where I get to use both an ID and a Class inside of the same element.
If you use my suggestion of changing the link into a block element, your original code becomes an example of this. The ID is being used to apply the block display and the alignment, and the class is being used to change the color.
3) I also would like to know why it didn't work for my example.
I believe this is the same as question #1. See the answer to that one above.
Reza Sorasti
491 PointsYou code did work, and in fact center the text, but the think is one of the reasons I was playing with that code was because I wanted to get the experience of working with an element that has both an ID and a Class, because, I do see some times codes that an element has both properties of ID and Class. But I don't understand why in this case it was not working. I don't understand why a container needed to be created in order for centering to work.
Steven Parker
243,266 PointsI revised my answer above to include a more explicit description and an alternative solution using only CSS.
Reza Sorasti
491 PointsMy question has two parts: 1) First of all I like to know why my code: <a id = "MyLink" class="colortext" href="https://www.w3schools.com/html/">Visit our HTML tutorial</a> does not center the text in the <a> element. 2) The only reason I put both ID and a Class inside of the <a> tag is try to get the exprience of dealing with a tag that both an ID and a class. Can you please give me an example where I get to use both an ID and a Class inside of the same element. 3) I also would like to know why it didn't work for my example. Thanks
Steven Parker
243,266 PointsSee my original answer again. I revised it to address each question by number.
Reza Sorasti
491 PointsThanks for explaining these things to me Steven. You used two terms: "inline element and block element" which I am not familiar with. What exactly are they?
Steven Parker
243,266 PointsSome of the differences between block and inline elements are that block elements (such as "p" and "div") normally start on a new line and are as wide as the page, and inline elements (such as "a" and "span") can start anywhere on a line and are only as wide as their content. For more information, see these MDN documentation pages on block-level and inline elements.