SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    daejeon, South Korea
    1 Post(s)
    0 Thread(s)

    difference between div AND span

    <!DOCTYPE html>
      <meta charset="utf-8">
      <title>Fluid transparent round corners - no images required</title>
    <style type="text/css">
    .bdr3{border:2px solid #00f}
    .floatRight {float:right}
    <body class="pdg5">
    <div style="background:yellow">
      <div class="inline-block bdr3 pdg5 bdr15">logo  9999</div>
      <div class="inline-block bdr3 pdg5 bdr15 floatRight">logIn 555</div>
    I have the code above at .

    I tried to change <div> to <span> so that I made the code below at .

    <div style="background:yellow">
      <span class="inline-block bdr3 pdg5 bdr15">logo  9999</span>
      <span class="inline-block bdr3 pdg5 bdr15 floatRight">logIn 555</span>
    I see there is no defference between them.
    What is the difference between them?
    If I put the rest code of each page, the difference between them will happen?
    What is the better choice between them?

  2. #2
    padawan silver trophybronze trophy markbrown4's Avatar
    Join Date
    Jul 2006
    Victoria, Australia
    28 Post(s)
    2 Thread(s)
    div are block level by default.
    span are inline.

    That is the only difference, they are nothing-elements only used as hooks for grouping or styling purposes.
    I generally only use spans if I need to style a section within a line of inline elements like paragraphs and images.

  3. #3
    The CSS Clinic is open silver trophybronze trophy
    Paul O'B's Avatar
    Join Date
    Jan 2003
    Hampshire UK
    182 Post(s)
    6 Thread(s)
    Quote Originally Posted by dotJoon View Post
    I see there is no defference between them.
    That's because you have used CSS to style then to be the same. Without CSS a span will not create a new line and its display is "inline" by default and therefore cannot take dimensions unless you change the display in some way (such as block,float or inline-block etc).

    Note that you cannot nest block elements inside inline elements so the choice of a span is usually applied to text/content that is usually contained within a larger context (such as an odd word in a paragraph).

    CSS doesn't care what most elements look like (form controls excluded) as you can always style then to suit. The more important question is "which tag is semantically correct to use in each situation ?". Don't confuse appearance with the structural semantics of html.

    In your example above you have two snippets of separate text so the choice is not clear cut and a case could be made for a span or a div but using a span will limit you later on should you wish to add structural html inside the span. Indeed looking at your example the left span seems to be a logo of some sorts and therefore should be a more structural html element. If the logo was the main logo of the page then I tend to use an h1 for that and use a p element for the login. If its just a general logo then I would use a p element for both. If you feel that you may add more data later to those elements (popups or more html elements) then both should most likely be divs.


Posting Permissions

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