0

I am trying to center a div. This is my code:

<!Doctype Html>
<html>
    <head>
        <title>title</title>
        <style type="text/css">
            * {
                text-align: left;
            }
        </style>
    </head>
    <body>
        <div id="container" style="width: 500px; margin-left: auto; margin-right: auto; text-align: center;">
            <div id="content" style="text-align: center;">
                <p>Beispiel: DIV Container horizontal zentrieren.</p>
            </div>
        </div>
    </body>
</html>

It isn't completely center, because I set in the CSS reset that text-align is left. If I remove this line everything is fine. Why is text-align: center; of the div with the id #content not overriding the CSS reset?

Niklas
  • 23,674
  • 33
  • 131
  • 170

4 Answers4

1

You need !important to overwrite that (leaving the p tag)

#content{margin:0 auto; background:red; width: 500px; text-align: center !important;}

DEMO 1

Or else you can write css for p tag

#content p{text-align: center }

DEMO 2

Sowmya
  • 26,684
  • 21
  • 96
  • 136
0

Try this

Key point is

margin: 0 auto
Brend
  • 194
  • 6
0

Even better, less markup:

<!Doctype Html>
<html>
    <head>
        <title>title</title>
        <style type="text/css">
            #content{
                width: 500px;
                margin:0 auto;
            }
            #content p {
                text-align:center; 
            }
        </style>
    </head>
    <body>
        <div id="content">
            <p>Beispiel: DIV Container horizontal zentrieren.</p>
        </div>
    </body>
</html>

In most cases it's better to prevent useing ìnline-styles`. Also move your styles in an seperate css file for a better maintainability.

gearsdigital
  • 13,915
  • 6
  • 44
  • 73
0

In HTML * tag means its consider all tags strictly the property as you mention in between, you need to little change your style please visit link

The AV
  • 609
  • 3
  • 7
  • 17