简体   繁体   English

如何更改浏览器搜索结果中突出显示的文本的样式?

[英]How can I change the style of highlighted texts in the browser search result?

In major browsers, if I press Ctrl-F and input a phrase, the corresponding text in the webpage will be highlighted. 在主要的浏览器中,如果按Ctrl-F并输入短语,则网页中的相应文本将突出显示。 I'm wondering if there is a way to customize the style of the highlighted text. 我想知道是否有一种方法可以自定义突出显示的文本的样式。

屏幕截图

eg Is it possible to change the color of world to red, in the screenshot? 例如,是否可以在屏幕截图中将world的颜色更改为红色?

UPDATE: 更新:

::selection does not work, as the text is not selected unless I close the search dialog. ::selection不起作用,因为除非关闭搜索对话框,否则不会选择文本。 See this screenshot: 看这个截图:

屏幕截图

The results from the Find function cannot be changed - this is the default User agent styling and I don't think there is any standard attribute that allows you to do it. 查找”功能的结果无法更改-这是默认的用户代理样式,我认为没有任何标准属性允许您执行此操作。

What you can do is to set it up for the browser you use, or just as much as set up the text selection color. 您可以做的就是为您使用的浏览器设置它,或者与设置文本选择颜色一样多。

Browser Settings 浏览器设定

If you want just to modify the style for a specific browser - see what you can do for: 如果您只想修改特定浏览器的样式,请执行以下操作:

  1. Firefox - add ui.textSelectBackgroundAttention with value as the color you need in about:config Firefox-about:config添加ui.textSelectBackgroundAttention ,并将其值设置为所需的颜色

  2. Chrome - you don't have any hidden settings like Firefox has and there is no way to change it AFAIK. Chrome-您没有像Firefox这样的任何隐藏设置,也无法更改AFAIK。

Note that the default styles may pick up the find highlight color from the OS theme too. 请注意,默认样式也可能会从OS主题中选取查找突出显示颜色。

Text Selection 文字选择

However, there is a way for styling text selection - you can use the selection psuedo element - see demo below: 但是,有一种样式化文本选择的方法 -您可以使用selection psuedo元素 -请参见下面的演示:

 .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> 

Use ::selection , like: 使用::selection ,例如:

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

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

Have a look at the working snippet below: 看看下面的工作片段:

 ::-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> 

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM