html - 带弹性引导的中心元素
问题描述
虽然我讨厌 css,但我必须使用它
我正在尝试使用 flex 将元素居中,这被宣传为简单的解决方案。现实是它不起作用。
我做错了什么?我希望图像徽标的宽度和高度正好位于页面的中间,正好在中间。
指南说你必须证明和对齐中心。
这是我的密码笔
另一个简化的codepen:这里 代码:
HTML
<div class="maindiv d-flex">
<div class="mainlogo justify-content-center align-items-center">
<img id="fadinglogo" class="mainlogo justify-content-center align-items-center" src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/f3/The_Venus_Project_logo_image.png/600px-The_Venus_Project_logo_image.png"/>
</div>
</div>
CSS
/*DEFAULT MVC PROJECT CSS*/
html body {
padding-top: 50px;
padding-bottom: 20px;
width: 100%;
height: 100%; }
/* Set padding to keep content from hitting the edges */
.body-content {
padding-left: 15px;
padding-right: 15px; }
/* Override the default bootstrap behavior where horizontal description lists
will truncate terms that are too long to fit in the left column
*/
.dl-horizontal dt {
white-space: normal; }
/* Set width on the form input elements since they're 100% wide by default */
input,
select,
textarea {
max-width: 280px; }
/*BEGINNING CUSTOM SASS*/
.maindiv * {
width: 100%;
height: 100%; }
#fadinglogo {
width: auto; }
我错过了什么?
解决方案
这是你做错了什么。显示 flex 和它的 flex 属性应该应用到同一个父级,但是你已经将 flex 应用到了“mainDiv”,它的属性应用到了“mainlogo”div。
这是修复。
.maindiv{//display:flex;}
.mainlogo{display:flex;
justify-content:center;
align-items:center;}
将高度值更改为在代码笔中完全居中。
推荐阅读
- reactjs - 如何使用 react 和 typescript 将 ref 添加到 react 样式的组件 div?
- c# - 调用事件 Propertieschanged 时不重新绑定
- maps - 这里 Maps Geocode API 导致 Cross-Origin 错误
- javascript - 有没有办法将选定的 PHP 数组存储在
- java - 如何在我的 android NFC 应用程序中显示十六进制唯一 ID NFC 标签?
- r - gganimate 中的幽灵 geom_text
- javascript - 使用多个处理程序进行快速路由器测试
- c# - 为什么自动映射器不反转设置“派生所有继承”?
- scala - 过滤 RDD 中的空分区
- sql - Jupyter Notebooks 中的 Impala SQL