Forum Moderators: not2easy

Message Too Old, No Replies

CSS descendant selectors and INPUT elements

XHTML/CSS problem

         

vincevincevince

8:54 am on May 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I want to nest a DIV within an INPUT element, which should be allowed in XHTML from what I've read of the standard, unfortunately it doesn't seem to work.

CSS:

input div {border:1px solid green;}

XHTML:
<input name="name"><div>TEST</div></input>

DECLARATION:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-tranitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

Can someone please tell me how this can be used? The validator complains that I didn't close the <input> tag, despite it being clearly closed with </input>. The DOM inspector in Firefox shows the <div> element as under a <fieldset> which is above the <input> element.

Do I need to change my XHTML declaration? Do I need to edit the DTD?

Update:
I have tried changing the declaration to:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

But that didn't make any difference!

Update 2:
I edited the xhtml1-strict.dtd (locally copied) and changed

<!ELEMENT input EMPTY>
to
<!ELEMENT input %Inline>
, then changed the path in the declaration to match the new file location. Still refused to recognise the DIV within the INPUT element.

Robin_reala

9:28 am on May 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you look at line 724 of the XHTML1.0 DTD [w3.org] you’ll see that input is an EMPTY element. This means that in XHTML it must be self-closing: i.e.
<input />
. So no, you can’t nest a <div> inside it. What are you trying to achieve? Maybe there’s a different way around this.

Ah, just seen your update 2. I’m afraid that browsers don’t actually read DTDs in the way you envisage.

[edited by: Robin_reala at 9:29 am (utc) on May 5, 2007]

encyclo

12:43 pm on May 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As Robin says, browsers don't actually read DTDs, rather they use their own internal DTD for everything served as HTML.

What exactly are you aiming to do with the

div
within an
input
? You mention CSS selectors, but I'm not sure what you're trying to style. Can you expand a bit?

vincevincevince

1:17 pm on May 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I want to have 'help tips' associated with an INPUT element, and my plan is this:

<input name="something"><div>help tip</div></input>

When used with CSS:


input div {display:none;}
input:focus div {display:block;}

I hoped that when changing focus to the input, the DIV would automatically display.

Is there any way to make the browser read the DTD or otherwise? Is there another way to achieve this?

Robin_reala

3:21 pm on May 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Considering that the :focus pseudoclass you're using doesn't work with IE anyway we can just use adjacent sibling selector [w3.org]:

<input/><div class="help">Help text</div>

input + div { display: none; }
input:focus + div { display: block; }

The '+' adjacent sibling selector selects elements that follow directly after another element.

[edited by: Robin_reala at 3:23 pm (utc) on May 5, 2007]

vincevincevince

3:18 am on May 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The '+' adjacent sibling selector

Thanks a lot for that! I'd never even seen that selector before, it look absolutely perfect.

<edit>Not really going to work though as it's not supported in IE6 and unlike :focus I can't find any workaround or javascript hack to make it work...</edit>

Xapti

3:53 am on May 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



IE7.js? not sure if it supports it or not but maybe.

vincevincevince

1:50 pm on May 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Xapti... it does seem to work :) thanks