Tips for overlaying text onto a MaterialUI icon

https://i.stack.imgur.com/cFr5Y.pngI am currently working on a project that requires me to overlay a number on top of an icon. Specifically, I am utilizing the MaterialUI ModeComment icon and attempting to display text over it. Despite trying the following approaches, none have proven successful:

<ModeComment color='primary'>
  <Typography>2</Typography>
</ModeComment>
<ModeComment color='primary'>
   <span>2</span>
</ModeComment>

If anyone has insight on how to achieve this, I would greatly appreciate the help. Thank you in advance!

Answer №1

If you're looking to add a badge using Material UI, you can easily do so by utilizing the Badge component. Check out this helpful link for more information.

<Badge 
  badgeContent={4} 
  anchorOrigin={{
    vertical: 'center',
    horizontal: 'center',
  }} 
  color="white">
   <ModeComment />
</Badge>

Answer №2

https://i.stack.imgur.com/biTxp.png


Regrettably, trying to pass a children prop in the manner you have done will not function correctly with material-ui's Icon component. This component anticipates that the children should be the name of the icon font ligature.

An alternative approach could involve creating a custom component tailored to your specific needs - perhaps a ModeCommentIconWithNumber component? This custom component could come with predefined styles for positioning its icon and number elements.

The code snippet below provides a starting point for achieving your desired outcome. You can customize this component by incorporating features such as accepting an icon prop for rendering or a color prop to specify the icon's color.

...
import { makeStyles } from "@material-ui/core/styles";

const useStyles = makeStyles({
  root: {
    position: "relative",
    display: "inline-flex",
    justifyContent: "center",
    alignItems: "center"
  },
  icon: {
    fontSize: "2.5em"
  },
  count: {
    position: "absolute",
    lineHeight: 1,
    color: "#fff",
    top: "0.5em",
    fontSize: "1em"
  }
});

function ModeCommentIconWithNumber({ size = 16, count = 0 }) {
  const classes = useStyles();

  return (
    <div className={classes.root} style={{ fontSize: size }}>
      <ModeCommentIcon color="primary" className={classes.icon} />
      <Typography component="span" className={classes.count}>
        {count}
      </Typography>
    </div>
  );
}

https://codesandbox.io/s/cool-monad-xo5r3?fontsize=14&hidenavigation=1&theme=dark

Answer №3

Hello there! If you're looking to add text inside an icon (similar to a child element), one way to achieve this is by placing the Typography component outside the icon and applying some CSS styles. Here's an example:

const styles = {
  typography: {
    color: "white",
    fontSize: 13,
    position: "absolute",
    top: "1.2%",
    left: "2%"
  }
};

function TextIcon() {
  return (
    <div>
      <Typography style={styles.typography}>2</Typography>
      <ModeComment color="primary" />
    </div>
  );
}

If you'd like to see a live example, check out this codesandbox demo.

Answer №4

It seems unlikely that Mui offers the customization you're looking for. One workaround could be to place text adjacent to an icon and position it on top using absolute positioning.

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

Display data-content vertically using CSS

Looking for a way to display text vertically one by one with CSS? While the rotation method produces this result: https://i.stack.imgur.com/utAiN.png However, I aim to achieve something more like this: https://i.stack.imgur.com/fuVyb.png If you have an ...

I am struggling to style a form effectively with CSS grid

I am working on setting up a 2-column HTML form using CSS grid. In the first column, I have labels placed on individual rows. In the second column, I have form elements also placed on individual rows. This is my initial attempt at this task and I am sti ...

A preselected default option determined by your geographical location in an HTML document

After searching extensively for a solution, I've found this to be quite challenging... In my website's HTML page, there is a dropdown menu containing all the countries. <option value="AF">Afghanistan</option> <option value="AX"&g ...

Utilize NodeJS to redirect external asset requests to the local filesystem

Consider this scenario: We are tasked with fetching an image from an S3 bucket. <img src="https://s3-us-west-2.amazonaws.com/{BUCKET}/logo.png" /> Due to limited internet connectivity, I must find a way within my Express server to redirect requests ...

Updating the Origin of a Sound Element in HTML5

I am currently working on developing a personalized browser-based music application. I am at the initial stage of this project where my main goal is to create a button that allows users to change the song being played by an HTML5 audio player. I have attem ...

What are some strategies for accessing the original values of form components that have not been altered when using ngModel?

I am currently developing a form and I intend to utilize the previous values as "value" in the form. By employing ngModel dynamically, I am able to change some properties. However, I have encountered an issue where the field that remains unchanged by the u ...

Issues with positioning images using media queries

Can anyone help me center an img when the viewport width is 320px? I've attempted different approaches but so far, nothing has been successful. .logo { width: 55px; height: 55px; margin: 10px 0 10px 30px; float: left; } @media only screen a ...

What could be causing the failure of the update for this computed property in my Vue 3 application?

Currently, I am in the process of creating an audio player using Vue 3 and the Napster API. About the Project I have managed to make the vinyl rotate by utilizing a CSS keyframes-based animation paired with the computed property isSpinning. I intend for ...

Cascading Style Sheets can be disrupted by Scalable Vector Graphics

Currently, I am incorporating SVG Icons that I created in Illustrator into my Vue.js Webapp. I have utilized Webpack's "require" to load the icons. Rather than accessing the SVGs directly using the src attribute of the img tag, I am inserting them usi ...

Incorporate a vertical scrollbar in the tbody while keeping the thead fixed for smooth vertical scrolling

I'm seeking a solution to implement horizontal and vertical scroll for my table. Currently, the horizontal scroll is working fine, but when trying to add vertical scroll, the table header also moves along with it. Is there a way to keep the table hea ...

When clicked, the menu disappears successfully with the "show" functionality, but unfortunately, the "hide" feature is not working as intended

Within my menu, I have a section for portfolios that transforms into a dropdown on desktop hover and mobile click. The issue arises on mobile devices where the first click displays the menu correctly, but subsequent clicks do not trigger any change. I att ...

Bootstrap 4 allows for the use of the d-lg-flex class to create a flexible column layout

I'm facing an issue with my table on smaller screens where some columns need to be hidden. Here's the code snippet I'm using: <table> <tr> <td>Normal 1</td> <td class="d-lg-flex d-none">hidden 1</t ...

Revamp the Bootstrap 4 Form Check: Rearrange Options from left-to-right and top-to-bottom to top-to-bottom and left-to-right

I am currently using a Bootstrap form that displays a checkbox with 13 different options, including: Urban Plans Commercial Entity Cultural Approval Education Sector Hospitality Industrial Design Interiors Art Leisure/ Sporting Residential Care Retail Spa ...

Aligning Text at the Center in Your React Native Application

I'm facing a few issues with my visualization: Despite aligning it to the center, the text on my first button is positioned at the right end of the button. How can I move the text 'login' to the center? Currently, the 'Sign Up Her ...

Prevent ui-select from being highlighted when clicked

here's a dilemma : (all in angular 1) I'm using a ui-select like this : https://i.stack.imgur.com/RzH2u.png Here's the code snippet : <div class="formZone-group"> <div class="mandatory-fl"> <div class="man ...

Enhancing Autocomplete in React MUI with custom styles for the Popper component

Looking to customize the width of the Popper within the Autocomplete component in my project. The Autocomplete is placed within a parent component, next to a TextField. Check out the Codesandbox snippet here: https://codesandbox.io/s/basictextfields-mate ...

The reason behind the clickable issue with my duplicate <ion-select>

I've been working on a form using HTML, CSS, and JavaScript where I implemented an "Add" button to duplicate the form. The issue arises with the ion-select element within the duplicated form. While the original form displays all predefined options upo ...

What advantages does using ImageMagick to resize images offer compared to using CSS?

My Ruby on Rails application has a feature that allows users to upload images. I have discovered that Active Storage can utilize ImageMagick to resize images using this code: model.image.variant(resize: "100X100") However, resizing images can also be ach ...

Images in gallery slider are excessively large

Hello, this is my very first post on stackoverflow. Please forgive any atypical elements in this post. I have a Django Website with a gallery slider, but I am facing an issue where the images appear way too big. You can view them here: I have dedicated n ...

What could be causing an error when using a straightforward pagination tag in Material UI?

I'm attempting to implement pagination using Joy/Material UI, but I'm running into an issue where even adding a simple <Pagination /> tag is resulting in an error. I am working with Next.js. Below is the code snippet that I have: import La ...