简体   繁体   English

CSS-左侧边栏不在左侧

[英]css - left sidebar not on left side

The left sidebar won't position itself on the left side of the main area. 左侧边栏不会将自己放置在主区域的左侧。 It seems to get stuck on the navigation link. 它似乎卡在导航链接上。 It should be placed "below" the navigation links and the "buttons" should overlap the sidebar just as they overlap the header. 应将其放置在导航链接的“下方”,“按钮”应与侧栏重叠,就像它们与标题重叠一样。 Then I want the text to wrap around the sidebar (works in the code I posted here). 然后,我希望文本环绕在侧边栏(在我在此处发布的代码中起作用)。

I have tried using z-index on the navigation and sidebar. 我尝试在导航栏和侧边栏上使用z-index I have also tried using position:relative; 我也尝试过使用position:relative; and float:left; float:left; on the sidebar without result. 在侧栏上没有结果。 The text should also wrap around the sidebar as it is in the example below. 文本也应像下面的示例中那样围绕边栏。 I managed to move the sidebar to the left using position:relative; 我设法使用position:relative;将边栏移到左侧position:relative; but then the text won't wrap it. 但随后的文字将不会自动换行。

HTML: HTML:

<html lang="sv">
<head>
<link href="layout1.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="topbanner">
    <h1>TopBanner</h1>
</div>
<div class="header">
    <h1>Header</h1>
    <h2>underrubrik</h2>
</div>
<div class="navigation">
    <ul>
        <li><a href="#">Page 1</a></li>
        <li><a href="#">Page 2</a></li>
        <li><a href="#">Page 3</a></li>
        <li><a href="#">Page 4</a></li>
    </ul>
</div>
<div id="main">
    <div class="leftsidebar">
        <ul>
            <li><a href="#">Page 1</a></li>
            <li><a href="#">Page 2</a></li>
            <li><a href="#">Page 3</a></li>
            <li><a href="#">Page 4</a></li>
        </ul>
    </div>
    <div class="content">
        <p>Content text here</p>
    </div>
</body>
</html>

CSS CSS

body {
    margin: 0;
    background: #fff;
}
/* BANNER */
.topbanner {
    width: 980px;
    height: 80px;
    margin: auto;
    border: 1px solid;
    text-align: center;
    font-family: sans-serif;
}
/* HEADER */
.header {
    width: 980px;
    height: 100px;
    margin: auto;
    border: 1px solid;
    text-align: center;
    font-family: sans-serif;
}
.header h1 {
    margin: 0;
}
.header h2 {
    margin: 0;
}
/* NAVIGATION LINKS */
.navigation {
    width: 980px;
    margin: auto;
}
.navigation a {
    text-decoration: none;
    color: #000;
}
.navigation ul  {
    float: left;
    margin: -10px 0 0 0;
    list-style: none;
}
.navigation ul li {
    display: inline;
    padding: 0 10px 0 10px;
    border: 1px solid;
    background: #fff;
}
.navigation ul li:first-child {
    border-radius: 10px 0 0 10px;
}
.navigation ul li:last-child {
    border-radius: 0 10px 10px 0;
}
/* CONTENT */
#main {
    width: 980px;
    margin: 0 auto;
    border: 1px solid;
}
.leftsidebar {
    width: 20%;
    position: relative;
}
.leftsidebar ul {
    margin: 0;
    border: 1px solid;
    list-style: none;
}
.leftsidebar ul li {
}
.content {      
}
.content p {
    font-family: sans-serif;
}

Set the following for .navigation ul styles .navigation ul样式设置以下内容

margin: -10px 0 -10px 0;
position: relative;
z-index: 2;
list-style: none;

Add float: left; 添加float: left; to .leftsidebar to allow text to wrap around that, and add padding-top: 20px 30px; .leftsidebar以允许文本环绕,并添加padding-top: 20px 30px; (adjust to your liking) to .leftsidebar ul to compensate for the overlap. (根据您的喜好进行调整)到.leftsidebar ul以补偿重叠。

http://jsfiddle.net/ocfjsqpp/3/ http://jsfiddle.net/ocfjsqpp/3/

尝试将padding-left:0放入您的ul容器中。

Here have a look at what I did here with you code, also you haven't ended one of you h2 tags at the start. 这里看看我在这里对您的代码所做的事情,同样,您在开始时还没有结束其中一个h2标签。

Visit http://jsfiddle.net/4p6gdka9/ enter code here 访问http://jsfiddle.net/4p6gdka9/ enter code here

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM