css – 固定位置div里面的div容器

前端之家收集整理的这篇文章主要介绍了css – 固定位置div里面的div容器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图在相对容器内创建固定位置div.我正在使用bootstrap css框架.我试图创建一个固定的位置推车.所以每当用户滚动页面时,它将显示购物车内容.但现在的问题是,它运行在容器div之外.

这必须在响应模式下工作.

在这里我的尝试:

  1. <div class="wrapper">
  2. <div class="container">
  3. <div class="element">
  4. fixed
  5. </div>
  6. </div>
  7. </div>

CSS代码

  1. .wrapper {
  2. width:100%
  3. }
  4. .container {
  5. width:300px;
  6. margin:0 auto;
  7. height:500px;
  8. background:#ccc;
  9. }
  10. .element {
  11. background:#f2f2f2;
  12. position:fixed;
  13. width:50px;
  14. height:70px;
  15. top:50px;
  16. right:0px;
  17. border:1px solid #d6d6d6;
  18. }

See live example here.

解决方法

截图:

这是如何位置:固定;表现:

MDN link

Do not leave space for the element. Instead,position it at a
specified position relative to the screen’s viewport and doesn’t move
when scrolled. When printing,position it at that fixed position on
every page.

因此,要得到你想要的东西,你必须使用比固定定位更多的东西:

可能这个:demo

  1. .wrapper {
  2. width:100%
  3. }
  4. .container {
  5. width:300px;
  6. margin:0 auto;
  7. height:500px;
  8. background:#ccc;
  9. }
  10. .element {
  11. background:#f2f2f2;
  12. position:fixed;
  13. width:50px;
  14. height:70px;
  15. margin-left:250px;
  16. border:0px solid #d6d6d6;
  17. }

猜你在找的CSS相关文章