2

我有一个非常奇怪的问题,我不确定原因。我页面上的元素并没有固定在它们定义的位置,而是向上“跳跃”。

我当前的布局看起来是这样的,下面是 HTML 和 CSS: 当前布局

HTML

<!DOCTYPE html>

<head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Screening - Your movie database | Discover. Watch. Share.</title>
    <meta name="description" content="Screening is a brand new take on the traditional movie database, fusing social networking and multimedia to provide a clear, concise experience allowing you to share your favourite movies, and discover new classics.">
    <meta name="keywords" content="Movies, Films, Screening, Discover, Watch, Share, experience, database, movie database, film database, share film, share films, discover film, discover films, share movie, share movies, discover movie, discover movies">
    <link rel="stylesheet" href="stylesheet.css" type="text/css" />
    <link rel="stylesheet" href="fonts.css" type="text/css" />

</head>

<body>

    <header>

<p class="logo"><a href="index.php" id="logo">Screening</a></p>

<nav>

    <ul>
        <li><a href="index.php" title="Home">Home</a></li>      
    </ul>

</nav>

<section id="header_search">

    <form id="search" action="search.php" method="get">
        <input type="text" title="name" name="title" placeholder="search for a movie">
    </form>

</section>

</header>   
    <div id="content">

        <div id="home_banner">
            <h1>Discover. Watch. Share.</h1>
            <h2>Screening. Your movie database.</h2>
        </div>

        <form id="homesearch" action="search.php" method="get">
            <input type="text" title="name" name="title" placeholder="search for a movie">
        </form>

    </div>

    <footer>

<p class="logo"><a href="index.php" id="logo">Screening</a></p>

<nav>

    <ul>
        <li><a href="index.php" title="Home">Home</a> | </li>
        <li><a href="about.php" title="About">About</a> | </li>
        <li><a href="privacy.php" title="Privacy Policy">Privacy Policy</a></li>            
    </ul>

</nav>

<p id="copyright">©Screening 2012</p>

</footer>   
</body>

</html>

CSS

/* HTML reset */
html, * {
    margin: 0px;
    padding: 0px;
}

/* General styling */
body {
    font-family: 'RobotoLtRegular', Verdana, Helvetica, Arial, sans-serif;
    font-weight: normal;
    font-size: 1em;
    color: white;
    background-color: black;
    line-height: 20px;
    letter-spacing: 1.5px;
}

/*Custom link styles*/

a:link {
    color: white;
    background-color: transparent;
    text-decoration: none;
    -webkit-transition: color .4s linear;
    -moz-transition: color .4s linear;
    -ms-transition: color .4s linear;
    -o-transition: color .4s linear;
    transition: color .4s linear;
}

a:visited {
    color: white;
    background-color: transparent;
    text-decoration: none;
}

a:hover {
    color: #b70000;
    background-color: transparent;
    text-decoration: underline;
}

.logo a:link {
    text-decoration: none;
}

.logo a:hover {
    color: #b70000;
    background-color: transparent;
}

/* Header styling */
header {
    margin-bottom: 20px;
}

header, footer {
    height: 50px;
    padding-left: 50px;
    padding-right: 50px;
    font-size: .75em;
    clear: both;
}

header nav, .logo, footer nav {
    float: left;
}

header nav, footer nav, #header_search, #copyright {
    margin-top: 20px;
}

header li, footer li {
    display: inline;
}

#header_search {
    width: 200px;
    float: right;
    clear: right;
}

#search input, #homesearch input {
    padding: 0px 20px 0px 20px;
    color: #d7d7d7;
    background-color: #2d3035;
    border: solid 4px #2a2e31;
}

#search input:focus, #homesearch input:focus {
    outline: none;
    background-color: white;
    color: #6a6f75;
    -webkit-box-shadow: 0 1px 0 rgba(255, 255, 255, 0.1), 0 1px 0 rgba(0, 0, 0, 0.9) inset;
    -moz-box-shadow: 0 1px 0 rgba(255, 255, 255, 0.1), 0 1px 0 rgba(0, 0, 0, 0.9) inset;
    box-shadow: 0 1px 0 rgba(255, 255, 255, 0.1), 0 1px 0 rgba(0, 0, 0, 0.9) inset;
    text-shadow: 0 2px 3px rgba(0, 0, 0, 0.1);
}

#search input {
    width: 80%;
    height: 25px;
    font-size: 1.5em;
    border-radius:15px;
    -moz-border-radius:15px;
    -webkit-border-radius:15px;
}

/* Logo styling */
.logo {
    font-family: 'RobotoLtRegular', Verdana, Helvetica, Arial, sans-serif;
    font-size: 3em;
    width: 200px;
    margin-right: 10px;
    clear: left;
    border: solid 1px red;
}

/* Content wrapper */
#content {
    width: 90%;
    max-width: 1500px;
    margin: auto;
    padding: 30px 15px 30px 15px;
    clear: both;
    overflow: auto;
    background-color: rgba(203,203,203,0.2);
    background-image: url(assets/images/bkg.png);
    background-repeat: repeat;
}

/* Homepage */

#home_banner {
    padding-top: 50px;
    font-weight: normal;
    text-align: center;
    border: solid 1px red;
}

#home_banner h1 {
    font-size: 5em;
    clear: both;
    border: solid 1px blue;
}

#home_banner h2, #search input, #homesearch input, #searchresults, #details .title {
    font-family: 'RobotoThRegular', Verdana, Helvetica, Arial, sans-serif;
    font-weight: normal;
}

#home_banner h2 {
    font-size: 1.8em;
    clear: both;
    border: solid 1px yellow;
}

#homesearch {
    width: 480px;
    margin: 80px auto 70px auto;
    clear: both;
}

#homesearch input {
    width: 90%;
    font-size: 2em;
    -webkit-border-radius: 20px;
    -moz-border-radius: 20px;
    border-radius: 20px;
    text-shadow: 0 2px 2px rgba(0, 0, 0, 0.3); 
}

为了清晰起见,我为遇到问题的每个元素添加了边框。我最初正确地布置了所有东西,但是在某个地方出现了一些问题,我一生都无法弄清楚;我已经为此苦苦挣扎了好几个星期!我很确定这是一件小而简单的事情,但解决方案完全避开了我。

显然,预期的结果应该如下所示(忽略搜索栏的呈现,这是修改搜索栏之前的早期模型): 预期结果

4

2 回答 2

4

只需杀死 body 的固定行高: line-height: 20px; <--摆脱它。

于 2012-12-13T09:42:23.407 回答
2

您已将line-heighton设置<body>20px。任何没有 a 的元素line-height(例如你的<h1>and <h2>)都将继承这个行高大小,即使它们的字体大小要大得多。

通常最好设置相对于字体大小的行高,即不要在它们上指定像素单位。

body {
    ...
    font-size: 1em;
    line-height: 1.25;/* This makes 20px the base line height, but it’ll get bigger for lines with a bigger font size */
于 2012-12-13T09:44:34.683 回答