说我想要一个看起来像这样的课程的容器
这是不是很糟糕的制作"当然"它自己的flexbox?
.course-container {
display: flex;
flex-direction: column;
}
.course-options {
display: flex;
flex-direction: row;
justify-content: space-between;
}
.course-title {
display: flex;
flex-direction: row;
justify-content: center;
}
.course-grade {
display: flex;
flex-direction: row;
justify-content: flex-end;
}

<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<div class="course-container">
<div class="course">
<div class="course-options">
<i class='fa fa-pencil-square-o fa-lg' aria-hidden="true"></i>
<i class='fa fa-trash-o fa-lg' aria-hidden="true"></i>
</div>
<div class="course-title">
<a href="">Course 1</a>
</div>
<div class="course-grade">
<a href="">Grade: 0.00%</a>
</div>
</div>
<div class="course">
<div class="course-options">
<i class='fa fa-pencil-square-o fa-lg' aria-hidden="true"></i>
<i class='fa fa-trash-o fa-lg' aria-hidden="true"></i>
</div>
<div class="course-title">
<a href="">Course 2</a>
</div>
<div class="course-grade">
<a href="">Grade: 0.00%</a>
</div>
</div>
</div>
&#13;
答案 0 :(得分:1)
您无需在每个内部元素上定义display: flex
,而是可以使用align-self
。
.course-container,
.course {
display: flex;
flex-direction: column;
}
.course {
width: 300px;
border: 1px solid black;
padding: 10px;
height: 100px;
margin: 5px;
justify-content: space-between;
}
.course-options {
display: flex;
justify-content: space-between;
}
.course-title {
align-self: center;
}
.course-grade {
align-self: flex-end;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<div class="course-container">
<div class="course">
<div class="course-options">
<i class='fa fa-pencil-square-o fa-lg' aria-hidden="true"></i>
<i class='fa fa-trash-o fa-lg' aria-hidden="true"></i>
</div>
<div class="course-title">
<a href="">Course 1</a>
</div>
<div class="course-grade">
<a href="">Grade: 0.00%</a>
</div>
</div>
<div class="course">
<div class="course-options">
<i class='fa fa-pencil-square-o fa-lg' aria-hidden="true"></i>
<i class='fa fa-trash-o fa-lg' aria-hidden="true"></i>
</div>
<div class="course-title">
<a href="">Course 2</a>
</div>
<div class="course-grade">
<a href="">Grade: 0.00%</a>
</div>
</div>
</div>
答案 1 :(得分:0)
您的设置方式没有任何问题,是否最好是取决于course
框的行为方式。
根据我现在所知道的,您可以优化该代码,我会这样做,并获得相同的结果。
在这里,我删除了所有内部包装器并使用了auto margins,它使用Flexbox进行了升级,这使得在其父级中对齐项目变得容易。
使用这样的结构带来的好处,除了标记量少之外,您还可以根据内容或屏幕尺寸重新排序项目。
Stack snippet
.course-container {
display: flex;
flex-direction: column;
}
.course {
display: flex;
flex-wrap: wrap;
align-items: flex-start; /* align to top, and prevent from stretch */
width: 250px;
height: 120px;
margin: 5px;
border: 1px solid lightgray;
}
.course .fa-trash-o,
.course .grade {
margin-left: auto; /* push trash and grade to the right */
}
.course .grade {
margin-top: auto; /* push to bottom */
}
.course .title {
flex-basis: 100%; /* take full width, make it wrap on a row of its own */
margin: auto 0; /* center vertically */
text-align: center;
}
&#13;
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<div class="course-container">
<div class="course">
<i class='fa fa-pencil-square-o fa-lg' aria-hidden="true"></i>
<i class='fa fa-trash-o fa-lg' aria-hidden="true"></i>
<a class='title' href="">Course 1</a>
<a class='grade' href="">Grade: 0.00%</a>
</div>
<div class="course">
<i class='fa fa-pencil-square-o fa-lg' aria-hidden="true"></i>
<i class='fa fa-trash-o fa-lg' aria-hidden="true"></i>
<a class='title' href="">Course 2</a>
<a class='grade' href="">Grade: 0.00%</a>
</div>
</div>
&#13;
答案 2 :(得分:0)
这种情况不需要flexbox。 Flexbox仍然有less than 90% browser support worldwide(没有前缀),所以我不会使用它,除非你绝对没有其他选择。
这是适用于所有浏览器的正确解决方案:
.card {
text-align: center;
position: relative;
width: 100%;
max-width: 300px;
height: 100px;
line-height: 100px;
border: 1px solid black;
margin: 5px;
}
.card > * {line-height: 1.4em; position: absolute;}
.card > .edit {left: 0; top: 0;}
.card > .delete {right: 0; top: 0;}
.card > .grade {right: 0; bottom: 0;}
<div class="card">
<a href="#" class="edit">edit</a>
<a href="#" class="delete">delete</a>
<span class="grade">grade</span>
course 1
</div>
<div class="card">
<a href="#" class="edit">edit</a>
<a href="#" class="delete">delete</a>
<span class="grade">grade</span>
course 2
</div>