我正试图抓住flex
并努力创造我所追求的目标。
div
个元素,一个右对齐,宽度为640px
,另一个左对齐,占用剩余空间我的元素相互叠加显示在屏幕中央。
div.flex {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
justify-content: space-around;
align-items: stretch;
height: 100vh;
}
div.hero {
background-size: cover;
background-position: center bottom;
position: relative;
height: 100vh;
width: 100%;
margin: auto;
}
div.timeline {
width: 640px;
margin: auto;
}
div.header {
position: absolute;
top: 50%;
text-align: center;
width: 100%;
/* color: #fff; */
-ms-transform: translate(0, -50%);
/* IE 9 */
-webkit-transform: translate(0, -50%);
/* Safari */
transform: translate(0, -50%);
-ms-transform: translate(0, calc(-50% - 66px));
/* IE 9 */
-webkit-transform: translate(0, calc(-50% - 66px));
/* Safari */
transform: translate(0, calc(-50% - 66px));
}
<div class="flex">
<div class="hero">
<!-- Header -->
<div class="header">
<h1>Title</h1>
<h2 class="subtitle">Subtitle</h2>
</div>
<!-- End header -->
<!-- Timeline -->
<div class="timeline">
<ul class="timeline-both-side">
<li>
<div class="border-line"></div>
<div class="timeline-description">
<p>Quisque ac laoreet purus, eu dapibus ligula. Mauris nec tincidunt mi, eget finibus sem. Morbi viverra malesuada lobortis. Suspendisse sed augue luctus ex molestie convallis.</p>
</div>
</li>
<li class="opposite-side">
<div class="border-line"></div>
<div class="timeline-description">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed sed erat consectetur, tempor odio sit amet, euismod sapien.</p>
</div>
</li>
<li>
<div class="border-line"></div>
<div class="timeline-description">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed sed erat consectetur, tempor odio sit amet, euismod sapien.</p>
</div>
</li>
<li class="opposite-side">
<div class="border-line"></div>
<div class="timeline-description">
<p>Quisque ac laoreet purus, eu dapibus ligula. Mauris nec tincidunt mi, eget finibus sem. Morbi viverra malesuada lobortis. Suspendisse sed augue luctus ex molestie convallis.</p>
</div>
</li>
<li>
<div class="border-line"></div>
<div class="timeline-description">
<p>Quisque ac laoreet purus, eu dapibus ligula. Mauris nec tincidunt mi, eget finibus sem. Morbi viverra malesuada lobortis. Suspendisse sed augue luctus ex molestie convallis.</p>
</div>
</li>
</ul>
</div>
<!-- End timeline -->
</div>
</div>
如何使用flex
将这两个元素(100vh
)放在如下所示的行中?
+------------------------------------------+
|.flex |
|+-------------------------+ +------------+|
||.hero | |.timeline ||
|| | | ||
|| | | ||
|+-------------------------+ +------------+|
+------------------------------------------+
答案 0 :(得分:8)
简单本身。
body {
margin: 0;
}
.parent {
height: 100vh;
display: flex;
}
.hero {
flex: 1;
background: red;
}
.timeline {
flex: 0 0 640px;
background: green;
}
&#13;
<div class="parent">
<div class="hero"></div>
<div class="timeline"></div>
</div>
&#13;
答案 1 :(得分:1)
考虑对CSS的这些调整:
div.flex {
display: flex;
/* flex-direction: row; <-- not necessary; default value */
/* flex-wrap: nowrap; <-- not necessary; default value */
/* align-items: stretch; <-- not necessary; default value */
height: 100vh;
}
div.hero {
display: flex; /* nested flex container */
justify-content: space-around; /* moved here from div.flex, but not even necessary */
background-size: cover;
background-position: center bottom;
position: relative;
height: 100vh;
width: 100%;
/* margin: auto; <-- REMOVE */
}
div.timeline {
width: 640px;
/* margin: auto; <-- REMOVE */
}
div.header {
flex: 1;
}
注意:
创建弹性容器时(通过将display: flex
或display: inline-flex
应用于元素),子元素将成为弹性项目。灵活容器的后代超出子项不会成为弹性项目,因此不接受弹性属性。
在您的代码中,div.flex
是Flex容器。这意味着它唯一的子项 - div.hero
- 是一个弹性项。但是,div.hero
的子项不是弹性项目。它们仍然是标准块元素,这就是它们垂直堆叠的原因。
使div.hero
成为(嵌套的)Flex容器,以使其子项成为弹性项目。
margin: auto
将灵活项目置于容器中心。这似乎不是你想要的,所以删除它。
.header
的绝对定位。flex: 1
告诉flex项消耗容器中的所有可用空间。答案 2 :(得分:1)
您的结构不正确,.flex
只有一个孩子:.hero
您可以简化CSS并分成两个子容器.hero
&amp; .timeline
div.flex {
display: flex;
height: 100vh;
}
div.hero {
background-size: cover;
background-position: center bottom;
position: relative;
flex: 1;/* will use whole space left avalaible */
margin: auto;
}
div.timeline {
width: 640px;/* flex values are not usefull here since you want a static width */
margin: auto;
}
div.header {
text-align: center;
}
<div class="flex">
<div class="hero">
<!-- Header -->
<div class="header">
<h1>Title</h1>
<h2 class="subtitle">Subtitle</h2>
</div>
<!-- End header -->
</div>
<!-- end hero -->
<!-- Timeline -->
<div class="timeline">
<ul class="timeline-both-side">
<li>
<div class="border-line"></div>
<div class="timeline-description">
<p>Quisque ac laoreet purus, eu dapibus ligula. Mauris nec tincidunt mi, eget finibus sem. Morbi viverra malesuada lobortis. Suspendisse sed augue luctus ex molestie convallis.</p>
</div>
</li>
<li class="opposite-side">
<div class="border-line"></div>
<div class="timeline-description">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed sed erat consectetur, tempor odio sit amet, euismod sapien.</p>
</div>
</li>
<li>
<div class="border-line"></div>
<div class="timeline-description">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed sed erat consectetur, tempor odio sit amet, euismod sapien.</p>
</div>
</li>
<li class="opposite-side">
<div class="border-line"></div>
<div class="timeline-description">
<p>Quisque ac laoreet purus, eu dapibus ligula. Mauris nec tincidunt mi, eget finibus sem. Morbi viverra malesuada lobortis. Suspendisse sed augue luctus ex molestie convallis.</p>
</div>
</li>
<li>
<div class="border-line"></div>
<div class="timeline-description">
<p>Quisque ac laoreet purus, eu dapibus ligula. Mauris nec tincidunt mi, eget finibus sem. Morbi viverra malesuada lobortis. Suspendisse sed augue luctus ex molestie convallis.</p>
</div>
</li>
</ul>
</div>
<!-- End timeline -->
</div>