我有这个html struct :

<div class="single-apartment-typology-text">
  <span class="title-apartment primary-title text-animated">
    <div class="title">Testing</div>
    <div class="uptitle">
      <span> last 3 opportunity</span>
    </div>
  </span>
</div>

我已经使用:before属性添加了背景title-apartment.我的目标是让uptitle内的跨度与边框对齐,问题是当我更改分辨率时,内容不会粘在正确的边框上:

enter image description here

这是我的SCSS:

.title-apartment {
    position: relative;
    width: 400px;
    height: 100px;
    display: flex;
    justify-content: flex-end;
    align-items: center;

    .title {
       position: absolute;
       z-index: 2;
       display: block;
       left: 0;
     }

    &:before {
       content: "";
       position: absolute;
       top: 0;
       left: 0;
       width: 100%;
       height: 100%;
       background-color: $yellow;
       left: 65px;
    }

    .uptitle {
        position: relative;
        z-index: 1;
        margin-right: 10px;
        background-color: $blue;
        color: #fff;
        padding: 10px;
        transform: translateX(33.8%);

       span {
         display: block;
         font-size: 10px;
         font-weight: 600;
         text-transform: uppercase;
        line-height: 23px;
      }
  }
}

我try 使用translateX属性将深蓝色背景附加到右侧黄色背景的情况下保持跨度,但到目前为止没有成功.

有什么办法解决这个问题吗?

推荐答案

因为您要将:BEFORE伪元素右移65px,所以.uptitle元素不知道它在哪里,因为它是sibling 元素而不是子元素.最简单的做法是设置css custom property来记录:before伪元素的移位距离,然后将.uptitle元素右移相同的量.如果我们使用right属性,那么我们的参考点将是右边框,但它必须是负值才能将该元素右移.因此,这里有calc个.

标记了下面的代码

body {
  background: #BCB5AF;
}

.title-apartment {
  --left-shift: 65px; /* added this */
  position: relative;
  width: 400px;
  height: 100px;
  display: flex;
  justify-content: flex-end;
  align-items: center;
}

.title-apartment .title {
  position: absolute;
  z-index: 2;
  display: block;
  left: 0;
}

.title-apartment::before {
  content: "";
  position: absolute;
  top: 0;
  width: 100%;
  height: 100%;
  background-color: #E0CA36;
  left: var(--left-shift);
}

.title-apartment .uptitle {
  position: absolute; /*changed from relative to absolute */
  right: calc(-1 * var(--left-shift)); /*added this */
  z-index: 1;
  /*margin-right: 10px;  if you want this to be hard up agains the border then remove this. If you always want it to be 10px then keep it in */
  background-color: #444F5F;
  color: #fff;
  padding: 10px;
  
}

.title-apartment .uptitle span {
  display: block;
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  line-height: 23px;
}
<div class="single-apartment-typology-text">
  <span class="title-apartment primary-title text-animated">
    <div class="title">Testing</div>
    <div class="uptitle">
      <span> last 3 opportunity</span>
    </div>
  </span>
</div>

附注:我假设你想让它紧靠边框,所以我go 掉了.title-apartment .uptitle规则中的margin-right,但如果你想让它始终是边缘的10px,就恢复它.

Html相关问答推荐

将多个内联元素置于中心,而无需访问父级上的CSS

每次在视口中运行动画

调整一组IMG的大小以适应容器DIV

子元素的单一背景

穿过整个屏幕的旋转线

在Vue 3中使用v-Bind将计算(computed)属性传递给css url()

我的Django应用程序中出现模板语法错误

网格项未在同一行上对齐

如何在悬停时使用 CSS 更改许多同级元素

使用 popover api 和 `

使单行路径的 svg 填充父级的 100% 宽度

为什么每当我调整图片大小时它都会使我的其他元素移动

从垫分页器中删除输入边框

两个span,一个在div居中,好像没有另一个,另一个在右边

那边界从何而来?

如何为某些行具有 rowspan 的表的每个奇数行着色

我如何让这个动画播放,然后停止,然后在设定的时间后再次播放? (CSS)

html元素可以被css跳过吗?

为什么浏览器在 Select 一个包裹在 标签中的块级元素后包含相邻元素?

下拉菜单项在页面加载时显示,需要隐藏直到悬停