我如何使用CSS对齐我的div容器

时间:2014-06-18 04:27:49

标签: php html css

如何在另一个容器内水平对齐我的容器。我也希望在调整窗口大小时将它们对齐,因为每次我尝试它时,容器会继续弹出某个地方,我不希望它们请帮助我谢谢你。下面是代码:

我的索引页

<html>
<head>
<title> Kwiktable </title>
<link rel="stylesheet" type="text/css" href="jssor.css">
</head>
<body>
<div class="headercontainer" style="z-index:1000;">
  <div class="header clearfix"> <span class="logo"><img src="img/kwiktable.png"><a href = "index.php">&nbsp <img src = "img/kwik.png"> </a></span>
    <input type="text" class="textbox" style="color:#888;" 
    value="Search" onfocus="inputFocus(this)" onblur="inputBlur(this)">
    <script>
        =function inputFocus(i){
    if(i.value==i.defaultValue){ i.value=""; i.style.color="#000"; }
        }
    function inputBlur(i){
    if(i.value==""){ i.value=i.defaultValue; i.style.color="#888"; }
        }
    </script>
    <div class="nav">
      <input type="checkbox" id="toggle">
      <label for="toggle" class="toggle" onclick=""></label>
      <ul class="menu">
        <li><a class="current" href="index.php">Home</a></li>
        <li><a href="aboutus.php">About Us</a></li>
        <li><a href="reserve.php">Reservations</a></li>
        <li><a href="login.php">Log in</a> </li>
      </ul>
    </div>
  </div>
</div>
<center>
<br>    <br>    <br>    <br>
<div style="margin:0 auto;width:100%;max-width:1050px;background-color:#E8E8E8;overflow:hidden; border: 1px solid #000000;">
<div class="container" style=" margin-left: 10px; width 100%; max-width: 450px; background-color:#E8E8E8;overflow:hidden;">
  <?php include 'content1.html';?>
  <?php include 'content.html'; ?>
</div>
<div class="container1" style="margin-right: 10px; margin-top: 10px; width 100%; max-width: 450px; background-color:#E8E8E8;overflow:hidden; "> </div>
<div  class="container2">
  <?php 
    echo "<br><h1>Kwiktable's Best</h1>";
    include 'best.php';
    ?>
  <br>      <br>      <br>      <br>
</div>
</center>
<?php include 'footer.php'; ?>
</body>
</html>

我的css文件:

* {
    margin: 0;
    padding: 0;
    outline: 0;
}

html, body {
    height: 100%;
    background: #fff;
}

body, a {
    font: normal 16px Helvetica, Verdana, Geneva, sans-serif;
    color: #3f3f3f
}

.textbox {
    margin-top: 25px;
    width: 500px;
    height: 30px;
    background-color: #FFEAEA;
    border: solid 1px #646464;
    border-radius: 5px;
    outline: none;
    padding: 2px 2px;
}

.textbox:hover {
    background-color: #F7C4C4;
    -webkit-transition: all .25s linear;
    -moz-transition: all .25s linear;
    -o-transition: all .25s linear;
    transition: all .25s linear
}

.textbox:focus {
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.83);
    -webkit-transition: all .25s linear;
    -moz-transition: all .25s linear;
    -o-transition: all .25s linear;
    transition: all .25s linear
}

iframe {
    margin-top: 15px;
    margin-right: 15px;
    margin-left: 15px;
    margin-bottom: 15px;
    border-radius: 10px;
    border: solid 1px #000;
}

.headercontainer {
    opacity: 0.95;
    display: block;
    margin: 0 auto;
    margin: 0 auto;
    background-image: url(img/menubar.jpg);
    background-color: #D93625;
    z-index: 1000;
    position: fixed;
    width: 100%;
    top: 0;
    left: 0;
    border-bottom: 1px solid #000;
    box-shadow: 0px 1px 1px #888888;
}

.headercontainer:after {
    content: '';
    display: block;
    clear: both
}

.footer {
    bottom: 0;
    margin: 0 auto;
    height: 60px;
    padding: 0 0;
    background: #bbbfbf;
    font-size: 12px;
    width: 100%;
    border-top: 1px solid #51c1f1
}
 @media only screen and (max-width:480px) {

.copyright { display: none }
}

body {
    -webkit-animation: bugfix infinite 1s;
    -webkit-font-smoothing: antialiased
}
 @-webkit-keyframes 
bugfix {  from {
 padding:0;
}

to { padding: 0; }
}

.header {
    position: relative;
    top: 0;
    left: 0;
    margin: 0 auto;
}

#toggle, .toggle { display: none }

.menu>li {
    list-style: none;
    float: left
}

.clearfix:before, .clearfix:after {
    display: table;
    content: ""
}

.clearfix:after { clear: both }
 @media only screen and (max-width:768px) {

.textbox {
    width: auto;
    -webkit-transition: all .2s linear;
    -moz-transition: all .2s linear;
    -o-transition: all .2s linear;
    transition: all .2s linear;
}

.menu {
    display: none;
    opacity: 0;
    width: 100%;
    position: absolute;
    right: 0
}

.menu>li {
    display: block;
    width: 100%;
    margin: 0
}

.menu>li>a {
    display: block;
    width: 100%;
    text-decoration: none;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box
}

.toggle {
    display: block;
    position: relative;
    cursor: pointer;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
    margin-top: 8px
}

#toggle:checked~.menu {
    display: block;
    opacity: 1
}
}

.header {
    min-height: 80px;
    max-width: 1500px;
    height: 100%;
    padding: 0 20px;
    background: #D93625;
    color: #fff;
}

.header>.logo {
    float: left;
    padding: 10px 50px;
    font-style: italic;
    font-size: 28px;
    line-height: 50px
}

.nav {
    display: block;
    float: right;
    text-align: right
}

.nav, .menu, .menu>li, .menu>li>a { height: 100% }

.menu>li>a {
    display: block;
    padding: 20px 17px;
    text-decoration: none;
    font-weight: normal;
    font-size: 16px;
    line-height: 2.8;
    color: #fff;
    border-radius: 10px;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    -webkit-transition: all .25s linear;
    -moz-transition: all .25s linear;
    -o-transition: all .25s linear;
    transition: all .25s linear
}

.menu>li>a:hover, .menu>li>a:focus {
    background: #8B0000;
    border-radius: 10px;
    color: #fff;
    background-color: #8B0000;
    box-shadow: 1px 1px 1px 1px #3D0000 inset;
    border-bottom: solid 1px #FF9292;
    text-shadow: 0px 0px 10px #E6FF00;
}

.menu>li>a.current {
    color: #fff;
    font-weight: 900
}

.toggle { z-index: 2 }
 @media only screen and (max-width:820px) {

.textbox {
    width: auto;
    -webkit-transition: all .2s linear;
    -moz-transition: all .2s linear;
    -o-transition: all .2s linear;
    transition: all .2s linear;
}

.menu {
    background: #363636;
    border-top: 1px solid #fff;
}

.menu, .menu>li, .menu>li>a { height: auto }

.menu>li>a {
    padding: 15px 15px;
    text-align: center;
    background-color: #363636;
    border-bottom: 1px solid #fff;
}

.menu>li>a:hover, .menu>li>a:focus {
    background: #D70000;
    padding: 15px 15px 15px 25px;
}

.toggle:after {
    content: 'Menu';
    box-shadow: 0px 0px 5px 0px #3D0000 inset;
    text-shadow: 0px 0px 10px #E6FF00;
    display: block;
    width: 80px;
    margin: 0 0;
    margin-top: 25px;
    padding: 10px 0;
    background: #D70000;
    -webkit-border-radius: 2px;
    border-radius: 2px;
    text-align: center;
    font-size: 12px;
    color: #fff;
    -webkit-transition: all .5s linear;
    -moz-transition: all .5s linear;
    -o-transition: all .5s linear;
    transition: all .5s linear;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box
}

.toggle:hover:after { background: #920000 }

h1 {
    margin: auto;
    width: 70%;
}

.container {
    width: 700px;
    margin-left: auto;
    margin-right: auto;
}

.container1 {
    margin-left: auto;
    margin-right: auto;
    width: 100%;
}

#toggle:checked+.toggle:after { content: 'Close' }
}

.share-icon {
    display: inline-block;
    float: left;
    margin: 4px;
    width: 32px;
    height: 32px;
    cursor: pointer;
    vertical-align: middle;
    background-image: url(img/share/share-icons.png)
}

1 个答案:

答案 0 :(得分:0)

您似乎错过了结束</div>标记,这很可能是为什么它会在整个网页上跳转的原因。使用这样的工具来帮助您:http://www.freeformatter.com/html-validator.html

响应式设计很好,但您可能希望更多地关注HTML和CSS。

<center>标签是一个糟糕的黑客。你应该用CSS做到这一点。此外,您还应使用CSS来使用<br> s作为间距。看看填充和边距。

如果你想<center>一个div,那么css替代方法是这样的:

<div class="container">
    <div style="margin:0 auto; width: 800px;">This will be centered </div>
</div>

只是为了让你开始。

这里发生的是容器div填充100%作为标准div应该。子div(带有样式标记的div)将其上下边距设置为0,将其左右边距设置为auto。这里的重要部分是div也有固定的宽度。因此,在这种情况下,div将占用800px,任何剩余的空间将auto在边缘上均匀分布。

你可以谷歌这个,并得到一个更好的解释我确定。