开发者

Why is <div /> not treated the same as <div></div>

开发者 https://www.devze.com 2022-12-17 21:40 出处:网络
开发者_如何学JAVAI was just testing this and in both IE8 & Chrome I see the same thing, empty (styled) divs are rendered differently depending which way you do it. It annoys me because the former

开发者_如何学JAVAI was just testing this and in both IE8 & Chrome I see the same thing, empty (styled) divs are rendered differently depending which way you do it. It annoys me because the former seems so much neater.

Why?

EDIT: thanks for the clarifications on XHTML Vs HTML. Currently I have this:

<html>
<head>
<meta http-equiv="Content-Language" content="en-gb" />
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
<title>Test</title>

What is a better choice? I'd prefer XHTML as I believe it's a bit nicer.


In XHTML (served with Content-Type of application/xhtml+xml), <div /> would indeed work. But in HTML mode (text/html), then no; HTML is not XML, and XML empty tag syntax is not recognised.


<div /> is invalid markup. Since a div can't be self closing (it would just render an empty div), some browsers might render it differently. The HTML 4.01 specs state that divs (and spans) have to have both a start and end tag.

Also take a look at this question.


XHTML and HTML work differently.

In XHTML the 2 are identical and are semantically no different.

In plain HTML, you cannot self close tags. And making your tags self close is not bad, it just doesn't make a difference. The self closures are ignored.

So in plain HTML <div/> is seen as <div> which is never closed. So you need the </div> to close the tag.

HTML browsers have a hardcoded list of self closing tags and handle it for you.

0

精彩评论

暂无评论...
验证码 换一张
取 消