Centering not working. Inside a DIV tag.

Discussion in 'CSS' started by kinch, Jan 20, 2007.

  1. #1
    Hello all, I'm just learning CSS, so pardon my stupidness :p

    I just can't seem to center one piece of text within a div tag. Here is the code.

    <div id="content"><h1 style="text-align: 
    
    center">Kinch Designs</h1>
    Code (markup):
     
    kinch, Jan 20, 2007 IP
  2. kinch

    kinch Peon

    Messages:
    64
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Ok I just did a test in dreamweaver and I see it just creates another Div tag with the "align" attribute. According to here, the align attribute is deprecated for the DIV tag. Any alternatives?
     
    kinch, Jan 20, 2007 IP
  3. ajsa52

    ajsa52 Well-Known Member

    Messages:
    3,426
    Likes Received:
    125
    Best Answers:
    0
    Trophy Points:
    160
    #3
    Where is that extra div, inside the h1 ? Post the code to examine it.
     
    ajsa52, Jan 20, 2007 IP
  4. kinch

    kinch Peon

    Messages:
    64
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Ok, I have edited the code and got the effect I wanted:

    <html>
    <head>
    <link rel="stylesheet" 
    type="text/css" href="styles.css" />
    <title>Kinch Designs</title>
    </head>
    <body>
    <div id="content"><div style="text-align: 
    
    center;"><h1>Kinch Designs</h1></div>
    <p>I'm a freelance graphic artist!</p></div>
    <div id="leftnav">Navigation Bar!</div>
    </body>
    </html>
    Code (markup):
    However I do have one more question. When I type </div> I see it ends the <div style="..."> tag. Is that the order the ending tags always work, does the </div> always apply to the last <div> I wrote?
     
    kinch, Jan 20, 2007 IP
  5. ajsa52

    ajsa52 Well-Known Member

    Messages:
    3,426
    Likes Received:
    125
    Best Answers:
    0
    Trophy Points:
    160
    #5
    To have well written code, each opened tag need a closed tag. The </div> always apply to the PREVIOUS <div>. Maybe indenting your code helps:
    
    <html>
      <head>
        <link rel="stylesheet" type="text/css" href="styles.css" />
        <title>Kinch Designs</title>
      </head>
      <body>
        <div id="content">
          <div style="text-align: center;">
            <h1>Kinch Designs</h1>
          </div>
          <p>I'm a freelance graphic artist!</p>
        </div>
        <div id="leftnav">Navigation Bar!</div>
      </body>
    </html>
    
    Code (markup):
    Also, you can check your code online. Take a look at: http://validator.w3.org/
     
    ajsa52, Jan 20, 2007 IP
  6. kinch

    kinch Peon

    Messages:
    64
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Do you know of any editors that will automaticly indent the code?

    I have dreamweaver and front page... but right now I want to edit in just text.. and I know I will be tempted to move around page elements without code if it is just right in front of me.
     
    kinch, Jan 20, 2007 IP
  7. audax

    audax Peon

    Messages:
    83
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    audax, Jan 20, 2007 IP
  8. Breakpoint

    Breakpoint Peon

    Messages:
    164
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #8
    cool this has helped me
     
    Breakpoint, Jan 28, 2007 IP
  9. Ginger Ninja

    Ginger Ninja Guest

    Messages:
    161
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Indenting your code should really be considered a must. It helps readability and it's much easier to understand the overall structure when you look back at it.:)

    In Dreamweaver, with each new line you make just press TAB.
     
    Ginger Ninja, Jan 30, 2007 IP
  10. duilen

    duilen Active Member

    Messages:
    354
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    58
    #10
    You don't need an extra div if you put the text-align center in the div instead of the h1 tag. text-align:center centers content within the element not the element itself.

    <div id="content" style="text-align:

    center"><h1>Kinch Designs</h1></div>


    BTW, since you already have an id for the content div it would be better to simply put text-align:center in the stylesheet.
     
    duilen, Jan 31, 2007 IP