gpt4 book ai didi

css - 防止按钮占据其容器的整个宽度

转载 作者:行者123 更新时间:2023-11-28 11:53:01 25 4
gpt4 key购买 nike

我在我的应用程序中使用 Material UI 按钮(在样式组件中进行自定义)。以前,通过将 size="small"属性传递给按钮,按钮只占用按钮文本的宽度,加上我定义的一些填充。即使放在我创建的 Flexbox 容器中,它也会这样做。但是,当我为该 FlexContainer 组件定义宽度时,小按钮增加到该容器宽度的 100%。

我不想在按钮上定义默认宽度,因为它应该根据按钮文本适当调整大小。我尝试在按钮本身上设置“display: inline-flex”,但这没有做任何事情。

这是容器组件(较大组件的片段):

       <RightFields flexDirection="column">
<FieldSpacing>
<PasswordDisplay />
</FieldSpacing>
<FieldSpacing>
<PaymentDisplay />
</FieldSpacing>
<Button pink
buttonText="Save Changes"
buttonSize="small"
/>
</RightFields>


...

const RightFields = styled(FlexContainer)`
width: 321px;
`

按钮.js

const Button = ({buttonText, buttonSize, ...props}) => {

return (
<>
{buttonSize === 'large' ?
<LargeButton
variant="contained"
fullWidth={true}
size="large"
{...props}
>
{buttonText}
</LargeButton> :
<SmallButton
variant="contained"
size="small"
{...props}
>
{buttonText}
</SmallButton>
}
</>
)

}

const StyledButton = styled(MaterialButton)`
...
`

const LargeButton = styled(StyledButton)`
...
`

const SmallButton = styled(StyledButton)`
display: inline-flex;
flex-grow: 0;
font-size: 15px;
font-weight: 500;
line-height: 18px;
border-radius: 16px;
height: 28px;
min-width: 50px;
padding: 0 16px;
`

最佳答案

尝试使用 width: auto;

const SmallButton = styled(StyledButton)`
display: inline-flex;
flex-grow: 0;
font-size: 15px;
font-weight: 500;
line-height: 18px;
border-radius: 16px;
height: 28px;
min-width: 50px;
width: auto;
padding: 0 16px;
`

这应该只换行到文本。

关于css - 防止按钮占据其容器的整个宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57596612/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com