在CSS中,通过添加阴影来美化图片是一种常见的技巧。这是通过使用boxshadow属性来实现的。下面我们来看一下如何在图片下面添加阴影。 首先,我们需要一个HTML代码片段,其中包含一张图片: ...
在CSS中,通过添加阴影来美化图片是一种常见的技巧。这是通过使用box-shadow属性来实现的。下面我们来看一下如何在图片下面添加阴影。
首先,我们需要一个HTML代码片段,其中包含一张图片:
<div class="container">
<img src="example.jpg" alt="Example Image">
</div> .container {
display: inline-block; /* 将容器设置为行内元素 */
position: relative; /* 设置容器为相对定位 */
}
<br>
.container::after {
content: ""; /* 添加内容 */
position: absolute; /* 设置为绝对定位 */
z-index: -1; /* 将伪元素放置在底部 */
bottom: -10px; /* 定义距离容器底部的距离 */
left: 0; /* 将其放置在容器左上角 */
width: 100%; /* 宽度跨越整个容器 */
height: 10px; /* 阴影高度 */
box-shadow: 0px 0px 10px rgba(0,0,0,.7); /* 阴影属性 */
} <div class="container">
<img src="example.jpg" alt="Example Image">
</div>
<br>
<style>
.container {
display: inline-block; /* 将容器设置为行内元素 */
position: relative; /* 设置容器为相对定位 */
}
<br>
.container::after {
content: ""; /* 添加内容 */
position: absolute; /* 设置为绝对定位 */
z-index: -1; /* 将伪元素放置在底部 */
bottom: -10px; /* 定义距离容器底部的距离 */
left: 0; /* 将其放置在容器左上角 */
width: 100%; /* 宽度跨越整个容器 */
height: 10px; /* 阴影高度 */
box-shadow: 0px 0px 10px rgba(0,0,0,.7); /* 阴影属性 */
}
</style>