如何在涉及浮动左元素时修复边距自动

sou*_*rgy 1 html css html5 css3

有一个类page,一个containerdiv行的行box类,以及为所有框设置样式的类.

div框的行需要在页面上居中.

需要什么组合width + display + margin(跨浏览器)?

这些盒子是左浮动的,这似乎是问题的根源.

目前的CSS:

.page {
  width: 100%;
}

.container {
  width: 100%;
  display: block;
  margin: 0 auto;
}

.box {
  float: left;
  margin: %;
}
Run Code Online (Sandbox Code Playgroud)

koa*_*dev 5

你想display:inline-block在你的盒子里使用,有效地将它们当作文本处理,然后设置text-align:center在你的容器中

.container {
  width: 100%;
  display: block;
  margin: 0 auto;
  text-align: center;
}

.box {
  display: inline-block;
  width: 200px;
  height: 200px;
  background: grey;
}
Run Code Online (Sandbox Code Playgroud)

演示小提琴