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

Switching from file:// to http:// in Angular / Ionic is a necessary step when incorporating external scripts like the Disqus directive into your project

Currently, I am attempting to incorporate the disqus directive into my project. The documentation for this directive can be found here. However, I have encountered some issues due to the particular setup of my application. There is a line in the script wh ...

Can one intercept the window.location.replace event?

Suppose I am currently browsing the URL "example.com/somepage#somehash" and then decide to execute window.location.hash = "anotherhash", the URL will be updated to "example.com/somepage#anotherhash". This action triggers the window.hashashchange event. On ...

Smooth scrolling in JavaScript can lead to unexpected jumps when using scroll-margin-top

I am currently working on implementing smooth scrolling into my website using JavaScript. However, I have run into a problem with CSS property scroll-margin-top causing a sudden jump at the end. The CSS I am using for scroll margins looks like this: class ...

Demystifying Vertical Alignment: Everything You Need to Know

Struggling with vertical alignments has proven to be more complicated than expected. Despite reading numerous resources on stackexchange, a common understanding of the process remains elusive. After gathering some guidelines, it turns out that vertical-al ...

Is it possible to replicate the dynamic depth effect of the cards on music.google.com using Angular Material?

Upon hovering over the "day of week music station cards," I observed that they gain depth or height. Can the same effect be achieved with Angular Material Design for cards? Please provide a demo code as an example. ...

The CSS and Javascript files are failing to load

My web page is not displaying my CSS and JavaScript properly when I access it through a folder in the browser. Both files are located within multiple subfolders. I have attempted to rename the files to troubleshoot the issue. <head> <link rel=" ...

Customizing the design of vuetify table header to your liking

My goal is to implement a custom style for v-data table headers using the fixHeader method. The CSS code is intended to keep the header fixed in place even when scrolling horizontally. The issue arises as the style is applied to the inner <span> ele ...

What is the best approach to retrieve a JSON object from a form exception that has a specific name?

I have a certain form with input fields. My goal is to generate a JSON object from the form, excluding any input whose name is "point". However, my code doesn't seem to be working properly when I use the "not()" function. What am I doing wrong and how ...

How can you create a blurred effect on a navbar?

Looking to achieve a unique blur effect in the background of my navbar component. The standard CSS blur effect isn't giving me the desired result, so I'm seeking some guidance on how to customize it. For example: https://i.sstatic.net/bPEXZ.png ...

Having trouble with displaying the results of JQuery Ajax in Chrome?

I am facing a compatibility issue between IE 8 and Chrome 16.0.912.77 with the following code. Any suggestions? <html> <head> <script src="http://code.jquery.com/jquery-latest.js"></script> <script> $(document).ready(fu ...

Is there a way to disable the calendar linking feature in the iPhone Mail Client

I am currently working on an application where I generate emails with time stamps. However, when viewing these emails in the iPhone mail client, the timestamp gets turned into a link with an ugly blue color and underline. Is there a way to prevent this fro ...

What is the correct way to invoke a function from an external JavaScript file using TypeScript?

We are currently experimenting with incorporating Typescript and Webpack into our existing AngularJS project. While I have managed to generate the webpack bundle, we are facing an issue at runtime where the program is unable to locate certain functions in ...

arrange fixed-top elements in a stacked manner using Bootstrap 4

I am looking to inform users about enabling JavaScript for optimal use of the website, and I want this message to appear above the navigation bar using Bootstrap. However, currently they are stacking on top of one another instead of displaying in the desir ...

Issue with AngularJS $scope data binding not working properly inside an iframe element

I find myself in a foreign place.... in my home, why does this happen: Video Url: {{exercise.videos[0].url}} It works correctly by displaying the video url... but: <iframe src="http://fast.wistia.net/embed/iframe/{{exercise.videos[0].url}}"></ ...

What is the technique for creating a repeating image rather than stretching it?

Is there a way to repeat a background image to fit its content? I have a background image that needs to be repeated in this manner: https://i.sstatic.net/qVKkp.png However, when using the following code: .imgbord { background: url('https://i.imgur ...

Replacing a div with another div can be achieved in a single swap

My goal is to swap one div with another div upon clicking using the provided code. It functions properly for a single instance. However, the issue arises when the class appears multiple times, causing all instances to change due to sharing the same class n ...

Having difficulty decoding audio streams in Flask Socket.IO due to a file received by the Python server

Thank you for taking the time to go through this post. I have been diligently working on a project that involves capturing audio input from the client side and then sending the recorded audio to a Python server for analysis. However, I have hit a roadblock ...

How to add a background image in CSS with HTML's help

I am a beginner learning HTML and CSS, attempting to incorporate a Full Screen Background Image using CSS. Unfortunately, the code I have written is not working as expected despite following various tutorials. Below is my code: HTML file <!DOCTYPE htm ...

Is it possible to restrict the draggable area?

Looking at this JSFiddle example, there is a box that can be dragged around the body. But, is it feasible to restrict dragging only when the user clicks on the purple section identified by the id "head"? $(document).ready(function(){ $( "#box" ).dra ...

Using Python and Selenium, you can locate an element inside another element

I've got the xpath for an element: /html/body/div[1]/div/div[2]/div[3]/div/div/div/div[2]/div[1]/div[2]/div[6]/div/div/div/div[1] Inside that div, there's another element: /html/body/div[1]/div/div[2]/div[3]/div/div/div/div[2]/div[1]/div[2]/div[ ...