如何在CSS中移除选择输入框的背景

来自:互联网
时间:2023-08-27
阅读:

如何在<a href=https://www.freexyz.cn/dev/css/ target=_blank class=infotextkey>CSS</a>中移除选择输入框的背景

HTML表单元素的默认样式通常会有些乏味和缺乏灵感。其中一个经常需要设计改进的元素是select输入框,它用于向用户展示可供选择的选项列表。在本文中,我们将向您展示如何使用CSS去除select输入框的默认背景。

通过这样做,您将能够自定义选择输入的外观,使其更具视觉吸引力,并与您的网站或应用程序的整体设计保持一致。

Various Inputs fields in CSS

Let us first understand the basic knowledge that, we need to have before moving on to solving this problem. The first thing we need to know is, what are inputs in a web page? Any intractable section of the website through which the user can enter and submit data is called input field in a website.

在HTML中提供了不同类型的输入。例如,文本区域,单选按钮,选择选项等等。每个输入都有不同的默认样式。在许多情况下,我们需要使用自定义样式来给我们的网站带来影响力,并使其与其他网站区分开来。

在网站中使用的一些常见的输入字段的示例如下所示。

<form action="someAction">
   <label for="YourName">Your First name:</label>
   <input type="text" id="YourName" name="YourName"><br><br>
   <label for="YourLastName">Your Last name:</label>
   <input type="text" id="YourLastName" name="YourLastName"><br><br>
   <input type="submit" value="Submit">
</form>

我们需要知道的第二件事是关于这些输入的“状态”。每当我们与输入字段进行交互以输入一些数据时,输入字段被称为“活动”状态,但是如果输入字段未被触摸,则被称为“非活动”状态。另一个用于描述它们的术语是“聚焦”和“非聚焦”状态。默认情况下,所有输入都处于非聚焦状态。

现在我们可以开始解决这个问题的方法。CSS为我们提供了各种“伪类”来针对某些输入字段的不同或特殊状态进行定位。其中一个类是焦点伪类。它应用于在网站上处于“焦点”状态的元素,这意味着用户通过键盘或鼠标点击元素与该元素进行交互。我们将利用这个伪类,以便在焦点状态下移除输入背景。为此,我们将更改默认样式以实现我们的目标。

但这仅适用于用户输入的输入类型。往往,网站包含下拉列表,其中包含一些预先存在的选项列表,用户可以从给定的列表中选择一个或多个。

与其他输入形式一样,它也是一个输入字段,并且有与之关联的默认样式。我们可以使用html中的select标签创建自定义下拉列表。它主要用于在表单中收集用户数据。下面是使用select标签的下拉列表的示例。

示例

 

<!DOCTYPE html>
<html lang="en">
<head>
   <title>Select Without background</title>
</head>
<body>
   <form id="contactForm" method="POST" action="">
      <label for="movies">Which movie would you like to see?</label>
      <select name="movies" id="movies">
         <option value="a">Movie A</option>
         <option value="b">Movie B</option>
         <option value="c">Movie C</option>
         <option value="d">Movie D</option>
      </select>
      <br />
   </form>
 </body>
</html>

In the above example, we provided the select tag a name and an id. The name attribute is for refer it after its submission by user, while the id attribute is used to access the data that will be sent after submission. To provide the options that are avAIlable for the user to choose we use the option tag. We can also use several other attributes to make the list more accessible and distinct.

Example

的中文翻译为:

示例

在下面的例子中,我们可以使用 –webkit-appearance : none 来完全移除背景。

<!DOCTYPE html>
<html lang="en">
<head>
   <title>Select Without background</title>
   <style>
      .form-control:focus,
      .form-control:active {
         background: transparent;
      }
      #movies,
      #movies:focus,
      #movies:active {
         background: none;
         -webkit-appearance: none;
      }
   </style>
</head>
<body>
   <form id="contactForm" method="POST" action="">
      <input
         name="YourName"
         type="text"
         class="someClass"
         placeholder="Can you please tell me your name :)"
         required
      />
      <br />
      <input
         name="YourEmail"
         type="email"
         class="someClass"
         placeholder="Your email please :)"
      />
      <br />
      <label for="movies">Which movie would you like to see?</label>
      <select name="movies" id="movies">
         <option value="a">Movie A</option>
         <option value="b">Movie B</option>
         <option value="c">Movie C</option>
         <option value="d">Movie D</option>
      </select>
      <br />
   </form>
</body>
</html>

As we can already notice that the visual output of the example above is different and does not have that default grayish toned background color in it. Another unintuitive approach would be to use a div as a container for the select box. After that set a width restriction and hide the overflow, and let the select box’s width be somewhat smaller than the container div. That way the default styled arrow will disappear from the webpage.

请注意,与本文所做的单个标签定位不同,大多数开发人员使用id和类来创建通用的样式,这样在类似情况下就不需要重复代码。

Conclusion

本文讨论了在CSS中删除输入背景的问题的解决方案。在这个过程中,我们学习了输入框、它们的类型、输入框的默认样式以及它们的状态。我们还学习了如何使用伪类选择器来定位元素的特殊状态。

返回顶部
顶部