HTML-CSS: Custom text selection

HTML-CSS : Exercise-2 with Solution

Using HTML, CSS change the styling of text selection.

  • Use the ::selection pseudo-selector to style text within it when selected.

HTML Code:

<!--License: https://bit.ly/3GjrtVF-->
<!DOCTYPE html>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Using HTML, CSS change the styling of text selection</title>
<div class="w3rcontainer"><strong>Preview:</strong>
<p class="custom-text-selection">Select some of this text.</p>

CSS Code:


::selection {
  background: orange;
  color: black;

.custom-text-selection::selection {
  background: green;
  color: white;


HTML-CSS Editor:

See the Pen html-css-practical-exercises by w3resource (@w3resource) on CodePen.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

HTML-CSS: Tips of the Day

How can I show dots ("...") in a span with hidden overflow?

For this you can use text-overflow: ellipsis; property. Write like this

span {
    display: inline-block;
    width: 180px;
    white-space: nowrap;
    overflow: hidden !important;
    text-overflow: ellipsis;
<span>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book</span>

Ref: https://bit.ly/2RqEbdh