SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Addict
    Join Date
    Feb 2003
    Location
    eez
    Posts
    331
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Is this bad practice?

    Hi!

    I wopuld like to know if putting inline elements directly in Divs is bad practice or not? Or should I prefer:

    HTML Code:
    <div id="test"><p>text comes here</p></div>
    Wait a minute... why not just make:

    HTML Code:
    <p id="test">text comes here</p>

    Is this last line of code bad practice if the <p is floated left or right? (I mean, should floats only be applied on divs and so on? and never on <p>s?)

    Thanks

  2. #2
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by aqw
    Is this last line of code bad practice if the <p is floated left or right? (I mean, should floats only be applied on divs and so on? and never on <p>s?)
    It's not bad practice. In fact, it's excellent practice!

    Keep the markup as clean as possible. DIVs are last resorts; something you use when all other options have been exhausted. CSS can be applied to any HTML element.
    Birnam wood is come to Dunsinane

  3. #3
    SitePoint Addict
    Join Date
    Feb 2003
    Location
    eez
    Posts
    331
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks!

  4. #4
    Caveat surfer Buddy Bradley's Avatar
    Join Date
    May 2003
    Location
    Cambridge, UK
    Posts
    2,366
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AutisticCuckoo
    DIVs are last resorts; something you use when all other options have been exhausted.
    You should add "for applying a certain styling using CSS" to that sentence. There's nothing wrong with DIVs from a markup point-of-view.

  5. #5
    SitePoint Wizard bronze trophy Tyssen's Avatar
    Join Date
    Oct 2005
    Location
    Brisbane, QLD
    Posts
    4,067
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by aqw
    should floats only be applied on divs and so on?
    Floats can be applied to whatever you like.

  6. #6
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Buddy Bradley
    You should add "for applying a certain styling using CSS" to that sentence. There's nothing wrong with DIVs from a markup point-of-view.
    Except that they don't convey much semantics, other than 'a division of the page'.

    You don't use <div class="page-heading"> when <h1> would suffice.

    What I tried to say was that you only resort to DIVs when there is no semantically correct element type available.
    Birnam wood is come to Dunsinane

  7. #7
    Caveat surfer Buddy Bradley's Avatar
    Join Date
    May 2003
    Location
    Cambridge, UK
    Posts
    2,366
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AutisticCuckoo
    Except that they don't convey much semantics, other than 'a division of the page'.
    You could say the same about any other HTML element.

  8. #8
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Buddy Bradley
    You could say the same about any other HTML element.
    Au contraire. The only element type with weaker semantics than DIV is SPAN. Everything else has clearly (well...) defined semantics.

    P is a paragraph of text (one or more sentences dealing with a certain thought).
    UL is a unordered list: a list where the order of the items does not impact the meaning of the list as a whole.
    OL is an ordered list: a list where you cannot change the order of the items without changing the meaning of the entire list.
    TABLE is a table of data that has relations in two or more dimensions.
    ADDRESS is contact information of the page (or a part thereof).
    EM signifies emphasis.
    Etc. etc. etc.
    Birnam wood is come to Dunsinane

  9. #9
    SitePoint Evangelist Ed Seedhouse's Avatar
    Join Date
    Aug 2006
    Location
    Victoria, B.C. Canada
    Posts
    592
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by aqw

    HTML Code:
    <div id="test"><p>text comes here</p></div>
    Wait a minute... why not just make:

    HTML Code:
    <p id="test">text comes here</p>
    Is this last line of code bad practice if the <p is floated left or right? (I mean, should floats only be applied on divs and so on? and never on <p>s?)
    To the contrary the first way, using a meaningless and unnecessary DIV, is bad style. You should only use elements that are required and they should be the elements that best describe your content.

    DIVs are handy for putting several elements in a group for a purpose such as positioning, but by themselves are largely semantically meaningless and to be avoided unless no more appropriate element is available.
    Ed Seedhouse


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •