将背景图像添加到 div 底部。

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

Add background image to the bottom of div

问题

我有以下的HTML/CSS代码:

<div class="tip">
    Some tip text
</div>
div.tip {
   background: green url("green-tip.png") no-repeat center bottom / 100% auto;
   border-radius: 4px;
}

基本上我需要复制这个设计:

将背景图像添加到 div 底部。

green-tip.png 只是底部的指针:

将背景图像添加到 div 底部。

但是我只得到一个有圆角的矩形,没有底部的指针。

要如何做到这一点?

英文:

I have the following HTML / CSS code:

<div class="tip">
    Some tip text
</div>
div.tip {
   background: green url("green-tip.png") no-repeat center bottom / 100% auto;
   border-radius: 4px;
}

Basically I need to replicate the design:

将背景图像添加到 div 底部。

The green-tip.png is just the pointer in the bottom:

将背景图像添加到 div 底部。

However I get only a rectangle with round corners without the pointer in the bottom.

How to do this?

答案1

得分: 3

使用多个背景图层:

div.tip {
  --h: 50px; /* 底部部分的高度 */

  border-radius: 18px 18px 0 0;
  height: 200px;
  width: 222px;
  padding: 10px;
  background: 
   linear-gradient(rgb(42 157 143) 0 0)
    top / 100% calc(100% - var(--h)) no-repeat,
   url("https://i.stack.imgur.com/qN0S5.png") 
    bottom / 101% var(--h) no-repeat;
}
<div class="tip">
  一些提示文本
</div>
英文:

Use multiple background layer:

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

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

div.tip {
  --h: 50px; /* height of the bottom part */

  border-radius: 18px 18px 0 0;
  height: 200px;
  width: 222px;
  padding: 10px;
  background: 
   linear-gradient(rgb(42 157 143) 0 0)
    top / 100% calc(100% - var(--h)) no-repeat,
   url(&quot;https://i.stack.imgur.com/qN0S5.png&quot;) 
    bottom / 101% var(--h) no-repeat;
}

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

&lt;div class=&quot;tip&quot;&gt;
  Some tip text
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 2

在底部添加另一个 div 是一种简单的方法:

<div class="container">
  <div class="tip">
    Some tip text
  </div>
  <div class="bottom">
  </div>
</div>

另一种方法是使用 ::after 伪元素(如果你想在网站的多个地方使用):

.container::after {
  background: url("https://i.stack.imgur.com/qN0S5.png") no-repeat center bottom / 100% auto;
  display: block;
  content: "";
  height: 42px;
  width: 223px;
}
英文:

An easy way to do this would be to add another div at the bottom:

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

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

div.tip {
  background: rgb(42, 157, 143);
  border-radius: 18px 18px 0 0;
  height: 100px;
  width: 222px;
  box-sizing: border-box;
  padding: 10px;
}

.bottom {
  background: url(&quot;https://i.stack.imgur.com/qN0S5.png&quot;) no-repeat center bottom / 100% auto;
  height: 42px;
  width: 223px;
}

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

&lt;div class=&quot;container&quot;&gt;
  &lt;div class=&quot;tip&quot;&gt;
    Some tip text
  &lt;/div&gt;
  &lt;div class=&quot;bottom&quot;&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

Another way, using ::after pseudoelement (useful if you want to use it in several places on your site):

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

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

div.tip {
  background: rgb(42, 157, 143);
  border-radius: 18px 18px 0 0;
  height: 100px;
  width: 222px;
  box-sizing: border-box;
  padding: 10px;
}

.container::after {
  background: url(&quot;https://i.stack.imgur.com/qN0S5.png&quot;) no-repeat center bottom / 100% auto;
  display: block;
  content: &quot;&quot;;
  height: 42px;
  width: 223px;
}

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

&lt;div class=&quot;container&quot;&gt;
  &lt;div class=&quot;tip&quot;&gt;
    Some tip text
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案3

得分: 0

这是你的矩形样式类

    .rounded-top-rectangle {
        宽度: 100px;
        高度: 300px;
        背景颜色: #f0f0f0;
        边框顶部左侧半径: 10px;
        边框顶部右侧半径: 10px;
        边框底部左侧半径: 0px;
        边框底部右侧半径: 0px;
        底部外边距: 10px;
    }

这是你的图片样式类

    .image-below {
        显示: ;
        宽度: 100px; /* 调整此值以控制图片大小 */
        高度: 自动; 
        背景图像: url('green-tip.png'); /* 或类似的 */
        背景尺寸: 覆盖;
        背景重复: 不重复; 
    }
<div class="rounded-top-rectangle"></div>
<div class="image-below"></div>
英文:

If you want the image to be part of the background as well, a simple (not necessarily most extendable) way of doing this would be to set both as background divs and place them in vertical block together

CSS

Here's your rectangle style class:

.rounded-top-rectangle {
    width: 100px;
    height: 300px;
    background-color: #f0f0f0;
    border-top-left-radius: 10px;
    border-top-right-radius: 10px;
    border-bottom-left-radius: 0px;
    border-bottom-right-radius: 0px;
    margin-bottom: 10px;
}

Here's your image style class:

.image-below {
    display: block;
    width: 100px; /* Adjust this to control the size of the image */
    height: auto; 
    background-image: url(&#39;green-tip.png&#39;); /* or similar */
    background-size: cover;
    background-repeat: no-repeat; 
}

HTML

&lt;div class=&quot;rounded-top-rectangle&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;image-below&quot;&gt;&lt;/div&gt;

答案4

得分: -3

你可以这样编写:

<body>
    <div class="tip">
        <p>一些提示文本</p>
    </div>
</body>
<style>
    div.tip {
        background-image: url("green-tip.png");
        border-radius: 4px;
        display: flex;
        padding-left: 20px;
        background-repeat: no-repeat;
        background-position: left;
    }
</style>

将背景图像添加到 div 底部。


<details>
<summary>英文:</summary>


you can write like this


    &lt;body&gt;
        &lt;div class=&quot;tip&quot;&gt;
          &lt;p&gt;Some tip text&lt;/p&gt;
        &lt;/div&gt;
      &lt;/body&gt;
      &lt;style&gt;
        div.tip {
          background-image: url(&quot;green-tip.png&quot;);
          border-radius: 4px;
          display: flex;
          padding-left: 20px;
          background-repeat: no-repeat;
          background-position: left;
        }
      &lt;/style&gt;

[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/RNZae.png

</details>



huangapple
  • 本文由 发表于 2023年6月15日 02:19:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76476498.html
匿名

发表评论

匿名网友

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

确定