Html 通过css设置标签的大小属性?

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

Set tag's size attribute through css?

htmlcsshtml-select

提问by Boris Callens

Normally you can do this:

通常你可以这样做:

<select size="3">
    <option>blah</option>
    <option>blah</option>
    <option>blah</option>
</select>

And it would render as a selectionbox where all three options are visible (without dropping down)
I'm looking for a way to set this size attribute from css.

它会呈现为一个选择框,其中所有三个选项都可见(不下拉)
我正在寻找一种从 css 设置此大小属性的方法。

采纳答案by scunliffe

There ins't an option for setting the size, but if you do set the size some browsers will let you set the width/height properties to whatever you want via CSS.

没有设置大小的选项,但是如果您确实设置了大小,某些浏览器将允许您通过 CSS 将宽度/高度属性设置为您想要的任何值。

Some = Firefox, Chrome, Safari, Opera.

一些 = Firefox、Chrome、Safari、Opera。

Not much works in IE though (no surprise)

虽然在 IE 中没有多少工作(不足为奇)

You could though, if you wanted, use CSS expressions in IE, to check if the size attribute is set, and if so, run JS to (re)set it to the size you want... e.g.

但是,如果您愿意,您可以在 IE 中使用 CSS 表达式来检查是否设置了 size 属性,如果是,则运行 JS 以(重新)将其设置为您想要的大小......例如

size = options.length;

回答by pilif

I don't think that this is possible.

我不认为这是可能的。

CSS properties are very generic (applicable to any element) and are unable to alter the functionality of an element in any ways (only the looks).

CSS 属性非常通用(适用于任何元素)并且无法以任何方式(仅外观)改变元素的功能。

The size attribute is changing the functionality of the element at least in the case of size = 1 / size != 1.

size 属性至少在 size = 1 / size != 1 的情况下改变了元素的功能。

回答by pilif

I disagree, I have been able to set select width and height using CSS with something like this:

我不同意,我已经能够使用 CSS 设置选择宽度和高度,如下所示:

select { width: 5em; height: 5em; }

Works in IE(7) as well as I just tested it.

在 IE(7) 和我刚刚测试过的一样工作。

回答by marco

Try this:

尝试这个:

select[attr=size] {
    width:auto;
    height:auto;
}

回答by kimomat

You can use following CSS selector to style select items with the size attribute:

您可以使用以下 CSS 选择器来设置具有 size 属性的选择项的样式:

select[size]{
/*your style here */
}

回答by Sam Murray-Sutton

In general you can't add attributes set within html via CSS. You can add to the html using the pseudo selectors :before and :after, but that's about it, plus they still aren't compatible across all browsers. If you really need to do this, I think you'd have to resort to Javascript.

通常,您无法通过 CSS 添加在 html 中设置的属性。您可以使用伪选择器 :before 和 :after 添加到 html,但仅此而已,而且它们仍然不兼容所有浏览器。如果您真的需要这样做,我认为您必须求助于 Javascript。

回答by redsquare

I do not believe this is possible no

我不相信这是可能的 不