简体   繁体   English

CSS右侧栏未与右侧对齐

[英]CSS right side bar not aligned to the right

On my webpage the right side bar is at the bottom of the main content. 在我的网页上,右侧栏位于主要内容的底部。 and is not properly aligned with the main content section. 并且未与主要内容部分正确对齐。 The CSS code for the right side bar is: 右侧栏的CSS代码为:

#sidebar{
 background: #ddd;
 float: right;
 }

There is a <div> section named sidebar-wrap which is the container for the sidebar that has the following code: 有一个名为sidebar-wrap的<div>节,它是带有以下代码的边栏的容器:

#sidebar-wrap {
float: right; 
width: 200px; 
padding: 20px;
margin-top: 20px;
margin-bottom: 20px;
padding-left:20px;
padding-right:20px;
}

You have right-side padding that pushes the text to the left. 您可以使用右侧填充将文本推到左侧。 Just remove that if you don't want it. 如果您不想要,请将其删除。 Eg 例如

#sidebar-wrap {
  float: right;
  width: 200px;
  padding: 20px;
  margin: 20px 0;
  background: rgb(221, 221, 221);
}

Update: I've moved the background color to #sidebar-wrap and replaced the padding, for a better effect. 更新:我已经将背景色移到#sidebar-wrap并替换了填充,以获得更好的效果。

Also, for the sidebar to sit beside the main content, you need to move it above the .entry-content div. 另外,要使侧边栏位于主要内容旁边,您需要将其移动到.entry-content div上方。 There are other ways to do this and keep the content first in the HTML, but this is the easiest way to do it if you don't mind the sidebar coming first in the HTML. 还有其他方法可以将内容保留在HTML中,但如果您不介意边栏在HTML中排在首位,则这是最简单的方法。

Here's an example of re-ordering the code (with a few inline styles for simplicity, though they should be moved to the style sheet, of course): 这是重新排序代码的示例(为简单起见,有一些内联样式,尽管当然应该将它们移到样式表中):

<h1>Main Content</h1>
<div id="sidebar-wrap" style="margin-top: 0; padding-top: 0;">
    <div id="sidebar">
        <p>this is the sidebar.</p>
    </div>
</div>
<div class="entry-content" style="overflow: hidden;">
    <p>There was nothing so VERY remarkable in that; nor did Alice think it so VERY much out of the way to hear the Rabbit say to itself, &#8220;Oh dear! Oh dear! I shall be late!&#8221; (when she thought it over afterwards, it occurred to her that she ought to have wondered at this, but at the time it all seemed quite natural); but when the Rabbit actually TOOK A WATCH OUT OF ITS WAISTCOAT-POCKET, and looked at it, and then hurried on, Alice started to her feet, for it flashed across her mind that she had never before seen a rabbit with either a waistcoat-pocket, or a watch to take out of it, and burning with curiosity, she ran across the field after it, and fortunately was just in time to see it pop down a large rabbit-hole under the hedge.</p>
    <p>In another moment down went Alice after it, never once considering how in the world she was to get out again. The rabbit-hole went straight on like a tunnel for some way, and then dipped suddenly down, so suddenly that Alice had not a moment to think about stopping herself before she found herself falling down a very deep well.</p>
</div>

you need to add 2 extra divs and increase the size of page to 800px or so, add two divs ie 您需要添加2个额外的div,并将页面大小增加到800px左右,再添加两个div,即

    .leftDiv {
    float:left;
    width:500px;
    }

    .rightDiv{
    float:right;
    width:295px;
    }

    .clear{
    clear:both;
    }

the divs need to seperate the text and the side bar so for example: div需要将文本和侧栏分开,例如:

            <div class="leftDiv">


                                <h1>Main Content</h1>
           <div class="entry-content">
               <p>There was nothing so VERY remarkable in that; nor     ll.</p>
            </div>
            </div>

            <div class="rightDiv">

            <div id="sidebar-wrap">
            <div id="sidebar">
            <p>this is the sidebar.</p>
            </div>
            </div>

    <div class="clear"> </div>

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

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