简体   繁体   English

如何使用内部的div调整固定的div / nav大小?

[英]How to make a fixed div/nav resize with the div it's inside of?

I'm pretty fresh to web development and cannot figure this one out. 我对Web开发非常新鲜,无法解决这一问题。 Appreciate any help! 感谢任何帮助!

On re-size the fixed div moves out of the container instead of re-sizing. 调整大小后,固定div会移出容器,而不是重新调整大小。 The site I'm working on has the nav as the fixed section and is inside of the main container. 我正在工作的站点将导航作为固定部分,并且位于主容器内部。

Any help is appreciated. 任何帮助表示赞赏。 Thanks! 谢谢!

<div class="container">
<div class="fixed"></div>
</div>

.container {
  border: 1px solid;
  max-width: 600px;
  width: 100%;
  min-height: 1600px;
}

.fixed {
  max-width: 600px;
  width: 100%;
  height: 50px;
  border: 1px solid green;
  position: fixed;
}

http://jsfiddle.net/KqvQr/ http://jsfiddle.net/KqvQr/

When you specify position as fixed the Element, even thought it is inside a parent container, It won't behave as a child of a parent container. 当您指定位置为固定元素时,甚至认为它在父容器中,它也不会充当父容器的子元素。 It won't adjust his width according to the parent width. 它不会根据父级宽度调整其宽度。 But I can give you a solution where when user resize the page the fixed element also get resize yet it is a position fixed 但是我可以为您提供一种解决方案,当用户调整页面大小时,固定元素也会获得调整大小,但它是固定位置

.fixed {
   height: 50px;
   border: 1px solid green;
   position: fixed;
   right:0;
   left:0;
}

Don't specify widths for the container. 不要为容器指定宽度。 instead of that specify left and right values. 而不是指定值。 so then when page is resizing css only check for the left and right margin values. 因此,当页面调整CSS大小时,仅检查左右边距值。 by keeping those values it will adjust its inner width always. 通过保留这些值,它将始终调整其内部宽度。

Here is the working fiddle http://jsfiddle.net/KqvQr/5/ 这是工作的小提琴http://jsfiddle.net/KqvQr/5/

I don't think you can achieve what you want if you stick with that constraints. 如果坚持这些约束,我认为您将无法实现想要的目标。 Your width and max-width will work as expected if you change your position to relative instead of fixed .. 如果将位置更改为relative而不是fixed 。,则widthmax-width将按预期工作。

Check out this Fiddle 看看这个小提琴

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

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