Highlighted text with CSS

For this exercise, I was provided with the following HTML code:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Selected text</title>
</head>
<body>
<div class="container">
  <div class="container_inside">
    <h1>Pseudo-elements</h1>
    <p class="my_paragraph">The Almighty CSS allows you to not only work with
      elements declared in your HTML code but to also customize those parts of
      the page you can not address using simple selectors. This might be the
      customization of the first line of the paragraph, the first letter of the
      paragraph, or even the part of the text user has just selected.</p>
    <p>Here is the list of the most-used pseudo-elements:</p>
    <ul class="my_list">
      <li>::first-line</li>
      <li>::first-letter</li>
      <li>::placeholder</li>
      <li>::marker</li>
      <li>::before</li>
      <li>::after</li>
      <li>::selection</li>
    </ul>
  </div>
</div>
</body>
</html>

The goal is to 'Change the color of the text inside .my_paragraph to white and give it a black background when it is selected.'

Below is the given CSS code for reference:

/* Your solution goes here */


.my_paragraph::first-line {
    background: linear-gradient(90deg, #9d3ce9, #5fbf85);
    color:white;
}

.my_list li::marker {
    color:#9d3ce9;
}

.container {
    align-items:center;
    display:flex;
    height:100vh;
    justify-content:center;
    width:100vw;
}

.container_inside {
    align-items:center;
    display:flex;
    flex-direction:column;
    height:80%;
    justify-content:center;
    text-align:center;
    width:60%;
}

.my_list {
    text-align:left;
}

To address the task, I included the following snippet in the CSS section:

.my_paragraph::selection {
    color:white;
    background-color:black;
}

If there are any issues or corrections needed, please let me know.

Thank you in advance.

Answer №1

Include the following to highlight selection

 .custom_selection::selection {
        color: yellow;
        background-color: blue;
    }
 .custom_selection::-moz-selection { 
        color: yellow;
        background-color: blue;
}

Answer №2

Your lack of style in your HTML file is evident! To remedy this, insert the following code into your HTML header and substitute "style.css" with your actual CSS file name:

<link rel="stylesheet" href="style.css">

Answer №3

Success! The accurate code has been located:

.my_paragraph::selection {
    color: white;
    background: black;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Display content in two columns. If the width of the left column is too narrow, move the content in the right column

In my layout, there is a wrapper div with two child elements: left div and right div. The left div contains text which can be lengthy, while the right div holds an image loaded asynchronously without known width: <div id="wrapper"> <div id="l ...

Alert users before visiting external websites

Is there a way to notify users about external pages without using popups like in the first example here: Instead of the unwanted code shown above, is there an alternative solution? Is there a different approach that can be taken? For instance, check out ...

How to insert an image into a placeholder in an .hbs Ember template file

I'm looking to enhance a .hbs template file in ember by incorporating an image. I am not a developer, but I'm attempting to customize the basic todo list app. <section class='todoapp'> <header id='header'> & ...

Spacing between navigation items

After working on my navigation menu locally, I noticed that when I uploaded it online, some changes didn't take effect on Chrome and the design on Firefox was different from what I intended. I'm not sure what I might be doing wrong here. Here is ...

Positioning children within a div element

Howdy! I have a neat little setup with two divs stacked on top of each other, each containing child elements. My goal is to hide the first div and have the second div seamlessly take its place while keeping the child elements in their original position. C ...

The Bootstrap card is elegantly framed by a crisp white border surrounding the

After referencing a tutorial, I implemented the following code: <div class="card" style="width:400px"> <img class="card-img-top" src="image.png" alt="Card image"> <div class="card-body"> <h4 class="card-title">John Doe</ ...

What is causing the Bootstrap 5 navbar to not collapse?

Struggling to compile Bootstrap 5 with sass, I have a feeling that I might be missing some important scss files. When I added the <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7 ...

``Troubleshooting Problem with Tailwind's Flex Box Responsive Grid and Card Elements

Starting point: Preview https://i.sstatic.net/zfzBU.jpg Code : <div class="container my-12 mx-auto"> <div className="flex flex-wrap "> {error ? <p>{error.message}</p> : null} {!isLoading ...

iOS exhibits a flashy display of Material Select lighting up

In my attempt to develop a website using Material Design, I encountered an issue with the Material Select component. Whether I utilize MDB (Material Design for Bootstrap) or the Materialize CSS framework, both function well on Windows/OSX/Android devices. ...

When the browser is resized, elements do not change position

I am attempting to build a website with solely horizontal scrolling. Check out this demo for reference. My issue arises when I resize the browser, as the content (paragraph within the light yellow box) fails to reposition itself. Ideally, I would like that ...

Remove the image from the container in Bootstrap

I'm looking to position an image outside the container so that the right margin remains within the default container width (responsive) while keeping the image aligned with a button consistently. Please refer to the image below for clarification: ht ...

Creating a unique Bootstrap background design

I have recently started using bootstrap and I am facing an issue. I want to change the background of a container when hovering over it, but I am having trouble getting it right. HTML <div class="container bg-black h-25 w-25 box"></div&g ...

Determining the dimensions of a div with a scrollbar using Jquery

Is there a way to get the width and height of a div with scroll that includes both visible and hidden content using JQuery? If anyone has a solution for getting these values, please share. <div id="area" class="divLeftCem area"> <div id="pan ...

Various image dimensions cause divs to shift unevenly

Here is the current setup: <div class="row"> <div class="col-md-4 content-column"> <img class="btn-center btn-desktop" src="images/buttons/btn-desktop.svg" alt="desktop button"> <h2 class="btn-deskt ...

The unequal division of space between two flexed child divs is caused by varying image sizes

In my parent container, I have set the display property to flex. Inside, there are two divs with both having flex:1 set. However, the first child div contains an image and takes up twice as much space as the second div. If the screen is 1000px in height, ...

Tips on boosting CSS specificity in Material-UI's makeStyle for styled components in order to successfully override the root style

As a newcomer in the world of React, I am attempting to utilize MUI makeStyles to customize the existing styles. However, I have encountered an issue where setting fontWeight to 'bold' does not seem to work, even when using !important. Any sugges ...

Tips for showcasing an image prior to uploading within a personalized design input file

I am facing an issue with displaying multiple images inside custom style input file labels before uploading them to the database. Currently, the script I am using only displays one image at a time and in random positions. My goal is to have each image ap ...

Unlocking the full potential of Bootstrap with the colspan feature

I'm currently in the process of developing an Angular 2 and Bootstrap application. Here's a snippet of my code: <div class="panel-body"> <table class="table table-striped table-bordered" style="width:100%;"> < ...

Is it possible to conceal the Google recaptcha badge using CSS and show the legal text in its place instead?

We have successfully hidden the Google reCaptcha badge on our website using CSS: <style> .grecaptcha-badge { visibility: hidden; } </style> As a result, there is now an empty space where the reCaptcha badge used to be display ...

Error in JavaScript Slideshow

I am attempting to create a slider that changes with a button click. Below is my JavaScript code: var img1=document.getElementById("p1"); var img2=document.getElementById("p2"); var img3=document.getElementById("p3"); function slide(){ ...