CSS 覆盖高度属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7707102/
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-30 01:51:19  来源:igfitidea点击:

Override height property

css

提问by David542

I have an element which inherits a height of 40px.

我有一个继承 40px 高度的元素。

However, the element I need to style is a textarea box, with variable height / width. How would I 'cancel' the height of an element, such that there is no height property for the element and it can be resized?

但是,我需要设置样式的元素是一个 textarea 框,高度/宽度可变。我将如何“取消”元素的高度,以便元素没有高度属性并且可以调整其大小?

采纳答案by Nightfirecat

If you simply want to make your textarea element able to be resized, try just applying the following CSS property:

如果您只是想让 textarea 元素能够调整大小,请尝试应用以下 CSS 属性:

textarea {
    resize: both;
}

An example can be seen herewhere the height is set, but using the resize property, it can still be resized.

可以在此处看到一个示例,其中设置了高度,但使用 resize 属性,它仍然可以调整大小。

回答by dougczar

To override a set height in a CSS element, you can simply put this in the inherited element (either in a style block or inline):

要覆盖 CSS 元素中的设置高度,您可以简单地将其放在继承的元素中(在样式块中或内联中):

height: auto;

This will override the set value inherited, without statically setting a new one.

这将覆盖继承的设置值,而不会静态设置新的值。

回答by Fabricio PH

Another way is to use height auto with !important marker:

另一种方法是将 height auto 与 !important 标记一起使用:

height: auto !important;

'important' will make things work, but if this HTML is then imported by someone else, they won't be able to override this marker. So my advice is use only when extremely necessary.

'important' 会起作用,但是如果这个 HTML 是由其他人导入的,他们将无法覆盖这个标记。所以我的建议是只在非常必要的时候使用