当前位置:Gxlcms > html代码 > css实现元素水平、垂直居中

css实现元素水平、垂直居中

时间:2021-07-01 10:21:17 帮助过:26人阅读

在我们实际项目中,有很多关于垂直居中的方法,比如,在手机页面中有很多的弹框提示内容,简单整理如下,希望可以帮助到大家。
做了很多的页面,感觉垂直居中这个问题一直存在,感觉有的方法比较简单,有的却需要根据实际情况去算,我整理的是我在实际中使用过的方法,可能不是最全面的,但实操效果还是不错的哟。

废话少说直接上代码咯:

  1. /* 常用的三种方法 */
  2. /* 第一种 */
  3. div{
  4. width: 200px;
  5. height: 200px;
  6. margin: auto;
  7. background: pink;
  8. position: fixed;
  9. top: 0;
  10. left: 0;
  11. right: 0;
  12. bottom: 0;
  13. }
  14. /* 第二种 */
  15. div{
  16. width: 200px;
  17. height: 200px;
  18. background: green;
  19. position: fixed;
  20. top: 50%;
  21. left: 50%;
  22. margin-left: -100px;
  23. margin-top: -100px;
  24. }
  25. /* 第三种 */
  26. div{
  27. width: 200px;
  28. height: 200px;
  29. background: green;
  30. position: fixed;
  31. top: 50%;
  32. left: 50%;
  33. transform: translate(-50%, -50%);
  34. }

html部分直接去实现去套吧

相关推荐:

CSS控制滚动条样式的解析

CSS3自定义滚动条样式的示例详解

CSS设置div滚动条样式的示例

以上就是css实现元素水平、垂直居中的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行