简体   繁体   English

固定侧边栏,它下面的内容,如何修复?

[英]Fixed sidebar, content under it, how to fix?

I'm trying to make a fixed sidebar, that will stay on that position when you scroll the page(as in the 2nd image), but the page content is going under that sidebar, not on its right (first image, you can see the blue part on the sidebar, that's the div going under the bar.)我正在尝试制作一个固定的侧边栏,当您滚动页面时它将保持在该位置(如第二张图片所示),但页面内容位于该侧边栏下方,而不是右侧(第一张图片,您可以看到侧边栏上的蓝色部分,即栏下方的 div。)

Img1:图像1:在此处输入图片说明

img2:图片2:在此处输入图片说明

(I'm using images links because i can't post them) (我正在使用图片链接,因为我无法发布它们)

html: html:

<div class='barralado'>

        ~sidebar content~
</div>

    <div class='conteudo'>

        <div class='inicio'>
            <div class='topo'>
                <p class='titulo'>Liga Juizforana</p>
            </div>
        </div>

    </div>

css: css:

.barralado {
    position: fixed;
    top: 0;
    left: 0;
}

.conteudo {
    }

.conteudo .topo {
    background-color: #ffffff;
}

.topo .titulo {
    font-size: 4em;
    color: white;
    text-shadow: 2px 2px 1px rgba(150, 150, 150, 1);
    margin-top: 3em;
    margin-left: 3.5em;
}

I tried to put both to float left, i tried to clear, i tried all the positions on both, and it didn't worked我试图让两者都向左浮动,我试图清除,我尝试了两者的所有位置,但没有奏效

2nd mini question: how to make the "conteudo" div 100vh after put it on the right of the sidebar?第二个小问题:将“conteudo”div 放在侧边栏右侧后如何使其变为 100vh? When i try it, it doesn't change to 100vh.当我尝试时,它不会更改为 100vh。

A fixed positioned div will be fixed on the page, even if you scroll it. 即使滚动它,固定位置的div也将固定在页面上。 And all the stuff goes under it (it has z-index priority). 所有东西都放在它下面(它具有z-index优先级)。

To fix it, give your content div left padding equal to the width of your sidebar. 要解决此问题,请使内容div的左侧填充等于边栏的宽度。

.barralado {
    position: fixed;
    top: 0;
    left: 0;
    min-height: 300px; // I suggest you give a value to your sidebar. 
    width:300px; //This is your width;
}

.contneudo{
    padding-left:300px; //This is the padding that will go around your sidebar
}

If your sidebar has a fixed width you could using padding to the left of your content container of the sidebar width. 如果侧边栏具有固定的宽度,则可以在侧边栏宽度的内容容器左侧使用填充。 Or you could float it to the right and set the width with the CSS calc function. 或者,您可以将其浮动到右侧,然后使用CSS calc函数设置宽度。

.conteudo {
  width: -webkit-calc(100% - 200px);
  width: calc(100% - 200px);
  float:right;
}

Using your code and padding option: 使用代码和填充选项:

.barralado {
    position: fixed;
    top: 0;
    left: 0;
    width: 300px;
}

.conteudo {
    padding-left: 300px;
}

.conteudo .topo {
    background-color: #ffffff;
}

.topo .titulo {
    font-size: 4em;
    color: white;
    text-shadow: 2px 2px 1px rgba(150, 150, 150, 1);
    margin-top: 3em;
    margin-left: 3.5em;
}

There are a few other ways but hopefully one of the 2 options will help you achieve what you want. 还有其他几种方法,但希望这两个选项之一可以帮助您实现所需的目标。

I find it easier to set a grid for your body content, with an empty spacer div, and then your main content within the div.我发现为正文内容设置网格更容易,使用空的间隔 div,然后是 div 中的主要内容。

HTML File: HTML文件:

   <div class="gridWrapper">
      <div class="spacer"> </div>
      <div class="yourMainContent"> Your content here</div>
   </div>

CSS File: CSS文件:

.gridWrapper {
  display: grid;
  grid-template-columns: 250px 1fr;
}

In this scenario of fixed widths, you can make your content div absolute positioned, to avoid going under your sidebar: 在这种固定宽度的情况下,您可以使内容div绝对定位,以避免进入侧边栏:

.conteudo {
   position: absolute;
   top: 0;
   left: 192px; // this is your sidebar width, according to your screenshot
}

or if for some reason you need to avoid absolute positioning, you can offset it with margin: 或由于某种原因需要避免绝对定位时,可以用边距补偿:

.conteudo {
   margin-left: 192px; // this is your sidebar width, according to your screenshot
}

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

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