css怎么继承父集宽度,Inherit the width of the parent element when `position: fixed` is applied...

Earlier this year I came across a question on SOF asking how can a child element that is fixed inherit the width of its parent. I was shocked to see a solution that suggested using inherit was up-voted several times! This is not the correct solution. So I wrote a somewhat technical article on how to approach it. Here's the made-for-dev version of that answer.

The truth is, you can't use inherit reliably to set the width of the of the child element while its fixed. This has to do with a misunderstanding, or no understanding, of how fixed actually works.

Understand Fixed

Unlike absolute, fixed doesn't position itself from its closest relative parent. Instead, fixed positions itself relative to the viewport. The viewport will always stay fixed, which is why you get the effect that you do.

That being said, whenever you "inherit" any width it will be respective to the viewport. So it does us no good when we're trying set the width of our target element to the width of it's parent.

Learn more about the different behaviors of position.

Quick Solutions

Now that we have a better understanding of fixed, here are two approaches to make this happen

Pure CSS

We can use pure CSS to fix this problem, but we would need to know the width in advance. Suppose that its parent element is 300px;

.parent{

width: 300px;

}

.parent .fixed_child{

position: fixed;

width: 300px;

}

JS

Now with mobile devices, we don't really have the luxury of having set widths, especially anything over 300px. Using percentages won't work either, since it will be relative to the viewport and not the parent element. We can use JS, in this case with jQuery to achieve this. Lets take a look at a function that will always set the width of the parent at the given moment:

function toggleFixed () {

var parentwidth = $(".parent").width();

$(".child").toggleClass("fixed").width(parentwidth);

}

css:

.fixed{

position:fixed;

}

View in CodePen

Dynamic Widths

That's fine and dandy, but what happens if the width of the window changes while the user is still on the page, changing the parent element with this? While the parent may adjust its width, the child will stay the set width that the function set it. We can fix this with jQuery's resize() event listener. First we'll need to split the function we created into two:

function toggleFixed() {

adjustWidth();

$(".child").toggleClass("fixed");

}

function adjustWidth() {

var parentwidth = $(".parent").width();

$(".child").width(parentwidth);

}

Now that we've separated each part, we can call them individually, we'll include our original button method that toggles the fixed and width:

$("#fixer").click(

function() {

toggleFixed();

});

And now we also add the resize event listener to the window:

$(window).resize(

function() {

adjustWidth();

})

View in CodePen

There! Now we have a fixed element who's size will be adjusted when the window is resized.

Conclusion

We've tackled this challenge by understanding fixed position and it's limitations. Unlike Absolute, fixed only relates to the view port and therefore cannot inherit its parent's width.

To solve this, we need to use some JS magic, which didn't take very much with jQuery, to achieve this.

In some cases, we need a dynamic approach with scaling devices of varying widths. Again, we took the JS approach.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. The keyword final is used to declare a constant variable, which means the value of the variable cannot be changed once it is assigned. It can also be used to make a method or class immutable, which means they cannot be overridden or extended by any subclass. The keyword static is used to define a class-level or static variable, method, or block. A static variable belongs to the class and is shared by all instances of the class. A static method can be called without creating an instance of the class. A static block is used to initialize static variables when the class is loaded. 2. The Parent Delegation Model is a design pattern used in object-oriented programming, where a subclass delegates some or all of its functionality to its parent class. In this model, the parent class is responsible for providing the common functionality to all its subclasses, while the subclasses only add or modify their own specific functionality. This model is used in many programming languages, including Java, where it is implemented through the concept of inheritance. In Java, a subclass can inherit the properties and methods of its parent class using the extends keyword. The subclass can then use these inherited properties and methods, as well as add its own properties and methods. The Parent Delegation Model is a powerful technique that promotes code reuse and reduces duplication of code. It also makes it easier to maintain and update code, as changes made to the parent class are automatically inherited by all its subclasses.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值