简体   繁体   English

CSS位置固定在div容器中,宽度与父容器相同

[英]CSS position fixed within a div container with a width same as the parent container

Currently, I have 3 div, left center and right, I wish to have a div (notification) inside center section and is fixed position so that when scroll, it always stay at that position. 目前,我有3个div,左中和右,我希望在中间部分有一个div(通知),并且固定位置,以便在滚动时始终保持在该位置。 And when change the browser size, the notification will still stay inside the center div and change size accordingly. 而且,当更改浏览器大小时,通知仍将停留在div中心,并相应地更改大小。 Currently, when I use position: fixed, it is using viewport instead of the center div. 当前,当我使用position:fixed时,它使用的是视口而不是center div。

My desired output will be the notification blue bar is the same size relative to the center div with left and right padding and when reducing the size of the browser, the blue bar still stays within the center div. 我想要的输出将是通知蓝色条相对于中心div具有相同的大小,并带有左右填充,并且当减小浏览器的大小时,蓝色条仍停留在中心div内。

Current Display: 当前显示: 当前显示 Expected Display: 预期显示: 预期展示

 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css"> <div class="container"> <div class="row"> <div class="col-lg-2" style="background-color: red; height: 1000px">Test</div> <div class="col-lg-7" style="background-color: yellow; height: 1000px"> <div style="display: block; position: relative; z-index: 9999; width: 100%; padding-left: 5; top: 50"><div style="position: fixed; background-color: blue; ">Notification</div></div> Test<br> Test<br> Test<br> Test<br> Test<br> Test<br> Test<br> Test<br> Test<br> Test<br> Test<br> Test<br> Test<br> Test<br> Test<br> </div> <div class="col-lg-3" style="background-color: green; height: 1000px">Test3</div> </div> </div> 

added below css to the notification div css下方添加到通知div

position: absolute; background-color: blue; height:30px;
      width:100%; top:20px; color: white

 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css"> <div class="container"> <div class="row"> <div class="col-lg-2" style="background-color: red; height: 1000px">Test</div> <div class="col-lg-7" style="background-color: yellow; height: 1000px"> <div style="display: block; position: relative; z-index: 9999; width: 100%; padding-left: 5; top: 50"><div style="position: absolute; background-color: blue; height:30px; width:100%; top:20px; color: white">Notification</div></div> Test<br> Test<br> Test<br> Test<br> Test<br> Test<br> Test<br> Test<br> Test<br> Test<br> Test<br> Test<br> Test<br> Test<br> Test<br> </div> <div class="col-lg-3" style="background-color: green; height: 1000px">Test3</div> </div> </div> 

Use position absolute replace of fixed. 使用位置绝对替换固定。 I add 我加

position: absolute;top:30px;width: 90%;text-align:center;left:5%; 

Replace of position: fixed; 更换职位:固定;

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">

<div class="container">
    <div class="row">
        <div class="col-lg-2" style="background-color: red; height: 1000px">Test</div>
        <div class="col-lg-7" style="background-color: yellow; height: 1000px">

            <div style="display: block; position: relative; z-index: 9999; width: 100%; padding-left: 5; top: 50"><div style="position: absolute;top:30px;width: 90%;text-align:center;left:5%; background-color: blue; ">Notification</div></div>
        Test<br>
        Test<br>
        Test<br>
        Test<br>
        Test<br>
        Test<br>
        Test<br>
        Test<br>
        Test<br>
        Test<br>
        Test<br>
        Test<br>
        Test<br>
        Test<br>
        Test<br>

        </div>
        <div class="col-lg-3" style="background-color: green; height: 1000px">Test3</div>
    </div>
</div>

You can try using Position : sticky, which entirely does the same thing as fixed other than positioning an element based on the user's scroll position. 您可以尝试使用Position:sticky,除了根据用户的滚动位置定位元素外,它的功能与固定功能完全相同。 The only exception being, it is not supported in IE 11.0 唯一的例外是,IE 11.0不支持它

 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css"> <div class="container"> <div class="row"> <div class="col-lg-2" style="background-color: red; height: 1000px">Test</div> <div class="col-lg-7" style="background-color: yellow; height: 1000px"> <div style=" position: sticky; z-index: 9999; padding-left: 5; top: 50px;"> <div style="background-color: blue; ">Notification</div> </div> Test<br> Test <br> Test <br> Test <br> Test <br> Test <br> Test <br> Test <br> Test <br> Test <br> Test <br> Test <br> Test <br> Test <br> Test <br> </div> <div class="col-lg-3" style="background-color: green; height: 1000px">Test3</div> </div> </div> 

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

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