Can child elements be centered using their pseudo-elements using CSS alone?

Consider the code snippet below:

<div class="example-container">
  <p class="example">a</p>
  <p class="example">bbb</p>
  <p class="example">cc</p>
</div>

and the corresponding css:

.example-container {
  display: grid;
  /* adapted grid-template-column for mobile view */
  grid-template-column: repeat(1, 1fr);
  justify-items: center;
}
.example-container .example {
  /* some styles */
}
.example-container .example::before {
  content: "symbol";
  /* some style for content */
  display: inline-block;
  width: 1.5rem;
  margin-left: -1rem;
}

Although justify-content centers the child element, it does not align the ::before pseudo element properly. To achieve this, I tried the following css rules:

.example-container {
  width: max-content;
  margin: auto;
}
.example-container .example {
  min-width: 100%;
}

This only centers the child element without its pseudo element (similar to how margin: auto works). Even making the parent of .example-container a flex container did not align both elements in the center.

I believe achieving this alignment can be done using CSS tricks only, as some users may have JavaScript disabled or restricted on certain websites. One possible solution could involve calculating the widths of the pseudo element and child element with JavaScript, but I prefer a pure CSS approach.

-----Update----- Here is a visual representation of the desired layout:

+--------------------------+
|          O   a           |
|          O  bbb          |
|          O   cc          |
+--------------------------+
# I use O to mimic a symbol.

Answer №1

Simply remove width: 1.5rem; and margin-left: -1rem; from the styling of

.sample-container .sample::before
. Instead, utilize display:inline-flex and position it using
position:relative; left:50%; transform: translateX(-50%);

 .sample-container {
     display: inline-flex;
     flex-direction: column;
     justify-content: center;
     position: relative;
     left: 50%;
     transform: translateX(-50%);
}
.sample-container .sample {
  /* some styles */
}
.sample-container .sample::before {
  content: "symbol";
  /* some style for content */
  display: inline-block;
  margin-right: 1rem;
}
<div class="sample-container">
  <p class="sample">a</p>
  <p class="sample">bbb</p>
  <p class="sample">cc</p>
</div>

Answer №2

This content appears properly aligned and centered in my observation. Perhaps enclosing everything within a span or div that is centered could enhance the presentation?

.sample-container {
  display: grid;
  /* adjusted grid-template-column for better mobile view */
  grid-template-column: repeat(1, 1fr);
  justify-items: center;
}

.sample-container .sample {
  /* add your styles here */
}

.sample-container .sample::before {
  content: "symbol";
  /* customize this content's style as needed */
  display: inline-block;
  margin-left: -1rem;
  color: red;
}

.all {
display:inline-block;
border: 1px solid blue;

}
<div class="sample-container">
  <div class="all">
    <p class="sample">a</p>
    <p class="sample">bbb</p>
    <p class="sample">cc</p>
  </div>
</div>

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

I have a specific resolution in mind where I want to run jQuery code during window scrolling

I have a jQuery code that I want to run only when the resolution is greater than 950px. If not, I risk losing the responsive logo. My goal is to create a navigation bar similar to the one on the Lenovo homepage. $(document).ready(function(){ //left ...

Controlling the Flow of Events in JavaScript Documents

Explore the integration of two interconnected Javascript files and delve into managing event propagation between them effectively. 1st js file var red = [0, 100, 63]; var orange = [40, 100, 60]; var green = [75, 100, 40]; var blue = [196, 77, 55]; var ...

Utilizing PHP & AJAX to dynamically update JSON files

In the process of creating a game that requires registration for use, I am attempting to add the username and password inputted into a form to my JSON file. The current structure of the JSON file is as follows: { "LogIns":[ { "Username":"mike ...

Duplicate text content from a mirrored textarea and save to clipboard

I came across some code snippets here that are perfect for a tool I'm currently developing. The codes help in copying the value of the previous textarea to the clipboard, but it doesn't work as expected when dealing with cloned textareas. Any sug ...

PHP HTML failing to recognize "mandatory" attributes

I'm facing an issue with adding validation for an empty field. When the field is left empty, the form should not be submitted. However, the "required" attributes seem to be ineffective in achieving this. Birthdate: <select name="month" r ...

Unable to utilize external JavaScript files in Angular 8

I've been working on integrating an HTML template into my Angular project and for the most part, everything is going smoothly. However, I've encountered an issue where my JS plugins are not loading properly. I have double-checked the file paths i ...

Equal-height rows and columns using Flexbox

I've been working on a row layout with two columns, each set to 50% width. The left column contains an image, while the right column displays text. Here is my HTML: .row{ display:flex; ...

The functionality of HTML5 Camera is experiencing issues while being used with Tomcat7

My Angular2 project includes HTML5 camera access functionality. I launch the project using Angular CLI (ng serve), which starts the web container for testing purposes. When trying to access the camera, the browser prompts me for permission. Once granted, e ...

Using multiple <img> tags with identical src attributes

Imagine having a large number of <img> tags all with the same src value, like this: <img src="https://something.com/images/myimg.png" alt="" /> <img src="https://something.com/images/myimg.png" alt="" /> <img src="https://something.co ...

CSS alert problem encountered

I have an HTML and CSS notification template that I would like to incorporate into my PHP code. You can view the template here. HTML <div class="alert-box error"><span>error: </span>Write your error message here.</div> &l ...

Creating a stunning effect with radial-gradient on an image element

I am looking to achieve a fading effect on an element using a radial gradient. While it works when I set an image as a background image, it does not work on the element directly. Is there a way I can accomplish this? My goal is to have the image fade fro ...

The intricate dance between JAVA and HTML

Can Java be compatible with HTML and JS? Is it possible for them to cooperate without using JSP? In order to connect the WEKA function, we utilized Java code through a JAR file, but now we also require HTML and JS links for visualization. Is there an alte ...

Guide on including a dropdown-item within the navbar

I've implemented a header with a navbar inside. I want to create a dropdown menu element, but for some reason, my item is not appearing. On this link, it looks simple: https://getbootstrap.com/docs/4.0/components/navs/ Pills with dropdowns <l ...

The Reason Behind Angular Error when Using CSS Custom Tags

I have the following SCSS code: main{ width: 100%; height: 840px; /*background: red;*/ margin: 10px auto; position: relative; padding: 5px 0; } section{ width: 98%; height: 600px; margin: auto; display: flex; ...

How can I synchronize two webkit-animations at the same speed but with one covering a greater distance?

[Update: The solution involved creating two containers, with the second animation container configured to have left: 100%.] I designed a simple animation that moves a large gif image across the page horizontally, where the width of the gif is 1536px. Sin ...

Is there a way to insert animated images in front of every navigation menu item?

I have successfully implemented the HTML and CSS for my navigation menu items, but now I'm facing an issue trying to incorporate unique images right before each menu item in WordPress. Due to the hover animation on the menu names, I couldn't get ...

Error encountered using Meteor 1.3 autoform/quickform integration

Having recently transitioned to using Meteor 1.3, I've encountered a few challenges related to debugging due to missing imports or exports in tutorials. This particular issue seems to be in the same vein. I wanted to incorporate the autoform package ...

The function 'myfunc' was called within a render, however, it is not defined in the instance

I keep seeing this error message Property 'myfunc' was accessed during render but is not defined on instance, and I'm not sure why. Below are my HTML and JavaScript code snippets. const ListRenderingApp = { data() { return { todo ...

Utilize orientation detection technology to ascertain the exact placement of an image

On my HTML page, I have a centered header (h1) with a title that displays in portrait mode above an image. When switching to landscape orientation, I want the image to move to the left side of the title. I've experimented with using <br> along ...

Stingray Riverbed SteelApp, showcasing an image on a maintenance landing page

We have implemented a feature on riverbed stingray that displays a maintenance page whenever the system administrator needs to update the application. In order to achieve this, we designed a custom HTML page with a logo image and uploaded both the .html an ...