Html 通过添加 css 删除位置:绝对属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19351151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 14:25:24  来源:igfitidea点击:

Remove position:absolute attribute by adding css

htmlcssdynamicpositioncss-position

提问by Ben Pearce

I have a html element with id="#item" I have a UI event that programaticaly alters the css for "#item" by adding the class ".new" to "#item". Initially I want "#item" to have "position:absolute". However once the class ".new" is added to "#item" the only way I can get the formatting I want in Chrome inspector is to removed position:absolute from the css for "#item". I'd like to accomplish this instead via the css in ".new", however in Chrome inspector my options for changing the position attribute are

我有一个带有 id="#item" 的 html 元素我有一个 UI 事件,它通过将类“.new”添加到“#item”来以编程方式更改“#item”的 css。最初我希望“#item”具有“位置:绝对”。但是,一旦将“.new”类添加到“#item”中,我在Chrome检查器中获得所需格式的唯一方法就是从“#item”的css中删除position:absolute。我想通过“.new”中的 css 来完成此操作,但是在 Chrome 检查器中,我更改位置属性的选项是

static
absolute
relative
initial 
inherit
fixed

As far as I can tell none of these do the same thing as removing "position:absolute" in Chrome inspector. Can anyone suggest what to put in the css for ".new" to revert to the css default positioning.

据我所知,这些都与在 Chrome 检查器中删除“位置:绝对”没有相同的作用。任何人都可以建议在 css 中为“.new”放置什么以恢复到 css 默认定位。

回答by

http://jsbin.com/ICeweli/1/

http://jsbin.com/ICeweli/1/

#test {
  position: absolute;
}

#test {
  position: static;
}

Remove one or the other to see the difference.

删除一个或另一个以查看差异。

回答by Sébastien

The CSS2 specificationsays that the initial positionvalue of an element is static.

CSS2规范说,初始position的元件的值是static

So in your case if you can't actually remove a declaration then reset it to the "default" which is static.

因此,在您的情况下,如果您实际上无法删除声明,则将其重置为“默认”,即static.

#item {
    position: static;
}