How can I maintain code reusability while customizing buttons with pseudo-elements to display unique content on each button?

I have a ul in my HTML with lis that contain navigation options such as home, contact, about, etc. I am trying to figure out the best way to add a unique effect to each button since they all have different content and require different icons. Essentially, I want to create a hover effect that changes the position of the text and icon for each button. Is there a way to do this in a more efficient and DRY (Don't Repeat Yourself) manner?

For example, instead of using

  • HOME
  • in the HTML, I am now using a before pseudo-element that contains "HOME" which, on hover, moves up -20px (with overflow hidden). Additionally, there is an after pseudo-element that displays the corresponding icon for the button (such as a home icon or book icon) positioned below the button, which moves up on hover to the same position as the before pseudo-element.

    If you want to see the code in action, here is a codepen link:

    https://codepen.io/anon/pen/MqzVQe

    .menu a{
      display:block;
      color:white;
      background-color: #0099ff;
      padding: 10px;
      text-decoration:none;
      text-align: center;
      height: 40px;
      border-radius: 5px;
      text-transform: uppercase;
      font-family: 'Raleway', sans-serif;
      font-weight: lighter;
      letter-spacing: 1;
      position: relative;
      overflow: hidden;
    }
    .menu a::before{
      content: '\f015';
      font-family: 'Font Awesome 5 Free';
      font-weight: 900;
      top:-30px;
      position: absolute;
      top:50%;
      left:50%;
      transform:translate(-50%, -50%);
      color:black;
      transition: all .2s ease-in-out;
    }
    
    .menu a:hover::before{
      top: -20px;
    }
    
    .menu a::after{
      content: 'Home';
      position:absolute;
      display:block;
      top:calc(100% + 20px);
      left:50%;
      transform: translate(-50%, -50%);
      transition: all .2s ease-in-out;
    }
    
    .menu a:hover::after{
      top:50%;
    }
    

    Answer №1

    both before and after have an interesting feature that allows content to utilize data attribute values.

    for your icon css, it will appear like this:

    content: attr(data-icon)
    

    you can then add the icon in an html data attribute.

    <li><a href="../nextpage/next.html" data-icon="\f015"></a></li>
    

    Check out the demonstration here: https://codepen.io/anon/pen/NLEYZG

    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

    Fade out a component in Material UI/React by setting a timeout in the parent's useEffect hook for when it unmounts

    Incorporating a fade out transition into my child component, <Welcome />, using Material UI's Fade component is my current goal. This transition should be triggered by two specific conditions: The timeout set in the useEffect function expires. ...

    When the browser is resized, the fadeIn() function restarts from the

    My plan was to create a dynamic effect where a browser-sized div would rotate through 3 different backgrounds using jQuery's fading effect. However, I encountered an issue - whenever I resize the browser window, the effect restarts from the beginning ...

    The flex item does not appear as intended when styled with justify-content: space-between

    I'm trying to create a menu bar with sub-menus in CSS using Flexbox. I want to have an "Account" or "Profile" link on the right side of the menu bar on the same line, but for some reason, it's displaying under the main menu. I believe there' ...

    Instructions on integrating iframe into Slides on material-auto-rotating-carousel

    Looking to swap out the carousel slide image with a loom video. Any ideas on how to replace the media img with an iframe in material-auto-rotating-carousel? const AutoRotatingCarouselModal = ({ handleOpen, setHandleOpen, isMobile }) => { return ( ...

    Graphic selectors: a new take on radio buttons

    I've been attempting to make this work, but it's not functioning correctly. Below is the CSS code: .input_hidden { position: absolute; left: -9999px; } .selected { background-color: #000000; } #carte label { display: inline-bl ...

    Use jQuery to permit only numeric characters and the symbol "-" to be entered into a textbox

    I have been working on a JQuery script that only allows users to input numbers in a text field. However, I am now trying to modify the code to also allow users to enter "-" (hyphen), but so far it's not working as expected. If anyone can provide assis ...

    clicking on links to different sections of the page does not automatically bring you to the top of

    I am working on a design similar to this: http://jsfiddle.net/MTWu5/ The layout consists of a centered page with a sticky header. The header contains menu links that lead to anchors within the page. My issue arises when clicking on these links - I would l ...

    Is it possible to have more than one button for each item on my Shopping List App?

    I am developing a shopping list app that allows users to add items to a list. Each item added triggers the display of a button next to it, enabling users to remove the item from the list when needed. However, a problem arises as more items are added – i ...

    Maintain font kerning for span elements that are not displayed inline

    I am looking to add some transform effects to individual letters in a word. To achieve this, I have enclosed each letter within a span element. However, when setting the display of these spans to inline-block, the font kerning gets disrupted. I have exper ...

    Angular's parent div height can be adjusted using CSS transitions

    I have implemented a CSS transition to create a sliding effect in my pagination. However, I am facing an issue where the height of the containing div changes even when I set the position of the transitioned elements. Here is a snippet of my CSS: .slide { ...

    Several DIV elements lined up side by side

    I've been working on a program that retrieves data from a database, lists some of the data when a button is clicked within the same div, and then creates new divs with buttons that have onclick events until it reaches a certain maximum value. To keep ...

    Getting rid of all CSS properties currently set for the Mobile view

    Utilizing third-party library components in my project. The library includes components that come with predefined styles for mobile devices. I am seeking to completely UPDATE these properties within my own code. When I say "update," I mean removing all th ...

    Exponential calculator in Javascript

    I'm working on a basic calculator project using html5, css3 and javascript. However, I've run into an issue with the exponent button not functioning properly. Here's my code snippet: <html> <head> <meta charset = "utf-8" ...

    As I adjust the size of my browser, my website dynamically resizes to match

    Recently, I created a website but encountered a bug where the website resizes when I resize my browser. This issue has been observed in Firefox (10.0.2), Chrome (16.0.912.77), and Opera (11.60) browsers. Interestingly, when testing other websites such as ...

    Video with dynamic sizing doesn't adapt properly

    I successfully achieved my main goal, which was to have a background video at the top of my page. However, I am facing an issue with making the video responsive. Currently, as I decrease the screen size, the video shrinks in both width and height rather th ...

    Printing Multiple Pages Using JavaScript and Cascading Style Sheets

    Encountering difficulties displaying page numbers when printing multiple multipage reports Here is the provided HTML Format : <style type="text/css> body { counter-reset: report 1 page 0; } td.footer:after { counter-increment: page; content ...

    Generating HTML tables with charts using FireFox

    I am encountering an issue: My table contains charts and tables that are displayed correctly in browsers. However, when I attempt to print it (as a PDF) in Mozilla Firefox, the third speedometer gets cut off, showing only 2.5 speedometers. Using the "s ...

    CSS background-image property should not alter the size of the first image

    I am attempting to ensure that two images have the same dimensions without altering the size of the first image. My current code looks like this: background-image: url("1.png"), url("2.jpg"); background-position: 3% 10%, 83% 27%; background-size: auto, ...

    Why isn't the float left property working as expected?

    Why won't my image float to the left? I'm using a class called align-left with float: left but it's not working. You can see the live version at - (check out the review grid halfway down under the heading 'High customer satisfaction r ...

    Tips for modifying hover effects using jQuerystylesheet changes

    I attempted running the following code snippet var buttonElement = $('<button>Button</button>'); $('body').append(buttonElement); buttonElement.hover().css('background-color', 'red'); Unfortunately, the ...