Tips for incorporating a Spotify-style play button using CSS into a table row

Hey there! I recently attempted to customize my button like Spotify.

I wanted the table row to display a play button when hovered over, and when clicked, the image would change.

https://i.sstatic.net/aJIna.jpg

After seeking advice from another fiddle and making some updates, now when I click on one element, the other one hides. However, I'm still figuring out how to hide one of them initially.

a {
  display: block;
  font-size: 18px;
  border: 2px solid gray;
  border-radius: 100px;
  width: 100px;
  height: 100px;
  text-align: center;
  line-height: 100px;
}
a:hover {
  font-size: 18px;
  border: 2px solid green;
  border-radius: 100px;
  width: 100px;
  height: 100px;
}
a:target {
  display: none;
  font-size: 18px;
  border: 2px solid red;
  border-radius: 100px;
  width: 100px;
  height: 100px;
}
<a id="btn" href="#btn">Play</a>
<a id="btn2" href="#btn2">Pause</a>

Answer №1

If you want to achieve this effect using only CSS and no scripting, you can follow the method below.

a {
  display: block;
  font-size: 18px;
  border: 2px solid gray;
  border-radius: 100px;
  width: 100px;
  height: 100px;
  text-align: center;
  line-height: 100px;
}
a:hover {
  font-size: 18px;
  border: 2px solid green;
  border-radius: 100px;
  width: 100px;
  height: 100px;
}
a:target {
  display: none;
  font-size: 18px;
  border: 2px solid red;
  border-radius: 100px;
  width: 100px;
  height: 100px;
}
a:nth-last-child(2){
  display:none;
  }
a:first-child:target + a:nth-last-child(2){
  display:block;
  }
<a id="btn" href="#btn">Play</a>
<a id="btn2" href="#btn2">Pause</a>

Answer №2

To hide an element, you have a couple of options. One way is to use inline style and set the css property display to none:

<a id="btn2" href="#btn2" style="display:none;">Pause</a>

Alternatively, you can achieve the same result using JavaScript:

document.getElementById('btn2').style.display = 'none';

I hope this information proves helpful.

Answer №3

To achieve this with Jquery, you only need a single line of code. Simply change the text when the link is clicked.

$('.play').click(function(){
    var $this = $(this);
    $this.toggleClass('active');
    if($this.hasClass('active')){
        $this.text('Pause');         
    } else {
        $this.text('Play');
    }
});

Check out the demo here

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

What is the process for using a click event to redirect to a different page on a website?

Currently, I am working with vue-bootstrap and I have a set of cards, each containing a button. I want each of these buttons to redirect me to a different form, but I am struggling with implementing this functionality. Below is the snippet of my code: < ...

Basic click event triggered every second click in JavaScript and HTML

onclick="HandleAction(\'playnow\');HandleAction(\'stop\');" This element performs two functions simultaneously. However, it only executes the actions \playnow\ and then \stop\ immediately after. ...

The Vue.js input for checkboxes and radios fails to toggle when both :checked and @input or @click are used simultaneously

Check out this example on JSFiddle! <script src="https://unpkg.com/vue"></script> <div id="app"> <label> <input type="checkbox" name="demo" :checked="isChecked" @input=" ...

What is the best way to identify a modification in a select element within React?

My HTML form has a dropdown menu for users to select colors, and I'm attempting to determine when a new color is chosen. Despite adding the `onchange` attribute with a `console.log()` function, nothing appears in the console when I pick a different co ...

Encountering a problem when attempting to utilize AngularFire's Cloud Messaging Angular module due to missing configuration values for the app

Working with Firebase Cloud Messaging in my Angular application using AngularFire2 has presented a challenge. I am facing an error when attempting to retrieve the user's current token. I have already set up the "firebase-messaging-sw.js" and "manifes ...

Discover the Names of Elements associated with the identical Input File tag

Having a bit of trouble with my jQuery code here. I've got two HTML input file elements with different names. How can I retrieve the name of the second input file using jQuery? Below is my HTML markup: <input type="file" name="photo_a" /> &l ...

Updating a JSON property in JavaScript on the fly

I am looking to dynamically replace the content of "placeholder" with {"John": "Dough"} using a function call. This initial method seems to work fine: a = {foo:{bar:{baz:"placeholder"}}}; a.foo.bar.baz = {"John" : "Dough"}; console.log(JSON.stringify(a)) ...

In Chrome, the $http GET request fails to send the JSESSIONID, but it functions properly on Firefox with AngularJS

Here is the code snippet I am working with: $http({ 'method': 'GET', 'url': 'http://www.example.com', 'withCredentials': true, headers: { 'Content-type': &apo ...

Looking to implement an underline effect using CSS for the active tab that mimics the hover effect already applied

How can I ensure that the underline on active tabs (Domov) matches the hover effect on other tabs? The border-bottom currently creates a border that is lower than the hover effect, resulting in inconsistent underlines as seen on this page - (try moving ...

The AJAX file retrieval function is not functioning

I'm working on downloading a file using ajax, but I seem to be facing an issue. Can anyone help me figure out what's going wrong with the code below? url = "https://firebasestorage.googleapis.com/v0/b/analyst-3206a.appspot.com/o/research_reports ...

What causes certain content to be inaccessible when using Safari?

When I visit this page using Safari or my iPhone, the list of social network members does not appear. Can anyone explain why this might be happening? I am unable to figure out the reason behind this issue. Thank you in advance for your help! ...

Adjust camera view according to the rotation in three.js

I am in the process of creating a demonstration, and I'm facing an issue with moving the camera in my scene in the direction it is pointing. The concept is similar to pointer lock controls, but I need the camera to have the ability to move up, down, f ...

Use the useEffect hook to pass newly updated data to a FlatList component

I have encountered an issue with updating a FlatList component in my React Native application. The scenario involves running a graphql query to render items and then refetching the data when a mutation is executed using Apollo's refetch option. Althou ...

Make the mp3 file automatically download/save as by forcing the link

My website has links to mp3 files using normal <a href="file.mp3"> tags. However, many users with Apple Quicktime installed experience the mp3 files opening instead of saving when clicking on the links. Is there a way to force the browser to save t ...

Customizing the hover behavior of a div element without causing displacement of surrounding elements

I am facing an issue where the descriptions in my list of 100 items are longer than the width of the div, causing the text to be cut off. I want to display the full description on hover without affecting the layout of other items. Currently, when I hover o ...

Why am I getting multiple results when using the findOne method in my Mongoose query?

Why is my route not returning only one matched thing when using findOne with the thing object id as a parameter? I have attempted to use findOne with the id as a parameter. Schema of the Thing: const mongoose = require("mongoose"); const thingSchema = m ...

Expanding URL path parameters in Angular's ui-routerWould you like to

Can UI-router handle this type of routing? Parent state - /saved/:id Child state - /saved/:id/eat Here is the code snippet I am using. However, when I attempt to access it, the page redirects: .state('fruits.banana.saved', { url: &apo ...

Seeing the Bootstrap css load immediately upon the page loading and switching between pages

Upon loading my webpage or navigating between subpages, I've noticed that the Bootstrap.css styles are interfering with my CSS transitions. For some reason, my regular links appear orange initially, but they turn blue during loading or when transition ...

Sheetjs is only capable of processing the initial 1000 rows of data

I have successfully integrated sheetjs to import excel files into a database. However, I have encountered an issue where it only imports the first 1000 rows of data. Here is my code: Whenever a user uploads a file using the input field, a worker is create ...

What is the method to process a JSON path containing a special character like "@"?

I'm attempting to retrieve JSON data from an API with the following structure. My current approach involves using JavaScript. The code snippet I have utilized is displayed below. <p>"stations": [</p> <p id="data"></p> ...