html - 我怎样才能使这个 div 居中对齐?

标签 html css

我有这个页面:

http://fetr.zonedesign.ro/contact/

我有一张 map 和一张蓝色 div 上的 map ,我想在中心显示

这是 HTML 代码:

<div style="float:left;width:100%;padding:0 10%;text-
align:center;margin:10px auto;display:block;">
<div class="date-contact">proba</div>
<?php echo do_shortcode( '[huge_it_maps id="1"]' ); ?>
</div>

这是 CSS 代码:

@media (min-width: 800px) {
.date-contact
{
width:300px;
height:150px;
background:blue;
position:absolute;
z-index:10;
}
}

我尝试使用 margin: 0 auto 但不幸的是没有用。 你能帮我解决这个问题吗?

提前致谢!

最佳答案

如果你想保持其绝对位置,你可以使用 calc 计算顶部/左侧,但你需要知道你的 div 的高度/宽度。

此外,这个蓝框的父级需要是position relative/absolute/or fixed:

here's a demo

<div></div>

div {
    background: blue;
    position: absolute;
    width: 100px;
    height: 100px;
    top: calc(50% - 50px);
    left: calc(50% - 50px);
}

关于html - 我怎样才能使这个 div 居中对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30030726/

相关文章:

css - 为什么我的 CSS 变量添加注释标记?

css - SCSS 过渡属性覆盖

html - html中高效的表单布局

html - CSS布局问题

html - div 标签之间出现意外的空格

javascript - 动态添加 Css 动画持续时间

php - 如何使用 index.php?-url 和 colorbox

javascript - 如何在div内自动向下滚动?

javascript - 在 jQuery 中切换 div 的高度

javascript - jquery 点击 li 到下拉列表不起作用