| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Selectors/::search-text | [Back] [Original] |
Get to know MDN better
::search-text CSS pseudo-elementExperimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
The ::search-text CSS pseudo-element applies styles to search results identified by the user agent's "Find" or "Find in page" text search feature.
p::search-text {
color: crimson;
background-color: wheat;
}
<p>
Using your browser's "Find in page" functionality, find a word or phrase that
appears in this sentence, and note how, in supporting browsers, each result is
highlighted using the specified custom styles.
</p>
::search-text {
/* ... */
}
Most browsers include some kind of in-page text search functionality, usually labelled "Find" or "Find in page". The ::search-text pseudo-element, one of the highlight pseudo-elements, allows you to apply a limited set of styles to the text results highlighted by the browser search functionality.
Not all browsers and browser versions highlight search results using in-page highlights that are stylable with CSS. In such cases, ::search-text may be unimplemented or just ignored.
Using ::search-text as a selector on its own will style browser search results appearing anywhere on a page. If you want to only style browser search results appearing inside certain elements, you can combine ::search-text with other selectors, for example section::search-text.
Additionally, ::search-text can be combined with the :current pseudo-class to provide specific styles to the currently-focused search result, for example:
p::search-text {
color: white;
background-color: purple;
}
p::search-text:current {
background-color: crimson;
}
The ::search-text pseudo-element follows a special inheritance model common to all highlight pseudo-elements, wherein styles are inherited from both their parent elements and the pseudo-elements of their parents. For more details on how this inheritance works, see the Highlight pseudo-elements inheritance section.
A limited subset of CSS properties can be used with ::search-text:
colorbackground-colortext-decoration shorthand and associated longhand properties:
text-decoration-line: the grammar-error, spelling-error, line-through, none, and underline values only.text-decoration-colortext-decoration-styletext-decoration-thicknesstext-decoration-skip-inktext-underline-offsettext-shadowOverride text search result styles sparingly, especially when doing so for purely aesthetic reasons. For people experiencing cognitive concerns or who are less technologically literate, unexpected changes to these styles may hurt their understanding of the functionality.
A primary use case of ::search-text is to increase color contrast compared to the default browser styling. When customizing highlighted text, it is important to ensure that the contrast ratio between the foreground and background colors is great enough for people to be able to perceive the content of the highlighted text.
This example shows how to use ::search-text and :current to create custom styles for your browser's "Find in page" search results.
The HTML consists of a basic paragraph of text. We won't show the HTML source, both for brevity, and so that it is easier to navigate the search results in the rendered example.
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec finibus est
eget eros congue pellentesque. Etiam a augue accumsan, scelerisque nisl sit
amet, lobortis nulla. Aliquam condimentum eu orci eu elementum. Donec
porttitor quam et posuere commodo. Mauris rhoncus diam a scelerisque molestie.
Integer sollicitudin risus dui, ut sagittis lorem laoreet eget. Duis eget
pretium enim. Morbi tristique, diam sit amet gravida finibus, metus ex
tincidunt nibh, ac volutpat urna purus et arcu. Donec risus risus, semper vel
purus sit amet, gravida vestibulum est. Sed et tristique urna. Nam vel mi eget
nisi consectetur elementum. Aenean faucibus aliquam cursus. Morbi posuere
tincidunt velit, et sagittis quam sagittis in. Nam eget ante ultrices, auctor
dui vel, euismod lacus. Vivamus tincidunt, sem ac sodales aliquet, tortor
tortor consequat diam, nec tempor mi dui vel eros. Aliquam ac erat et metus
egestas scelerisque.
</p>
In our CSS, we start by styling the ::search-text pseudo-element. We give it custom background-color, color, and text-shadow styles.
html {
font-family: "Helvetica", "Arial";
}
p {
font-size: 1.5rem;
line-height: 1.5;
width: 90%;
margin: 0 auto;
}
@layer no-support {
body::before {
background-color: wheat;
display: block;
text-align: center;
padding: 1em 0;
}
@supports not selector(:current) {
body::before {
content: "Your browser doesn't support the :current pseudo-class.";
}
}
@supports not selector(::search-text) {
body::before {
content: "Your browser doesn't support the ::search-text pseudo-element.";
}
}
}
::search-text {
background-color: purple;
color: white;
text-shadow: 1px 1px 1px black;
}
Finally, we style the currently-focused search result via ::search-text:current, providing it with a different background-color and some text-decoration styles so that it is distinguishable from the rest of the results.
::search-text:current {
background-color: crimson;
text-decoration-line: underline;
text-decoration-color: yellow;
text-decoration-thickness: 3px;
}
The example renders as follows:
Try using the browser's find in page interface to find a word that appears multiple times in the example text, such as "aliquam", "amet", or "tortor". Move between the previous and next results to check out the :current styling.
| Specification |
|---|
| CSS Pseudo-Elements Module Level 4 # selectordef-search-text |
This page was last modified on Apr 17, 2026 by MDN contributors.
-webkit-border-before-webkit-box-reflect-webkit-mask-box-image-webkit-mask-composite-webkit-mask-position-x-webkit-mask-position-y-webkit-mask-repeat-x-webkit-mask-repeat-y-webkit-tap-highlight-color-webkit-text-fill-color-webkit-text-security-webkit-text-stroke-webkit-text-stroke-color-webkit-text-stroke-width-webkit-touch-calloutCustom properties (--*): CSS variablesaccent-coloralignment-baselineallanchor-nameanchor-scopeanimationappearanceaspect-ratiobackdrop-filterbackface-visibilitybackgroundbaseline-shiftbaseline-sourceblock-sizeborder-blockborder-block-colorborder-block-endborder-block-end-colorborder-block-end-styleborder-block-end-widthborder-block-startborder-block-start-colorborder-block-start-styleborder-block-start-widthborder-block-styleborder-block-widthborder-bottomborder-bottom-colorborder-bottom-left-radiusborder-bottom-right-radiusborder-bottom-styleborder-bottom-widthborder-collapseborder-colorborder-end-end-radiusborder-end-start-radiusborder-imageborder-image-outsetborder-image-repeatborder-image-sliceborder-image-sourceborder-image-widthborder-inlineborder-inline-colorborder-inline-endborder-inline-end-colorborder-inline-end-styleborder-inline-end-widthborder-inline-startborder-inline-start-colorborder-inline-start-styleborder-inline-start-widthborder-inline-styleborder-inline-widthborder-leftborder-left-colorborder-left-styleborder-left-widthborder-radiusborder-rightborder-right-colorborder-right-styleborder-right-widthborder-shapeborder-spacingborder-start-end-radiusborder-start-start-radiusborder-styleborder-topborder-top-colorborder-top-left-radiusborder-top-right-radiusborder-top-styleborder-top-widthborder-widthborderbottomcaption-sidecaretclearclip-pathclip-ruleclipcolorcolumnscontaincontainer-namecontainer-typecontainercontent-visibilitycontentcorner-block-end-shapecorner-block-start-shapecorner-bottom-left-shapecorner-bottom-right-shapecorner-bottom-shapecorner-end-end-shapecorner-end-start-shapecorner-inline-end-shapecorner-inline-start-shapecorner-left-shapecorner-right-shapecorner-shapecorner-start-end-shapecorner-start-start-shapecorner-top-left-shapecorner-top-right-shapecorner-top-shapecursorcxcyddirectiondisplaydominant-baselinedynamic-range-limitempty-cellsfield-sizingfill-opacityfill-rulefillfilterflexfloatflood-colorflood-opacityfont-familyfont-feature-settingsfont-kerningfont-language-overridefont-optical-sizingfont-palettefont-sizefont-size-adjustfont-smoothfont-stretchfont-stylefont-synthesisfont-synthesis-positionfont-synthesis-small-capsfont-synthesis-stylefont-synthesis-weightfont-variantfont-variant-alternatesfont-variant-capsfont-variant-east-asianfont-variant-emojifont-variant-ligaturesfont-variant-numericfont-variant-positionfont-variation-settingsfont-weightfont-widthfontforced-color-adjustframe-sizinggapgridhanging-punctuationheighthyphenate-characterhyphenate-limit-charshyphensinitial-letterinline-sizeinsetinteractivityinterpolate-sizeisolationleftletter-spacinglighting-colorlink-parametersmarginmarkermaskmix-blend-modeoffsetopacityorderorphansoutlineoverflowoverlaypaddingpagepaint-orderpath-lengthperspective-originperspectivepointer-eventspositionprint-color-adjustquotesrreading-flowreading-orderresizerightrotaterulerxryscalescroll-behaviorscroll-initial-targetscroll-marginscroll-margin-blockscroll-margin-block-endscroll-margin-block-startscroll-margin-bottomscroll-margin-inlinescroll-margin-inline-endscroll-margin-inline-startscroll-margin-leftscroll-margin-rightscroll-margin-topscroll-marker-groupscroll-paddingscroll-padding-blockscroll-padding-block-endscroll-padding-block-startscroll-padding-bottomscroll-padding-inlinescroll-padding-inline-endscroll-padding-inline-startscroll-padding-leftscroll-padding-rightscroll-padding-topscroll-snap-alignscroll-snap-stopscroll-snap-typescroll-target-groupscroll-timelinescroll-timeline-axisscroll-timeline-namespeak-asstop-colorstop-opacitystroketab-sizetable-layouttext-aligntext-align-lasttext-anchortext-autospacetext-boxtext-box-edgetext-box-trimtext-combine-uprighttext-decorationtext-decoration-colortext-decoration-insettext-decoration-linetext-decoration-skiptext-decoration-skip-inktext-decoration-styletext-decoration-thicknesstext-emphasistext-emphasis-colortext-emphasis-positiontext-emphasis-styletext-indenttext-justifytext-orientationtext-overflowtext-renderingtext-shadowtext-size-adjusttext-spacing-trimtext-transformtext-underline-offsettext-underline-positiontext-wraptext-wrap-modetext-wrap-styletimeline-scopetoptouch-actiontransformtransitiontranslateunicode-bidiuser-modifyuser-selectvector-effectvertical-alignvisibilitywhite-spacewhite-space-collapsewidowswidthwill-changeword-breakword-spacingwriting-modexyz-indexzoom:active-view-transition:active-view-transition-type():active:any-link:autofill:blank:buffering:checked:current:default:defined:dir():disabled:empty:enabled:first-child:first-of-type:first:focus-visible:focus-within:focus:fullscreen:future:has-slotted:has():heading:heading():host-context():host:host():hover:in-range:indeterminate:interest-source:interest-target:invalid:is():lang():last-child:last-of-type:left:link:local-link:modal:muted:not():only-child:only-of-type:open:optional:out-of-range:past:paused:picture-in-picture:placeholder-shown:playing:popover-open:read-only:read-write:required:right:root:scope:seeking:stalled:state():target:user-invalid:user-valid:valid:visited:volume-locked:where():xr-overlay::-webkit-inner-spin-button::-webkit-meter-bar::-webkit-meter-even-less-good-value::-webkit-meter-inner-element::-webkit-meter-optimum-value::-webkit-meter-suboptimum-value::-webkit-progress-bar::-webkit-progress-inner-element::-webkit-progress-value::-webkit-scrollbar::-webkit-search-cancel-button::-webkit-search-results-button::-webkit-slider-runnable-track::-webkit-slider-thumb::after::backdrop::before::checkmark::column::cue::details-content::file-selector-button::first-letter::first-line::grammar-error::highlight()::marker::part()::picker-icon::picker()::placeholder::search-text::selection::slotted()::spelling-error::target-text@charset@color-profile@container@counter-style@custom-media@document@font-face@font-feature-values@font-palette-values@function@import@keyframes@layer@media-moz-device-pixel-ratio-webkit-animation-webkit-device-pixel-ratio-webkit-transform-2d-webkit-transform-3d-webkit-transitionany-hoverany-pointeraspect-ratiocolorcolor-gamutcolor-indexdevice-aspect-ratiodevice-heightdevice-posturedevice-widthdisplay-modedynamic-rangeforced-colorsgridheighthorizontal-viewport-segmentshoverinverted-colorsmonochromeorientationoverflow-blockoverflow-inlinepointerprefers-color-schemeprefers-contrastprefers-reduced-dataprefers-reduced-motionprefers-reduced-transparencyresolutionscanscriptingshapeupdatevertical-viewport-segmentsvideo-dynamic-rangewidth@namespace@page@position-try@property@scope@starting-style@supports@view-transition<absolute-size><alpha-value><angle-percentage><angle><axis><baseline-position><basic-shape><blend-mode><box-edge><calc-keyword><calc-sum><color-interpolation-method><color><content-distribution><content-position><corner-shape-value><custom-ident><dashed-function><dashed-ident><dimension><display-box><display-inside><display-internal><display-legacy><display-listitem><display-outside><easing-function><filter-function><flex><frequency-percentage><frequency><generic-family><gradient><hex-color><hue-interpolation-method><hue><ident><image><integer><length-percentage><length><line-style><line-width><named-color><number><overflow-position><overflow><percentage><position-area><position><ratio><relative-size><resolution><rule-list><self-position><shape><string><system-color><text-edge><time-percentage><time><timeline-range-name><transform-function><url>-moz-image-rect()abs()acos()alpha()anchor-size()anchor()asin()atan()atan2()attr()blur()brightness()calc-size()calc()circle()clamp()color-mix()color()conic-gradient()contrast-color()contrast()cos()counter()counters()cross-fade()cubic-bezier()device-cmyk()drop-shadow()dynamic-range-limit-mix()element()ellipse()env()exp()fit-content()grayscale()hsl()hue-rotate()hwb()hypot()if()image-set()image()inset()invert()lab()lch()light-dark()linear-gradient()linear()log()matrix()matrix3d()max()min()minmax()mod()oklab()oklch()opacity()paint()param()path()perspective()polygon()pow()progress()radial-gradient()random()ray()rect()rem()repeat()repeating-conic-gradient()repeating-linear-gradient()repeating-radial-gradient()rgb()rotate()rotate3d()rotateX()rotateY()rotateZ()round()saturate()scale()scale3d()scaleX()scaleY()scaleZ()sepia()shape()sibling-count()sibling-index()sign()sin()skew()skewX()skewY()sqrt()steps()superellipse()symbols()tan()translate()translate3d()translateX()translateY()translateZ()type()url()var()xywh()Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |