如何正确对齐背景与前置内容

huangapple go评论63阅读模式
英文:

How to properly align background with before content

问题

我有这个HTML结构:

<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内的span对齐到边框,问题是当我改变分辨率时,内容不会粘在右边框上。

这是我的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;
      }
  }
}

我尝试使用translateX属性将深蓝色背景的span粘在右侧黄色背景上,但目前没有成功。有什么想法来修复这个问题?

英文:

I have this html structure:

&lt;div class=&quot;single-apartment-typology-text&quot;&gt;
  &lt;span class=&quot;title-apartment primary-title text-animated&quot;&gt;
    &lt;div class=&quot;title&quot;&gt;Testing&lt;/div&gt;
    &lt;div class=&quot;uptitle&quot;&gt;
      &lt;span&gt; last 3 opportunity&lt;/span&gt;
    &lt;/div&gt;
  &lt;/span&gt;
&lt;/div&gt;

I've added a background title-apartment using the :before property. My goal is have the span within uptitle aligned to the border, the problem's that when I change the resolution the content doesn't stick to the right border:

如何正确对齐背景与前置内容

This is my 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;
     }

    &amp;:before {
       content: &quot;&quot;;
       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;
      }
  }
}

I tried to keep the span with the dark blue background attached to the right yellow background using the translateX property, but no luck so far.

Any idea to fix this?

答案1

得分: 1

由于您将:before伪元素向右移动了65px,.uptitle元素不知道它的位置,因为它是兄弟元素而不是子元素。最简单的方法是设置一个css自定义属性来记录您将:before伪元素向右移动的距离,然后将.uptitle元素向右移动相同的距离。如果我们使用right属性,那么我们的参考点将是右边框,但它将必须是负值才能将该元素向右移动。因此在这里使用了calc

标记的代码如下:

body {
  background: #BCB5AF;
}

.title-apartment {
  --left-shift: 65px; /* 添加了这个 */
  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; /* 从相对定位更改为绝对定位 */
  right: calc(-1 * var(--left-shift)); /* 添加了这个 */
  z-index: 1;
  /*margin-right: 10px; 如果您希望它与边框紧贴在一起,请删除此行。如果您始终希望它离边缘10px,请保留它 */
  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>

P.S. 我假设您希望它与边框紧贴在一起,因此在.title-apartment .uptitle规则中删除了margin-right,但如果您希望它始终离边缘10px,请恢复它。

英文:

Since you're shifting your :before pseudo element right by 65px, the .uptitle element doesn't know where this is as it's a sibling rather than a child. The easiest thing to do is to set up a css custom property to record how far you're shifting the :before pseudo element then shift the .uptitle element right by the same amount. If we use the right property then our reference point will be the right border but it'll have to be a negative value to then shift that element right. Hence the calc in there.

Marked up code below

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

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: &quot;&quot;;
  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;
}

<!-- language: lang-html -->

&lt;div class=&quot;single-apartment-typology-text&quot;&gt;
  &lt;span class=&quot;title-apartment primary-title text-animated&quot;&gt;
    &lt;div class=&quot;title&quot;&gt;Testing&lt;/div&gt;
    &lt;div class=&quot;uptitle&quot;&gt;
      &lt;span&gt; last 3 opportunity&lt;/span&gt;
    &lt;/div&gt;
  &lt;/span&gt;
&lt;/div&gt;

<!-- end snippet -->

P.S. I've assumed you want it hard up against the border hence I've removed the margin-right in the .title-apartment .uptitle rule but reinstate it if you want it always to be 10px from the edge.

答案2

得分: 1

以下是翻译的部分:

实现你的目标的一种方法是执行以下操作:

  • .titlewidth 设置为 20%;
  • .title-apartment::beforewidth 设置为 80%;

这解决了你的问题吗?

请参见下面的代码段或JSFiddle

.title-apartment {
  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;
  width: 20%;
}

.title-apartment::before {
  content: &quot;&quot;;
  width: 80%;
  height: 100%;
  background-color: yellow;
  left: 65px;
}

.title-apartment .uptitle {
  position: absolute;
  right: 0;
  z-index: 1;
  background-color: blue;
  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>
英文:

One way to achieve your goal is to do the following:

  • set width: 20%; to the .title and
  • set width: 80%; to the .title-apartment::before.

Does this solve your problem?

See the snippet below or JSFiddle.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.title-apartment {
  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;
  width: 20%;
}

.title-apartment::before {
  content: &quot;&quot;;
  width: 80%;
  height: 100%;
  background-color: yellow;
  left: 65px;
}

.title-apartment .uptitle {
  position: absolute;
  right: 0;
  z-index: 1;
  background-color: blue;
  color: #fff;
  padding: 10px;
}

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

<!-- language: lang-html -->

&lt;div class=&quot;single-apartment-typology-text&quot;&gt;
  &lt;span class=&quot;title-apartment primary-title text-animated&quot;&gt;
    &lt;div class=&quot;title&quot;&gt;Testing&lt;/div&gt;
    &lt;div class=&quot;uptitle&quot;&gt;
      &lt;span&gt; last 3 opportunity&lt;/span&gt;
    &lt;/div&gt;
  &lt;/span&gt;
&lt;/div&gt;

<!-- end snippet -->

答案3

得分: 1

I understood the problem, but tested it without the space.

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

Not sure if I understood the problem but tested and without it you don't have that space

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

huangapple
  • 本文由 发表于 2023年5月17日 22:01:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76272967.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定