html - 如果我缩放元素,为什么我不能居中
问题描述
我正在使用它在 CSS 中居中:
.testclass {
display: flex;
justify-content: center;
}
但是当我想使用width
and缩放元素时height
,它不起作用并且我的元素没有居中。
像这样:
.testclass {
display: flex;
justify-content: center;
width: 200px;
height: 200px;
}
有什么问题?
解决方案
这看起来像预期的行为。
请记住,在这种情况下justify-content: center;
,将容器内的内容居中 - 而不是容器本身。
编辑:我添加margin: 0 auto;
到容器的中心。
#container1 {
display: flex;
justify-content: center;
border: 1px solid red;
}
#container1 > div {
border: 1px solid blue;
background: yellow;
}
#container2 {
display: flex;
justify-content: center;
border: 1px solid red;
width: 200px;
height: 200px;
margin: 0 auto;
}
#container2 > div {
border: 1px solid blue;
background: yellow;
}
<div id="container1">
<div>test 1</div>
</div>
<div id="container2">
<div>test 2</div>
</div>
推荐阅读
- javascript - 回调返回空数组 - Node.js
- c++ - g++ TestCpp.cpp -pthread -std=c++11 -o 测试;
- flutter - 在重新启动应用程序进入初始状态
- c - 从c中的文件加载列表
- flutter - 如何使用 FutureBuilder 实现 ScrollInfinitive
- c# - 断开连接时销毁的光子用户对象
- django - 如何在 django 表单中的 ManyToMany 字段生成的 CheckboxSelectMultiple ChoiceWidget 的单个选项上设置属性?
- java - 使用图标而不是主菜单的图标
- react-virtuoso - 在 Android Webview 上的 react-virtuoso 上托管时,youtube iframe 的全屏无法正常工作
- c# - 在 WinUI 3 中将 TabItemTemplateSelector 与 TemplateSelector 结合使用会导致奇怪的嵌套控件