html - 如何将元素与容器的顶部和底部对齐?
问题描述
我可以使用 flexbox 将所有元素与容器底部对齐。但我不知道如何将这些元素之一与容器顶部对齐。例如,在此示例中,我无法将文本放到容器的顶部。
jQuery(document).ready(function($) {
$('.slider').slick({
speed: 500,
slidesToShow: 3
});
});
html, body {
background: #102131;
color: white;
}
.container {
position: relative;
height: 400px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: flex-end;
}
.slick-slider {
background: #3a8999;
font-size: 20px;
text-align: center;
width: 90%
}
<link href="https://cdn.jsdelivr.net/npm/slick-carousel@1.8.1/slick/slick-theme.css" rel="stylesheet"/>
<link href="https://cdn.jsdelivr.net/npm/slick-carousel@1.8.1/slick/slick.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/slick-carousel@1.8.1/slick/slick.min.js"></script>
<div class="container">
<div>
<h1>This text should go to the top </h1>
</div>
<div class="slider" data-slick="{'arrows': true}">
<div>Slider stays directly above text below</div>
<div>Slider stays directly above text below</div>
<div>Slider stays directly above text below</div>
<div>Slider stays directly above text below</div>
<div>Slider stays directly above text below</div>
</div>
<div>
<h1>This text should stay at the bottom</h1>
</div>
</div>
解决方案
在 flex 容器中时,您可以使用margin: auto
样式将项目推送到容器的一侧或另一侧。在这种情况下,如果您希望顶部元素紧贴容器顶部,请margin-bottom: auto
在顶部元素上使用。
这是您添加的代码段:
jQuery(document).ready(function($) {
$('.slider').slick({
speed: 500,
slidesToShow: 3
});
});
html, body {
background: #102131;
color: white;
}
.container {
position: relative;
height: 400px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: flex-end;
}
.hug-top {
margin-bottom: auto;
}
.slick-slider {
background: #3a8999;
font-size: 20px;
text-align: center;
width: 90%
}
<link href="https://cdn.jsdelivr.net/npm/slick-carousel@1.8.1/slick/slick-theme.css" rel="stylesheet"/>
<link href="https://cdn.jsdelivr.net/npm/slick-carousel@1.8.1/slick/slick.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/slick-carousel@1.8.1/slick/slick.min.js"></script>
<div class="container">
<div class="hug-top">
<h1>This text should go to the top </h1>
</div>
<div class="slider" data-slick="{'arrows': true}">
<div>Slider stays directly above text below</div>
<div>Slider stays directly above text below</div>
<div>Slider stays directly above text below</div>
<div>Slider stays directly above text below</div>
<div>Slider stays directly above text below</div>
</div>
<div>
<h1>This text should stay at the bottom</h1>
</div>
</div>
推荐阅读
- javascript - GitHub API to retrieve files uploaded via issue comment
- python - Tensorflow not importing properly - Tensor flow version 2.3.0, Python 3.8.3
- javascript - 如何拆分命令
- javascript - How to handle facebook login with a subdomain?
- php - 无法在 swarm 模式下挂载卷
- java - 逗号分隔文本但不包含数字的正则表达式
- c# - 可以从使用 mongodb c# 驱动程序 AscendingGuidGenerator 生成的 GUID 中提取时间戳吗?
- mysql - 何时在 MySQL 中显示列条件值的用例
- php - 使用 jQuery Ajax 将数据发布到另一个 php 文件
- ansible - 当 JSON 键包含冒号时 JSON_QUERY 不工作