Unsplash 是一个 免费高清图片资源平台,提供海量高质量图片,所有图片均可免费用于个人或商业项目(无需授权,非常友好)。
核心用途:在网页中快速引入美观的图片,无需自己拍摄或设计,适合前端开发时填充页面内容。
Unsplash 每张图片都有公开的 URL,可直接在 HTML 的 <img>
标签中使用。
步骤:
<img src="..." />
中。例子:
<!-- 引用一张Unsplash的风景图 -->
<img
src="https://images.unsplash.com/photo-1534796636912-3b95b3ab5980e8?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D"
alt="自然风光"
class="w-96 h-64 object-cover rounded-md" <!-- 配合Tailwind CSS调整样式 -->
>
https://images.unsplash.com/photo-...
是图片的直接地址,后面的参数(如 ixlib
)是Unsplash的标识,无需修改。w-96 h-64
设定图片宽高,object-cover
让图片适应容器比例,rounded-md
加圆角。如果需要随机图片或按关键词动态加载,可以用 Unsplash 的免费 API。
例子(获取随机猫咪图片):
<img id="cat-img" alt="随机猫咪" class="w-64 h-64 rounded-full">
<script>
// 调用Unsplash API获取随机图片URL
fetch("https://api.unsplash.com/photos/random?query=cat&client_id=YOUR_ACCESS_KEY")
.then(response => response.json())
.then(data => {
document.getElementById("cat-img").src = data.urls.regular;
});
</script>
client_id
(免费),替换代码中的 YOUR_ACCESS_KEY
。<!DOCTYPE html>
<html>
<head>
<link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
</head>
<body>
<!-- 带图片的卡片,用Tailwind CSS布局,图片来自Unsplash -->
<div class="max-w-md mx-auto bg-white shadow-md rounded-lg overflow-hidden">
<img
src="https://images.unsplash.com/photo-1507191515990-7f6f17d39c76?q=80&w=2070&auto=format&fit=crop&ixlib=rb-4.1.0&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D"
alt="书籍"
class="w-full h-48 object-cover" <!-- 图片宽度撑满,高度固定,裁剪保持比例 -->
>
<div class="p-4">
<h2 class="text-lg font-bold">好书推荐</h2>
<p class="text-gray-700">阅读是成长的最佳途径</p>
</div>
</div>
</body>
</html>