You need to enable JavaScript to run this app. React+Styled Components项目li内相邻元素无法同行显示问题排查
优惠活动
大模型
产品
解决方案
定价
更多

React+Styled Components项目li内相邻元素无法同行显示问题排查

预期效果与实际效果对比
因无法分享实际界面的原始截图,请参考我附上的对比示意图。

实现li元素的React代码

import { Typography } from "@material-ui/core";
...
const { subscriptions } = useContext(SubscriptionsContext);
...
    const renderSubscriptionLinks = () => {
      return (<S.List>{subscriptionsList.map((el : SubscriptionDescription) => (
    <S.LinkContainer key={el.name}><div><S.LinkStyled href="google.com"><Typography variant="body2">{el.name + "  "}</Typography></S.LinkStyled> - <S.BestFor variant="body3">{el.bestFor + "  "}</S.BestFor></div></S.LinkContainer>
  ))}</S.List>);
};

Styled Components相关代码

export const LinkStyled = styled("a")`
    color: black;
    text-decoration: none;
    margin-right: ${({ theme }) => theme.spacing(0.25)};
    ${({ theme }) => theme.breakpoints.up('md')} {
            margin-right: ${({ theme }) => theme.spacing(0.5)};
    }
    p { font-weight: 600; }
`
export const LinkContainer = styled("li")`
    padding: 0;
    min-height: 40px;
    &:not(:last-child) { margin-bottom: ${({ theme }) => theme.spacing(1)}; }
    ::marker {
      font-size: 25px;
    }
    ${({ theme }) => theme.breakpoints.up('md')} {
        &:not(:last-child) { margin-bottom: ${({ theme }) => theme.spacing(2)}; min-height: unset; }
    }
    div {
      display: flex;
      flex-wrap: wrap;
      font-size: 14px;
      align-items: center;
      position: relative;
      top: -3px;
      color: black;
    }
    box-sizing: content-box;
    color: ${({ theme }) => theme.palette.red};
`
export const BestFor = styled(Typography)`
    margin-left: ${({ theme }) => theme.spacing(0.25)};
    ${({ theme }) => theme.breakpoints.up('md')} {
        margin-left: ${({ theme }) => theme.spacing(0.5)};
    }
`
export const List = styled("ul")`
  width: 90%;
  padding: 0;
  margin: 0;
  max-width: 904px;
  margin-left: ${({ theme }) => theme.spacing(3)};
  ${({ theme }) => theme.breakpoints.up('md')} {
      margin-left: ${({ theme }) => theme.spacing(8.75)};
  }
`;

项目背景

我开发的是一个订阅服务说明页面,相关数据从API获取。

问题复现代码

function Subscriptions () {
  return (<>
<S.List><S.LinkContainer key={0}><div><S.LinkStyled href="google.com"><Typography variant="body2">Lorem lorem lorem lorem lorem lorem lorem lorem lorem</Typography></S.LinkStyled> - <S.BestFor variant="body3">Ipsum ipsum ipsum</S.BestFor></div></S.LinkContainer>
<S.LinkContainer key={0}><div><S.LinkStyled href="google.com"><Typography variant="body2">Lorem lorem lorem lorem lorem lorem lorem lorem lorem</Typography></S.LinkStyled> - <S.BestFor variant="body3">Ipsum ipsum ipsum</S.BestFor></div></S.LinkContainer>
<S.LinkContainer key={0}><div><S.LinkStyled href="google.com"><Typography variant="body2">Lorem lorem lorem lorem lorem lorem lorem lorem lorem</Typography></S.LinkStyled> - <S.BestFor variant="body3">Ipsum ipsum ipsum</S.BestFor></div></S.LinkContainer>
<S.LinkContainer key={0}><div><S.LinkStyled href="google.com"><Typography variant="body2">Lorem lorem lorem lorem lorem lorem lorem lorem lorem</Typography></S.LinkStyled> - <S.BestFor variant="body3">Ipsum ipsum ipsum</S.BestFor></div></S.LinkContainer>
      </S.List>
</>)
}

问题排查结果

我在Devtools中排查发现,LinkStyled(即a元素)宽度占满了外层容器,因此即使下一行剩余空间足够放下短文本,其相邻兄弟元素也无法和它显示在同一行。


内容的提问来源于stack exchange,提问作者Mila A

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.10 23:27:04