Using a pseudo-element after to center CSS content

Although there are numerous posts discussing this topic, my situation is unique.

In my case, I have a collection of colors represented by circles. When I click on a circle, I add content in the form of a check mark by including a class selector with an after pseudo-element:

The CSS for the parent circle (ul element):

.lk-color-chooser__color {
  display: inline-block !important;
  width: 30px !important;
  height: 30px !important;
  border-radius: 50% !important;
  margin-right: 5px !important;
  cursor: pointer !important;
  opacity: 0.5 !important;
  filter: alpha(opacity=50) !important;
}

The child element (li representing the circle):

.lk-color-chooser__color:last-child {
  margin-right: 0 !important;
 }

When a circle is selected, I apply this class:

.color__selected {
  opacity: 1 !important;
  filter: alpha(opacity=100) !important;
}
.color__selected:after{
  content: "✔";
  margin-left: 15%;
  top: 20;
  color:#000000;
}

On deselection, I remove the color__selected class and add:

.color__not_selected {
  opacity: 0.5 !important;
  filter: alpha(opacity=50) !important;
}

However, I am facing an issue where the added check mark is not centered inside the circle.

You can access a sample code on Plunker.

Answer №1

Ensure the circle is centered by adding position:relative; to the container.

Next, give your tick a specific height and width, along with

position:absolute; right:0;left:0;top:0;bottom:0;margin:auto;

This method positions the tick absolutely relative to the LI container, keeping it perfectly centered using auto margin. It's a versatile technique that can be used for various elements.

Regardless of the circle's size, this will center the tick accurately.


.color__selected_black:after {
  content: "✔";
  /* margin-left: 15%; */
  top: 0;
  left: 0;
  color: #FFFFFF;
  right: 0;
  bottom: 0;
  margin: auto;
  width: 12px;
  height: 18px;
  position: absolute;
}
.lk-color-chooser__color {
  position: relative;
  display: inline-block !important;
  width: 30px !important;
  height: 30px !important;
  border-radius: 50% !important;
  margin-right: 5px !important;
  cursor: pointer !important;
  opacity: 0.5 !important;
  filter: alpha(opacity=50) !important;
}

Answer №2

Consider adjusting the line height for this class by adding the following CSS code snippet: View demo here

.lk-color-chooser__color {
  display: inline-block !important;
  width: 30px !important;
  height: 30px !important;
  line-height: 30px;
  vertical-align: middle;
  border-radius: 50% !important;
  margin-right: 5px !important;
  cursor: pointer !important;
  opacity: 0.5 !important;
  filter: alpha(opacity=50) !important;
}

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

Troubleshooting problems with data rendering in jQuery

Currently, my goal is to use JQuery to display a menu of checkboxes based on a specific template in a div. To enhance user experience, I have included a search box that will filter the menu items. However, there is an unusual issue occurring. When the men ...

Editing rows directly within the ng table interface

Hey there, I'm currently working on implementing inline row editing using ng table After clicking the edit icon and changing values, I encounter an error upon clicking the save icon: TypeError: Cannot read property 'untrack' of undefined ...

Cypress has the ability to exclude certain elements from its testing

How do I locate a clickable element using the cypress tool? The clickable element always contains the text "Login" and is nested inside the container div. The challenge lies in not knowing whether it's an <button>, <a>, or <input type=& ...

I'm having an issue with Internet Explorer where the link doesn't function properly when I enclose the content within an <a> tag. Could anyone

I understand that this might not be the preferred approach, but a customer has requested live text wrapped in just one HTML tag.... Here's the challenge I'm facing: I currently have some code structured like this: <a href="google.com"> ...

Adding -moz & -o prefixes to code that only has -webkit tags can be done by manually duplicating each line of

Imagine having a webpage located at this link: After saving the page and running the HTML file on my localhost, everything seems to work smoothly in Chrome due to the presence of -webkit prefixes in the CSS. Now the question arises - how can I ensure that ...

Creating a default homepage across various HTML files using Google Apps Script and deploying it as a WebApp

Is there a way to set a default main page on multiple HTML and GS files in Google AppScript? I plan to publish it as a Web App. Have you attempted using the doGet function? ...

What is the reason behind the `h-full`/`height:100%` not completely filling the void within a div element?

The issue lies in the structure of my content, which includes a title, a subtitle, and a footer. My desire is for the subtitle's div to fill the gap between the title and the footer. I attempted using h-full on the div element, but it had no noticeab ...

Angular 2 navbar malfunctioning

I'm currently working on creating a navigation bar with images that, when clicked, navigate to specific components. Here is the code snippet I have so far: <nav class="sidenav col-md-1"> <ul class="menu" routerLinkActive="active"> ...

Using a timer to make Ajax calls twice on a single webpage

Can multiple ajax calls be made simultaneously on the same page to different receiving div tags? I am struggling to find a solution to this issue. I have a total of 3 pages: a home page, and two PHP pages named status and alert which echo the results. Wi ...

Tips for creating an array within an AngularJS Service and effectively sharing it across two controllers

I have two controllers, FirstController and SecondController, along with a service defined as follows: app.factory('Data', function(){ return []; }); In both controllers, I am utilizing the service in this manner: app.controller("FirstCont ...

Avoiding page refresh while submitting a form can be tricky, as both e.preventDefault() and using a hidden iFrame seem

I've been stuck on this issue for a while now, scouring Stack Overflow and Google for solutions, but nothing seems to be working. My main goal is to avoid page reloads after uploading a file because it resets the dynamic HTML I need to display afterwa ...

Fancybox is experiencing some technical difficulties

Can anyone help me troubleshoot why my fancybox code isn't working properly after adding the script below to the body? Any thoughts on what might be causing this issue? $(document).ready(function() { $("a#example4").fancybox({ 'opa ...

Retrieving list item contents using PHP DOM scraping

Is it possible to use PHP DOM screen scraping to extract the content from an HTML tag named <li style="margin-top:10px"> that appears on one of my web pages? I am looking to retrieve all the data within the <li> tag and present it as HTML co ...

How come my flex-box navigation bar is not collapsing as I shrink the browser window size?

I'm currently experimenting with FlexBox to enhance the design of my website, specifically focusing on creating a responsive and collapsible navigation bar for mobile users. I'm encountering issues with the flex commands in the .nbar class not wo ...

Style Vue slots one by one

I am a beginner delving into the world of vue (specifically, vue 2) and aiming to grasp the concept of utilizing slots in the most effective manner. Below is the code I'm working with: <template> <div> <button cla ...

I am currently utilizing Ajax to trigger the forgot password button, which then sends an email to a specific email address through CodeIgniter

When using ajax to click the forget button and send an email to a particular email address, I am getting an error "Fail". Can anyone help me figure out what the problem might be? Below you can see the form and controller code: Form <div class="field-w ...

Turn off the Antd Datepicker tooltip

I'm using Antd Datepicker in my website. We get an array of numbers representing disabled dates from an external api and show only the dates in the datepicker(no month, year etc). The date selection is for a monthly subscription and the user just sele ...

Having trouble uploading images using ReactJS on the web platform

I am currently in the process of developing a portfolio website using react js, but I'm experiencing an issue where the image I intended to display is not showing up on the live site. Despite thoroughly checking my code, I can't seem to identify ...

Learn how to utilize ng-class to assign an ID selector to HTML elements

In my code, I have an icon that is displayed conditionally using ng-class. I am looking to include ID's for both the HTML elements. <i ng-show="ctrl.data.length > 1" ng-class="{'glyphicon glyphicon-chevron-down': !ctrl.isOpen, ...

The importance of CSS styling links: the difference between a:link, a:visited and simply using a

Curious about the difference between using: a { ... } versus a:link, a:visited { ... } ...