html - 如何使用边距使元素居中而不删除边距顶部和左侧的旧值?
问题描述
有什么方法可以使用自动边距使元素居中而不删除边距顶部和底部的旧值?请注意,它们是未知的。
我试图使左右边距自动并且它起作用了,但我想知道是否有任何方法可以在一行中做到这一点,你可以写一些东西而不是 0 来保持旧值
<style>
.test {
display: block;
margin-left: auto;
margin-right: auto;
background-color: grey;
width: 200px;
}
</style>
<div class="test">this is test div</div>
它在这里确实有效,但是有没有办法通过在边距顶部和底部写入一个值来使用边距来保持旧值而不知道它?
解决方案
你可以试试这个
<style>
.parent {
display: flex;
justify-content: center;
}
.test {
background-color: grey;
width: 200px;
}
</style>
<div class="parent">
<div class="test">this is test div</div>
</div>
推荐阅读
- node.js - 如何使用node.js从指定路径发送文件以响应本地存储
- salesforce - 如何在 Azure 数据工厂的 Web 活动中为 POST 请求传递正文中的内容
- c++ - 使用 Boost ASIO 通过 TCP 发送时,Flatbuffers 会导致分段错误
- javascript - 使用自己的数据运行 D3.js 示例不起作用
- python - 函数未返回预期列表
- bash - Bash - 递归限制子shell
- c++ - 如何制作接受字符串并返回 char 数组的函数
- azure - Docker 中的 Nginx 代理使用 HTTPS 到 Azure AKS
- android - Android 说 findViewById 为空,而它存在于另一个活动的 xml 文件中
- python - 为什么 Spark 比 Python 运行大文件慢?