简体   繁体   English

为什么 flex-direction: row 在列中渲染项目?

[英]Why is flex-direction: row rendering items in a column?

Hope someone can shed light on this question of mine.希望有人能阐明我的这个问题。

I have a flex container, which has 2 flex containers with flex-direction: column .我有一个 flex 容器,它有 2 个 flex 容器, flex-direction: column

However, it all displays in 1 column.但是,它全部显示在 1 列中。

Initially, it displayed in 2 columns but did not start at the same height, and now it is in one column only.最初,它显示在 2 列中,但没有从相同的高度开始,现在它只显示在 1 列中。

Advise here will be appreciated.在这里提供建议将不胜感激。

 <!-- ***** finding container ***** --> section.finding-container { top: 180px; display: flex; flex-direction: row; position: absolute; justify-content: space-between; align-items: center; /* height: 100% */ /* margin-right: 215px; */ } .find-agent { display: flex; flex-direction: column; justify-content: center; align-items: center; margin-top: 350px; margin-bottom: auto; margin-left: 50px; } .find-agent img, h1, p, button { display: block; margin-left: auto; margin-right: auto; } .find-agent h1 { font-weight: 550; color: #1E95EE; text-align: center; margin-top: 12px; margin-bottom: 0; } .find-agent p { color: #3A3C3E; font-weight: 350; width: 445px; height: 71px; text-align: center; opacity: 1; } .try { border: 2px solid #1E95EE; border-radius: 5px; opacity: 1; color: #1E95EE; font-size: 18px; font-weight: Regular; height: 50px; width: 143px; text-align: center; vertical-align: middle; text-decoration: none; justify-content: center; } .agent-profiles { display: flex; flex-direction: column; justify-content: center; align-items: center; margin-top: 0; margin-bottom: auto; } .agent-profiles h2, img { display: block; margin-left: auto; margin-right: auto; } .agent-profiles h2 { font-weight: 350; color: #1E95EE; text-align: center; width: 182px; height: 44px; letter-spacing: 0; opacity: 1; } ```
 <!-- ***** finding container ***** --> <section class="finding-container"> <div class="find-agent"> <img src="./images/search.svg" alt="search"> <h1>Find the perfect agent</h1> <p> No more browsing through thousands of applications. Each week, we'll send you a fresh batch of hand-picked, personally-vetted candidates. </p> <button type="button" class="try">Try today</button> </div> <div class="agent-profiles"> <h2> Selections from the Overpass Marketplace </h2> <img src="./images/profiles.svg" alt="profiles"> </div> </section>

I have a flex container, which has 2 flex containers with flex-direction: column .我有一个 flex 容器,它有 2 个 flex 容器, flex-direction: column

You've got an invalid comment in your CSS, which is breaking your first line of code:您的 CSS 中有一条无效注释,这破坏了您的第一行代码:

<!-- ***** finding container ***** -->

section.finding-container {
  top: 180px;
  display: flex;
  flex-direction: row;
  position: absolute;
  justify-content:space-between;
  align-items: center;
  /* height: 100% */
  /* margin-right: 215px; */
}

That's HTML commenting syntax.那是 HTML 注释语法。 In CSS, it's invalid, so the following section.finding-container selector is seen as an error and ignored.在 CSS 中,它是无效的,因此下面的section.finding-container选择器被视为错误并被忽略。 The container then falls back to its default layout model, display: block , which stacks child elements vertically.然后容器回退到它的默认布局模型display: block ,它垂直堆叠子元素。 (You can see the proper CSS commenting syntax at the bottom of your rule.) (您可以在规则底部看到正确的 CSS 注释语法。)

More details about CSS commenting syntax and error handling:有关 CSS 注释语法和错误处理的更多详细信息:


Initially, it displayed in 2 columns but did not start at the same height ...最初,它显示在 2 列中,但没有从相同的高度开始......

You've got all sorts of margins and alignment properties (such as justify-content ) set on the items and container, so they're rendering at different heights.您已经在项目和容器上设置了各种边距和对齐属性(例如justify-content ),因此它们以不同的高度呈现。

 section.finding-container { /* top: 180px; */ display: flex; flex-direction: row; position: absolute; justify-content: space-between; /* align-items: center; */ border: 2px dashed red; /* for illustration purposes */ padding: 10px; /* for illustration purposes */ } .find-agent { display: flex; flex-direction: column; justify-content: center; align-items: center; /* margin-top: 350px; */ /* margin-bottom: auto; */ /* margin-left: 50px; */ border: 1px solid green; /* for illustration purposes */ } .find-agent img, h1, p, button { display: block; margin-left: auto; margin-right: auto; } .find-agent h1 { font-weight: 550; color: #1E95EE; text-align: center; margin-top: 12px; margin-bottom: 0; } .find-agent p { color: #3A3C3E; font-weight: 350; width: 445px; height: 71px; text-align: center; opacity: 1; } .try { border: 2px solid #1E95EE; border-radius: 5px; opacity: 1; color: #1E95EE; font-size: 18px; font-weight: Regular; height: 50px; width: 143px; text-align: center; vertical-align: middle; text-decoration: none; justify-content: center; } .agent-profiles { display: flex; flex-direction: column; justify-content: center; align-items: center; margin-top: 0; /* margin-bottom: auto; */ border: 1px solid black; /* for illustration purposes */ } .agent-profiles h2, img { display: block; margin-left: auto; margin-right: auto; } .agent-profiles h2 { font-weight: 350; color: #1E95EE; text-align: center; width: 182px; height: 44px; letter-spacing: 0; opacity: 1; }
 <section class="finding-container"> <div class="find-agent"> <img src="./images/search.svg" alt="search"> <h1>Find the perfect agent</h1> <p> No more browsing through thousands of applications. Each week, we'll send you a fresh batch of hand-picked, personally-vetted candidates. </p> <button type="button" class="try">Try today</button> </div> <div class="agent-profiles"> <h2> Selections from the Overpass Marketplace </h2> <img src="./images/profiles.svg" alt="profiles"> </div> </section>

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

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