Css 我应该使用边距0自动还是位置、顶部、左侧将内容对齐到中心 我想知道在中间和中间的中心内容的最佳方法是什么。我的意思是,最好使用“margin”属性还是应该使用position:absolute和top,left属性?为此目的使用其中一个的利弊是什么?

Css 我应该使用边距0自动还是位置、顶部、左侧将内容对齐到中心 我想知道在中间和中间的中心内容的最佳方法是什么。我的意思是,最好使用“margin”属性还是应该使用position:absolute和top,left属性?为此目的使用其中一个的利弊是什么?,css,Css,使用它(当你知道你正在居中的物体的大小时) .centered { position: fixed; top: 50%; left: 50%; margin-top: -50px; margin-left: -100px; } .centered { position: fixed; top: 50%; left: 50%; /* bring your own prefixes */ transform: translate(-50%, -50%); }

使用它(当你知道你正在居中的物体的大小时)

.centered {
  position: fixed;
  top: 50%;
  left: 50%;
  margin-top: -50px;
  margin-left: -100px;
}
.centered {
  position: fixed;
  top: 50%;
  left: 50%;
  /* bring your own prefixes */
  transform: translate(-50%, -50%);
}
或者这个(当你不知道你正在定心的物体的大小时)

.centered {
  position: fixed;
  top: 50%;
  left: 50%;
  margin-top: -50px;
  margin-left: -100px;
}
.centered {
  position: fixed;
  top: 50%;
  left: 50%;
  /* bring your own prefixes */
  transform: translate(-50%, -50%);
}
来源:

你能试试吗