英文:
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;
}
基本上我需要复制这个设计:
green-tip.png
只是底部的指针:
但是我只得到一个有圆角的矩形,没有底部的指针。
要如何做到这一点?
英文:
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:
The green-tip.png
is just the pointer in the bottom:
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("https://i.stack.imgur.com/qN0S5.png")
bottom / 101% var(--h) no-repeat;
}
<!-- language: lang-html -->
<div class="tip">
Some tip text
</div>
<!-- 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("https://i.stack.imgur.com/qN0S5.png") no-repeat center bottom / 100% auto;
height: 42px;
width: 223px;
}
<!-- language: lang-html -->
<div class="container">
<div class="tip">
Some tip text
</div>
<div class="bottom">
</div>
</div>
<!-- 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("https://i.stack.imgur.com/qN0S5.png") no-repeat center bottom / 100% auto;
display: block;
content: "";
height: 42px;
width: 223px;
}
<!-- language: lang-html -->
<div class="container">
<div class="tip">
Some tip text
</div>
</div>
<!-- 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('green-tip.png'); /* or similar */
background-size: cover;
background-repeat: no-repeat;
}
HTML
<div class="rounded-top-rectangle"></div>
<div class="image-below"></div>
答案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>
<details>
<summary>英文:</summary>
you can write like this
<body>
<div class="tip">
<p>Some tip text</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>
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/RNZae.png
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论