按钮使用绝对定位时,不像页面上的其他绝对定位元素一样行为一致。

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

Button with absolute positioning not behaving like other absolutely positioned elements on page

问题

I have a fairly simple "promo banner" container, which is basically just an image with some overlayed text and a button. I'm using https://www.w3schools.com/howto/howto_css_image_text.asp as a reference with the outer container (.promo-banner) positioned relative, with all child elements positioned absolute.

This has worked fine until I introduced my button, which has pre-set styles from another component (position: relative) but I added a button-container with absolute positioning to offset that, regardless I cannot achieve the button displaying how I expect (below the p) without using hacky top/bottom properties with lots of px offset.

.promo-banner {
  position: relative;
  img {
    position: absolute;
    width: 1440px;
    height: 671px;
  }
  .banner-content {
    position: absolute;
    padding-top: 222px;
    padding-left: 56px;
  }
  h2 {
    font-size: 44px;
    margin-bottom: 24px;
    color: white;
    font-style: italic;
    span {
      font-size: 44px;
      padding-top: 8px;
      font-style: normal;
    }
  }
  p {
    position: absolute;
    font-size: 16px;
    color: white;
    margin-bottom: 24px;
  }
  .button-container {
    position: absolute;
  }
  .link-btn {
    position: relative;
    color: green;
    background-color: transparent;
    font-size: 16px;
    border: 0;
    padding: 0;
  }
  .link-btn span {
    font-size: 16px;
    padding-right: 12px;
    padding-bottom: 4px;
  }
  .link-btn span::after {
    position: absolute;
    content: '';
    border-bottom: 2px solid green;
    width: 80%;
    bottom: 0;
    left: 0;
  }
}
<div class="promo-banner">
  <img src="../../assets/images/promo-banner.png" alt="Promo banner" />
  <div class="banner-content">
    <h2>
      My content<span>that displays on an image</span>
    </h2>
    <p>
      A magnificent piece of copy with a button below...
    </p>
    <div class="button-container">
      <button type="button">
        <span>Button link</span>
      </button>
    </div>
  </div>
</div>
英文:

I have a fairly simple "promo banner" container, which is basically just an image with some overlayed text and and a button. I'm using https://www.w3schools.com/howto/howto_css_image_text.asp as a reference with the outer container (.promo-banner) positioned relative, with all child elements positioned absolute.

This has worked fine until I introduced my button, which has pre-set styles from another component (position: relative) but I added a button-container with absolute positioning to offset that, regardless I cannot achieve the button displaying how I expect (below the p) without using hacky top/bottom properties with lots of px offset.

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

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

.promo-banner {
  position: relative;
  img {
    position: absolute;
    width: 1440px;
    height: 671px;
  }
  .banner-content {
    position: absolute;
    padding-top: 222px;
    padding-left: 56px;
  }
  h2 {
    font-size: 44px;
    margin-bottom: 24px;
    color: white;
    font-style: italic;
    span {
      font-size: 44px;
      padding-top: 8px;
      font-style: normal;
    }
  }
  p {
    position: absolute;
    font-size: 16px;
    color: white;
    margin-bottom: 24px;
  }
  .button-container {
    position: absolute;
  }
  .link-btn {
    position: relative;
    color: green;
    background-color: transparent;
    font-size: 16px;
    border: 0;
    padding: 0;
  }
  .link-btn span {
    font-size: 16px;
    padding-right: 12px;
    padding-bottom: 4px;
  }
  .link-btn span::after {
    position: absolute;
    content: &#39;&#39;;
    border-bottom: 2px solid green;
    width: 80%;
    bottom: 0;
    left: 0;
  }
}

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

&lt;div class=&quot;promo-banner&quot;&gt;
  &lt;img src=&quot;../../assets/images/promo-banner.png&quot; alt=&quot;Promo banner&quot; /&gt;
  &lt;div class=&quot;banner-content&quot;&gt;
    &lt;h2&gt;
      My content&lt;span&gt;that displays on an image&lt;/span&gt;
    &lt;/h2&gt;
    &lt;p&gt;
      A magnificent piece of copy with a button below..
    &lt;/p&gt;
    &lt;div class=&quot;button-container&quot;&gt;
      &lt;button type=&quot;button&quot;&gt;
        &lt;span&gt;Button link&lt;/span&gt;
      &lt;/button&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

以下是您要翻译的内容:

当您为.banner-content使用position: absolute;作为容器时,您不需要将position: absolute;应用于其子元素,这是导致问题的原因,我在这里注释掉了不必要的代码:

.promo-banner {
  position: relative;
  img {
    position: absolute;
    width: 1440px;
    height: 671px;
  }
  .banner-content {
    position: absolute;
    padding-top: 222px;
    padding-left: 56px;
  }
  h2 {
    font-size: 44px;
    margin-bottom: 24px;
    color: white;
    font-style: italic;
    span {
      font-size: 44px;
      padding-top: 8px;
      font-style: normal;
    }
  }
  p {
    // position: absolute;
    font-size: 16px;
    color: white;
    margin-bottom: 24px;
  }
  // .button-container {
  //   position: absolute;
  // } 您也删除了按钮的容器
  .link-btn {
    position: relative;
    color: green;
    background-color: transparent;
    font-size: 16px;
    border: 0;
    padding: 0;
  }
  .link-btn span {
    font-size: 16px;
    padding-right: 12px;
    padding-bottom: 4px;
  }
  .link-btn span::after {
    position: absolute;
    content: &quot;&quot;;
    border-bottom: 2px solid green;
    width: 80%;
    bottom: 0;
    left: 0;
  }
}
英文:

When you use position: absolute; for .banner-content as a container you don't need to apply position: absolute; to it's children, which is cause the problem here, I comment out the unnecessary code here:

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

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

.promo-banner {
  position: relative;
  img {
    position: absolute;
    width: 1440px;
    height: 671px;
  }
  .banner-content {
    position: absolute;
    padding-top: 222px;
    padding-left: 56px;
  }
  h2 {
    font-size: 44px;
    margin-bottom: 24px;
    color: white;
    font-style: italic;
    span {
      font-size: 44px;
      padding-top: 8px;
      font-style: normal;
    }
  }
  p {
    // position: absolute;
    font-size: 16px;
    color: white;
    margin-bottom: 24px;
  }
  // .button-container {
  //   position: absolute;
  // } You Remove the button&#39;s container too.
  .link-btn {
    position: relative;
    color: green;
    background-color: transparent;
    font-size: 16px;
    border: 0;
    padding: 0;
  }
  .link-btn span {
    font-size: 16px;
    padding-right: 12px;
    padding-bottom: 4px;
  }
  .link-btn span::after {
    position: absolute;
    content: &quot;&quot;;
    border-bottom: 2px solid green;
    width: 80%;
    bottom: 0;
    left: 0;
  }
}

<!-- end snippet -->

答案2

得分: 1

要在图像上叠加文本,只需在图像上使用position: absolute。其他元素可以在.promo-banner div内静态定位。只需从它们中删除position:absolute;,按钮将出现在段落元素下面。

标记的代码如下:

<div class="promo-banner">
  <img src="https://placekitten.com/1440/671" alt="Promo banner" />
  <div class="banner-content">
    <h2>
      我的内容<span>显示在图像上</span>
    </h2>
    <p>
      一个华丽的文本,下面有一个按钮...
    </p>
    <div class="button-container">
      <button type="button">
        <span>按钮链接</span>
      </button>
    </div>
  </div>
</div>

请注意,代码中已经删除了positon: absolute

英文:

To overlay your text on your image you only need to use position: absolute on your image. The others can be positioned statically within your .promo-banner div. Just remove the position:absolute; from them and the button will appear below the p element

Marked up code below:

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

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

.promo-banner {
  position: relative;
}

.promo-banner img {
  position: absolute;
  width: 1440px;
  height: 671px;
}

.promo-banner .banner-content {
  position: absolute;
  padding-top: 222px;
  padding-left: 56px;
}

.promo-banner h2 {
  font-size: 44px;
  margin-bottom: 24px;
  color: white;
  font-style: italic;
}

.promo-banner h2 span {
  font-size: 44px;
  padding-top: 8px;
  font-style: normal;
}

.promo-banner p {
  /*positon: absolute deleted this */
  font-size: 16px;
  color: white;
  margin-bottom: 24px;
}

.promo-banner .button-container {
  /*positon: absolute deleted this */
}

.promo-banner .link-btn {
  position: relative;
  color: green;
  background-color: transparent;
  font-size: 16px;
  border: 0;
  padding: 0;
}

.promo-banner .link-btn span {
  font-size: 16px;
  padding-right: 12px;
  padding-bottom: 4px;
}

.promo-banner .link-btn span::after {
  position: absolute;
  content: &quot;&quot;;
  border-bottom: 2px solid green;
  width: 80%;
  bottom: 0;
  left: 0;
}

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

&lt;div class=&quot;promo-banner&quot;&gt;
  &lt;img src=&quot;https://placekitten.com/1440/671&quot; alt=&quot;Promo banner&quot; /&gt;
  &lt;div class=&quot;banner-content&quot;&gt;
    &lt;h2&gt;
      My content&lt;span&gt; that displays on an image&lt;/span&gt;
    &lt;/h2&gt;
    &lt;p&gt;
      A magnificent piece of copy with a button below..
    &lt;/p&gt;
    &lt;div class=&quot;button-container&quot;&gt;
      &lt;button type=&quot;button&quot;&gt;
        &lt;span&gt;Button link&lt;/span&gt;
      &lt;/button&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月13日 17:43:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76003973.html
匿名

发表评论

匿名网友

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

确定