When you hover over them, Material UI icons shrink in size due to the Border

I've been working on a React application that includes Material UI icons in the header. My goal is to add a border at the bottom of each icon when hovered over, but currently, the borders are too close to the icons.

Another problem I'm facing is that the icons shrink in size when hovered over. I've spent hours trying different solutions without success.

The icons should have a font size of 1.6rem and a border width of 4px. How can I resolve these issues?

To see the code in action, visit this CodeSandbox link.

Check out the code for the Header Component below:

import React from "react";
import "./Header.css";
import PowerSettingsNewIcon from "@mui/icons-material/PowerSettingsNew";
import SettingsIcon from "@mui/icons-material/Settings";

const Header = () => {
  return (
    <header className="header">
      <div className="header__leftContainer">
        <h1>Logo</h1>
      </div>

      <div className="header__rightContainer">
        <SettingsIcon className="header__rightContainer__icons" />
        <PowerSettingsNewIcon className="header__rightContainer__icons" />
      </div>
    </header>
  );
};

export default Header;

Below is the corresponding CSS:

.header {
  width: 100%;
  background: yellow;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header__leftContainer,
.header__rightContainer {
  padding: 15px 40px;
}

.header__leftContainer h1 {
  color: red;
}

.header__rightContainer svg {
  font-size: 1.6rem;
  margin: 0 15px;
  cursor: pointer;
}

.header__rightContainer svg:last-child {
  margin: 0 !important;
}

.header__rightContainer svg:hover {
  border-bottom: 4px solid blue;
}

Answer №1

Rather than adding padding to the left and right containers, consider setting a fixed height for your header. Give this a try:

.header {
  width: 100%;
  background: yellow;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 4rem;
}

.header__leftContainer,
.header__rightContainer {
  height: 100%;
  display: flex;
}

.header__leftContainer h1 {
  color: red;
}

.header__rightContainer svg {
  font-size: 1.6rem;
  margin: 0 15px;
  cursor: pointer;
  height: 100%;
  border-bottom: 4px solid transparent;
}

.header__rightContainer svg:last-child {
  
}

.header__rightContainer svg:hover {
  border-bottom: 4px solid blue;
}

Answer №2

Experimented with the following CSS:

.header {
  width: 100%;
  background: yellow;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 40px;
}
.header__leftContainer h1 {
  color: red;
}

.header__rightContainer {
  display: flex;
  flex-direction: row;
}

.header__rightContainer__icons {
  padding: 8px 24px;
  border-bottom: 4px solid yellow;
}

.header__rightContainer__icons:hover {
  border-bottom: 4px solid blue;
}

and

import React from "react";
import "./Header.css";
import PowerSettingsNewIcon from "@mui/icons-material/PowerSettingsNew";
import SettingsIcon from "@mui/icons-material/Settings";

const Header = () => {
  return (
    <header className="header">
      <div className="header__leftContainer">
        <h1>Logo</h1>
      </div>

      <div className="header__rightContainer">
        <div className="header__rightContainer__icons">
          <SettingsIcon />
        </div>
        <div className="header__rightContainer__icons">
          <PowerSettingsNewIcon />
        </div>
      </div>
    </header>
  );
};

export default Header;

There was a problem with the icons shrinking on hover.

I have resolved this issue and reorganized the code for better readability. Feel free to adjust the border size by modifying the horizontal padding in .header__rightContainer__icons

Answer №3

When hovering over your SVG, a border bottom is added, resulting in a color change. To only change the color of your border bottom, include the following code:

.header__rightContainer svg {
  font-size: 1.6rem;
  margin: 0 15px;
  cursor: pointer;
  border-bottom: 4px solid rgba(0,0,0,0);
}

However, be sure to resize your SVG appropriately according to your preferences.

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

"Exploring the versatility of Tailwind css: A guide to implementing multiple themes

I know how to customize dark and light themes in tailwind, but I'm curious about extending this functionality to include additional themes such as pink, blue, red, etc. Is there a way to achieve this with regular tailwind CSS? <p className="t ...

Save the output of Html.Raw() into a JavaScript variable when using ASP.NET MVC 3

One issue I'm encountering in my ASP.NET project involves retrieving a string of HTML from the database and assigning it to a client-side variable. Currently, I am using the following code: var x = '@Html.Raw(myModel.FishValue)' This work ...

Shutting down the jQuery pop-up

I am struggling with trying to display this code as a popup on my website. Here is the code I have: <div id="myDialog" title="myTitle"> <div class="table_cell"> <div class="message"></div> </div> <div class="tabl ...

What is the process for activating focus on an input element within a mat-select component?

How can I activate the cursor on the HTML input element (search field) using a keyboard inside mat-select? It functions properly when using a mouse, but in order to meet WCAG requirements, it needs to be fully functional with keyboard navigation. Click h ...

Challenge with sending CSV file as attachment on response in Express not resolving

Hey there, I've been facing an issue with sending a response from a post request that includes downloading an attachment. Although the response is correct, I don't see any download or save-as pop-up. I have verified that the headers are set corre ...

Could Express be considered the most reliable and efficient application framework for NodeJS?

While I have some experience with Express, I haven't explored many other Node-based web application frameworks. It's clear that Express is lightweight and versatile, but my usage has been limited to small experimental projects rather than large-s ...

Combining useEffect with getServerSideProps in Next.js for optimal data fetching

While working on my project with next.js, I encountered a problem where the getServerSideProps function is being called 6 times when the page is loaded. Here is the code snippet from the pages section: const NewFindstay: React.FC<INewFindstayProps> ...

When utilizing Next.js with TypeScript, you may encounter an error message stating that the property 'className' is not found on the type 'IntrinsicAttributes & IntrinsicClassAttributes'

I recently put together a basic nextjs app using typescript. Here's the link to the example: https://github.com/zeit/next.js/tree/master/examples/with-typescript However, I encountered an issue where I am unable to add the className attribute to any ...

Is there a way to create an image gallery layout similar to Pinterest using CSS?

I am currently developing a dynamic PHP gallery that will feature thumbnails with the same width but varying heights. These thumbnails will be arranged from left to right, so I would prefer not to use a traditional five-column layout. I suspect that achiev ...

Having trouble with overriding an @media query for material-ui react components?

I've been trying to modify a @media CSS rule on a Material UI component, similar to the discussions on How to over-ride an @media css for a material-ui react component and Override components like MuiTab that use media queries. However, I have not bee ...

What strategies can I implement to ensure my modal dialog box remains responsive? Adjusting the window size causes the modal box to malfunction and lose its structure

Whenever I adjust the size of the browser window, the elements inside the modal box become misaligned. HTML <div class='modal'> <div class='modal-content'> </div> </div> Below is the CSS for the modal ...

Issue with npm configuration permissions

I am encountering permission issues when using the npm config command. It appears that there is an attempt to alter the owner of my ~/.npmrc file without authorization. Upon executing npm config set color false, I encounter the following error: npm ERR! E ...

Creating a star-based rating feature through directive implementation

Can anyone help me figure out why my static star rating system using angularjs/ionic is not showing up on the screen? I've been struggling with it and would appreciate some guidance. service.html <ion-list> <ion-item ng-repeat="busine ...

Need help debugging your React code in VS Code? Set an error breakpoint that may not be bound yet

After creating a React website and running it using Gulp, I encountered an issue when trying to debug it in VS Code. Whenever I set a breakpoint, I receive a warning indicating that the breakpoint is unverified and not yet bound. What could be causing this ...

Tailored design - Personalize interlocking elements

I am currently working on a custom theme and I am trying to adjust the font size of Menu items. In order to achieve this, I have identified the following elements in the tree: ul (MuiMenu-list) MuiListItem-root MuiListItemText-root If I want to modify th ...

What is the best way to find all documents based on a particular field in MongoDB?

Within my MongoDB database, I have a collection of bets structured like this: { id: String, user_id: String, type: String, events: [{ eventID: String, sport: String, evento: String, ...

Managing user input in Node.js

Users are required to input a URL in the form (e.g: "") and I need to be able to access and read the content from that URL. I am uncertain about my current method. What should I enter in the URL field below? var options = { url: '....', ...

"Customizing a footer to resemble a bootstrap nav-bar: A step-by-step guide

My situation involves the need to have a footer that functions similarly to a navbar. I want to be able to hide the list items of a ul list by clicking on a custom button, similar to the functionality of the bootstrap hamburger menu. Is it possible to use ...

Toggle the visibility of an element using a checkbox in JavaScript

In my scenario, there are 8 checkboxes where only one is checked by default. If you click on the unchecked checkboxes twice, the table linked to them will hide. Ideally, I want the unchecked checkboxes to be hidden by default and the checked one to be sh ...

Tips for customizing MUI button to maintain functioning color prop

I've been experimenting with customizing the MUI Button component and while my changes are consistent, I'm facing an issue where the color prop is no longer able to change the color of the Button. const theme = useTheme( components: { ...