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

Develop a line using botbuilder

Good day. I'm currently working on a vue js component that will function as a botbuilder. The main goal is to manipulate the cards displayed on the screen and establish links between them to define the flow of actions. Here's an image for referen ...

The header location functionality is not functioning properly on the live server

I have a single registration and payment page where the program flow goes from registration to confirmation and then to payment. Upon validation and calculation on the registration page, it should redirect to the confirmation page. This is my code: < ...

Could you please review my PHP code? I encountered a syntax error: unexpected '}' character. Thank you

I encountered an issue in the final line <?php } ?> as it reports a syntax error, unexpected '}' <?php if (isset($_SESSION['u_id'])) { //echo "You are logged in!"; include_once 'database.php&apo ...

Adaptable Design for Smartphones

Currently in the process of creating a website for my brother with Joomla 3.4 and Bootstrap. This marks my first venture into building a responsive site, so I'm seeking guidance on essential elements to include in my CSS such as font sizes in specific ...

alter objective response

Currently, I am in the process of developing an educational game for children inspired by the classic "whack-a-mole" style. In this game, kids are presented with a math question and must click on the correct number that appears to solve it. For instance, i ...

The CSS file is not updating

Recently, I updated an HTML file and the global CSS style sheet on my personal website. While the changes appear correctly on my computer, the website still reflects the old settings. I've taken several steps to troubleshoot this issue: Emptied my b ...

"Is there a way to extract text enclosed within HTML tags, such as <div>text</div>, with Selenium using C#

How can I extract the text 'True' from within the HTML tags <div>text</div> using Selenium and C#? <div type='data' id='OfferInCart' style='display:none;'>True</div> I have attempted to retr ...

Is `html.fa-events-icons-ready` causing clutter and disrupting the layout of the body?

I am seeking some clarification on my code and the resulting issues it is causing. Check out this image for reference: https://i.stack.imgur.com/jBQYd.png Why is the body height not taking up 100% of the space? The background image appears to be affect ...

Can you explain the Runtime Environment for React applications and detail the request-response flow within a React application?

Having previously worked on Java based web applications and now transitioning to ReactJS applications, I have some questions that I hope you can help me with: I understand that Babel will convert ECMAScript2015+ code and JSX code to ECMAScript2015 du ...

What is the process for embedding a 3D model using three.js into a specific div on a webpage?

Lately, I've been struggling with a persistent issue. I can't seem to figure out how to integrate my three.js scene into a div on my website. Despite watching numerous YouTube videos and reading countless articles on the topic, I still haven&apos ...

At what precise moment does the ng-checked function get executed?

When utilizing AngularMaterial, I have implemented ng-checked in the following manner: <md-list> <md-list-item ng-repeat="option in options"> <p> {{ option }} </p> <md-checkbox class="md-secondary" aria-label=" ...

States are consistently maintained in React and do not impact the rendering process

I am keeping track of a state value by declaring it like this: const [count, setCount] = useState(0); To increment the count: const incrementCount = () => { setCount(count + 1); } I use this function in a loop to iterate through an array, exec ...

What is the best way to dismiss the modal window in React upon clicking the "Add product" button?

Hello all, I'm having trouble figuring out how to close the modal window in React. I want it so that when the user clicks on the "Add" button, a modal window opens for data input, and then upon clicking the "Add product" button, the window closes imme ...

Creating a dynamic table based on selected dropdown option

I need to create a dynamic table of questions depending on the user's selection from a dropdown menu. When the user chooses a category, I want to pass that information to an API using the GET method. My controller is built using AngularJS. However, I ...

What is the story behind the shared chart legend feature in react-highcharts?

Is it possible to create two charts with a shared legend in React using react-highcharts? I came across a solution using jQuery in this jsFiddle, but I'm struggling to implement the same functionality in React while ensuring that components' opt ...

The JQUERY code for refreshing a div requires a timeout delay

I'm looking for a way to refresh a specific div on my website that's used for chat. Here's the code I currently have: var refreshId = setInterval(function() { $('#chat_grab').load('chat_grab.php?randval=' + Math.rand ...

What could be causing my difficulty in locating an HTML element using jQuery/JS string interpolation?

In my project, I have a set of div blocks each identified by a unique numerical id. For instance: https://i.sstatic.net/lYod8.png One of the tasks in my project is to select the 29th element following a specific chosen element. For example, if I choose t ...

Is it possible to transfer a JSON object from Silverlight to JavaScript?

Is it possible to pass a JSON object directly from Silverlight to JavaScript, or does it have to be serialized first? ...

The width of the Div element is not determined by the parent CSS grid, leading to a lack of ellipsis

I am working on a grid layout where each column contains a div with text. My goal is to have the text show ellipsis when it overflows the column, even though the number of columns is random. I attempted to use the following CSS for the text-containing div ...

Is client-side rendering the new trend, bypassing traditional server-side rendering?

I'm exploring the idea of rendering my pages on the client side to reduce server load and minimize traffic. Typically, the first request to the server requires it to render the requested page and then send it to the user. Once the user interacts with ...