问题在div中定位h1

时间:2014-03-16 01:39:32

标签: css html positioning

我正试图将h1放在div中。具体来说,我试图将它们水平和垂直居中,但我遇到了这样做的问题。我的代码粘贴在下面。任何帮助将不胜感激。

HTML

<!DOCTYPE HTML>
<html lang="en">

    <head>
        <link href="http://fonts.googleapis.com/css?family=Inconsolata" rel="stylesheet" type="text/css">
        <meta charset="utf-8">
        <meta name="description" content="Hi. I'm Zack. I like to code and build things.">
        <meta name="viewport" content="width=device-width, user-scalable=no">
        <link href="favicon.ico" rel="icon" type="image/ico" />
        <link href="favicon.ico" rel="shortcut icon" />
        <link rel="stylesheet" type="text/css" href="style.css">
        <link rel="stylesheet" type="text/css" href="meyer-reset.css">

        <title>Zack</title>
    </head>

    <body>

        <header>
            <nav>
                <ol>
                    <li id="A">About</li>
                    <li id="B">Blog</li>
                </ol>
            </nav>
        </header>

        <div id="teal1">
            <h1 class="text">Zack </h1>
        </div>

        <div id="teal2">
        </div>

        <div id="teal3">
        </div>

        <div id="blue1">
        </div>

        <div id="blue2">
        </div>

        <div id="blue3">
        </div>

        <div id="purple1">
        </div>

        <div id="purple2">
        </div>

        <div id="red1">
        </div>

    </body>



</html>

CSS 1

  body {
    font-family: 'Inconsolata', sans-serif;
    letter-spacing: 2px;
    text-transform: uppercase;
    font-size: 5px;

}

header{
    top: 0px;
    width: 100%;
    height: 40px;
    background-color: black;
    position: fixed;
}

header li {
    margin-right: 20px;
    margin-top: 10px;
}

li {
    color: white;
    list-style-type: none;
    display: inline-block;
}

ol {
    text-align: center;
}

#A {
    text-decoration: underline;
}

h1 {
    text-align: center;
    position: relative;
    margin-top: 200px;
}

div {
    height: 350px;
    width: 100%;
}

#teal1 {
    background: #1CF9D4;
}

#teal2 {
    background: #1DE4DB;
}

#teal3 {
    background: #1FD0E2;
}

#blue1 {
    background: #20BBE9;
}

#blue2 {
    background: #22A7F0;
}

#blue3 {
    background: #2392F7;
}

#purple1 {
    background: #4F87DC;
}

#purple2 {
    background: #727EC7;
}

#red1 {
    background: #DC6387;
}

CSS 2(meyer reset)

/* http://meyerweb.com/eric/tools/css/reset/ 
   v2.0 | 20110126
   License: none (public domain)
*/

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
    margin: 0;
    padding: 0;
    border: 0;
    font-size: 100%;
    vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
    display: block;
}
body {
    line-height: 1;
}
ol, ul {
    list-style: none;
}
blockquote, q {
    quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
    content: '';
    content: none;
}
table {
    border-collapse: collapse;
    border-spacing: 0;
}


    header li {
        margin-right: 20px;
        margin-top: 10px;
    }

    li {
        color: white;
        list-style-type: none;
        display: inline-block;
    }

    ol {
        text-align: center;
    }

    #A {
        text-decoration: underline;
    }

    h1 {
        text-align: center;
        position: relative;
        margin-top: 200px;
    }

    div {
        height: 350px;
        width: 100%;
    }

    #teal1 {
        background: #1CF9D4;
    }

    #teal2 {
        background: #1DE4DB;
    }

    #teal3 {
        background: #1FD0E2;
    }

    #blue1 {
        background: #20BBE9;
    }

    #blue2 {
        background: #22A7F0;
    }

    #blue3 {
        background: #2392F7;
    }

    #purple1 {
        background: #4F87DC;
    }

    #purple2 {
        background: #727EC7;
    }

    #red1 {
        background: #DC6387;
    }

1 个答案:

答案 0 :(得分:2)

这是一个修复:

h1 {
    text-align: center;
    position: relative;
    top: 200px;        <- made change
}