Customize the Position of Icons in Material UI

I am looking for guidance on how to position my components within an image using Material UI. Specifically, I want to move my heart component to the top right corner of the image and place the ratings at the bottom.

import image from "../../Assets/pic.jpg";
import React, { useState } from "react";
import { makeStyles } from "@material-ui/core/styles";
import Card from "@material-ui/core/Card";
import CardActionArea from "@material-ui/core/CardActionArea";
import FavoriteBorderIcon from "@material-ui/icons/FavoriteBorder";
import CardContent from "@material-ui/core/CardContent";
import CardMedia from "@material-ui/core/CardMedia";
import Typography from "@material-ui/core/Typography";
import Grid from "@material-ui/core/Grid";
import styles from "./Cards.module.css";
import Stars from "../Stars/Stars";

const useStyles = makeStyles({
  root: {
    maxWidth: 345,
    display: "flex",
  },
  text: {
    textAlign: "center",
    color: "#333333",
  },
  textCardBottom: {
    display: "flex",
    justifyContent: "center",
  },

  textPrice: { color: "#333333" },

  textStrike: { margin: "0px 10px 0px 10px" },

  textDiscount: { color: "#ff6a6a" },
  stars: {
    right: 9,
  },
  ratings: {},
});

const Cards = () => {
  const [showComponent, setShowComponent] = useState(false);
  const classes = useStyles();

  const handleToggleHoverIn = (event) => {
    event.preventDefault();
    setShowComponent(true);
  };

  const handleToggleHoverOut = (event) => {
    event.preventDefault();
    setShowComponent(false);
  };

  console.log("The state showComponent value is ", showComponent);

  return (
    <Card
      onMouseEnter={handleToggleHoverIn}
      onMouseLeave={handleToggleHoverOut}
      className={classes.root}
    >
      <CardActionArea>
        <CardMedia
          component="img"
          alt=""
          image={image}
          title="Contemplative Reptile"
        />
      
        {/* {showComponent ? ( */}

        <Grid container>
          <Stars right="40%" /> // placing this rating component in the center of the image
          <FavoriteBorderIcon fontSize="large" /> // moving this heart to the top right corner
        </Grid>
        {/* ) : null} */}
        <CardContent>
          <Typography
            gutterBottom
            variant="h5"
            component="h2"
            className={classes.text}
          >
            Printed round Neck
          </Typography>
          <Typography
            variant="body2"
            color="textSecondary"
            component="div"
            className={classes.textCardBottom}
          >
            <Typography
              variant="body2"
              color="textSecondary"
              component="b"
              className={classes.textPrice}
            >
              Rs. 454
            </Typography>

            <Typography
              variant="body2"
              color="textSecondary"
              component="strike"
              className={classes.textStrike}
            >
              Rs. 699
            </Typography>

            <Typography
              variant="body2"
              color="textSecondary"
              component="span"
              className={classes.textDiscount}
            >
              (35 % off)
            </Typography>
            {/* <p>
              <b>Rs. 454</b>
              <strike>Rs. 699</strike>
              <span style={{ color: "#FF7F7F" }}> (35 % off) </span>
            </p> */}
          </Typography>
        </CardContent>
      </CardActionArea>
    </Card>

  );
};

export default Cards;

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

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

I experimented with the positioning properties in Material UI but faced challenges in moving the components as desired. Despite consulting the Material UI documentation, I couldn't find suitable properties to achieve the desired layout.

Answer №1

Have you experimented with incorporating inline styles in this manner:

<FavoriteBorderIcon fontSize="large" style={{ position: "absolute", top: "5px", right: "5px" }}/>
<Stars right="40%" style={{ position: "absolute", bottom: "5px" }}/> 

It may also be necessary to add a positioning attribute to the Grid element

<Grid style={{ position: "relative" }}>

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

Tips for smoothly transitioning from a simple transform to a rotate effect

I need help with an HTML element that needs to rotate when hovered over. Here is the code I have: The issue I'm facing is that I don't want a transition for translateX, only for the rotation. What steps should I take to achieve this? .cog { ...

ASP.NET Dynamic Slideshow with Horizontal Reel Scrolling for Stunning

I'm curious if there is anyone who can guide me on creating a fascinating horizontal reel scroll slideshow using asp.net, similar to the one showcased in this mesmerizing link! Check out this Live Demo for a captivating horizontal slide show designed ...

What could be causing my token to not save after registering a user?

I am currently facing an issue when submitting a registration form. While the user is successfully created, the token is not stored in localStorage, which prevents me from being redirected immediately to the app and forces me to log in again. Here is my R ...

Clear a form in a React application and print the information to the server console

Is there a way to clear a form after it has been submitted? Currently, even after submitting the form, the details remain visible in the form. The form is only submitting to the server without any interaction with a database. Additionally, can the submitte ...

Distributing elements evenly across the parent width using Angular FlexLayout for a responsive design

I need to create 3 div squares: div-left, div-middle, and div-right, each with a width of 300px. These squares should be arranged horizontally within the parent div main-section, which will adjust its size based on the screen width, being 1300px if the scr ...

The TableHead component in mui does not extend to the full width

Below is a simplified version of the code: return ( <Table> <TableHead> <TableCell></TableCell> <TableCell>Name</TableCell> <TableCell>Date</TableCell> <TableCel ...

Submitting a form via email without the need for PHP

Currently, I am focusing on a project that is small and emphasizes presentation more than security. The main objective is to create a form where users can input data, then click submit for all the information gathered to be sent to a specified 'mailt ...

How to vertically center a div using CSS

I need assistance center aligning #container with the following code: <div id="container"> <p>new</p> </div> The CSS provided is as follows- #container { position:relative; width:100%; background:red; padding:10px; ...

HTML structure consisting of a 3 by 3 grid

Creating a 3x3 grid with cells that have varying heights depending on content is proving to be challenging. Except for the cells marked with red arrows, the rest of the grid has fixed height in pixels. I attempted to build a 3x3 grid using divs to experi ...

What could be the reason for react-query searching for dispatch even when redux is not activated or present in the component?

I am currently working on a component that is supposed to fetch logged-in users from the server. Despite Swagger indicating that the server code returns correctly, the component fails to make the necessary fetch request when loaded. Below is the code snip ...

Strategies for modifying state in reactjs

My chat application, built using reactjs, nodejs, and mongodb, is experiencing an issue. Although I am storing data in mongodb and adding single messages to the 'messages' array, the chat app does not display these messages. It seems that even be ...

A div housing a jQuery progress bar within an unordered list (ul)

Having an issue incorporating a jQuery progress bar into a ul li tag. <ul> <li> <a href="test">test</a> <div id="progressbar"></div> </li> </ul> I have set display: block for the anchor tag and ...

Ensure that both tables contain columns of equal size

I am facing a challenge with 2 HTML tables that are positioned directly on top of each other. Each table has the same number of columns, however, the text within them may vary. Additionally, both tables can consist of multiple rows. My goal is to ensure th ...

Is there a better option than using public methods when transitioning from class-based to function-based React components?

When working with React components that utilize hooks, they must be function-based rather than class-based. I've encountered a challenge transitioning from calling methods on child components in class-based components to achieving the same functionali ...

CSS Image Placement

If I have an image with a width of 2000px, where the snazzy design aspect is located about 600px in and spans about 900px. My goal is to use this image as the background for a website, ensuring that the snazzy design remains centered even when stretching t ...

What is the best way to initiate a Bootstrap carousel so that it begins with the first image every time a modal window appears

I am in the process of creating my own portfolio to showcase my work. I have implemented a feature where clicking on an image opens up a Bootstrap carousel. However, I'm facing an issue where the carousel doesn't always start with the first imag ...

Having trouble adjusting the vertical position of jQuery Spinner buttons

I'm attempting to customize the jQuery spinner with styling applied like this: <span class="ui-spinner ui-widget ui-widget-content ui-corner-all" style="height: 14px;"> <input id="testSpinner" class="ui-spinner-input" name="value" aria-value ...

Mui Material. Eliminate the padding on the grid component

I am having trouble removing the 8px padding that is being added to my grid items. I've tried modifying the code below but haven't had any success. The padding is set by .Mui-GridItem with a value of 8px and I can't seem to get rid of it. Ca ...

next.js encountered an issue with the product.map function not being recognized

I am trying to retrieve data from a REST API to display on the front end. I have used getStaticProps() for this purpose, but when I try to map the data from the props, it throws an error saying it is not a function. Upon inspection, I found that the data r ...

Insert a new, unique div onto an HTML webpage using JavaScript, ensuring no duplicates are created

Having an issue with this code snippet where nothing is being added. It seems like the variable result may not be taking a number or something else, but I'm not entirely sure why. $(document).ready(function () //document.getElementById(1).style. ...