【问题标题】:How can I change the style of highlighted texts in the browser search result?如何更改浏览器搜索结果中突出显示文本的样式?
【发布时间】:2017-05-05 18:57:09
【问题描述】:

在主流浏览器中,如果我按下Ctrl-F并输入一个词组,网页中对应的文本会被高亮显示。我想知道是否有办法自定义突出显示文本的样式。

例如截图中world的颜色可以改成红色吗?

更新:

::selection 不起作用,因为除非我关闭搜索对话框,否则不会选择文本。看这个截图:

【问题讨论】:

    标签: html css google-chrome firefox browser


    【解决方案1】:

    使用::selection,比如:

    /* For other Browsers */    
    ::selection {
      background: red;
      color: white;
    }
    
    /* For Firefox */
    ::-moz-selection {
      background: red;
      color: white;
    }
    

    看看下面的工作 sn-p:

    ::-moz-selection {
      background: red;
      color: white;
    }
    
    ::selection {
      background: red;
      color: white;
    }
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nisi unde reprehenderit voluptas! Deserunt ducimus reiciendis necessitatibus adipisci obcaecati iste alias voluptate ratione ut at exercitationem, eum, dicta, excepturi eius suscipit.</p>

    希望这会有所帮助!

    【讨论】:

    • 如果我不关闭搜索对话框,::selection 将不起作用。我在问题中添加了屏幕截图。
    • @xmcp 不,你不能真正改变那种颜色。它是特定于浏览器的。
    【解决方案2】:

    Find 函数的结果 无法更改 - 这是默认的用户代理样式,我认为没有任何标准属性可以让您这样做。

    你可以做的是为你使用的浏览器设置它,或者就像设置文本选择颜色一样。

    浏览器设置

    如果您只想修改特定浏览器的样式 - 看看您能做什么:

    1. Firefox - 在 about:config 中添加 ui.textSelectBackgroundAttention 值作为您需要的颜色

    2. Chrome - 您没有像 Firefox 那样的任何隐藏设置,也无法更改它。

    请注意,默认样式也可能会从 OS 主题中获取查找突出显示颜色。

    文本选择

    但是,有一种方法可以设置样式文本选择 - 您可以使用selection psuedo element - 请参阅下面的演示:

    .custom::selection {
      background: red; /* WebKit/Blink Browsers */
    }
    .custom::-moz-selection {
      background: red; /* Gecko Browsers */
    }
    <div class="custom"> This is some text</div>
    <div>More text here</div>

    【讨论】:

    • 如果我不关闭搜索对话框,::selection 将不起作用。我在问题中添加了屏幕截图。
    • 那么突出显示的文本是否有类似::-webkit-search-result 的内容?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-25
    • 2018-03-25
    • 2016-11-02
    • 1970-01-01
    • 2021-07-24
    • 1970-01-01
    • 2014-07-25
    相关资源
    最近更新 更多