还有其他方法可以定位绝对元素吗?

时间:2019-01-29 06:48:08

标签: html css css-position absolute

.wrap {
  position: relative;
}

.links-tags {
  display: flex;
}

.special,
.in-transit {
  background: #000;
  color: #fff;
  padding: 5px;
  position: absolute;
  top: 0;
  font-size: 10px;
}

.in-transit {
  left: 67px; /*Added temporarily*/ 
}
<div class="wrap">
  <img src="https://via.placeholder.com/200" alt="">
  <div class="links-tags">
    <div class="special">Lorem Ipsum</div>
    <div class="in-transit">in-transit</div>
    <div class="icon">
      <span>X</span>
      <span>Y</span>
      <span>Z</span>
    </div>
  </div>
</div>

需要将“运输中”标签恰好位于第一个包含动态文本的标签之后。目前,我已将left: 67px添加到in-transit

尝试left: auto无效。在不更改HTML的情况下还有其他方法可以实现吗?

注意:我无法更改HTML。

3 个答案:

答案 0 :(得分:1)

在另一个div标签中包装 special transit div,然后将该div绝对放在图片顶部。如下所示:

<div class="wrap">
  <img src="https://via.placeholder.com/200" alt="">
  <div class="links-tags">
    <div class="tags-wrap">
      <div class="special">Lorem Ipsum</div>
      <div class="in-transit">in-transit</div>
    </div>
    <div class="icon">
      <span>X</span>
      <span>Y</span>
      <span>Z</span>
    </div>
  </div>
</div>

.wrap {
  position: relative;
}

.links-tags {
  display: flex;
}
.tags-wrap {
  position: absolute;
  top: 0;
  left: 0;
}
.special,
.in-transit {
  background: #000;
  color: #fff;
  padding: 5px;
  font-size: 10px;
  float: left;
}

答案 1 :(得分:1)

与其给absolutespecial分配in-transit位置,不如给它的包装div links-tags-参见下面的演示

.wrap {
  position: relative;
}

.links-tags {
  display: flex;
  position: absolute; /* ADDED */
  top: 0; /* ADDED */
}

.special,
.in-transit {
  background: #000;
  color: #fff;
  padding: 5px;
  /*position: absolute;*/
  top: 0;
  font-size: 10px;
}
<div class="wrap">
  <img src="https://via.placeholder.com/200" alt="">
  <div class="links-tags">
    <div class="special">Lorem Ipsum</div>
    <div class="in-transit">in-transit</div>
    <div class="icon">
      <span>X</span>
      <span>Y</span>
      <span>Z</span>
    </div>
  </div>
</div>

编辑:这通常是不可能的-但是如果我们假设specialin-transit的宽度都小于图像宽度的50%,这就是 hack 填满wrap

  1. 使用{em> flexline 设置为link-tags的{​​em> flexbox 包装space-between

  2. width: 50%添加到icon并将其向下缩小100%尺寸

请参见下面的演示

.wrap {
  position: relative;
}

.links-tags {
  display: flex;
  position: absolute; /* ADDED */
  top: 0; /* ADDED */
  flex-wrap: wrap; /* ADDED */
  height: 100%; /* ADDED */
  align-content: space-between; /* ADDED */
}

.special,
.in-transit {
  background: #000;
  color: #fff;
  padding: 5px;
  /*position: absolute;*/
  top: 0;
  font-size: 10px;
}

.icon { /* ADDED */
  width: 50%;
  transform: translateY(100%);
}
<div class="wrap">
  <img src="https://via.placeholder.com/200" alt="">
  <div class="links-tags">
    <div class="special">Lorem Ipsum</div>
    <div class="in-transit">in-transit</div>
    <div class="icon">
      <span>X</span>
      <span>Y</span>
      <span>Z</span>
    </div>
  </div>
</div>

答案 2 :(得分:0)

如果您想隐藏图标,只需使用text-indent

.wrap {
  position: relative;
}

.links-tags {
  display: flex;
  position:absolute;
  top:0;
  left:0;
}

.special,
.in-transit {
  background: #000;
  color: #fff;
  padding: 5px;
  font-size: 10px;
  position:relative;
  margin:2px;
}
.icon {
  text-indent:-100px;
}
<div class="wrap">
  <img src="https://via.placeholder.com/200" alt="">
  <div class="links-tags">
    <div class="special">Lorem Ipsum</div>
    <div class="in-transit">in-transit</div>
    <div class="icon">
      <span>X</span>
      <span>Y</span>
      <span>Z</span>
    </div>
  </div>
</div>