时间:2018-03-20 04:53:42

标签: html css button

目前我正在尝试创建一些看起来像按钮的链接。它工作得相当好,除了我希望能够水平对齐它们。这就是我到目前为止:

.border {
  display: table;
  width: 220px;
  height: 120px;
  border: 2px solid #1E5034;
  transition: all 250ms ease-out;
  box-sizing: border-box;
  border-spacing: 10px;
  float:left;
}

.border:hover {
  border-spacing: 2px;
}

a.btn {
  display: table-cell;
  vertical-align: middle;
  text-align: center;
  color: #ffffff;
  font: 17.5px sans-serif;
  text-decoration: none;
  background-color: #1E5034;
  line-height: 20px;
  margin-bottom: 0;
}

a.btn:hover,
a.btn:focus,
a.btn:active,
a.btn:visited,
a.btn:link {
  color: #ffffff;
  background-color: #1E5034;
  text-decoration: none;
  cursor: pointer;
}
<div class="btn-grp">
  <div class="border">
    <a class="btn" href="#">Some really long text link #1</a>
  </div>
  <div class="border">
    <a class="btn" href="#">Some other really long text link #2</a>
  </div>
  <div class="border">
    <a class="btn" href="#">Some more really really long text link #3</a>
  </div>
  <div class="border">
    <a class="btn" href="#">The last really long text link #4</a>
  </div>
</div>

修改What I am trying to achieve

如果它有display:inline-block;,它会使高度格式化,而不是文本居中。

我正在尝试创建如此处所示的内容,但也可以将其集中在页面上。

谢谢!

2 个答案:

答案 0 :(得分:1)

支持包括IE在内的所有浏览器。

.btn-grp {
  position: absolute;
  top: 0%;
  left: 50%;
  transform: translate(-50%, 0%);
  width: 80vw;
}

.border {
  display: table;
  width: 25%;
  height: 120px;
  border: 2px solid #1E5034;
  transition: all 250ms ease-out;
  box-sizing: border-box;
  border-spacing: 10px;
  float: left;
}

.border:hover {
  border-spacing: 2px;
}

a.btn {
  display: table-cell;
  vertical-align: middle;
  text-align: center;
  color: #ffffff;
  font: 17.5px sans-serif;
  text-decoration: none;
  background-color: #1E5034;
  line-height: 20px;
  margin-bottom: 0;
}

a.btn:hover,
a.btn:focus,
a.btn:active,
a.btn:visited,
a.btn:link {
  color: #ffffff;
  background-color: #1E5034;
  text-decoration: none;
  cursor: pointer;
}
<div class="btn-grp">
  <div class="border">
    <a class="btn" href="#">Some really long text link #1</a>
  </div>
  <div class="border">
    <a class="btn" href="#">Some other really long text link #2</a>
  </div>
  <div class="border">
    <a class="btn" href="#">Some more really really long text link #3</a>
  </div>
  <div class="border">
    <a class="btn" href="#">The last really long text link #4</a>
  </div>
</div>

如果您需要垂直居中的4 div,请使用:

.btn-grp {
  position: absolute;
  top: 50%;
  left:50%;
  transform: translate(-50%, -50%);
}

答案 1 :(得分:0)

因为您在px使用宽度,这就是为什么然后不要单行...所以尝试使用%宽度,即25% ... < / p>

&#13;
&#13;
.border {
  display: table;
  width: 25%;
  height: 120px;
  border: 2px solid #1E5034;
  transition: all 250ms ease-out;
  box-sizing: border-box;
  border-spacing: 10px;
  float: left;
}

.border:hover {
  border-spacing: 2px;
}

a.btn {
  display: table-cell;
  vertical-align: middle;
  text-align: center;
  color: #ffffff;
  font: 17.5px sans-serif;
  text-decoration: none;
  background-color: #1E5034;
  line-height: 20px;
  margin-bottom: 0;
}

a.btn:hover,
a.btn:focus,
a.btn:active,
a.btn:visited,
a.btn:link {
  color: #ffffff;
  background-color: #1E5034;
  text-decoration: none;
  cursor: pointer;
}
&#13;
<div class="btn-grp">
  <div class="border">
    <a class="btn" href="#">Some really long text link #1</a>
  </div>
  <div class="border">
    <a class="btn" href="#">Some other really long text link #2</a>
  </div>
  <div class="border">
    <a class="btn" href="#">Some more really really long text link #3</a>
  </div>
  <div class="border">
    <a class="btn" href="#">The last really long text link #4</a>
  </div>
</div>
&#13;
&#13;
&#13;

我建议您在这里使用 Flexbox ...它将为您提供完全控制以垂直和水平对齐项目...此外,您需要使用:after背景,因为flexbox不允许border-spacing

&#13;
&#13;
.btn-grp {
  display: flex;
  justify-content: center;
}

.border {
  width: 20%;
  height: 120px;
  border: 2px solid #1E5034;
  transition: all 250ms ease-out;
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  padding: 10px;
}

a.btn {
  text-align: center;
  color: #ffffff;
  font: 16px sans-serif;
  text-decoration: none;
  line-height: 20px;
  margin-bottom: 0;
  flex: 1;
}

a.btn:hover,
a.btn:focus,
a.btn:active,
a.btn:visited {
  color: #ffffff;
  background-color: #1E5034;
  text-decoration: none;
  cursor: pointer;
}

.border:before {
  content: "";
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background: #1f5034;
  position: absolute;
  z-index: -1;
  transition: all .3s ease;
  transform: scale(0.85);
}

.border:hover:before {
  transform: scale(0.95);
}
&#13;
<div class="btn-grp">
  <div class="border">
    <a class="btn" href="#">Some really long text link #1</a>
  </div>
  <div class="border">
    <a class="btn" href="#">Some other really long text link #2</a>
  </div>
  <div class="border">
    <a class="btn" href="#">Some more really really long text link #3</a>
  </div>
  <div class="border">
    <a class="btn" href="#">The last really long text link #4</a>
  </div>
</div>
&#13;
&#13;
&#13;

IE 8支持使用display:inline-blocktransform将内容放在中心...

&#13;
&#13;
.btn-grp {
  text-align: center;
  font-size: 0;
}

.border {
  width: 20%;
  height: 120px;
  border: 2px solid #1E5034;
  transition: all 250ms ease-out;
  box-sizing: border-box;
  display: inline-block;
  vertical-align: top;
  position: relative;
  padding: 10px;
}

a.btn {
  text-align: center;
  color: #ffffff;
  font: 16px sans-serif;
  text-decoration: none;
  line-height: 20px;
  margin-bottom: 0;
  position: relative;
  top: 50%;
  transform: translateY(-50%);
  display: block;
}

.border:before {
  content: "";
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background: #1f5034;
  position: absolute;
  z-index: -1;
  transition: all .3s ease;
  transform: scale(0.85);
}

.border:hover:before {
  transform: scale(0.95);
}
&#13;
<div class="btn-grp">
  <div class="border">
    <a class="btn" href="#">Some really long text link #1</a>
  </div>
  <div class="border">
    <a class="btn" href="#">Some other really long text link #2</a>
  </div>
  <div class="border">
    <a class="btn" href="#">Some more really really long text link #3</a>
  </div>
  <div class="border">
    <a class="btn" href="#">The last really long text link #4</a>
  </div>
</div>
&#13;
&#13;
&#13;