如何让我的侧边栏浮动?

时间:2014-10-15 13:09:45

标签: html css css-float sidebar floating

所以这让我疯了。过去两天我一直试图让我的侧边栏向右浮动,但它只是一直显示在页面底部!可能答案很简单,但我似乎无法自己弄明白......也许你们中的一个可以帮助我!

#wrap { 
	width: 1200px;
	/*margin: 0 auto;*/
	background-color:#e6fdff;
	color:#185358;
	padding:0;
}
#content {
	float:left;
	clear:right;
	padding:50px;
	padding-bottom:80px;
	/*margin:0 auto;*/
	right:250px;
}
#sidebar {
	float:right;
	width:300px;
}
#sidebar ul {
	list-style:none;
}
#sidebar ul a {
	text-decoration:none;
	font-family:'Crushed', cursive;
	color:#4B3C27;
}
#footer {
	width:100%;
	height:60px;
	position:fixed;
	bottom:0;
	left:0;
	border:1px solid #4B323F;
	border-width:1px 0;
	background-color:#E6FDFF;
}
<div id="wrap">
			<!--<div style="clear:both;"></div>-->
			<div id="content">
				<h2>Welcome to The Life of Esther!</h2>
				<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. 

Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. 

Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. 

Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. 

Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis. 
                </p>
				<div id="sidebar"> 
					<ul>
						<li><a href="blog1.html">Blog1</a></li>
						<li><a href="blog1.html">Blog1</a></li>
						<li><a href="blog1.html">Blog1</a></li>
						<li><a href="blog1.html">Blog1</a></li>
						<li><a href="blog1.html">Blog1</a></li>
						<li><a href="blog1.html">Blog1</a></li>
						<li><a href="blog1.html">Blog1</a></li>
						<li><a href="blog1.html">Blog1</a></li>
						<li><a href="blog1.html">Blog1</a></li>
						<li><a href="blog1.html">Blog1</a></li>
						<li><a href="blog1.html">Blog1</a></li>
						<li><a href="blog1.html">Blog1</a></li>
						<li><a href="blog1.html">Blog1</a></li>
					</ul>
				</div> <!--end sidebar div-->
			</div> <!--end content div-->
			<div id="footer"></div> <!--end footer div-->
		</div> <!--end wrap div-->

3 个答案:

答案 0 :(得分:0)

只需将#sidebar放在#content之外,然后为#content添加width:800px

答案 1 :(得分:0)

我将你的HTML更改为:

<div id="content">
                <div id="main-content"> 
                    <h2>Welcome to The Life of Esther!</h2>
                    <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. 

    Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. 

    Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. 

    Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. 

    Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis. 
                    </p>
                    </div>
                <div id="sidebar"> 
                    <ul>
                        <li><a href="blog1.html">Blog1</a></li>
                        <li><a href="blog1.html">Blog1</a></li>
                        <li><a href="blog1.html">Blog1</a></li>
                        <li><a href="blog1.html">Blog1</a></li>
                        <li><a href="blog1.html">Blog1</a></li>
                        <li><a href="blog1.html">Blog1</a></li>
                        <li><a href="blog1.html">Blog1</a></li>
                        <li><a href="blog1.html">Blog1</a></li>
                        <li><a href="blog1.html">Blog1</a></li>
                        <li><a href="blog1.html">Blog1</a></li>
                        <li><a href="blog1.html">Blog1</a></li>
                        <li><a href="blog1.html">Blog1</a></li>
                        <li><a href="blog1.html">Blog1</a></li>
                    </ul>
                </div> <!--end sidebar div-->
            </div>

然后我将css更改为:

#sidebar {
    float:right;
    width:300px;
    z-index: 10;
    position: absolute;
    right: 0;
    top: 0;
}

#main-content {
    position: absolute;
}

你必须使用right和top属性玩一点。您还必须设置主内容角色的宽度。

答案 2 :(得分:0)

首先,你需要从sidebar div中取出content并修改一些样式。请参阅http://jsfiddle.net/w8gek7u8/

上的更新代码

请参阅以下代码:

<style>
    #wrap { 
        width: 1200px;
        /*margin: 0 auto;*/
        background-color:#e6fdff;
        color:#185358;
        padding:0;
    }
    #content {
        float:left;
        clear:right;
        padding:50px;
        padding-bottom:80px;
        width:900px;
    }
    #sidebar {
        float:right;
        width:150px;
    }
    #sidebar ul {
        list-style:none;
    }
    #sidebar ul a {
        text-decoration:none;
        font-family:'Crushed', cursive;
        color:#4B3C27;
    }
    #footer {
        width:100%;
        height:60px;
        position:fixed;
        bottom:0;
        left:0;
        border:1px solid #4B323F;
        border-width:1px 0;
        background-color:#E6FDFF;
    }
    <style>

和HTML as

<div id="wrap">
            <!--<div style="clear:both;"></div>-->
            <div id="content">
                <h2>Welcome to The Life of Esther!</h2>
                <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. 

Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. 

Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. 

Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. 

Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis. 
                </p>
                            </div> <!--end content div-->

    <div id="sidebar"> 
                    <ul>
                        <li><a href="blog1.html">Blog1</a></li>
                        <li><a href="blog1.html">Blog1</a></li>
                        <li><a href="blog1.html">Blog1</a></li>
                        <li><a href="blog1.html">Blog1</a></li>
                        <li><a href="blog1.html">Blog1</a></li>
                        <li><a href="blog1.html">Blog1</a></li>
                        <li><a href="blog1.html">Blog1</a></li>
                        <li><a href="blog1.html">Blog1</a></li>
                        <li><a href="blog1.html">Blog1</a></li>
                        <li><a href="blog1.html">Blog1</a></li>
                        <li><a href="blog1.html">Blog1</a></li>
                        <li><a href="blog1.html">Blog1</a></li>
                        <li><a href="blog1.html">Blog1</a></li>
                    </ul>
                </div> <!--end sidebar div-->

            <div id="footer"></div> <!--end footer div-->
        </div> <!--end wrap div-->