javascript - Material-UI Dialog 如何在对话框的右上角放置关闭按钮

标签 javascript html css reactjs material-ui

enter image description here

想要在标题部分右上角添加一个关闭图标。

请帮我解决一下这个。我使用了 Material UI 对话框。一切正常,但我想在顶部有一个关闭按钮。

最佳答案

一些注意点:

  • position: 'absolute'启用调整 close button的立场。
  • overflowY: 'unset'通过覆盖相关的样式 Prop paper 启用对话框外溢出.
  • 使用 MUI IconButton带有图标 CloseIcon用于需求 UI。
  • 使用 MUI makeStyles样式 Hook 来自定义样式。


  • 引用:
  • Dialog CSS API : 纸
  • styles solution : makeStyles


  • 示例代码:
    import { makeStyles } from "@material-ui/core/styles";
    
    const useStyles = makeStyles(theme => ({
      paper: {
        overflowY: 'unset',
      },
      customizedButton: {
        position: 'absolute',
        left: '95%',
        top: '-9%',
        backgroundColor: 'lightgray',
        color: 'gray',
      }
    }));
    
    import CloseIcon from '@material-ui/icons/Close';
    import { IconButton } from '@material-ui/core';
    
    <Dialog
      classes={{paper: classes.paper}}
    >
      <DialogActions>
        <IconButton className={classes.customizedButton}>
          <CloseIcon />
        </IconButton>
        ...
      </DialogActions>
    </Dialog>
    

    在线演示:

    https://stackblitz.com/edit/mz5jx2?file=demo.js

    enter image description here

    关于javascript - Material-UI Dialog 如何在对话框的右上角放置关闭按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61318855/

    相关文章:

    javascript - 文档就绪时淡入淡出效果不起作用

    javascript - html 图片上传不适用于 firefox

    javascript - 从对象数组创建字符串的最简单方法

    javascript - HSL 随机颜色将其应用于函数

    html - 为什么每增加一个链接我的 div 就会变高?

    javascript - 如何使用 angularjs 指令创建可重用组件?

    html - 在另一个较窄的 DIV 中居中一个宽的 DIV

    css - float li 元素 : Why do they cover up the parent div element?

    asp.net - 从自定义 ASP Net 服务器控件发送动态 CSS

    javascript - 如何在滚动时修复导航栏中的按钮