
在网页设计中,我们经常需要对图片进行视觉上的处理,例如将其转换为黑白、添加模糊效果或改变色调。css filter 属性正是为此目的而生,它允许我们直接对元素(包括图片)应用各种图形效果,而无需修改原始图片文件。与通过叠加一个半透明div来模拟滤镜不同,filter属性直接作用于元素的像素内容,提供更真实的视觉效果。
常见的filter函数包括:
为了实现响应式图片、边框、叠加文本和滤镜效果共存,我们需要一个清晰的HTML结构和精确的CSS布局。关键在于将图片和文本包装在一个父容器中,并利用定位属性来管理它们的层叠关系。
每个图片单元应包含一个父容器div,内部放置img标签以及所有叠加的文本元素(如h1和p)。
<section class="flex-section">
<div class="flex-div">
<img class="img-fit" src="https://ichef.bbci.co.uk/news/976/cpsprodpb/C9F6/production/_118720715_gettyimages-51246880.jpg" alt="描述图片内容的替代文本">
<h1 class="title">标题 1</h1>
<p class="portfolio-tools">副标题/工具描述</p>
</div>
<div class="flex-div">
<img class="img-fit" src="https://ichef.bbci.co.uk/news/976/cpsprodpb/C9F6/production/_118720715_gettyimages-51246880.jpg" alt="描述图片内容的替代文本">
<h1 class="title">标题 2</h1>
<p class="portfolio-tools">副标题/工具描述</p>
</div>
<div class="flex-div">
<img class="img-fit" src="https://ichef.bbci.co.uk/news/976/cpsprodpb/C9F6/production/_118720715_gettyimages-51246880.jpg" alt="描述图片内容的替代文本">
<h1 class="title">标题 3</h1>
<p class="portfolio-tools">副标题/工具描述</p>
</div>
</section>全局样式与弹性布局: 使用Flexbox布局来排列多个图片单元,并重置默认的margin和padding。
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
.flex-section {
display: flex;
justify-content: space-around; /* 使图片单元均匀分布 */
flex-wrap: wrap; /* 允许在小屏幕上换行 */
}父容器 (.flex-div): 这是每个图片单元的容器。它需要设置position: relative,以便内部的文本元素可以使用position: absolute进行定位。同时,在这里定义边框和尺寸。
.flex-div {
flex-basis: 30%; /* 响应式宽度,可根据需要调整 */
min-width: 280px; /* 最小宽度,防止过小 */
border: 8px solid black;
height: 40vh; /* 固定高度,可调整为百分比或固定值 */
margin: 4vh;
position: relative; /* 关键:为内部绝对定位元素提供参考 */
overflow: hidden; /* 隐藏超出容器的内容,确保图片和文本被包含 */
display: flex; /* 使得内部的图片可以更好地控制 */
align-items: center; /* 垂直居中图片 */
justify-content: center; /* 水平居中图片 */
}响应式图片 (.img-fit): 图片应填充其父容器,并使用object-fit: cover来裁剪图片以适应容器,同时保持其宽高比。
.img-fit {
width: 100%;
height: 100%;
object-fit: cover;
display: block; /* 移除图片底部默认的空白 */
/* filter 属性将直接应用于此元素 */
transition: filter 0.3s ease; /* 为滤镜效果添加过渡动画 */
}叠加文本 (.title, .portfolio-tools): 文本元素使用position: absolute进行定位,并结合z-index确保它们位于图片之上。
.title, .portfolio-tools {
position: absolute; /* 关键:相对于父容器定位 */
padding: 0.5em 1em;
color: wheat;
background-color: rgba(0, 0, 0, 0.7); /* 半透明背景,提高可读性 */
z-index: 2; /* 确保文本在图片和滤镜之上 */
text-align: center;
width: fit-content; /* 根据内容调整宽度 */
left: 50%; /* 水平居中 */
transform: translateX(-50%); /* 精确水平居中 */
}
.title {
bottom: 8vh; /* 距离底部定位 */
font-size: 1.8em;
color: white;
}
.portfolio-tools {
bottom: 3vh; /* 距离底部定位 */
font-size: 1.1em;
color: lightgray;
}现在,我们可以直接在.img-fit类上应用filter属性。这将只影响图片本身,而不会影响到使用position: absolute定位的文本。
立即学习“前端免费学习笔记(深入)”;
/* 应用一个静态的灰度滤镜 */
.img-fit {
/* ... 其他样式 ... */
filter: grayscale(80%); /* 图片显示为80%灰度 */
}为了实现鼠标悬停时图片滤镜效果的变化,我们可以结合:hover伪类和transition属性。
.img-fit {
width: 100%;
height: 100%;
object-fit: cover;
display: block;
filter: grayscale(0%); /* 默认无灰度 */
transition: filter 0.3s ease; /* 0.3秒平滑过渡 */
}
.img-fit:hover {
filter: invert(100%); /* 鼠标悬停时反转颜色 */
}通过这种方式,当用户鼠标悬停在图片上时,图片颜色会反转,而上方的标题和副标题则保持不变。
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>响应式图片滤镜与文本叠加教程</title>
<style>
/* 全局样式 */
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
}
/* 弹性布局容器 */
.flex-section {
display: flex;
flex-wrap: wrap;
justify-content: center; /* 居中排列 */
padding: 20px;
gap: 40px; /* 增加项目之间的间距 */
}
/* 单个图片单元容器 */
.flex-div {
flex-basis: calc(33.333% - 80px); /* 考虑gap和margin,实现三列布局 */
min-width: 280px; /* 防止在小屏幕上过小 */
border: 8px solid black;
height: 40vh; /* 固定高度,可根据需要调整 */
position: relative; /* 关键:为内部绝对定位元素提供参考 */
overflow: hidden; /* 隐藏超出容器的内容 */
display: flex; /* 用于居中图片 */
align-items: center;
justify-content: center;
background-color: rgba(255, 127, 80, 0.532); /* 容器背景色,如果图片不完全覆盖可见 */
}
/* 响应式图片 */
.img-fit {
width: 100%;
height: 100%;
object-fit: cover; /* 裁剪图片以填充容器 */
display: block; /* 移除图片底部默认空白 */
filter: grayscale(0%); /* 默认无滤镜 */
transition: filter 0.5s ease; /* 滤镜过渡效果 */
}
.img-fit:hover {
filter: invert(100%); /* 鼠标悬停时反转颜色 */
}
/* 叠加标题和副标题 */
.title, .portfolio-tools {
position: absolute; /* 关键:相对于父容器定位 */
padding: 0.5em 1em;
background-color: rgba(0, 0, 0, 0.7); /* 半透明背景 */
color: white;
z-index: 2; /* 确保文本在图片和滤镜之上 */
text-align: center;
left: 50%; /* 水平居中 */
transform: translateX(-50%); /* 精确水平居中 */
white-space: nowrap; /* 防止文本换行 */
}
.title {
bottom: 8vh; /* 距离底部定位 */
font-size: 1.8em;
font-weight: bold;
color: wheat;
}
.portfolio-tools {
bottom: 3vh; /* 距离底部定位 */
font-size: 1.1em;
color: lightgray;
}
/* 媒体查询,适应小屏幕 */
@media (max-width: 768px) {
.flex-div {
flex-basis: calc(50% - 40px); /* 两列布局 */
}
}
@media (max-width: 480px) {
.flex-div {
flex-basis: calc(100% - 40px); /* 单列布局 */
height: 30vh; /* 调整小屏幕下的高度 */
}
.title {
font-size: 1.5em;
bottom: 6vh;
}
.portfolio-tools {
font-size: 1em;
bottom: 2vh;
}
}
</style>
</head>
<body>
<section class="flex-section">
<div class="flex-div">
<img class="img-fit" src="https://ichef.bbci.co.uk/news/976/cpsprodpb/C9F6/production/_118720715_gettyimages-51246880.jpg" alt="城市风光,高楼林立,蓝天白云">
<h1 class="title">城市之光</h1>
<p class="portfolio-tools">现代建筑摄影</p>
</div>
<div class="flex-div">
<img class="img-fit" src="https://ichef.bbci.co.uk/news/976/cpsprodpb/C9F6/production/_118720715_gettyimages-51246880.jpg" alt="自然风光,山峦叠嶂,云雾缭绕">
<h1 class="title">山川壮丽</h1>
<p class="portfolio-tools">风景艺术作品</p>
</div>
<div class="flex-div">
<img class="img-fit" src="https://ichef.bbci.co.uk/news/976/cpsprodpb/C9F6/production/_118720715_gettyimages-5以上就是为响应式图片应用CSS滤镜并叠加文本的专业指南的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号