亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 JS 中將自定義 css 轉換為 Material UI CSS

如何在 JS 中將自定義 css 轉換為 Material UI CSS

米琪卡哇伊 2022-12-22 15:33:20
有這個項目,有人在其中編寫了帶有自定義 css 的組件。我在里面看到了這個東西 這是一個類似于Material ui中的Container的wrapper組件,或者只是一個應用css的div wrapper。export const Container = styled.div`  position: relative;  margin: 0 auto;  margin-top: ${p => p.marginTop ? p.theme.spacing[p.marginTop] : 0};  width: 100%;  max-width: ${p => (p.maxWidth && p.theme.screen[p.maxWidth])};  padding: ${p => p.padding ? `0 ${p.theme.spacing[p.padding]}` : `0 ${p.theme.spacing.sm}`};  z-index: ${p => p.zIndex && p.theme.zIndex[p.zIndex]};  background-color: ${p => p.color && p.theme.colors[p.color]};  border-radius: ${p => p.radius && p.theme.radius[p.radius]};`;但我不明白 p.marginTop、p.theme 和所有其他但現在我只想將它轉換為簡單的 div 包裝器,并以 material ui 的方式賦予它樣式屬性。像這樣的事情const useStyles = makeStyles((theme) => ({  container: {    position: 'relative',    margin: '0 auto',   // margin-top: ${p => p.marginTop ? p.theme.spacing[p.marginTop] : 0},    width: '100%',   // max-width: ${p => (p.maxWidth && p.theme.screen[p.maxWidth])},  //  padding: ${p => p.padding ? `0 ${p.theme.spacing[p.padding]}` : `0 ${p.theme.spacing.sm}`},    padding: themeIntance.spacing.sm,  //  z-index: ${p => p.zIndex && p.theme.zIndex[p.zIndex]}, //   background-color: ${p => p.color && p.theme.colors[p.color]}, //   border-radius: ${p => p.radius && p.theme.radius[p.radius]},  }}))但是里面所有的注釋都顯示錯誤,說它不承認p。(以前那些 p.theme 的東西,我找到了解決辦法,有一個 theme.js 文件,我可以從那里導入所有 p.theme.spacing.sm,但我不明白什么是 p.padding 或p.maxWidth 是)請幫助我理解這一點。
查看完整描述

1 回答

?
繁星coding

TA貢獻1797條經驗 獲得超4個贊

要風格化 material-ui 組件容器,試試這個:


import Container from '@material-ui/core/Container';

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


const useStyles = makeStyles(theme => ({

  container: {

    marginTop: "100px",

    position: "relative",

    ...

  },

}));


export default function App(){

    const classes = useStyles();

    return (

        <Container className={classes.container}>

          ...

        </Container>

    )

}

您在 useStyles 中為容器定義的所有配置都將應用于組件容器。


你也可以做你自己的組件,像這樣創建一個新文件:


import styled from "styled-components";


const Container = styled.div`

  margin-top: 100px;

  margin-left: 320px;

  margin-right: 40px;


  h1 {

    font-size: 18px;

    display: flex;

    flex-direction: row;

    align-items: center;

    min-height: auto;

    font-weight: 500;

    font-family: "Roboto", Helvetica, Arial, sans-serif;

    margin-bottom: 3px;

    text-decoration: none;

    color: #413e3e;

  }

`;


export default Container;

創建自己的組件后,您可以將其導入任何您想要的文件中。


查看完整回答
反對 回復 2022-12-22
  • 1 回答
  • 0 關注
  • 82 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號