在伪元素之前对齐::之前和::之后

时间:2017-04-21 13:33:57

标签: html css

有人能解释一下我在这个例子中做错了什么吗?我正在尝试创建两边都有线条的div。

.bottom-logo {
  width: 50px;
  height: 50px;
  border-radius: 100%;
  background-color: orange;
  margin: 0 auto;
  position: relative;
}

.bottom-logo::before {
  content: "";
  margin-right: 50px;
  margin-top: 20px;
  border-bottom: 4px solid black;
  display: inline-block;
  width: 100px;
  float: right;
}

.bottom-logo::after {
  content: "";
  display: inline-block;
  border-bottom: 4px solid black;
  width: 100px;
  margin-left: 50px;
}
<div class="bottom-logo"></div>

2 个答案:

答案 0 :(得分:3)

我建议对伪元素使用绝对位置。还更新为使用百分比值以使其更灵活。

.bottom-logo {
  width: 50px;
  height: 50px;
  border-radius: 100%;
  background-color: orange;
  margin: 0 auto;
  position: relative;
}

.bottom-logo::before,
.bottom-logo::after {
  content: "";
  border-bottom: 4px solid black;
  width: 100px;
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
}

.bottom-logo::before {
  right: 100%;
}

.bottom-logo::after {
  left: 100%;
}
<div class="bottom-logo"></div>

或者,您可以添加<span>标记,然后使用带垂直对齐的内联块。

.bottom-logo {
  text-align: center;
}

.bottom-logo span {
  display: inline-block;
  vertical-align: middle;
  width: 50px;
  height: 50px;
  border-radius: 100%;
  background-color: orange;
}

.bottom-logo::before,
.bottom-logo::after {
  display: inline-block;
  vertical-align: middle;
  content: "";
  border-bottom: 4px solid black;
  width: 100px;
}
<div class="bottom-logo"><span></span></div>

另一种方法是使用带有<span>标签的flexbox。

.bottom-logo {
  display: flex;
  justify-content: center;
  align-items: center;
}

.bottom-logo span {
  width: 50px;
  height: 50px;
  border-radius: 100%;
  background-color: orange;
}

.bottom-logo::before,
.bottom-logo::after {
  content: "";
  border-bottom: 4px solid black;
  width: 100px;
}
<div class="bottom-logo"><span></span></div>

答案 1 :(得分:0)

请添加float:left;

.bottom-logo::after {
        content: "";
        display: inline-block;
        border-bottom: 4px solid black;
        width:100px;
        margin-left:50px;
        float:left;
    }