tags:

views:

60

answers:

6

Hi, i'm trying to understand css and first thing i tried was this:

<div style="
    height: 100px;
    width: 100px;
    border: 1px solid black"/>

<div style="
    height: 100px;
    width: 100px;
    border: 1px solid red"/>

and

<div style="
    height: 100px;
    width: 100px;
    border: 1px solid black"></div>

<div style="
    height: 100px;
    width: 100px;
    border: 1px solid red"/>

can anybody please explain what's happening? i thought there should be no difference.. until now i was experiencing just pure xml so this is kinda frustrating for me..

and one more side question.. how does it work with displaying the whole thing? I assume first thing is the browser builds the DOM tree which would normally be displayed dependent from order of elements. Is it the same with css with only that little change defined by styles? i mean the rule from stylesheet is first applied when some concrete element is being displayed, no other tricks in it..right?

+1  A: 

CSS can be applied to any "tag" but you use of <div> is just not correct. A div tag always needs a closing tag:

<div><!-- HTML here --></div>

And what is happening is that you give the div a width and height of 100 pixels and a black border which is 1 pixel wide.

Maybe you want to read some CSS tutorials.

You can also try

<img src="path/to/some/image" style="border:5px solid black;" />

to see that it works with self-closing tags ;)

A list of valid self-closing tags in XHTML.

Felix Kling
Incorrect — it doesn’t always need a closing tag. `<div/>` or `<div />` is perfectly valid XHTML, which will create an empty, self-closing `div` element. http://stackoverflow.com/questions/2171471/is-css-for-pair-tags-only/2171499#2171499
Mathias Bynens
You're both half right. `<div />` is fine in XHTML but **not** in HTML-compatible XHTML (where it needs an end tag, although it doesn't need content).
David Dorward
@David: You are right, I mixed up the notations e.g. `<br>` with `<br />`.
Felix Kling
A: 

I'm not sure that your DIV problem is based on CSS, I think it's more a case of browser interpretation. Browsers expect DIVs to have content and maybe don't render those correctly. XHTML is XML compatible, but isn't itself (meaning it doesn't abide by ALL the rules of), XML.

CSS does work for self-closing tags - e.g. IMG or HR.

Your second question seems right. The browser effectively caches the stylesheet and applies the styles whenever it sees a relevant tag within the DOM. You never really see all this happening because it's generally done before any drawing is done to the page. If you dynamically add a DOM Tag, with a CSS reference, it would automatically take on that style defined in your stylesheet.

Amadiere
+1  A: 

<div>s are supposed to have content, so they are not self closing tags. Your CSS properties are fine, but your markup is incorrect.

kemp
+1  A: 

As mentioned, a div element needs start and end tags… Unless you do what you did in some of your examples:

<div />

Or…

<div/>

This is a self-closing (empty) div.

So, for your CSS tests, you probably don’t wanna use <div /> — use <div>Foo</div> instead.

Mathias Bynens
no I actually want just void box with no text :) and thus it's a bit illogical self-closing tag isn't enough, I'll have to get used though :)
stupid_idiot
Which browser are you using? It should work in modern browsers: http://jsbin.com/ugehu3
Mathias Bynens
I use mozilla, in the first case I described, it shows me the black box and the red box on the same place but one pixel shifted in each direction displayed over the black one. In the second case which is what I really desired, the red box is under the black one.
stupid_idiot
@stupid_idiot: You should have explained what you see in your question. Just from giving CSS code and *i thought there should be no difference* it is hard to guess what problem you face. A screenshot would have been nice.
Felix Kling
+5  A: 

You are, no doubt, serving your XHTML with a text/html content-type.

This is a hack designed to ease the transition from HTML to XHTML, so people can start using XHTML before browsers support it. After a decade, Internet Explorer still doesn't support it and I've gone back to HTML.

Since you are using this hack, you must write your XHTML to be compatible with HTML parsers as well as XML parsers. This means following the HTML Compatibility Guidelines which include:

If an element permits content (e.g., the div element) but an instance of that element has no content (e.g., an empty section), DO NOT use the "minimized" tag syntax (e.g., <div />).

Elements which do not permit content are defined as "EMPTY" and can be easily spotted on the HTML list of elements.

David Dorward
thx for the refrences. It will come handy
stupid_idiot
+1  A: 

If you really want self-closing DIVs:

  1. Serve your content as XHTML. This includes using the correct mime-type (application/xhtml+xml), but be aware that you'll need to handle browsers that still don't understand XHTML.

  2. Use a valid XHTML doctype and namespace,

e.g.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;

This will then display your DIVs as you expect but, in my experience, serving good old HTML 4 strict is a lot less hassle.

Bobby Jack