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

标签 html css

目前,我有 3 个 div,左中右,我希望在中心部分有一个 div(通知)并且位置固定,这样当滚动时,它总是停留在那个位置。并且当更改浏览器大小时,通知仍将保留在中心 div 内并相应地更改大小。目前,当我使用 position: fixed 时,它使用的是视口(viewport)而不是中心 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>

最佳答案

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>

关于html - CSS 位置固定在与父容器宽度相同的 div 容器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55719981/

相关文章:

html - 如何取消选中angular2中的复选框

javascript - 如何将参数 lang 传递给另一个 url

html - 水平线 - 登录表格

javascript - 如何避免在 keydown 事件上不断重复

javascript - 有没有办法标记音频文件

css - matSelect 的样式宽度困难

css - 如何在没有 float 的情况下制作具有流体中心的 3 列布局

css - 显示 :inline resets height and width

html - 如何让我的有序列表编号在边框中间居中?

javascript - 在更改文档时选择文本