居中问题浅谈

2016-08-26  本文已影响0人  _MChao

在CSS的世界里居中一直都是人们抱怨的一件事。“居个中为什么就这么难呢?”人们无奈的说。我认为困难的不是怎么做他,而是怎样在不同的情况下,用不同的方法去达到这相同的目的---居中。

所以 让我们来解决这个问题吧

水平居中(Horizontally)

.center-children {
text-align: center;
}
```

.center-me {
margin: 0 auto;
}
```

垂直居中(Verticallly)

    .parent {
      position: relative;
    }
    .child {
      position: absolute;
      top: 50%;
      height: 100px;
      margin-top: -50px; /* account for padding and border if not using box-sizing: border-box; */
    }
    .parent {
        position: relative;
    }
    .child {
        position: absolute;
        top: 50%;
        transform: translateY(-50%);
    }
    .parent {
        display: flex;
        flex-direction: column;
        justify-content: center;
    }

水平与垂直同时居中(Both Horizontally and Vertically)

总结(Conclusion)

只要你愿意,css会帮你完成这一切...

参考资料

上一篇 下一篇

猜你喜欢

热点阅读