Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

html

css

I 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.

like image 945
Mr. Boy Avatar asked Jan 25 '10 20:01

Mr. Boy


People also ask

What is div /> in HTML?

The <div> tag defines a division or a section in an HTML document. The <div> tag is used as a container for HTML elements - which is then styled with CSS or manipulated with JavaScript.

What is the difference between a div and a div?

The <div> tag is a block level element. The <span> tag is an inline element. It is best to attach it to a section of a web page. It is best to attach a CSS to a small section of a line in a web page.

Why is the div element still used in HTML5?

The Div is the most usable tag in web development because it helps us to separate out data in the web page and we can create a particular section for particular data or function in the web pages. It is used to the group of various tags of HTML so that sections can be created and style can be applied to them.

What is the difference between div and paragraph in HTML?

DIV is a generic block level container that can contain any other block or inline elements, including other DIV elements, whereas P is to wrap paragraphs (text).


2 Answers

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.

like image 84
Chris Jester-Young Avatar answered Sep 26 '22 16:09

Chris Jester-Young


<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.

like image 29
Brandon Avatar answered Sep 24 '22 16:09

Brandon