Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如何实现盒子水平垂直居中 #8

Open
Liqiuyue9597 opened this issue Jul 30, 2020 · 4 comments
Open

如何实现盒子水平垂直居中 #8

Liqiuyue9597 opened this issue Jul 30, 2020 · 4 comments

Comments

@Liqiuyue9597
Copy link
Owner

实现盒子水平垂直居中的方法有哪些

@Liqiuyue9597
Copy link
Owner Author

阿里巴巴淘系技术出品之网页布局
这个非常值得一看

@Liqiuyue9597
Copy link
Owner Author

Liqiuyue9597 commented Sep 17, 2020

//第一种
    #box1 {
      width: 200px;
      height: 200px;
      background-color: #ccc;
      position: absolute;
      left: 50%;
      right: 50%
      transform: translateX(-50%) translateY(-50%);
    }
//第二种
    #box1 {
      width: 200px;
      height: 200px;
      background-color: #ccc;
      margin: auto;
      position: absolute;
      left: 0;
      right: 0;
      top: 0;
      bottom: 0;
    }
//第三种flex:父元素设置
display:flex;
justify-content:center;
align-items:center;
//第四种grid。给父元素设置
dispaly: grid;
justify-items: center;
align-items: center;
//也可以将justify-items和align-items合并为place-items:center

@Maurice-crislin
Copy link

第一种不能实现效果,我猜博主应该粘贴漏了
#box1 { width: 200px; height: 200px; background-color: #ccc; position: absolute; left: 50%; right: 50%; top: 50%; botton: 50%; transform: translateX(-50%) translateY(-50%); }

@Liqiuyue9597
Copy link
Owner Author

第一种不能实现效果,我猜博主应该粘贴漏了 #box1 { width: 200px; height: 200px; background-color: #ccc; position: absolute; left: 50%; right: 50%; top: 50%; botton: 50%; transform: translateX(-50%) translateY(-50%); }

没有粘贴漏,这个需要父元素是相对定位,你可能没有添加

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants