Aligning content in a parent DIV using CSS after pseudo-element and float

Is there a way to center content vertically, which is generated by a ::after pseudo-element, within the parent element even when the ::after pseudo-element has a float: right declaration?

The CSS code I have looks like this:

.label-dot::after, .label-dot-1::after {
  content: "";
  border-radius: 50%;
  display: inline-block;
  float: right;
  height: 10px;
  width: 10px; }

.label-dot-1::after {
  background-color: #66c2a5; }

This CSS is applied to the test HTML below:

<div style="width: 500px; background-color: red;">
  <div class="label-dot-1">Neato</div>
</div>

You can view the test case on JSFiddle here: http://jsfiddle.net/EvilClosetMonkey/jR9sV/

As you can see, the green dot aligned to the top and right of the div. How can I make it align with the vertical center instead, similar to how the text behaves?

UPDATE

I have two additional pieces of information:

  1. I do know the height of the parent div, although I forgot to mention that before.
  2. I am open to options other than using float, but the 'dot' needs to be aligned to the right side.

A revised fiddle with updated heights can be found here: http://jsfiddle.net/EvilClosetMonkey/jR9sV/2/

Answer №1

When we add position:relative to the parent element with a specified height, we can then use absolute positioning to place the pseudo element relative to it:

#container {
 position:relative;
 height:20px;
 width: 500px;
 background-color: red;
}
.label-dot-1{

}
.label-dot-1::after {
 content:"";
 position:absolute;
 right:0;
 top:5px; /* (20px - 10px)/2 */
 border-radius: 50%;
 display: inline-block;
 height: 10px;
 width: 10px;
 background-color: #66c2a5;
}

See Updated JSFiddle Example

Update

The code below aligns the pseudo element relative to its parent, even if the parent's height is unknown. It uses a combination of absolute centering techniques for vertical alignment and the css3 calc() function for horizontal alignment:

#container {
 width: 500px;
 background-color: red;
}
.label-dot-1{
 position:relative;
}
.label-dot-1::after {
 content:"";
 position:absolute;
 top:0;
 right:0;
 bottom:0;
 left:0;
 margin:auto calc(100% - 10px);
 height: 10px;
 width: 10px;
 border-radius: 50%;
 background-color: #66c2a5;
}

View JSFiddle Demo

Answer №2

easy method to include

position:absolute; top:50%;

add .label-dot-1::after and then make sure the parent is relatively positioned

http://jsfiddle.net/QzRbH/ for reference

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 an image gallery with animated effects: A step-by-step guide

I am looking to develop a creative image gallery with animated effects. I also want to include panels and hyperlinks for each image, similar to the functionality on this website: http://www.microsoft.com/en-lk/default.aspx Can anyone guide me on how to ac ...

Several dropdown menus within the same container, working independently of each other

I recently encountered an issue with a drop-down navigation bar. When I have multiple drop-downs and click on one, it opens as expected. However, if I then proceed to click on another drop-down while the first one is already open, both of them remain open ...

Having trouble locating HTML elements by their id when typing into a box using cy.get in Cypress with JavaScript

I am a novice in working with cypress and HTML, and I am currently attempting to enter an address in the specified address field. <input type="text" title="Street Address 1" name="billing[street][]" id="billing:street1" value="" class="input-text " ...

The contents of my div extend beyond the border-radius

Here is the issue I'm facing: My challenge involves having an <h1> nested inside a <div>. However, when I apply a border-radius to the <div>, the <h1> appears outside of the div. Below is the code snippet: <div class="test ...

Enhancing the appearance of list options in Autocomplete Material UI by utilizing the renderOption feature

I'm putting in a lot of effort to customize the option elements in the autocomplete list. My plan is to achieve this using the renderOptions prop, where I can create DOM elements and easily apply styles with sx or styled components. However, somethin ...

Tips for making markers act responsively

Recently, I put together a collection of rain radar images that can quickly show whether rain is on the way or not. As you resize the URL window, the two pictures positioned side by side will also shrink. For an illustration, check it out here ... Each p ...

Can the floated DIVs be prevented from breaking onto a new line if they exceed the container's width?

Take a look at this sample HTML code: <div id="container" style=""> <div id="left" style="float: left; width: 200px;">...</div> <div id="right" style="float: right; width: 100px;">...</div> </div> How can we prevent ...

Creating separation between a dropdown menu and its corresponding button

I'm dealing with a situation where I have a button that reveals a hidden droplist upon hover. The problem is that there is no space between the button and the droplist, causing interference with the functionality. My attempt to create some distance b ...

Problem with CSS hovering on images when hovering on links?

While attempting to add 2 hover images to my website, I encountered an issue where a black border appeared in the middle of the images. This was caused by the hover effects on the links. Below is the code snippet: a:hover,a:active { color:Black; o ...

What is the best way to choose a web element using Selenium web driver when it is nested inside another HTML tag

<div> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed et tristique libero. Cras vel nisl eu ante congue aliquam ut non magna.</p> <br> <p>Integer est tortor, bibendum ac lacus in, porttitor consequat e ...

I'm having trouble applying a CSS border to this particular content

Currently, I am utilizing WordPress to manage my blog/website. While I successfully added borders to the regular pages, I am facing difficulty in adding borders around boxes within the blog template. Here is the snippet of code I have implemented so far: ...

Incorporate an external website into HTML5 code

I have a dilemma with embedding one website into another. I am currently utilizing embed code for this purpose. <object data="http://mywebsite.com" width="100%" height="100%"> <embed src="http://mywebsite.com" width="100%" height="100%"> ...

Aligning text beneath a positioned span element

I have a code snippet where I am trying to center a number within a div and then center the units right below the number. The number is centered correctly, but the units are not aligning as desired. How can I achieve this layout? div.div1 { height: 20 ...

Tips for organizing and re-indexing numbered list items in an unordered HTML list

I'm working on sorting and reindexing a sample list that is currently unordered but numbered. <ul> <li>0001 - Apple</li> <li>0002 - Banana</li> <li>0003 - Mango</li> </ul> My goal is to rearran ...

How can I toggle the visibility of a div based on whether a variable is defined or not?

How can I display a specific div on my webpage only when certain variables in PHP pull out a specific result from a database? I attempted to use the code snippet below, but it's not working as expected. Can someone provide guidance on how to achieve ...

Issues with rendering HTML5 drag and drop styles are not visible on a Windows Server 2003 platform

I am currently developing a file upload tool utilizing Valum's Ajax-Uploader as the foundation. The concept is reminiscent of how attaching files works in Gmail. Users should be able to simply drag and drop a file from their desktop into the browser w ...

Prevent content overlap in Tumblr's sidebars by using CSS positioning techniques

I'm facing an issue with my blog on Tumblr where the sidebar keeps overlapping the main content. I have modified a premade template and am trying to position the sidebar in the top right corner using absolute or fixed positioning: #sidebar{ position: ...

How to style 1 out of every 3 div elements using absolute positioning in CSS

Imagine a setup where there is a banner at the top, with 3 divs placed side by side underneath it. The interesting part is that when you scroll down, only the center and right div should move along. #banner { background:blue; color:white; position ...

CSS for when the mouse hovers over an element

Two php files and a js file are involved in this scenario. The issue at hand is that when the div is clicked, it navigates to a new page with an appended # to the URL, causing it to scroll to the specified comment ID. However, instead of scrolling to the c ...

I am trying to collapse the table header but I am having trouble doing so

@import url(https://fonts.googleapis.com/css?family=Roboto:400,500,700,300,100); @import url(https://fonts.googleapis.com/css?family=Roboto:400,500,700,300,100); body { bac ...