Set tag's size attribute through css?

2019-01-27 13:36发布

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.

7条回答
放我归山
2楼-- · 2019-01-27 13:47

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).

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

查看更多
贪生不怕死
3楼-- · 2019-01-27 13:49

I do not believe this is possible no

查看更多
forever°为你锁心
4楼-- · 2019-01-27 13:50

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.

Some = Firefox, Chrome, Safari, Opera.

Not much works in IE though (no surprise)

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.

size = options.length;
查看更多
够拽才男人
5楼-- · 2019-01-27 13:50

Try this:

select[attr=size] {
    width:auto;
    height:auto;
}
查看更多
smile是对你的礼貌
6楼-- · 2019-01-27 13:50

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.

查看更多
来,给爷笑一个
7楼-- · 2019-01-27 14:01

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

select[size]{
/*your style here */
}
查看更多
登录 后发表回答