Forum Moderators: not2easy

Message Too Old, No Replies

Seek help with horizontal line

         

PaulPA

10:56 pm on Dec 30, 2004 (gmt 0)

10+ Year Member



I'm trying to place a <hr /> tag as a separator between menu items. The menu is being generated within a CMS which automatically applies other coding to the item. Coding includes placement within a <td> and within a <span> that is inside the <td>. I am having problems using the <hr /> as a separator. When I check for validation of the code at WC3 I get this message:

"The mentioned element is not allowed to appear in the context in which you've placed it; the other mentioned elements are the only ones that are both allowed there and can contain the element mentioned. This might mean that you need a containing element, or possibly that you've forgotten to close a previous element.

One possible cause for this message is that you have attempted to put a block-level element (such as "<p>" or "<table>") inside an inline element (such as "<a>", "<span>", or "<font>")."

Anyone know a solution to this? I'd still like to use the <hr /> since it has special characteristics in the CSS.

Thanks.

bumpaw

9:58 pm on Dec 31, 2004 (gmt 0)

10+ Year Member



You didn't say what Doctype you were using but I tried this and it validated at W3C.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"></meta>
<title>Hr Test</title>
</head>
<body>
<table border="1" bgcolor="#00ffff" cellspacing="0" cellpadding="0">
<tr>
<td><span>Some Text</span><hr /></td>
</tr>
</table>
</body>
</html>

encyclo

10:05 pm on Dec 31, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<hr />
is a block-level element, but
<span>
is an inline element - hence the validation error (you can't have a block element within an inline one).

As bumpaw suggests, you could move the

<hr />
outside of the
<span>
, or possibly replace the
<span>
by a
<div>
, depending on your existing markup.

PaulPA

10:26 pm on Dec 31, 2004 (gmt 0)

10+ Year Member



Thanks - the <span> is built into the CMS scripting so I'd have to hack that. But it is probably doable.

createErrorMsg

3:29 am on Jan 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



the <span> is built into the CMS scripting so I'd have to hack that

You could avoid the potential nesting problems by setting the hardwired <span> to display:block in the CSS (just use the context it's in to ensure that other <span>s remain unaffected). The result is a block level span holding a block level horizontal rule.

The problem with this is that the HTML still won't validate (the html validator does not read your CSS, but rather bases it's error findings on the defaults for the code you use). However, if you're stuck with it, I'd say better to have an invalid, but working, page than a valid one that doesn't do what you want.

If the CMS won't allow you to insert the HR there, then that's another problem entirely.

cEM

PaulPA

11:53 pm on Jan 1, 2005 (gmt 0)

10+ Year Member



cEM - Do you have an example of this? The CMS I use does allow for unique classes for the <span> in question. Currently the code looks something like this:

<td><span class="main-menu" ><hr /></span></td>

Would your suggestion about a display : block work in this situation?

createErrorMsg

4:51 pm on Jan 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Paul, the question is what, exactly, you are aiming to fix. If nesting the <hr> in an inline <span> is causing rendering problems with your site, setting the <span> to display block ought to solve it. If nesting it isn't causing any problems with the site, but is merely preventing your pages from validating, then setting the span to display:block is not going to help. The HTML validator will not let you nest an element that defaults to display:block inside of an element that defaults to display:inline. It will throw up that error no matter what your CSS says.

I don't believe the actual specs state that you CANNOT nest blocks elements inside of inline elements (which makes the fact that the validator gives an error sort of odd), it merely runs the risk of doing weird things to your layout. I remember reading somewhere in the specs that the block level element splits the inline box in half, and results in some unpredictable alignments, etc. Again, I'm not 100% positive about this. I'll dig around and try to find that page.

[ADDED]Couldn't find a reference to what I hinted at above, but did locate the following quote:
Generally, inline elements may contain only data and other inline elements."
from this page [w3.org]

Note that they used the wording "Generally" as opposed to stating that you cannot nest them. Given the way spec documents are written, it's pretty clear that the wording is intentional, and I suspect the word choice stems from the fact that it is not a "page-breaker" sort of error. I'm not saying that we should go around flouting the specs, and it really makes no sense to intentionally nest a block element inside of an inline element, but in a case like this where the limits of your technology are up against a pretty vague HTML spec, I don't think its the end of the word if you do it.[/ADDED]

Regardless, I think your difficulty with doing this is likely to limit itself to validation. I don't suspect that putting that <hr> in that <span> is going to make the page behave wrongly, especially if it's the only thing in the table cell. So you may need to just make a decision: is it more important to use valid code, or to put that <hr> where I want it. Obviously, only you can make that decision.

cEM

PaulPA

10:42 pm on Jan 2, 2005 (gmt 0)

10+ Year Member



cEM

Thanks for a great answer. Right now I see no problems with how it is rendered. Seems to work well in IE, Mozilla and Firefox. I have not tried beyond those two but my guess is that it looks okay.

I am wondering, however, beyond the obvious potential problems with browsers handling something like this, are there any other ramifications? For instance, would any important spiders have a bad reaction to this?

Paul

createErrorMsg

2:58 am on Jan 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For instance, would any important spiders have a bad reaction to this?

Obviously, I can't answer that with any certainty, but nothing I've read, heard, or experienced would lead me to believe they would.

Spiders are generally after your content. The markup plays into that only insofar as it feeds the spider your content in an efficient, fast, and semantically relevant manner (or not). An <hr> tag has nothing to do with any of those things (and neither, incidentally, do the specific style properties in your CSS, with a few important exceptions), so I would say this combination is entirely a concern for your human visitors.

cEM

PaulPA

10:21 pm on Jan 3, 2005 (gmt 0)

10+ Year Member



Great. Thanks for your help.