【问题标题】:Adding a search icon to a Wordpress Search bar将搜索图标添加到 Wordpress 搜索栏
【发布时间】:2022-01-06 07:16:15
【问题描述】:

我正在使用以下 WordPress 函数来输出 WP 搜索表单:echo get_search_form();

前端顶栏上的结果输出:

<form class="search-form" method="get" action="http://bio-04.eproofs.ca/" role="search">
<label class="search-form-label screen-reader-text" for="searchform-1"> To search, type and hit enter.</label>
<input class="search-form-input" type="search" name="s" id="searchform-1" placeholder=" To search, type and hit enter.">
<input class="search-form-submit" type="submit" value="Search"><meta content="http://bio-04.eproofs.ca/?s={s}">
</form>

我想在占位符内添加一个搜索图标(放大镜)。目前我只有占位符内的文本。我尝试使用 before 伪元素没有运气。类似于:

label:before {
  content: "";
  position: absolute;
  left: 10px;
  top: 0;
  bottom: 0;
  width: 20px;
  background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='25' height='25' viewBox='0 0 25 25' fill-rule='evenodd'%3E%3Cpath d='M16.036 18.455l2.404-2.405 5.586 5.587-2.404 2.404zM8.5 2C12.1 2 15 4.9 15 8.5S12.1 15 8.5 15 2 12.1 2 8.5 4.9 2 8.5 2zm0-2C3.8 0 0 3.8 0 8.5S3.8 17 8.5 17 17 13.2 17 8.5 13.2 0 8.5 0zM15 16a1 1 0 1 1 2 0 1 1 0 1 1-2 0'%3E%3C/path%3E%3C/svg%3E") center / contain no-repeat;
}

该网站可以在这里找到:http://bio-04.eproofs.ca/

是否可以仅使用 css 和输出的现有类来做到这一点?

【问题讨论】:

    标签: php css wordpress genesis


    【解决方案1】:

    标签有一些属性(可能是有意设计或由于框架),会阻止您的图标显示:

    • 剪辑
    • 剪辑路径
    • 宽度:1px
    • 高度:1px

    如果您在标签上禁用这些(从也添加这些的另一个类中禁用),您将看到您的图标。但是你也会看到标签上的文字,这是你不想看到的。

    这是我的建议,你把你的伪代码放在表单标签而不是标签上。

    form.search-form:before {
          content: "";
          position: absolute;
          left: 10px;
          top: 0;
          bottom: 0;
          width: 20px;
          background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='25' height='25' viewBox='0 0 25 25' fill-rule='evenodd'%3E%3Cpath d='M16.036 18.455l2.404-2.405 5.586 5.587-2.404 2.404zM8.5 2C12.1 2 15 4.9 15 8.5S12.1 15 8.5 15 2 12.1 2 8.5 4.9 2 8.5 2zm0-2C3.8 0 0 3.8 0 8.5S3.8 17 8.5 17 17 13.2 17 8.5 13.2 0 8.5 0zM15 16a1 1 0 1 1 2 0 1 1 0 1 1-2 0'%3E%3C/path%3E%3C/svg%3E") center / contain no-repeat;
        }
    

    还添加以下内容,以便图标保留在您的表单中

    form.search-form {
      position:relative;
    }
    

    从那里您可以决定将图标放置在搜索框的右侧,而不是距离左侧 10 像素。 如果您不希望搜索词与搜索图标重叠,您可以在输入标签的右侧添加填充。

    【讨论】:

    • 谢谢,我想知道是否可以让它可点击,所以当用户点击图标区域时它会返回?
    • 在前面的伪代码中添加“pointer-events:none”行应该使其可点击。 pointer-events:none 使您的点击穿过图标并到达输入字段。
    猜你喜欢
    • 2016-04-04
    • 1970-01-01
    • 2022-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多