简体   繁体   English

为什么 flex 属性在反应样式的组件中不起作用?

[英]why isnt the flex attribute working in react styled components?

Okay so my code isn't complex at all.好的,所以我的代码一点也不复杂。 I am watching a tutorial on react styled components and my code got to this point:我正在观看关于反应样式组件的教程,我的代码到了这一点:

const Container = styled.div
    `height: 60px;
`

const Wrapper = styled.div`
    padding : 10px 20px;
    display : flex;
    justify-content : space-between;

`

const Left = styled.div`
    flex : 1;
`
const Center = styled.div`
    flex: 1;
`
const Right = styled.div`
    flex : 1;
`

This is the rest of the code:这是代码的rest:

const Navbar = () => {
    return (
        <Container>
            <Wrapper>
                <Left>loremmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm</Left>
                <Center>Center</Center>
                <Right>Right</Right>
            </Wrapper>
        </Container>
    )
}

export default Navbar

So what is basically happening is that i created 3 components on the same line named Left, Center and Right components and i put them inside a Wrapper component.所以基本上发生的事情是我在同一行上创建了 3 个组件,分别命名为 Left、Center 和 Right 组件,并将它们放在 Wrapper 组件中。 What the flex attribute is meant to do is to give Left, Center and Right components equal space on the browser. flex 属性的作用是在浏览器上为 Left、Center 和 Right 组件提供相等的空间。 Meaning that if there is some long string inside Left (as seen above), the Center and Right components maintain their positions and are not pushed to the side to accommodate Left.这意味着如果 Left 内部有一些长字符串(如上所示),则 Center 和 Right 组件保持其位置并且不会被推到侧面以容纳 Left。 Its meant to be working but when i open my browser, this is what i see:它本来可以工作,但是当我打开浏览器时,这就是我所看到的:

loremmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmCenterRight loremmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm中心右

meaning that the flex attribute is obviously not working?这意味着 flex 属性显然不起作用? so anyone knows what i am doing wrong?所以有人知道我做错了什么吗? and btw, the tutorial i am watching just came out in September 2021 so i doubt its because there's a change in the syntax or something, but if it is the case then please point it out.顺便说一句,我正在观看的教程刚刚于 2021 年 9 月发布,所以我怀疑它是因为语法或其他方面发生了变化,但如果是这种情况,请指出。

That is because flex attempts to size according to your proportions by organizing around element boundaries - or words in case of text.这是因为 flex 试图通过围绕元素边界(或文本情况下的单词)组织来根据您的比例调整大小。

But it is not an absolute rule.但这不是一个绝对的规则。 Since you're having a gigantic word that cannot be wrapped flex does the best it can do under this condition.由于您有一个无法包装的巨大单词,因此 flex 在这种情况下可以做到最好。 Similar to how you would usually not want to break up or hide parts of a large button to respect proportions at all costs.类似于您通常不希望分解或隐藏大按钮的部分以不惜一切代价尊重比例。 This is called overflow.这称为溢出。

Your CSS should work if you have different paragraph sizes with normal words.如果您的段落大小与普通单词不同,您的 CSS 应该可以工作。

One option is to hide overflow or show a scroll bar:一种选择是隐藏溢出或显示滚动条:

const Left = styled.div`
    flex : 1;
    overflow: hidden; # also scroll
`

Alternatively you can permit wrapping on letter boundary - breaking up long words will also make flex proportions achievable:或者,您可以允许在字母边界上换行 - 分解长词也可以实现弹性比例:

const Left = styled.div`
    flex : 1;
    overflow-wrap: anywhere;
`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM