简体   繁体   English

如何继承最小宽度和最大宽度的宽度?

[英]How to inherit width of min-width and max-width?

How to inherit the width of the parent with min-width and/or max-width ? 如何继承width与家长的min-width和/或max-width

The child element #main_header must have the same width as the parent element #main 子元素#main_header的宽度必须与父元素#main宽度相同

I have added #main_inner as an intermediate/helper element. 我添加了#main_inner作为中间元素/帮助元素。

jsfiddle jsfiddle

https://jsfiddle.net/p010bx5w/4/ https://jsfiddle.net/p010bx5w/4/

 #main { position:relative; min-width:200px; max-width:300px; background:blue; height:350px; } #main_inner { width:100%; background:red; height:300px; } #main_header { position:fixed; width:inherit; background:yellow } 
 <div id="main"> <div id="main_inner"> <div id="main_header"> hey <div style="text-align:right">right</div> </div> </div> </div> 

The simple answer to the title "How to inherit width of min-width and max-width?" 标题“如何继承最小宽度和最大宽度的宽度?”的简单答案。 is: 是:

min-width: inherit;
max-width: inherit;

Since there is also a helper container in the middle, you'll have to set it on that as well, and add width: 100%; 由于中间还有一个辅助容器,因此您也必须在其上进行设置,并添加width: 100%; to the child, it seems to be working well. 对于孩子来说,似乎运作良好。

 #main { position: relative; min-width: 200px; max-width: 300px; background: red; } #main_inner { min-width: inherit; max-width: inherit; width: 100%; height: 300px; background: yellow; } #main_header { min-width: inherit; max-width: inherit; width: 100%; position: fixed; background: blue; } 
 <div id="main"> <div id="main_inner"> <div id="main_header">hey</div> </div> </div> 

EDIT 编辑

Added a jQuery approach below: 在下面添加了jQuery方法:

$(window).on('resize', function() {
  $('#main_header').css('width', $('#main').width());
}).trigger('resize');

 $(window).on('resize', function() { $('#main_header').css('width', $('#main').width()); }).trigger('resize'); 
 #main { position: relative; min-width: 200px; max-width: 300px; background: red; } #main_inner { width: 100%; height: 300px; background: yellow; } #main_header { position: fixed; background: blue; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="main"> <div id="main_inner"> <div id="main_header"> hey <div style="text-align:right">right</div> </div> </div> </div> 

The issue with your code is that you're assuming min-width and max-width rules automatically effect Width for child elements itself. 代码的问题在于,假设最小宽度和最大宽度规则会自动影响子元素本身的宽度。 Simply set the min-width rules to inherit and you're good to go 只需设置最小宽度规则即可继承,您就可以开始了

#main_header {
  position:fixed;
  min-width:inherit;
  background:blue
}

In response to your edit: Having a child element that is exactly the same width as its parent is as simple as setting the child to 100% width 响应您的编辑:具有与父元素宽度完全相同的子元素,就像将子元素设置为100%宽度一样简单

#main_header {
  position:fixed;
  min-width:inherit;
  max-width:inherit;
  width:100%;
  background:blue
}

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

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