Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML Forms - In your opinion, how should they be done and why? (<div> vs. <p>)

Tags:

html

css

forms

I've been a developer for a long time, however forms have always been my least favourite part, more specifically designing them!

I'd really like to know how you do you forms, and why. I've had a few discussions with fellow developers over <div> vs <p> on form fields. Which one would you stick with, and why?

An example of what I am talking about:

<form action="" method="post">
    <p>
        <label for="username">Username:</label>
        <input type="text" name="username" id="username" />
    </p>

    <p>
        <label for="submit"></label>
        <input type="submit" name="submit" id="submit" value="Log In" />
    </p>
</form>

VS

<form action="" method="post">
    <div>
        <label for="username">Username:</label>
        <input type="text" name="username" id="username" />
    </div>

    <div>
        <label for="submit"></label>
        <input type="submit" name="submit" id="submit" value="Log In" />
    </div>
</form>

When it comes to styling, it seems you can do pretty much the same with both, so is it just personal preference, or is there logic behind using one over the other?

Thanks for your time :)

UPDATE I ended up following a nice article on HTML5 forms and have actually found it to allow MUCH better styling of forms. They are much more organised from a development perspective too. For anyone interested, it is located here: http://24ways.org/2009/have-a-field-day-with-html5-forms

like image 534
Sk446 Avatar asked Jun 14 '11 09:06

Sk446


People also ask

What is the difference between div and p tag 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).

Why do we use div in HTML?

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 are HTML forms and why are they so important?

A webform, web form or HTML form on a web page allows a user to enter data that is sent to a server for processing. Forms can resemble paper or database forms because web users fill out the forms using checkboxes, radio buttons, or text fields.

Should I use form or div?

<form> is what you use to create a form for people to fill in. Div is used to divide sections of the web page up.


1 Answers

HTML is all about semantics. There is no reason username or submit should be inside a paragraph (<p>) because it's not a paragraph.

So I would go using <div> instead. Ok, maybe <div> has no meaning at all. But it's better than <p>.

like image 196
insumity Avatar answered Oct 03 '22 01:10

insumity