如何在选择输入中样式选项

发布于 2025-01-23 17:55:06 字数 733 浏览 0 评论 0原文

我正在寻找选择输入中的选项,并添加一些填充以增强外观。以下是我当前的CSS和HTML设置,但是我在将填充物应用于选项上很难。我可以进行什么调整来实现这一目标?

CSS:

label {
  font-size: 15px;
  font-family: 'Arial'; 
  display: inline;
}

select {
  display: inline;
  padding: 5px 10px;
}

option {
  /* How can I add padding to the options? */
}

HTML:

<label for="fruits">Fruits</label>
<select name="fruits">
  <option>Apple</option>
  <option>Mango</option>
  <option>Banana</option>
  <option>MuskMelon</option>
</select>

我感谢有关如何实现选项元素的所需填充的任何指导或建议。谢谢你!

I'm looking to style the options within a select input and add some padding to enhance the appearance. Below is my current CSS and HTML setup, but I'm having trouble applying the padding to the options. What adjustments can I make to achieve this?

CSS:

label {
  font-size: 15px;
  font-family: 'Arial'; 
  display: inline;
}

select {
  display: inline;
  padding: 5px 10px;
}

option {
  /* How can I add padding to the options? */
}

HTML:

<label for="fruits">Fruits</label>
<select name="fruits">
  <option>Apple</option>
  <option>Mango</option>
  <option>Banana</option>
  <option>MuskMelon</option>
</select>

I'd appreciate any guidance or suggestions on how to achieve the desired padding for the option elements. Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

翻了热茶 2025-01-30 17:55:06

您不能在类似的选项菜单内更改太多。如果您想制作完整的自定义下拉菜单,则必须自己制作所有组件。我很确定您只能在选项元素中更改文本,而不是周围环境中的文本,如果可以,则可能必须使用JavaScript而不是CSS

/* Style The Dropdown Button */
.dropbtn {
  background-color: #4CAF50;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
  cursor: pointer;
}

/* The container <div> - needed to position the dropdown content */
.dropdown {
  position: relative;
  display: inline-block;
}

/* Dropdown Content (Hidden by Default) */
.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
}

/* Links inside the dropdown */
.dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}

/* Change color of dropdown links on hover */
.dropdown-content a:hover {background-color: #f1f1f1}

/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
  display: block;
}

/* Change the background color of the dropdown button when the dropdown content is shown */
.dropdown:hover .dropbtn {
  background-color: #3e8e41;
}
<div class="dropdown">
  <button class="dropbtn">Fruit</button>
  <div class="dropdown-content">
    <a href="#">Apple</a>
    <a href="#">Mango</a>
    <a href="#">Banana</a>
    <a href="#">MuskMelon</a>
  </div>
</div>

You can't change much inside of an options menu like that. If you want to make a full customization dropdown menu then you have to make all the components yourself. I'm pretty sure you can only change the text in the option element but not the surroundings and if you can you probably have to use javascript not css

/* Style The Dropdown Button */
.dropbtn {
  background-color: #4CAF50;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
  cursor: pointer;
}

/* The container <div> - needed to position the dropdown content */
.dropdown {
  position: relative;
  display: inline-block;
}

/* Dropdown Content (Hidden by Default) */
.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
}

/* Links inside the dropdown */
.dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}

/* Change color of dropdown links on hover */
.dropdown-content a:hover {background-color: #f1f1f1}

/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
  display: block;
}

/* Change the background color of the dropdown button when the dropdown content is shown */
.dropdown:hover .dropbtn {
  background-color: #3e8e41;
}
<div class="dropdown">
  <button class="dropbtn">Fruit</button>
  <div class="dropdown-content">
    <a href="#">Apple</a>
    <a href="#">Mango</a>
    <a href="#">Banana</a>
    <a href="#">MuskMelon</a>
  </div>
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文