为什么我的侧边栏一直向下?
Why is my sidebar all the way down?
我正在尝试复制 9gag 首页只是为了练习。而且我不明白为什么我的侧边栏一直向下。我相信一定有一些简单的答案,但我就是想不通。
body {
margin: 0;
padding: 0;
font-family: sans-serif, serif;
}
nav {
background: #000;
}
.logo {
margin-left: 5px;
float: left;
height: 50px;
}
.logo img {
max-height: 46px;
width: auto;
margin-right: 10px;
margin-top: 2px;
cursor: pointer;
}
ul {
height: 50px;
list-style: none;
display: inline-block;
margin: 0px;
margin-left: -40px;
}
ul li {
display: inline-block;
}
ul li a {
display: block;
box-sizing: border-box;
line-height: 50px;
color: #FFF;
text-decoration: none;
}
.BigNav {
font-size: 14px;
font-weight: bold;
}
.BigNav a:hover {
color: #FFF;
background: #333;
}
.BigNav a {
border-right: 1px solid #333;
padding: 0 10px;
}
.SmallNav {
margin-left: -30px;
font-size: 14px;
}
.SmallNav a {
color: #999;
padding: 0 5px;
}
.SmallNav a:hover {
color: #FFF;
}
.notification {
float: right;
height: 50px;
}
.notification img {
max-height: 30px;
width: auto;
margin-right: 12px;
margin-top: 10px;
cursor: pointer;
}
.search {
float: right;
height: 50px;
}
.search img {
max-height: 24px;
width: auto;
margin-right: 12px;
margin-top: 13px;
cursor: pointer;
}
.profile {
float: right;
height: 50px;
vertical-align: middle;
}
.profile img {
max-height: 46px;
width: auto;
margin-right: 10px;
margin-top: 3px;
cursor: pointer;
}
.upload {
color: #FFF;
float: right;
background: #0099FF;
padding: 0 5px;
cursor: pointer;
font-weight: bold;
}
.upload:hover {
background: #13AAFF;
}
.wrapper {
width: 66%;
margin: 0 auto;
clear: both;
}
.post {
width: 66%;
float: left;
}
.post img {
width: 550px;
height: auto;
}
.aside {
width: 300px;
float: left;
}
.aside img {
width: 300px;
height: auto;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="css-1.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript" src="javascript-1.js"></script>
<title>9 GAG</title>
</head>
<body>
<header>
<nav>
<div class="logo">
<img src="logo.png">
</div>
<!--Menu-->
<ul class="BigNav">
<li><a href="#">Hot</a></li>
<li><a href="#">Trending</a></li>
<li><a href="#">Fresh</a></li>
<li><a href="#">Sections▾</a></li>
</ul>
<ul class="SmallNav">
<li><a href="#">Video</a></li>
<li><a href="#">Cosplay</a></li>
<li><a href="#">Girl</a></li>
<li><a href="#">Comic</a></li>
<li><a href="#">NSFW</a></li>
<li><a href="#">GIF</a></li>
<li><a href="#">WTF</a></li>
<li><a href="#">Geeky</a></li>
<li><a href="#">ʕ •ᴥ• ʔ</a></li>
<li><a href="#">Anime & Manga</a></li>
</ul>
<!--Right side icons-->
<div class="upload">
<p> + Upload </p>
</div>
<div class="profile">
<img src="person-flat.png">
</div>
<div class="notification">
<img src="not.png">
</div>
<div class="search">
<img src="search.png">
</div>
</nav>
</header>
<!--Content area-->
<div class="wrapper">
<div class="post">
<h3>I know I'm a douche for wearing sunglasses at night but...</h3>
<img src="http://img-9gag-fun.9cache.com/photo/a77Pzr2_700b.jpg">
</div>
<div class="post">
<h3>I know I'm a douche for wearing sunglasses at night but...</h3>
<img src="http://img-9gag-fun.9cache.com/photo/a77Pzr2_700b.jpg">
</div>
<div class="post">
<h3>I know I'm a douche for wearing sunglasses at night but...</h3>
<img src="http://img-9gag-fun.9cache.com/photo/a77Pzr2_700b.jpg">
</div>
<div class="post">
<h3>I know I'm a douche for wearing sunglasses at night but...</h3>
<img src="http://img-9gag-fun.9cache.com/photo/a77Pzr2_700b.jpg">
</div>
<div class="aside">
<img src="http://img-9gag-fun.9cache.com/photo/ao9dLpe_700b_v1.jpg">
<h4>Comment your country's favourite hangover food!</h4>
</div>
<div class="aside">
<img src="http://img-9gag-fun.9cache.com/photo/ao9dLpe_700b_v1.jpg">
<h4>Comment your country's favourite hangover food!</h4>
</div>
<div class="aside">
<img src="http://img-9gag-fun.9cache.com/photo/ao9dLpe_700b_v1.jpg">
<h4>Comment your country's favourite hangover food!</h4>
</div>
</div>
</body>
</html>
是的,我需要添加一些文本,因为代码太多了。提前谢谢大家。
您需要将所有帖子放在它们自己的容器中,并将所有旁白放在另一个容器中,两者都在您的包装器中。将 100% 宽度应用于包装器,并为两个容器分配相对宽度,并将主 div 浮动到左侧,侧边栏 div 浮动到右侧。 (如果你想要侧边栏位于 div 和屏幕右边缘之间的 space 中间,则将它们都向左浮动。)
请参阅此 fiddle(使用您的代码)作为示例。您仍然需要根据需要更改缩放比例,但这显示了概念。
相关代码:
.main{
float:left;
width:60%;
}
.sidebar{
float:right;
width:30%;
}
这可以解决您的问题
<div class="wrapper">
<div class="posts">
<div class="post">
....
</div>
<div class="post">
....
</div>
</div>
<div class="aside">
<div class="aside-item">
....
</div>
<div class="aside-item">
....
</div>
</div>
</div>
.posts{width:66%; float:left;}
.aside{widht:34%; float:left;}
.post,.aside-item{width:100%;}
<div class="wrapper">
<div class="posts" style="
width: 60%;
float: left;
">
<div class="post">
<h3>I know I'm a douche for wearing sunglasses at night but...</h3>
<img src="http://img-9gag-fun.9cache.com/photo/a77Pzr2_700b.jpg">
</div>
<div class="post">
<h3>I know I'm a douche for wearing sunglasses at night but...</h3>
<img src="http://img-9gag-fun.9cache.com/photo/a77Pzr2_700b.jpg">
</div>
<div class="post">
<h3>I know I'm a douche for wearing sunglasses at night but...</h3>
<img src="http://img-9gag-fun.9cache.com/photo/a77Pzr2_700b.jpg">
</div>
<div class="post">
<h3>I know I'm a douche for wearing sunglasses at night but...</h3>
<img src="http://img-9gag-fun.9cache.com/photo/a77Pzr2_700b.jpg">
</div>
</div>
<div class="side" style="
width: 40%;
float: right;
">
<div class="aside">
<img src="http://img-9gag-fun.9cache.com/photo/ao9dLpe_700b_v1.jpg">
<h4>Comment your country's favourite hangover food!</h4>
</div>
<div class="aside">
<img src="http://img-9gag-fun.9cache.com/photo/ao9dLpe_700b_v1.jpg">
<h4>Comment your country's favourite hangover food!</h4>
</div>
<div class="aside">
<img src="http://img-9gag-fun.9cache.com/photo/ao9dLpe_700b_v1.jpg">
<h4>Comment your country's favourite hangover food!</h4>
</div>
</div>
</div>
给侧边栏一些边距.........
我正在尝试复制 9gag 首页只是为了练习。而且我不明白为什么我的侧边栏一直向下。我相信一定有一些简单的答案,但我就是想不通。
body {
margin: 0;
padding: 0;
font-family: sans-serif, serif;
}
nav {
background: #000;
}
.logo {
margin-left: 5px;
float: left;
height: 50px;
}
.logo img {
max-height: 46px;
width: auto;
margin-right: 10px;
margin-top: 2px;
cursor: pointer;
}
ul {
height: 50px;
list-style: none;
display: inline-block;
margin: 0px;
margin-left: -40px;
}
ul li {
display: inline-block;
}
ul li a {
display: block;
box-sizing: border-box;
line-height: 50px;
color: #FFF;
text-decoration: none;
}
.BigNav {
font-size: 14px;
font-weight: bold;
}
.BigNav a:hover {
color: #FFF;
background: #333;
}
.BigNav a {
border-right: 1px solid #333;
padding: 0 10px;
}
.SmallNav {
margin-left: -30px;
font-size: 14px;
}
.SmallNav a {
color: #999;
padding: 0 5px;
}
.SmallNav a:hover {
color: #FFF;
}
.notification {
float: right;
height: 50px;
}
.notification img {
max-height: 30px;
width: auto;
margin-right: 12px;
margin-top: 10px;
cursor: pointer;
}
.search {
float: right;
height: 50px;
}
.search img {
max-height: 24px;
width: auto;
margin-right: 12px;
margin-top: 13px;
cursor: pointer;
}
.profile {
float: right;
height: 50px;
vertical-align: middle;
}
.profile img {
max-height: 46px;
width: auto;
margin-right: 10px;
margin-top: 3px;
cursor: pointer;
}
.upload {
color: #FFF;
float: right;
background: #0099FF;
padding: 0 5px;
cursor: pointer;
font-weight: bold;
}
.upload:hover {
background: #13AAFF;
}
.wrapper {
width: 66%;
margin: 0 auto;
clear: both;
}
.post {
width: 66%;
float: left;
}
.post img {
width: 550px;
height: auto;
}
.aside {
width: 300px;
float: left;
}
.aside img {
width: 300px;
height: auto;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="css-1.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript" src="javascript-1.js"></script>
<title>9 GAG</title>
</head>
<body>
<header>
<nav>
<div class="logo">
<img src="logo.png">
</div>
<!--Menu-->
<ul class="BigNav">
<li><a href="#">Hot</a></li>
<li><a href="#">Trending</a></li>
<li><a href="#">Fresh</a></li>
<li><a href="#">Sections▾</a></li>
</ul>
<ul class="SmallNav">
<li><a href="#">Video</a></li>
<li><a href="#">Cosplay</a></li>
<li><a href="#">Girl</a></li>
<li><a href="#">Comic</a></li>
<li><a href="#">NSFW</a></li>
<li><a href="#">GIF</a></li>
<li><a href="#">WTF</a></li>
<li><a href="#">Geeky</a></li>
<li><a href="#">ʕ •ᴥ• ʔ</a></li>
<li><a href="#">Anime & Manga</a></li>
</ul>
<!--Right side icons-->
<div class="upload">
<p> + Upload </p>
</div>
<div class="profile">
<img src="person-flat.png">
</div>
<div class="notification">
<img src="not.png">
</div>
<div class="search">
<img src="search.png">
</div>
</nav>
</header>
<!--Content area-->
<div class="wrapper">
<div class="post">
<h3>I know I'm a douche for wearing sunglasses at night but...</h3>
<img src="http://img-9gag-fun.9cache.com/photo/a77Pzr2_700b.jpg">
</div>
<div class="post">
<h3>I know I'm a douche for wearing sunglasses at night but...</h3>
<img src="http://img-9gag-fun.9cache.com/photo/a77Pzr2_700b.jpg">
</div>
<div class="post">
<h3>I know I'm a douche for wearing sunglasses at night but...</h3>
<img src="http://img-9gag-fun.9cache.com/photo/a77Pzr2_700b.jpg">
</div>
<div class="post">
<h3>I know I'm a douche for wearing sunglasses at night but...</h3>
<img src="http://img-9gag-fun.9cache.com/photo/a77Pzr2_700b.jpg">
</div>
<div class="aside">
<img src="http://img-9gag-fun.9cache.com/photo/ao9dLpe_700b_v1.jpg">
<h4>Comment your country's favourite hangover food!</h4>
</div>
<div class="aside">
<img src="http://img-9gag-fun.9cache.com/photo/ao9dLpe_700b_v1.jpg">
<h4>Comment your country's favourite hangover food!</h4>
</div>
<div class="aside">
<img src="http://img-9gag-fun.9cache.com/photo/ao9dLpe_700b_v1.jpg">
<h4>Comment your country's favourite hangover food!</h4>
</div>
</div>
</body>
</html>
是的,我需要添加一些文本,因为代码太多了。提前谢谢大家。
您需要将所有帖子放在它们自己的容器中,并将所有旁白放在另一个容器中,两者都在您的包装器中。将 100% 宽度应用于包装器,并为两个容器分配相对宽度,并将主 div 浮动到左侧,侧边栏 div 浮动到右侧。 (如果你想要侧边栏位于 div 和屏幕右边缘之间的 space 中间,则将它们都向左浮动。)
请参阅此 fiddle(使用您的代码)作为示例。您仍然需要根据需要更改缩放比例,但这显示了概念。
相关代码:
.main{
float:left;
width:60%;
}
.sidebar{
float:right;
width:30%;
}
这可以解决您的问题
<div class="wrapper">
<div class="posts">
<div class="post">
....
</div>
<div class="post">
....
</div>
</div>
<div class="aside">
<div class="aside-item">
....
</div>
<div class="aside-item">
....
</div>
</div>
</div>
.posts{width:66%; float:left;}
.aside{widht:34%; float:left;}
.post,.aside-item{width:100%;}
<div class="wrapper">
<div class="posts" style="
width: 60%;
float: left;
">
<div class="post">
<h3>I know I'm a douche for wearing sunglasses at night but...</h3>
<img src="http://img-9gag-fun.9cache.com/photo/a77Pzr2_700b.jpg">
</div>
<div class="post">
<h3>I know I'm a douche for wearing sunglasses at night but...</h3>
<img src="http://img-9gag-fun.9cache.com/photo/a77Pzr2_700b.jpg">
</div>
<div class="post">
<h3>I know I'm a douche for wearing sunglasses at night but...</h3>
<img src="http://img-9gag-fun.9cache.com/photo/a77Pzr2_700b.jpg">
</div>
<div class="post">
<h3>I know I'm a douche for wearing sunglasses at night but...</h3>
<img src="http://img-9gag-fun.9cache.com/photo/a77Pzr2_700b.jpg">
</div>
</div>
<div class="side" style="
width: 40%;
float: right;
">
<div class="aside">
<img src="http://img-9gag-fun.9cache.com/photo/ao9dLpe_700b_v1.jpg">
<h4>Comment your country's favourite hangover food!</h4>
</div>
<div class="aside">
<img src="http://img-9gag-fun.9cache.com/photo/ao9dLpe_700b_v1.jpg">
<h4>Comment your country's favourite hangover food!</h4>
</div>
<div class="aside">
<img src="http://img-9gag-fun.9cache.com/photo/ao9dLpe_700b_v1.jpg">
<h4>Comment your country's favourite hangover food!</h4>
</div>
</div>
</div>
给侧边栏一些边距.........