html - 使用 flexbox 使视频和输入框居中
问题描述
我在页面中心将视频、输入框和按钮水平居中时遇到了一点问题。我已经看到一些答案说我不需要 flexbox,实际上我试图将我的边距设置为自动(以使内容在视口中间居中)无济于事。当我尝试使用 chrome 开发人员工具检查代码时,它显示我的视频、内容和表单 div 占据了视口宽度的 100%,所以这似乎是我无法居中的原因。当然,我可能是错的。我难住了。你们提供的任何帮助现在都会非常有用。如果不清楚,请告诉我。这就是我到目前为止所拥有的。谢谢,安娜
<div class="content">
<div class="video">
<h2>Want to read as fast as me? Follow the instructions below...
</h2>
<iframe width="560" height="315"
src="https://www.youtube.com/embed/7SbJLfqPJgI" frameborder="0"
allow="accelerometer; autoplay; encrypted-media; gyroscope;
picture-in-picture" allowfullscreen></iframe>
</div>
<form>
<input type "text" placeholder="Tell us more about what books you
like.">
<form>
</div>
<button type="submit" name="button">submit</button>
<footer>Anna Gibson @ 2018</footer>
解决方案
好的,这就是我所做的。我这样做是为了让 'content div' 包含视频、输入框、按钮和页脚,并将它们全部以文本对齐到 'center' 。下面是我的代码示例:
HTML:
<div class="content">
<div class="video">
<h2>Want to read as fast as me? Follow the instructions below...</h2>
<iframe width="560" height="315" src="https://www.youtube.com/embed/7SbJLfqPJgI" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>
<form>
<input type "text" placeholder="Tell us more about what books you like.">
<form>
<button type="submit" name="button">submit</button>
<footer>Anna Gibson @ 2018</footer>
</div>
CSS:
.content {
text-align: center;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
form {
width: 200px;
margin: 0 auto;
}
input{
margin: auto;
width: 200px;
height: 150px;
margin-top: 20px;
border: 2px solid black;
}
button {
margin-top: 40px;
font-size: 18px;
padding: 10px;
text-align: center;
background-color: #7a420a;
color: white;
margin: auto;
}
推荐阅读
- windows - VS Code 对已安装包中的文件名区分大小写
- c# - c# JsonConverter反序列化包含多态对象的Json
- javascript - 为什么初始状态只修改一次?
- python - 检查列表中的字符串是否包含特定字符,以及它是否只打印列表中的特定字符串
- php - 从 AJAX 响应中获取 ID 并将 ID 用于 Select2
- time-series - 创建动态参数以在用户放大时更新图形。(画面)
- ubuntu - E: 找不到包 snmp-mibs-downloader
- c++ - 名称的所有排列
- python - 如何改变 pandas 箱形图 Y 轴的分布?
- shell - zsh 别名未正确创建文件