简体   繁体   English

从子CSS覆盖父对象的最大宽度

[英]Override parent's max-width from child css

I am using VuePress for docs and for the home page I wanted something more customizable. 我正在将VuePress用于文档和主页,我想要一些可定制的东西。 My problem is that the class .theme-default-content has a max-width of 740px , if I override this class and change it's max-width, it'll mess up all the other pages too not only the main page, since on VuePress all pages are linked to the main theme. 我的问题是, .theme-default-content类的max-width740px ,如果我覆盖该类并更改其最大宽度,那么它将不仅弄乱主页,而且还会弄乱所有其他页面,因为VuePress的所有页面都链接到主主题。
What I want is the child class that'll be inside header to extend with width: 100% of the page. 我想要的是子类,该子类将在header中扩展为width: 100%页面的width: 100%
Currently this would doesn't work, no matter how much I try it'll never override it. 目前,这是行不通的,无论我尝试多少,都永远不会覆盖它。 I could make it absolute but that'll create even more problems. 我可以将其设为绝对,但这会带来更多问题。
Is there a way to override the parent's property from inside? 有没有办法从内部覆盖父母的财产?

You cannot override the parent styles with child styles in CSS, as there is no parent selector in CSS. 您无法在CSS中用子样式覆盖父样式,因为CSS中没有父选择器

However, you don't need to override anything to give the child element a width that spans 100% of the viewport; 但是,您无需重写任何内容即可为子元素提供一个跨视口100%width simply use the viewport unit vw as width: 100vw : 只需将视口单位 vw用作width: 100vw

 body { margin: 0; } .outer { height: 100px; width: 200px; max-width: 200px; background: red; padding: 25px; } .inner { height: 100px; width: 100vw; background: blue; } 
 <div class="outer"> <div class="inner"></div> </div> 

Notre that if you have padding on the parent, you may want to subtract this from the width of the viewport (so that there are no horizontal scrollbars). 请注意,如果在父级上有填充,则可能要从视口的width减去此值(这样就不会有水平滚动条)。 This can be done with calc() : 这可以通过calc()

 body { margin: 0; } .outer { height: 100px; width: 200px; max-width: 200px; background: red; padding: 25px; } .inner { height: 100px; width: calc(100vw - 25px); background: blue; } 
 <div class="outer"> <div class="inner"></div> </div> 

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

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