Box with plus symbol

One of my current projects involves creating a pill button with the following styling:

<button class="button">Pill Button</button>
.button {
  border: 1px solid red;
  color: black;
  padding: 10px 20px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  margin: 4px 2px;
  cursor: pointer;
  border-radius: 16px;
}

I'd like to add a cross inside this button, which will allow users to remove it from the page when clicked. The image for the cross can be found here. Can you advise on how I can achieve this functionality?

Answer №1

If you want to enhance the functionality of this button, consider adding another element and triggering a click event.

function expandBtn(){
 console.log('expand button clicked');
 event.stopPropagation();
}
function btnPress(){
 console.log('button pressed')
}
.custom-button {
  border: 1px solid blue;
  color: white;
  padding: 12px 24px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  margin: 6px 4px;
  cursor: pointer;
  border-radius: 20px;
}
<button onclick='btnPress()' class="custom-button">Round Button <span onclick='expandBtn()'> expand</span></button>

Answer №2

If you want the button and X to have the same function, simply use the remove method.

const btns = document.querySelectorAll("[data-action='delete']");
btns.forEach(function(btn) {
  btn.addEventListener("click", function(evt) {
    const clickedElem = evt.target.closest('button');
    clickedElem.remove();
  });
});
[data-action="delete"]::after {
  padding-left: .3rem;
  content: '\2297';
}
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b2d0ddddc6c1c6c0d3c2f2869c849c80">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet"/>

<button type="button" class="btn btn-outline-danger" data-action="delete">Something 2</button>
<button type="button" class="btn btn-outline-danger" data-action="delete">Something 2</button>

If you prefer the X and button to perform different actions, it's best to make them separate elements.

const groups = document.querySelectorAll(".btn-group");
groups.forEach(function (group) {
  group.addEventListener("click", function (evt) {
    const clickedElem = evt.target.closest('[data-action]');
    if(clickedElem.dataset.action === 'remove') {
      clickedElem.closest('.btn-group').remove();
    } else {
      console.log(clickedElem.textContent);
    }
  });
});
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="12707d7d66616660736252263c243c20">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div class="btn-group" role="group" aria-label="Basic example">
  <button type="button" class="btn btn-primary" data-action="details">Something 1</button>
  <button type="button" class="btn btn-outline-danger" data-action="remove">&otimes;</button>
</div>


<div class="btn-group" role="group" aria-label="Basic example">
  <button type="button" class="btn btn-primary" data-action="details">Something 2</button>
  <button type="button" class="btn btn-outline-danger" data-action="remove">&otimes;</button>
</div>

Answer №3

Here is a way to conceal the X inside the button:

.button {
  border: 1px solid red;
  color: black;
  padding: 10px 20px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  margin: 4px 2px;
  cursor: pointer;
  border-radius: 16px;
  display: block;
  max-width: 90px;
}

.button:after {
  content: "\271A";
  background: red;
  color: white;
  transform: rotate(45deg);
  position: absolute;
  width: 23px;
  height: 23px;
  border-radius: 30px;
  top: 17px;
  left: 115px;
}

.two:after {
  content: none;
}

#undoer,
:target {
  display: none;
}

:target+#undoer {
  display: block;
}
<a href="#start" id="start" class="button">Pill button</a>
<a href="#stop" id="undoer" class="button two">Pill button</a>

To completely conceal the button:

.button {
  border: 1px solid red;
  color: black;
  padding: 10px 20px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  margin: 4px 2px;
  cursor: pointer;
  border-radius: 16px;
  display: block;
  max-width: 90px;
}

.button:after {
  content: "\271A";
  background: red;
  color: white;
  transform: rotate(45deg);
  position: absolute;
  width: 23px;
  height: 23px;
  border-radius: 30px;
  top: 17px;
  left: 115px;
}

.button:target {
  display: none;
}
<a href="#start" id="start" class="button">Pill button</a>

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

Creating a dynamic table accordion with PHP and MySQL

I am currently working on creating an accordion table to display data retrieved from a database. I want the description data to be shown in a row below when clicking on the respective row. Despite my efforts in modifying various code snippets that I have c ...

Utilizing the power of HTML5 canvas technology, coupled with advanced

We are currently experimenting with using HTML5 canvas in conjunction with tablet styluses. However, we have encountered an issue where palm touching on multitouch tablets causes unintended lines to appear while drawing. Is there a way to disable multitou ...

Transferring class titles between div elements

I would like to implement a feature where class names on div elements shift after a brief delay, perhaps 10 seconds. My goal is for the 3 classes listed below to cycle through the div elements and start over once it reaches the last one. For instance: & ...

Pushing down menu items in a dropdown CSS navigation bar

I have a vertical navigation menu with parent and non-parent items. I want the parent items to display their child pages when hovered over, while also pushing down the items above them. HTML: <nav class="main"> <ul> <li> ...

When it comes to using jQuery, I find that it only functions properly when I manually input the code into the Google Chrome console. Otherwise

Below is the HTML snippet: <textarea cols="5" disabled id="textareRSAKeypair"> @Model["keypair"] </textarea> <a href="#" class="btn btn-primary" id="downloadKeypair">Download Key</a> Here is the jQuery code: <script src="ht ...

css, align top menu

Is it possible to achieve this using only CSS? Please take a look at my example here. Currently, when I hover over the menu, the submenu appears next to it. I would like to align all submenus' tops with the top of the second level menu. ---------- ...

Lumx motion not functioning properly

Exploring the world of Django, angularJS, and lumx in my website creation. Still navigating my way through this framework, especially when it comes to basic lumx functionalities. Followed the installation instructions from (using "bower install lumx"). Th ...

The styling in Docker for Asp.Net Core is not being properly displayed

I recently deployed my first ASP.NET Core application using Visual Studio for Mac inside a Docker container on Linux. However, I'm facing an issue where the CSS is not being applied and the JavaScript is not functioning as expected. It seems like the ...

Refresh data after modal is closed in AngularJS

-->html code <button type="button" class="btn btn-primary" ng-click="platformAppConfigurationCreate()"> <span class="fa fa-plus"></span> <span class="hidden-xs hidden-sm"> Create </span> </button> --> C ...

Is there a method to automatically execute an 'onchange' function whenever a value is updated due to an AJAX response?

Many drop down menus are present on my page; <... onchange="callthisfunction(...)"...> While a manual change easily triggers the function, using ajax to load saved data does not register the value as changed and the function is not triggered. Is th ...

Problem with AngularJS function execution - require a delay in executing the second function

I am developing a small utility in AngularJS to manage the shopping cart for users. Here is the code snippet from my cart-service.js file: var myStoreCartService = angular.module("myStoreCartService", []); myStoreCartService.factory('Cart', fu ...

Converting SQL table data into an array using JavaScript

I have some JavaScript code below that connects to a database and retrieves data from a table //Establishing database connection var sql = require("mssql"); var config = { user: '****', password: '*****', server: ' ...

Websites are operating at a sluggish pace

Yesterday, out of nowhere, we encountered a vserver issue. If you visit this particular Website: You will notice that it takes forever to load. This problem is affecting all websites hosted on the vserver. I reached out to the Provider to see if there we ...

Having difficulty linking a subclass in HTML with CSS

<div class="section-3"> <div class="section-3a"> <div class="section-3a(a)"> <p>light background color (header and sidebar)</p> </div> <div class="section-3a(b)"> <p>#FFFFFF</p> ...

What is the best way to conceal the blackberry cursor in a web browser?

Is there a way to conceal the blackberry cursor while browsing on the browser? Perhaps through Javascript or CSS? I am attempting to replicate the functionality found in native apps where users can flick through items with their finger. I find this featur ...

Tips for identifying HTML tags that include a specific attribute automatically

Is there a way to automatically identify an HTML tag that contains a specific attribute like data-wow-delay? This query revolves around wow.js. The goal is to have the classes wow bounce added automatically to any HTML tag that has this particular attribu ...

Having a peculiar margin problem in IE11 - potential connection between overflow and smooth scrolling

Lately, I've been facing a small issue with a website plenarto.github.io that has been bothering me for the past few days. There is an odd white margin that mysteriously appears on the right side of the website whenever you access it using IE11. This ...

Send JavaScript variable using a query parameter

I am working on passing a JavaScript variable across all pages in my project. The goal is to calculate the total value from various pages and display it at the end of navigation. Below is an example of my JS code: <script> $(document).ready ...

I recently ran npm install --save bootstrap and now I am unable to locate the bootstrap.min.css file anywhere

Trying to follow a tutorial based on a video, I'm running into an issue where I can't locate the bootstrap.min.css file mentioned. Despite searching online, I haven't found any relevant information. The files I have access to are: https://i ...

Combining CSS styles

Currently, I am in the process of integrating RTL support into a vast framework. To achieve this, I have replaced all directional rules with mixins like: a { @include padding(0, 1px, 0, 0); @include margin(0, 1px, 0, 0); } This implementation ensures ...