【问题标题】:Flex Items Moving Up/Down, not Left/Right in a Flex Box With Flex-Direction Row SetFlex 项目在具有 Flex-Direction 行集的 Flex Box 中向上/向下移动,而不是向左/向右移动
【发布时间】:2020-10-29 01:40:41
【问题描述】:

我想更新我的标题以使用弹性框。我想要一个图标,然后是左侧的 4 个 A 标签链接,然后是右侧的两个标签,其中包含一个图像。当我在任何项目上设置 align-self 时,它会上下移动,而不是像我预期的那样左右移动。请帮忙。

function renderSections() {
        return sections.map((section, i) => {
            let sect = section.toString();
            if (i !== 0) {
                return (
                    <p className="HeadLink" onClick={() => scrollToSection(sect)}>
                        {section}
                    </p>
                );
            } else {
                return (
                    <p className="HeadLink" key={section}>
                        {section}
                    </p>
                );
            }
        });
    }

return (
        <HeadCon id="HeadCon">
            <img alt="logo" className="logo" src={logo} />
            {renderSections()}
            <a className="project-icon-link" href="/#/weather">
                <img alt="Gove Weather" className="project__icon" src={weather} />
            </a>
            <a className="project-icon-link" href="/#/SuperHeroSmackDown">
                <img
                    alt="Super Hero Smack Down"
                    className="project__icon"
                    src={superHSD}
                />
            </a>
        </HeadCon>
    );
export const HeadCon = styled.div`
    * {
        box-sizing: border-box;
    }

    display: flex;
    flex-direction: row;
    width: 100%;
    align-items: center;
    position: -webkit-sticky;
    position: sticky;
    text-align: left;
    height: 70px;
    top: 0;
    right: 0;
    left: 0;
    /* border: 2px solid orange; */
    background: black;

.project-icon-link {
        align-self: flex-end;
    }

【问题讨论】:

  • 你试过 justify-self 吗?
  • 是的,我有,在 Firefox 开发工具中,它说 justify-self 对这个项目没有影响,因为它不是一个网格项目......

标签: reactjs flexbox styled-components


【解决方案1】:

我认为这不能用 align-self 来完成。将margin-left: auto 放在应该在右边的第一个元素上,或者将margin-right: auto 放在左边的最后一个元素上。或者,将左侧和右侧包裹在各自的弹性容器中,并将justify-content: space-between 放在容纳左右弹性容器的弹性容器上。

【讨论】:

  • 感谢自动页边距的帮助
  • @RickGove 很高兴听到这个消息!如果它对您有用,您介意投票并接受答案吗?
猜你喜欢
  • 2020-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多