如何将容器水平和垂直放在另一个容器内?

时间:2014-09-22 19:31:25

标签: html css

我在html中乱搞,我希望这篇文章在该部分的中心是水平和垂直的。我在网上查看了不同的论坛,但都没有。这是代码。

/* Style */

    #section {
    	width: 70%;
    	height: 800px;
    	background: #555;
    	margin-left: auto;
    	margin-right: auto;
    	margin-top: 50px;
    	margin-bottom: 50px;
    }
    
    #nav {
    	height: 100px;
    }
    
    #article {
    	width: 90%;
    	height: 400px;
    	background-color: #fff;
    }
    
    #footer {
    	width: 100%;
    	height: 200px;
    	background-color: #fff;
    }

/* Nav */
    
    body {
      font-family: 'Lucida Grande', 'Helvetica Neue', Helvetica, Arial, sans-serif;
      padding: 20px 50px 150px;
      font-size: 13px;
      text-align: center;
      background: #E3CAA1;
    }
    
    ul {
      text-align: left;
      display: inline;
      margin: 0;
      padding: 15px 4px 17px 0;
      list-style: none;
      -webkit-box-shadow: 0 0 5px rgba(0, 0, 0, 0.15);
      -moz-box-shadow: 0 0 5px rgba(0, 0, 0, 0.15);
      box-shadow: 0 0 5px rgba(0, 0, 0, 0.15);
    }
    ul li {
      font: bold 12px/18px sans-serif;
      display: inline-block;
      margin-right: -4px;
      position: relative;
      padding: 15px 20px;
      background: #fff;
      cursor: pointer;
      -webkit-transition: all 0.2s;
      -moz-transition: all 0.2s;
      -ms-transition: all 0.2s;
      -o-transition: all 0.2s;
      transition: all 0.2s;
    }
    ul li:hover {
      background: #555;
      color: #fff;
    }
    ul li ul {
      padding: 0;
      position: absolute;
      top: 48px;
      left: 0;
      width: 150px;
      -webkit-box-shadow: none;
      -moz-box-shadow: none;
      box-shadow: none;
      display: none;
      opacity: 0;
      visibility: hidden;
      -webkit-transiton: opacity 0.2s;
      -moz-transition: opacity 0.2s;
      -ms-transition: opacity 0.2s;
      -o-transition: opacity 0.2s;
      -transition: opacity 0.2s;
    }
    ul li ul li { 
      background: #555; 
      display: block; 
      color: #fff;
      text-shadow: 0 -1px 0 #000;
    }
    ul li ul li:hover { background: #666; }
    ul li:hover ul {
      display: block;
      opacity: 1;
      visibility: visible;
    }


HTML
<!DOCTYPE html>
    <html>
    	<head>
    		<meta charset="UTF-8">
    		<link rel="stylesheet" type="text/css" href="stylesheets/style.css">
    		<link rel="stylesheet" type="text/css" href="stylesheets/nav.css">
    		<title>Alec Grogan | Home</title>
    	</head>
    	<body>
    		<header id="header">
    			<h1>Alec Grogan</h1>
    		</header>
    		<nav id="nav">
    			<ul>
    				<li>Home</li>
    				<li>About</li>
    				<li>Resume</li>
    				<li>Portfolio
    					<ul>
    						<li>Web Design</li>
    						<li>Photoshop</li>
    					</ul>
    				</li>
    				<li>Blog</li>
    				<li>Contact</li>
    			</ul>
    		</nav>
    		<section id="section">
    			<article id="article">
    			</article>
    		</section>
    		<footer id="footer">
    		</footer>
    	</body>
    </html>

3 个答案:

答案 0 :(得分:0)

以水平和垂直为中心使用以下内容:

父容器将其添加到样式:

  

职位:相对;

您要居中的容器将其添加到样式中:

  保证金:自动;位置:绝对;顶部:0;左:0;底部:0;对:   0;

结果如下:

#section {
    width: 70%;
    height: 800px;
    background: #555;
    margin-left: auto;
    margin-right: auto;
    margin-top: 50px;
    margin-bottom: 50px;
    position: relative;
}
#nav {
    height: 100px;
}
#article {
    width: 90%;
    height: 400px;
    margin: auto;
    margin: auto;
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    background-color: #fff;
}
#footer {
    width: 100%;
    height: 200px;
    background-color: #fff;
}
body {
    font-family:'Lucida Grande', 'Helvetica Neue', Helvetica, Arial, sans-serif;
    padding: 20px 50px 150px;
    font-size: 13px;
    text-align: center;
    background: #E3CAA1;
}
ul {
    text-align: left;
    display: inline;
    margin: 0;
    padding: 15px 4px 17px 0;
    list-style: none;
    -webkit-box-shadow: 0 0 5px rgba(0, 0, 0, 0.15);
    -moz-box-shadow: 0 0 5px rgba(0, 0, 0, 0.15);
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.15);
}
ul li {
    font: bold 12px/18px sans-serif;
    display: inline-block;
    margin-right: -4px;
    position: relative;
    padding: 15px 20px;
    background: #fff;
    cursor: pointer;
    -webkit-transition: all 0.2s;
    -moz-transition: all 0.2s;
    -ms-transition: all 0.2s;
    -o-transition: all 0.2s;
    transition: all 0.2s;
}
ul li:hover {
    background: #555;
    color: #fff;
}
ul li ul {
    padding: 0;
    position: absolute;
    top: 48px;
    left: 0;
    width: 150px;
    -webkit-box-shadow: none;
    -moz-box-shadow: none;
    box-shadow: none;
    display: none;
    opacity: 0;
    visibility: hidden;
    -webkit-transiton: opacity 0.2s;
    -moz-transition: opacity 0.2s;
    -ms-transition: opacity 0.2s;
    -o-transition: opacity 0.2s;
    -transition: opacity 0.2s;
}
ul li ul li {
    background: #555;
    display: block;
    color: #fff;
    text-shadow: 0 -1px 0 #000;
}
ul li ul li:hover {
    background: #666;
}
ul li:hover ul {
    display: block;
    opacity: 1;
    visibility: visible;
}
<header id="header">
     <h1>Alec Grogan</h1>

</header>
<nav id="nav">
    <ul>
        <li>Home</li>
        <li>About</li>
        <li>Resume</li>
        <li>Portfolio
            <ul>
                <li>Web Design</li>
                <li>Photoshop</li>
            </ul>
        </li>
        <li>Blog</li>
        <li>Contact</li>
    </ul>
</nav>
<section id="section">
    <article id="article"></article>
</section>
<footer id="footer"></footer>

答案 1 :(得分:0)

实际上非常简单,您只需要添加 #article 以下代码:

float: right;

要了解有关浮动的更多信息,您可以查看此网站http://www.w3schools.com/css/css_float.asp并了解有关此元素的更多信息!

答案 2 :(得分:0)

尝试使用以下内容更新您的CSS以实现!!

CSS:

article{
display:table-cell;
vertical-align: middle;
}