集中div与显示内联块和位置相对 [英] Centralize div with display inline-block and position relative

查看:137
本文介绍了集中div与显示内联块和位置相对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在垂直和水平方向集中,但没有成功:

i'm trying to centralize in the vertically and horizontally, but without success:

https://jsfiddle.net/szg7hhph/1/embedded/result/

HTML:

<div id="new__event">
  <div class="target">
    <h2>Free Pass</h2>
  </div>
</div>


body {
  margin: 0;
}

#new__event {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  pointer-events: none;
  opacity: 0;
  transition: opacity 200ms;
}
#new__event {
  pointer-events: all;
  opacity: 1;
}
#new__event .target {
  cursor: pointer;
  position: relative;
  display: inline-block;
  padding: 48px;
  width: 50%;
  margin: 0 auto;
  box-shadow: 0px 12px 24px rgba(0, 0, 0, 0.2);
  background: white;
  color: #f98835;
  margin-top: 1.5rem;
}



我已经尝试过margin 0 auto,text-align,vertical-align

I already try margin 0 auto, text-align, vertical-align, but the box persist in the corner..

推荐答案

更改位置 #new_event .target absolute ,删除 margin-top 属性并添加以下新属性:

Change the position of #new_event .target to absolute, remove the margin-top property and add the following new properties:

box-sizing: border-box;
left: 25%;
top: 50%;
transform: translateY(-50%);

这应该从IE 9开始工作。

This should work starting from IE 9.

结果

这篇关于集中div与显示内联块和位置相对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆