Reputation: 3920
I passed a style sheet with the following to the W3C validator and it passed:
a {
display: block;
}
So want to make sure, is it valid markup to use an inline element as a block one? I know it will work but is it valid?
Thanks
Upvotes: 4
Views: 232
Reputation: 201588
The style sheet is valid, in the sense that it conforms to the CSS specifications. This is a purely formal thing. In CSS, the selector a
has no special meaning, it is just an identifier; CSS has no information about the meaning of a
in HTML, such as being an inline element, and the style sheet could in fact be used to style an XML document, where a
means something completely different.
HTML validity, on the other hand, does not depend on CSS at all. It is a formal thing about markup, and style sheets aren’t markup.
Whether it is “valid” in some other, informal sense (like “good practice” or “useful” or “conforming to a style guide”) is a different issue, and a debate issue rather than a technical question. Anyway, it is common usage to set display: block
on an a
element to make it possible to set its dimensions the way we can do for blocks (e.g., to make a link fill a table cell).
Upvotes: 0
Reputation: 207901
Yes, it's completely valid. And useful too.
One small note (since the above answer is very short), this method of styling normally inline elements as block elements is quite common. For example, when creating a horizontal navigation menu from a list, you'll often see <a>
elements styled with display:block
in order for the links to be able to take up the full width and height of the parent list item.
Upvotes: 5
Reputation: 12399
Yes it's valid but also you can use inline-block
to use an inline element while keeping the block properties.
Upvotes: 3
Reputation: 3072
Yes, it is valid.
There are elements, as you know, that are inline or block by default. But it is completely valid to then go and override this in CSS.
Upvotes: 2
Reputation: 59283
Yes, it is valid. However, if you really don't want to do that, you could wrap the a
in a div
. That would be useless though, because it is completely valid; that's why the display
attribute is there.
Upvotes: 1