【问题标题】:Restyle select dropdown using CSS使用 CSS 重新设置选择下拉菜单的样式
【发布时间】:2021-02-03 11:45:35
【问题描述】:

我的网站上有一个选择下拉菜单,并希望使用 CSS 重新设置它的样式,因此选项是单个点而不是下拉菜单。这是可以使用纯CSS的东西吗?

任何帮助将不胜感激,谢谢。

CSS

.radio-toolbar input[type="radio"] {
  display: none;
}

.radio-toolbar label {
  display: inline-block;
  background-color: #ddd;
  padding: 4px 11px;
  font-family: Arial;
  font-size: 16px;
  cursor: pointer;
}

.radio-toolbar input[type="radio"]:checked+label {
  background-color: #bbb;
}

HTML/Liquid

<label class="product-tag pb-2 mt-4" for="SingleOptionSelector-{{ forloop.index0 }}">
 {{ option.name }}
</label>                   

<select class="radio-toolbar single-option-selector single-option-selector-{{ section.id }} product-form__input" id="SingleOptionSelector-{{ forloop.index0 }}" data-index="option{{ forloop.index }}">
{% for value in option.values %}
<option value="{{ value | escape }}"{% if option.selected_value == value %} selected="selected"{% endif %}>{{ value }}</option>
{% endfor %}
</select>

Current

Aim

【问题讨论】:

标签: javascript html css liquid


【解决方案1】:

select 元素无法使用纯 CSS 呈现为按钮。

如果您能够更改标记,那么使用input[type="radio"] 元素可能会很有意义。单选按钮可以使用纯 CSS 设置样式 - CSS - How to Style a Selected Radio Buttons Label?


或者(如果必须保留select 元素)您的目标可以使用javaScript 来实现。隐藏下拉菜单,为每个选项显示一个按钮,并将单击处理程序绑定到将相应的 option 设置为 selected 的按钮,并为隐藏的 select 触发 change 事件。

【讨论】:

  • 快速搜索表明它可能的。
  • @MrUpsidown 这显然不是跨浏览器解决方案
  • 你说的这个是什么意思?您的“解决方案”跨浏览器如何?它不提供任何代码。只有一个链接到另一个甚至不是你的答案。就是VLQ。
  • @MrUpsidown 你会推荐什么是改变这种情况的最有效方法?我在液体(有问题的代码)中工作,这比在纯 HTML 中稍微复杂一些。感谢您的快速回复。
  • @Tom 你不能应用 CSS 吗?呈现的 HTML 似乎是标准代码。您是否尝试过应用重复链接中提供的解决方案(不是接受的答案,而是获得 6 票赞成的答案)?
猜你喜欢
  • 1970-01-01
  • 2013-08-07
  • 1970-01-01
  • 2011-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多