Adjust the color of the background in the p element with a touch

This webpage contains multiple instances of the <p> element. I want to change the background color of the paragraph that a mobile user touches. When another paragraph is touched, the previously selected paragraph should revert back to its original "default white" background color.

I have tried several selectors with no success. Is there a way to achieve this using CSS? Thank you for your help.

edit
I have 2-3 lines of data that need to be shown to the user. I decided to use paragraphs for each group of 2-3 lines so I can format and display the data like a vertical list. Therefore, I want to be able to select an item from the list and perform other actions related to the selected item.

.selectable:focus {
    background-color: lightgray;
}
<template name="myName">
  <p class="selectable"><b>{{value.[0]}}</b><br/>{{value.[1]}}<br/>{{value.[2]}}</p>
</template>

Answer №1

After taking @CBroe's advice into consideration, you have the option to implement the following:

<p class="selectable" tabindex="0"> Paragraph 1 </p>
<p class="selectable" tabindex="0"> Paragraph 2 </p>
<p class="selectable" tabindex="0"> Paragraph 3 </p>
<p class="selectable" tabindex="0"> Paragraph 4 </p>

.selectable:focus {
    background-color: lightgray;
}

To see a demo, visit the jsfiddle link.

If you're interested in learning more about tabindex, check out this resource here.

Answer №2

To achieve this effect using jQuery, you can follow this code snippet:

jQuery:

$("p.selectable").click(function(){
  $("p.selectable").removeClass("clicked");
  $(this).addClass("clicked");
})

CSS:

p.selectable {
  background:white;
  height:100px;
}
p.selectable.clicked {
  background: red;
}

You can view a demonstration on jsfiddle. While it is challenging to achieve this solely with CSS as it is primarily for styling, JavaScript allows for more interactive features. (There are hacky CSS methods like overlaying invisible checkboxes over paragraphs, but let's not delve into that)

UPDATE: An alternative method involves adding a tabindex as recommended by @CBroe. It's a clever trick :) For additional actions triggered by paragraph clicks, opt for the JavaScript approach. If only style changes are needed, stick with the CSS solution.

Answer №3

One unconventional approach to achieve this is by using the :checked pseudo-selector along with text to switch its "selected" status.

.selectable {
  display: none;
}

.selectable:checked + label {
  color: red;
}
<input type="checkbox" id="unique-id" class="selectable">
<label for="unique-id">
  <b>{{value.[0]}}</b><br/>{{value.[1]}}<br/>{{value.[2]}}

</label>

This method eliminates the need for JavaScript and offers stable performance on various devices and platforms.

If you have any queries or require further clarification, feel free to reach out!

Answer №4

  • First, set up an event listener for the desired action
  • Then, target the specific elements where you want this action to apply
  • Lastly, add the functionality you need - such as changing the background color by adding a new class and removing any existing color.

    $("p").on("click",function(){
        $('.new-color').removeClass('new-color');
        $(this).addClass('new-color');
    });
    

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

Transforming a dropdown menu to inline using CSS

Here is the current situation at the top, and below is what I'm aiming to achieve I am trying to customize the language switcher on this WordPress-based website. My goal is to have both flags displayed next to each other in a single line without requ ...

Centered image in circles positioned horizontally and vertically, all styled in Bootstrap 4

I am currently working on achieving a design similar to the one shown in the image below using Bootstrap 4 and CSS: https://i.sstatic.net/now05.png Here is the CSS code I have developed so far: .inner-circle { display: block; height: 70px; w ...

Ajax requests across different domains are unsuccessful, even when attempting to access a file on

I have a local html file that contains an ajax function attempting to retrieve xml content from x.com. When I run the file, it only works in Internet Explorer and fails in Firefox and Safari browsers. This issue is likely due to the Same Origin Policy. H ...

Tips for perfectly aligning an "inline-block" element with an ordered list (ol)

I'm experiencing an issue with the alignment of my ordered list. My list is structured as follows: <ol type="a"> <li><button style="display: inline-block">TEXT1</button></li> <li><button style="display: inli ...

The Bootstrap column and row elements are having difficulty cooperating with their respective parents and children

Bootstrap functionalities like -xl are working, but when I use multiple classes such as col-lg-4 and col-md-4, they do not align in one row. I have already set the parent to row and the child to col, but they are still not displaying correctly. Images th ...

Fluid Bootstrap Container Experiencing Alignment Issues with Rows when CSS Height Property set to 100%

My Bootstrap container has a fluid width and I'm trying to align three rows within a sidebar to sit at the top, middle, and bottom. I came across this demo in the Bootstrap documentation which shows how to do it for full-width layout, but I need it fo ...

Adjusting the position of the floating image on the left side will impact the height of the container

When attempting to execute the code below: ​<div id="container"> //This is a 200x200 image <img src="http://dummyimage.com/200x200/CCC/000" /> </div>​​​​​​​​​​​​​​​​​​​​​​ ...

Showing off the latest products at the top of the list

Typically, when utilizing ngFor, the most recent item is displayed underneath the initial element. For instance, a list containing: [Apple, Orange, Banana] If we use ngFor to display this list: Apple Orange Banana I am interested in learning a method t ...

What are the steps to display the entire image instead of a cropped version?

Currently in the process of constructing a website using HTML/CSS/Bootstrap/Js. I have encountered an issue while attempting to overlay a jumbotron on a container with a background image utilizing z-index. Unfortunately, the background image I am using k ...

Clicking on a select box causes the scrollbar area to vanish, shifting all the page content to the right

I have successfully implemented overflow-y: scroll; to keep the scrollbar space always reserved and prevent content from shifting when the scrollbar appears. However, I noticed that when I click on a select element, this scrollbar space is lost. How can I ...

The toggle feature in Bootstrap is stuck in the "on" position and refuses to "untoggle."

Currently, I am working on developing a basic website using Laravel and Vue.js. To ensure that the website is responsive, I integrated Bootstrap 4 into the project. For the most part, everything appears to be functioning correctly - the navbar collapses as ...

Determine the X and Y coordinates of a circle element using HTML and Javascript

I am trying to retrieve the X and Y coordinates of an HTML circle element using JavaScript. Here is the snippet of my HTML code: <svg id="hotspot_canvas" name="hotspot_canvas" class="hotspot" width="800" height="1570.66666667" style="background-ima ...

GET requests will not be allowed

Having an unusual issue where the GET parameters are not being passed properly. <!DOCTYPE html> <html> <body> <?php var_dump($_GET); var_dump($_POST); ?> <form action="test.php?ds=1" m ...

Full image with stylish CSS text overlay

I'm having trouble displaying an overlay text that should look like: https://i.sstatic.net/GccZy.jpg This is what I've tried: <div class="row"> <div class="col-md-3 program-cat"> <img src="<?php ech ...

Adjust the Weight of a Single Word in H1 CSS

I've been trying to figure out how to achieve the following for a while now. I know I can solve this issue by using different h1 tags and positioning them separately, but I'm curious to find out if there's a way to do it without dividing the ...

Guide on setting a personalized color for progress bars in Bootstrap 4

I'm working on a progress bar: <div class="progress"><div class="progress-bar" style="width:{{a.49}}%">{{a.49}} %</div> </div> Is there a way to apply a custom color to the progress bar based on a condition in CSS? : If a.49 ...

What is the correct way to format text as superscript?

In my application, I have noticed that there are a lot of numbers and letters displayed in superscript. For example, using <span style="vertical-align: super;">3</span> results in X<sup>3</sup>. However, simply using the sup tag als ...

Is it possible to execute HTML5/JS code without relying on the client side?

Looking to develop animations using HTML5 canvas and then convert them into a video by capturing each frame. The challenge lies in running the HTML5/JS code on the server side without requiring a client(browser). Is there a way to execute it without displ ...

Generating CSS and PHP through Grunt

While working with yeoman, I've integrated grunt-php and managed to load PHP files successfully. However, the CSS file seems inaccessible. This could be due to SCSS not being compiled or the path in my index.php not pointing to the temporary CSS file. ...

Can you tell me why one of these Sass codes is throwing an invalid CSS error while the other is not?

My code: @for $j from 1 to 6 { .text-#($j) { font-size: 15px * $j; } } I'm facing an issue where my code doesn't run as expected. In an attempt to fix this, I modified it to use a 'through' keyword instead of 'to' in the loop ...