The ::before pseudo-element is not functioning properly with the <i> tag

I'm attempting to add a ::before pseudo selector to an i element styled with fontawesome. However, when I target the icon through its parent, the ::before css overlaps with the Icon (or makes the icon disappear).

This issue arises only if there is a class selector within its path: .foo a i::before {}; doesn't work;

a i::before {} works as expected.

Non-working CSS:

.foo i {
  position: relative;
  color: red;
  font-size: 1em;
  z-index: 5;
}
.foo i::before {
  content: '';
  position: absolute;
  height: 2em;
  aspect-ratio: 1 / 1;
  background-color: blue;
  z-index: -5;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" rel="stylesheet"/>
<a href="" class="foo">
  <i class="fa-solid fa-chevron-right"></i>
</a>

Even adjusting the z-index does not resolve the issue.

The solution seems to be targeting the i element directly for it to work correctly.

Here's a functional example of the desired outcome:

i {
  position: relative;
  color: red;
  font-size: 1em;
  z-index: 5;
}
i::before {
  content: '';
  position: absolute;
  height: 2em;
  aspect-ratio: 1 / 1;
  background-color: blue;
  z-index: -5;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" rel="stylesheet"/>
<a href="" class="foo">
  <i class="fa-solid fa-chevron-right"></i>
</a>

Answer №1

This problem stems from a specificity issue.

The :before declaration in your code is overriding the one provided by FontAwesome for creating the icon. You should avoid restating the :before and instead focus on adding additional styles.

The use of position and z-index is unnecessary in this context.

.foo i {
  position: relative;
  color: red;
  font-size: 1em;
}

.foo i::before {
  display: block;
  height: 2em;
  aspect-ratio: 1 / 1;
  background-color: blue;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" rel="stylesheet" />
<a href="" class="foo">
  <i class="fa-solid fa-chevron-right"></i>
</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

Adjusting the width of a Material UI drawer: Tips and tricks

Currently, I am working on implementing the Material UI drawer and anchoring it to the top of my page. It is functioning correctly, however, I am encountering an issue where I am unable to adjust the width of the drawer. Despite trying to update the width ...

How can we avoid repeated evaluations by using memoization in a function?

Currently, I am working on a jQuery function designed to switch HTML elements based on specific viewports. The concept is quite simple: <div data-swap-for="#element" data-swap-on="phone"></div> This code snippet will insert the element with t ...

Restricting responsiveness in Bootstrap 4

How can I limit the responsiveness of my bootstrap project? For example, let's consider the following element: <div class="col-sm-12 col-md-6 col-lg-4">column</div> I want to prevent Bootstrap from resizing the website on .col-sm (websi ...

Is it possible to implement the SCSS CSS preprocessor in Angular 1.6.6, even though it is already utilizing LESS as the current CSS preprocessor?

Is it necessary to compile SCSS and Stylus files into CSS before importing them into my Angular version 1 app? Are there any other alternatives available? Context: I have two applications, one using Angular 4 with SCSS and the other using Angular 1 with L ...

In HTML, employing the float:left property and using relative positioning are essential

I've encountered a strange issue when using a div block with float:left and another with relative position. The second block is behaving unexpectedly, as the text is extending beyond the box and its background color. Can anyone help me understand what ...

Height not being generated by Div element

I am encountering an issue with a div element that contains an h2 and three nested div elements. Despite not explicitly setting a height for the container div, it is not expanding to accommodate the varying heights of the inner divs. This problem is causin ...

Utilizing AngularJS ng-repeat and Bootstrap to showcase database items in columns

I am currently customizing a template for my MEAN app and I am trying to showcase some information retrieved from the database in three columns. However, I am facing an issue where the ng-repeat function is only displaying the data in a single column. Bel ...

Learn how to display every ASCII or EBCDIC character once radio buttons have been selected

I'm currently working on a simple website that requires the user to input a minimum of 256 characters, choose between ASCII or EBCDIC for conversion, and then click the submit button (Run) to display the final converted result on the page. However, I& ...

I find myself unable to write any code using my Visual Studio Code

I'm facing an issue where I can't write any code on my Visual Studio Code. Despite following all the recommendations on the official website, I haven't been able to resolve the problem. More information here Here are the changes I have tri ...

adjusting iframe height dynamically without the ability to use JavaScript or stylesheets

My current situation is quite challenging - I must ensure that the height of an iframe adjusts to its content without the ability to access stylesheets or head on the server. Additionally, I am restricted from using JavaScript. ...

Understanding CSS classes in ReactJS Material UI componentsJust wanted to clarify some things

When it comes to styling Material UI components, the recommended approach is to utilize their useStyles function. Here's an example: const useStyles = makeStyles(theme => ({ root: { marginTop: '15px', display: 'f ...

Perform calculations using the provided input values or null values in Javascript/Jquery

I'm a beginner in Javascript and JQuery. Take a look at my code that calculates an average (A) value based on three input values. https://i.sstatic.net/teGkc.png Check out the JQuery snippet below: $(".q-value, .e-value, .t-value").click(f ...

Application that checks the frontend organization of displayed webpages

Our project adheres to a microfrontend architecture approach, where HTML pages are created by merging multiple web-components. This practice combines several HTML files into a single file which may pose the risk of generating invalid HTML code. To combine ...

Tips for integrating WMV files into an HTML5 document

I've been attempting to integrate some s3 WMV file URLs into my HTML page, but the following code doesn't seem to be functioning as expected. <object classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95" standby="Loading Microsoft® Windows® ...

Displaying images using <img src='path' /> in React is causing some troubles

I'm currently working on a React template for the front page and I need to display various local dummy images. Within my <Main /> component, I import other components that pass the image path as a parameter, like this: <Container type ...

Leveraging python capabilities within html documents

English is not my first language, so please excuse any spelling errors. I am working on combining HTML and Python to develop a graphical user interface (GUI) that can communicate with a bus system (specifically KNX bus). Currently, I have a Raspberry Pi ...

Is there a way to retrieve all "a" tags with an "href" attribute that contains the term "youtube"?

My goal is to capture all a tags that have the href attribute containing the word youtube. This task requires the use of jquery. ...

What is the process for incorporating an additional input in HTML as you write?

I am looking to create a form with 4 input boxes similar to the layout below: <input type="text" name="txtName" value="Text 1" id="txt" /> <input type="text" name="txtName2" value="Text 2" id="txt" /> <input type="text" name="txtName3" valu ...

Retrieve the outcome from the PHP webpage

I have recently developed a contact page, but I am experiencing an issue with the submit button's functionality. When I click on the submit button, the form is supposed to send the information to a PHP file on the server-side. However, the button does ...

Is there a way to display the value of an object within an HTML file?

I have a function in AngularJS that retrieves values from a checkbox set - determining which checkboxes were checked (true or false) and stores this information in an object. It then creates a variable to hold the value of the first element in the object. ...