Tips for properly using the :after pseudo-element with styled components in React.js

Recently, I came across this intriguing underline animation:

<ul>
  <li><a href="#">About</a></li>
  <li><a href="#">Portfolio</a></li>
  <li><a href="#">Blog</a></li>
  <li><a href="#">Contact</a></li>
</ul>

body,html {
  margin: 0;
  font: bold 14px/1.4 'Open Sans', arial, sans-serif;
  background: #000;
}
ul { 
  margin: 150px auto 0; 
  padding: 0; 
  list-style: none; 
  display: table;
  width: 600px;
  text-align: center;
}
li { 
  display: table-cell; 
  position: relative; 
  padding: 15px 0;
}
a {
  color: #fff;
  text-transform: uppercase;
  text-decoration: none;
  letter-spacing: 0.15em;
  
  display: inline-block;
  padding: 15px 20px;
  position: relative;
}
a:after {    
  background: none repeat scroll 0 0 transparent;
  bottom: 0;
  content: "";
  display: block;
  height: 2px;
  left: 50%;
  position: absolute;
  background: #fff;
  transition: width 0.3s ease 0s, left 0.3s ease 0s;
  width: 0;
}
a:hover:after { 
  width: 100%; 
  left: 0; 
}
@media screen and (max-height: 300px) {
    ul {
        margin-top: 40px;
    }
}

My next step is to integrate this code into my NavItem component. The animation works perfectly, but it seems to be applied to the entire Navbar instead of just the NavItem component. You can see the CSS for NavItem and the app image here.

As a new member, I'm still learning the best practices for showcasing a website when it's on my localhost. Also, please forgive any mistakes in my English. :)

Answer №1

To enhance the styling of your component, I recommend utilizing css.modules and assigning a className. Here's a simple guide to get you started:

  • Begin by creating a css module in the same directory as your component: name.module.css. Define the styles for your component here.
  • Next, import this module into your component:
import classes from './name.module.css';

(Feel free to choose a custom name for the constant)

  • Apply the className within the component tag:
<NavItem className={classes.nameofclass} />

Make sure to check if the styles from the class are being applied successfully.

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

MUI Object Selection Error (MUI: Invalid value provided)

Looking to implement a MUI Selection component for a list of objects with properties like name and id. The selection list should display both the name and id of each object, but only the name of the selected value. My main aim is to be able to retrieve th ...

Mapping through an undefined array in React causing an error: "map of undefined"

To display an image, title, and description in the browser, the data is stored in an array in a file named customData.js. Below is an example of the data structure: var DATA = [ { name: 'John Smith', imgURL: 'http://whi ...

Bootstrap: Arrange multiple images horizontally within a row

I am trying to design a list item for a game that includes a background, an iconholder with an icon, a title, description, and up to three resources. I have been experimenting with Bootstrap and attempted using a container-fluid with an inner row but the i ...

steps to remove the border of the select box in MUI component

i am struggling to disable the border on the select box and change the color of the label text when focused or blurred i have attempted it but have been unsuccessful i am not very skilled at Mui component customization this is the image that i desire ht ...

Tips for iterating through a collection of images

I am interested in creating a loop to iterate over the images folder and display them on the screen, similar to the code snippet below: <section id="photos"> <a target="_blank" href="images/1.jpg"> <img src="images/1.jpg ...

Simultaneously operating the front and backend in Electron

I'm looking to run my express backend and reactjs frontend at the same time in an electron app. I'm new to both react and electron, so the solution might be straightforward. Here's the question: What are some possible methods for achieving ...

Challenges with jQuery's 'this' selector in conjunction with if/else conditions

I am currently working on a project that involves allowing users to modify or move an image in the browser by clicking on buttons located on the side of the page. However, I am facing issues with the script in my if/else statement. Here is a snippet of wha ...

CSS Grid with dynamically changing number of rows set to "auto", while ensuring one row always has a fixed size of "1fr"

Currently, I am experimenting with a CSS grid-based frontend and have encountered a specific requirement that keeps repeating itself in various parts of the frontend: A grid with a dynamic number of rows. Each row should be of variable size (auto). The f ...

JavaScript-enhanced HTML form validation

I encountered a small glitch while working on simple form validation with JavaScript. I tried to catch the issue but have been unable to do so. Here is the code snippet, where the problem lies in the fact that the select list does not get validated and I ...

Update the row striping pattern once an element has been removed

I've been working on creating a todo-list and implemented row striping. Everything was going smoothly until I realized that when I delete a task, the row striping does not update as intended. Instead of changing to white, white, beige after a deletion ...

The following page shrouded in mystery is experiencing technical issues

Encountering some issues on the live server with this code. It's functioning perfectly fine on my local machine, but once I try to deploy it on Netlify, I'm running into this error message: ⨯ useSearchParams() should be wrapped in a suspense bo ...

The Unforeseen Consequences of Bootstrap Navbar CSS

I'm still learning CSS, so bear with me if I don't explain this issue correctly. Whenever I click on a navigation menu tab in Chrome or Safari, a mysterious blue rectangle appears. It doesn't show up in FireFox though. I'm not sure what ...

Pressing the reset button on a basic calculator

I am experiencing an issue with my calculator. It works fine initially, but after using the reset button, it stops functioning properly. Currently, I only have the multiplication set up as I debug this problem. I am new to JavaScript and would appreciate a ...

What is the best way to align this horizontal list in the middle of a fixed div?

I have a UL element that contains 4 icons within a div using absolute positioning to align it to the bottom of the screen. This div is located within a fixed sidebar. To create a horizontal list, I applied display: inline to the list items. You can see t ...

Bootstrap 5 - Dropdown menu - Links unresponsive to user interaction

Often, I want to incorporate a Bootstrap 5 dropdown menu into my project, but I encountered an unexpected issue. The menu opens correctly, but when I hover over the menu items, the hover effect is not triggered, and clicking on a link does not work. It ap ...

Troubleshooting Z-Index problem with Material UI Drawer component on iOS

I'm currently working on a project that involves the use of the Material UI Drawer component. However, I've encountered a specific issue when using an iPad, which presents two main problems: - The overlay does not appear above the navigation bar ...

What to do when encountering a problem with HTML, CSS, and JS while loading a webpage from an SD card to a WebView

I am facing an issue while loading an HTML file from the SD card to a webview. The problem is that the CSS, images, and videos are not loading properly in the webview. Compatible with Android 4.4 KitKat and Chrome version Not compatible with versions belo ...

What is the trick to getting the <label> and <input> elements to show up side by side in an HTML form?

I am designing a registration form for a new website. My goal is to have each label and its corresponding input element displayed on the same line. Here's the CSS code I'm using: #registration-form { background-color: #FFF; height: 600px; ...

Adjusting the width of images in Bootstrap is proving to be a challenge

Check out this website. There is an advertisement image below the navigation bar. I am having trouble adjusting its width. I want the image to expand and fill the empty white space. I've attempted using relative and absolute positioning, but the width ...

JavaScript is utilized to update HTML content through an Ajax request, but unfortunately, the styling is

Apologies for the unconventional title, I am currently in the process of creating a website. When the page is initially loaded, I call upon two scripts within the 'head' tag to create an attractive dynamic button effect. <script src="https:// ...