带有内联块项目的中心框

时间:2016-05-19 10:27:18

标签: html css

我有一个包含显示项目的框:内联块。如果框中的项目没有填满整行,我需要在中间对齐内容,但在左边对齐项目。

我试图在内联块中设置整个内容,但这仅在只有一行项目而不是两行或更多行时才有效。另外,我需要它具有响应性,因此不能使用固定缩进。

屏幕: enter image description here

代码:



.booking-time{
  font-size: 0;
  line-height: 0;
  border: 1px solid red;
  padding: 5px;
  text-align: center;
}
.inner{
  display: inline-block;
  text-align: left;
}
.btn{
  width: 100px;
  background: #3578d5;
  color: #fff;
  display: inline-block;
  vertical-align: middle;
  font-size: 14px;
  line-height: 20px;
  padding: 8px 16px;
  font-weight: 500;
  text-align: center;
  text-decoration: none;
  text-transform: uppercase;
  cursor: pointer;
  border: none;
  border-radius: 2px;
  overflow: hidden;
  position: relative;
  transition: box-shadow 0.3s, color 0.3s, background 0.3s;
  margin: 0 5px 5px 0;
}

<div class="booking-time">
  <div class="inner">
    <button type="button" class="btn btn-flat">
      8:00 AM
    </button>
    <button type="button" class="btn btn-flat">
      8:15 AM
    </button>
    <button type="button" class="btn btn-flat">
      8:30 AM
    </button>
    <button type="button" class="btn btn-flat">
      8:45 AM
    </button>
    <button type="button" class="btn btn-flat">
      9:00 AM
    </button>
    <button type="button" class="btn btn-flat">
      9:15 AM
    </button>
    <button type="button" class="btn btn-flat">
      9:30 AM
    </button>
    <button type="button" class="btn btn-flat">
      9:45 AM
    </button>
    <button type="button" class="btn btn-flat">
      10:00 AM
    </button>
    <button type="button" class="btn btn-flat">
      10:15 AM
    </button>
    <button type="button" class="btn btn-flat">
      10:30 AM
    </button>
    <button type="button" class="btn btn-flat">
      10:45 AM
    </button>
    <button type="button" class="btn btn-flat">
      11:00 AM
    </button>
    <button type="button" class="btn btn-flat">
      11:15 AM
    </button>
    <button type="button" class="btn btn-flat">
      11:30 AM
    </button>
    <button type="button" class="btn btn-flat">
      11:45 AM
    </button>
  </div>
</div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:4)

您可以从父容器中删除display:inline-block;,但将其保留在子元素中。

您可以去其他容器:

.inner{
  display:block;
  max-width:90%;
  margin:0 auto;
  text-align: left;
}

max-width设置为适合您的任何内容。

编辑:我知道这不是你想要的完美解决方案,但就我而言,如果没有一些脚本,你不可能做到。

This snippet can also be found on jsFiddle

.booking-time{
  font-size: 0;
  line-height: 0;
  border: 1px solid red;
  padding: 5px;
  text-align: center;
}
.inner{
  display:block;
  max-width:90%;
  margin:0 auto;
  text-align: left;
}
.btn{
  width:100px;
  background: #3578d5;
  color: #fff;
  display: inline-block;
  vertical-align: middle;
  font-size: 14px;
  line-height: 20px;
  padding: 8px 16px;
  font-weight: 500;
  text-align: center;
  text-decoration: none;
  text-transform: uppercase;
  cursor: pointer;
  border: none;
  border-radius: 2px;
  overflow: hidden;
  position: relative;
  transition: box-shadow 0.3s, color 0.3s, background 0.3s;
  margin: 0 5px 5px 0;
}
<div class="booking-time">
  <div class="inner">
    <button type="button" class="btn btn-flat">
      8:00 AM
    </button>
    <button type="button" class="btn btn-flat">
      8:15 AM
    </button>
    <button type="button" class="btn btn-flat">
      8:30 AM
    </button>
    <button type="button" class="btn btn-flat">
      8:45 AM
    </button>
    <button type="button" class="btn btn-flat">
      9:00 AM
    </button>
    <button type="button" class="btn btn-flat">
      9:15 AM
    </button>
    <button type="button" class="btn btn-flat">
      9:30 AM
    </button>
    <button type="button" class="btn btn-flat">
      9:45 AM
    </button>
    <button type="button" class="btn btn-flat">
      10:00 AM
    </button>
    <button type="button" class="btn btn-flat">
      10:15 AM
    </button>
    <button type="button" class="btn btn-flat">
      10:30 AM
    </button>
    <button type="button" class="btn btn-flat">
      10:45 AM
    </button>
    <button type="button" class="btn btn-flat">
      11:00 AM
    </button>
    <button type="button" class="btn btn-flat">
      11:15 AM
    </button>
    <button type="button" class="btn btn-flat">
      11:30 AM
    </button>
    <button type="button" class="btn btn-flat">
      11:45 AM
    </button>
  </div>
</div>

答案 1 :(得分:1)

只需将width添加到 .inner

即可
.inner{
  width: 80%; /* ADD THIS to set width of container */
  display: inline-block;
  text-align: left;
}

正如您可以通过background-color(jsFiddle)看到的,它可以正常工作,但有时您的按钮不适合空白空间并换行到下一行。 要解决此问题,您需要使用css媒体查询或引导程序。使用引导程序,您可以轻松地使按钮将宽度调整为可用空间(jsFiddle