載說明:原創(chuàng)不易,未經(jīng)授權(quán),謝絕任何形式的轉(zhuǎn)載
作為人類,我們有一種天生的傾向,喜歡收集不同的物品,并根據(jù)興趣將它們分組。從郵票到書籍,人們收集和分組的物品種類繁多。定義上,收藏是一組事物,通常是由某個人創(chuàng)建的。例如,很多孩子會收集漫畫書,還有些人會收集獎杯等等,我相信你可以理解。在本教程中,我們將制作一組圖像,并將它們分成不同的類別。這就是它的樣子。(當(dāng)然,你可以根據(jù)自己的喜好進(jìn)行自定義 )
將你的工作或設(shè)計(jì)劃分為不同的部分非常重要,我總是這樣做,它可以幫助我將其分解成較小的組件,易于構(gòu)建,然后再將它們組合起來形成更大的組件。
如果你已經(jīng)習(xí)慣了我的文章,你肯定知道我稱其為“分而治之”的方法
實(shí)質(zhì)上,我們有兩個主要部分,為了方便引用,我們將它們稱為“收藏分類”和“收藏圖像”。
如果已經(jīng)知道怎么安裝 TailwindCSS ,可以忽略這一小節(jié)。
原文并沒有提及安裝方式,我也嘗試了官網(wǎng)的建議,也沒有達(dá)到理想的效果,只能自己在那折騰,現(xiàn)在把安裝過程分享給大家。
1、首先建立項(xiàng)目文件夾,初始化項(xiàng)目
mkdir demo
cd demo
npm init -y
2、接下來在項(xiàng)目的文件夾里安裝相關(guān)依賴
npm install tailwindcss postcss-cli autoprefixer postcss-cli
3、接下來在項(xiàng)目文件夾里新建如下文件夾和文件
mkdir dist
cd dist
mkdir css
cd css
touch styles.css
cd ../
touch index.html
4、在此文件夾中創(chuàng)建一個新的Tailwind CSS配置文件:
npx tailwindcss init
這將在項(xiàng)目根目錄創(chuàng)建一個名為“tailwind.config.js”的文件,其中包含一些默認(rèn)配置,我們需要修改content的內(nèi)容,如下所示:
module.exports = {
content: ['./dist/**/*.html'],
theme: {
extend: {},
},
plugins: [],
}
5、接下來我們在項(xiàng)目的根目里新建個 tailwind.css 文件,文件名你可以隨意
touch tailwind.css
然后在空白的文件里,添加如下代碼:
@tailwind base;
@tailwind components;
@tailwind utilities;
6、最后回到 index.html 文件,編寫如下代碼,注意CSS的引用。
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Mini Project</title>
<link rel="stylesheet" href="css/styles.css">
</head>
<body class="bg-red-500 flex items-center justify-center min-h-screen">
<h1 class="text-3xl text-white ">歡迎來到前端達(dá)人</h1>
</body>
</html>
7、最后配置 package.json 文件,主要是這部分的修改
"scripts": {
"build": "postcss tailwind.css -o dist/css/styles.css",
"watch": "postcss tailwind.css -o dist/css/styles.css --watch"
}
8、最后運(yùn)行 npm run build ,打開index.html ,一切正常的話就會看到如下效果。
正如我經(jīng)常說的那樣,當(dāng)設(shè)計(jì)組件時,我總是采用相同的結(jié)構(gòu)。因?yàn)槲蚁嘈潘鼈冊谀撤N程度上有相同的基礎(chǔ)。
具體結(jié)構(gòu)如下:
<body>
<!-- 第一層 -->
<div>
<!-- 第二層 -->
<div>
<!-- 收藏分類 -->
<div></div>
<!-- 收藏圖像 -->
<div></div>
</div>
</div>
</body>
正如我們之前商量的那樣,我們將不同的部分分別稱為“收藏分類”和“收藏圖像”。
現(xiàn)在,我們來看看“收藏分類”的細(xì)節(jié)。
<!-- 收藏分類 -->
<div>
<!-- 標(biāo)題 -->
<div class="text-4xl font-bold">
<h2>Popular Collections</h2>
</div>
<!-- 不同的分類 -->
<div class="hidden md:flex items-center gap-4 mt-4 mb-8 [&>*]:bg-white [&>*]:px-4 [&>*]:py-2 [&>*]:rounded-lg [&>*:hover]:bg-slate-900 [&>*:hover]:text-white [&>*]:cursor-pointer">
<div>
<h2>Profile</h2>
</div>
<div>
<h2>New York</h2>
</div>
<div>
<h2>Relaxing</h2>
</div>
<div>
<h2>Person</h2>
</div>
<div>
<h2>Fashion</h2>
</div>
</div>
</div>
讓我們理解這個小小的代碼片段。
標(biāo)題:對于標(biāo)題,我們僅僅設(shè)置了一個 text-4xl 的字體大小,并加粗了字體,使用了 font-semibold。簡單明了,不是嗎?
不同的分類:你還記得 [&>] 的威力嗎?它的意思是從父元素中選擇所有直接子元素,并對其應(yīng)用此樣式。 所以,對于每個分類子元素,我們分別設(shè)置了 px-4 的內(nèi)聯(lián)內(nèi)邊距、py-2 的塊內(nèi)內(nèi)邊距,給它添加了圓角邊框(rounded-lg),并使用 [&>:hover] 添加了一些懸停效果。
至此,我們可以說第一部分已經(jīng)完成了。繼續(xù)往下看。
這個部分實(shí)際上有 3 個相同結(jié)構(gòu)的收藏集合,但是內(nèi)容(分類)不同。因此,如果我們構(gòu)建其中一個,只需要復(fù)制并更改內(nèi)容即可創(chuàng)建其他的。
<div class="w-full max-w-[20rem] p-6 bg-white rounded-2xl">
<!-- 圖片 -->
<div>
<img src="https://thumbs.dreamstime.com/b/asian-chinese-man-holiday-wearing-summer-shirt-over-isolated-yellow-background-smiling-love-showing-heart-symbol-shape-212738466.jpg" alt="" class="h-40 w-full rounded-3xl object-cover object-center cursor-pointer hover:scale-105 hover:-rotate-3">
</div>
<!-- 分類列表 -->
<div class="flex items-center py-4 justify-between [&>*]:mx-2 [&>*>img]:h-20 [&>*>img]:aspect-square [&>*>img]:object-cover [&>*>img]:object-center [&>*>img]:rounded-xl [&>*>img:hover]:scale-110 [&>*>img:hover]:-rotate-12 [&>*>img]:cursor-pointer">
<div>
<img src="https://images.prismic.io/ddhomepage/9643a4bb-cc11-468e-8441-36e67308a6aa_people-main-image.jpg?auto=compress,format&rect=0,0,570,684&w=570&h=684&fit=clip&cs=srgb" alt="">
</div>
<div>
<img src="https://media.istockphoto.com/id/1167770957/photo/indian-man-on-vacation-wearing-floral-shirt-hat-sunglasses-over-isolated-yellow-background.jpg?b=1&s=170667a&w=0&k=20&c=gX1Sd-0BL2kACkdcQNlgCjj98M_1jhdnSt3UeXQm97s=" alt="">
</div>
<div>
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRna0Yn_AMMEcnVGFuHNG0-UENJAFjsGKO8RQ&usqp=CAU" alt="">
</div>
</div>
<!-- 分類名稱 -->
<div class="flex items-center justify-between">
<h2>People</h2>
<div class="flex items-center justify-center gap-1 cursor-pointer">
<div class="text-2xl">
<svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24">
<path fill="currentColor" d="M19 5v14H5V5h14m0-2H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm-4.86 8.86l-3 3.87L9 13.14L6 17h12l-3.86-5.14z" />
</svg>
</div>
<p class="text-sm">144</p>
</div>
</div>
</div>
所以這就是每個“收藏品”元素的外觀。當(dāng)然,你可以將圖片源更改為你的收藏圖片。更好的是,你可以完全重新設(shè)計(jì)收藏品元素以符合你的喜好。這樣更有趣
對于其他兩個收藏品元素,基本上是相同的,只是圖片來源不同。
<!-- Collection-Images -->
<div class="flex items-center gap-4 flex-wrap">
<!-- First Collection Element -->
<div class="w-full max-w-[20rem] p-6 bg-white rounded-2xl">
<div>
<img src="https://thumbs.dreamstime.com/b/asian-chinese-man-holiday-wearing-summer-shirt-over-isolated-yellow-background-smiling-love-showing-heart-symbol-shape-212738466.jpg" alt="" class="h-40 w-full rounded-3xl object-cover object-center cursor-pointer hover:scale-105 hover:-rotate-3">
</div>
<div class="flex items-center py-4 justify-between [&>*]:mx-2 [&>*>img]:h-20 [&>*>img]:aspect-square [&>*>img]:object-cover [&>*>img]:object-center [&>*>img]:rounded-xl [&>*>img:hover]:scale-110 [&>*>img:hover]:-rotate-12 [&>*>img]:cursor-pointer">
<div>
<img src="https://images.prismic.io/ddhomepage/9643a4bb-cc11-468e-8441-36e67308a6aa_people-main-image.jpg?auto=compress,format&rect=0,0,570,684&w=570&h=684&fit=clip&cs=srgb" alt="">
</div>
<div>
<img src="https://media.istockphoto.com/id/1167770957/photo/indian-man-on-vacation-wearing-floral-shirt-hat-sunglasses-over-isolated-yellow-background.jpg?b=1&s=170667a&w=0&k=20&c=gX1Sd-0BL2kACkdcQNlgCjj98M_1jhdnSt3UeXQm97s=" alt="">
</div>
<div>
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRna0Yn_AMMEcnVGFuHNG0-UENJAFjsGKO8RQ&usqp=CAU" alt="" >
</div>
</div>
<div class="flex items-center justify-between">
<h2>People</h2>
<div class="flex items-center justify-center gap-1 cursor-pointer">
<div class="text-2xl">
<svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em"
preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24">
<path fill="currentColor"
d="M19 5v14H5V5h14m0-2H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm-4.86 8.86l-3 3.87L9 13.14L6 17h12l-3.86-5.14z" />
</svg>
</div>
<p class="text-sm">144</p>
</div>
</div>
</div>
<!-- Second Collection Element -->
<div class="w-full max-w-[20rem] p-6 bg-white rounded-2xl">
<div>
<img src="https://www.celebritycruises.com/blog/content/uploads/2022/01/most-beautiful-mountains-in-the-world-kirkjufell-iceland-1024x580.jpg" alt="" class="h-40 w-full rounded-3xl object-cover object-center cursor-pointer hover:scale-105 hover:-rotate-3">
</div>
<div class="flex items-center py-4 justify-between [&>*]:mx-2 [&>*>img]:h-20 [&>*>img]:aspect-square [&>*>img]:object-cover [&>*>img]:object-center [&>*>img]:rounded-xl [&>*>img:hover]:scale-110 [&>*>img:hover]:-rotate-12 [&>*>img]:cursor-pointer">
<div>
<img src="https://www.hostelworld.com/blog/wp-content/uploads/2018/12/kirkjufell-1313x875.jpg" alt="">
</div>
<div>
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSlah-eof7BDtWTo1gtz8F6Xe3z341c-yv-oc25SHzksBzDv-QPGMVpqn0omwvEvHYQ0OU&usqp=CAU" alt="">
</div>
<div>
<img src="https://cdn.europosters.eu/image/750/posters/aurora-borealis-i47499.jpg" alt="" >
</div>
</div>
<div class="flex items-center justify-between">
<h2>Nature</h2>
<div class="flex items-center justify-center gap-1 cursor-pointer">
<div class="text-xl">
<svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em"
preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24">
<path fill="currentColor"
d="M19 5v14H5V5h14m0-2H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm-4.86 8.86l-3 3.87L9 13.14L6 17h12l-3.86-5.14z" />
</svg>
</div>
<p class="text-sm">7k</p>
</div>
</div>
</div>
<!-- Third Collection Element -->
<div class="w-full max-w-[20rem] p-6 bg-white rounded-2xl">
<div>
<img src="https://www.holidify.com/images/cmsuploads/compressed/Taj_Mahal_20180814141729.png" alt="" class="h-40 w-full rounded-3xl object-cover object-center cursor-pointer hover:scale-105 hover:-rotate-3">
</div>
<div class="flex items-center py-4 justify-between [&>*]:mx-2 [&>*>img]:h-20 [&>*>img]:aspect-square [&>*>img]:object-cover [&>*>img]:object-center [&>*>img]:rounded-xl [&>*>img:hover]:scale-110 [&>*>img:hover]:-rotate-12 [&>*>img]:cursor-pointer">
<div>
<img src="https://abtoi.com/wp-content/uploads/2020/02/Famous-monuments-in-Italy-colosseum-scaled.jpg" alt="">
</div>
<div>
<img src="https://www.crtv.cm/wp-content/uploads/2022/04/0BE1E695-A727-4A0A-ACDA-F7B47587892A.jpeg" alt="">
</div>
<div>
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSQ6sHVW_qhWwGbinzhVWWYRLq_SvlE6JfrsQ&usqp=CAU" alt="" >
</div>
</div>
<div class="flex items-center justify-between">
<h2>History</h2>
<div class="flex items-center justify-center gap-1 cursor-pointer">
<div class="text-xl">
<svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em"
preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24">
<path fill="currentColor"
d="M19 5v14H5V5h14m0-2H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm-4.86 8.86l-3 3.87L9 13.14L6 17h12l-3.86-5.14z" />
</svg>
</div>
<p class="text-sm">431</p>
</div>
</div>
</div>
</div>
那應(yīng)該一切都好!
我們剛剛使用了驚人的圖像構(gòu)建了一個驚人的收藏品列表組件,而且這一切都是不需要打開 CSS 文件的。感謝 Tailwindcss。
許多雇主都需要在他們的網(wǎng)站上添加這樣的組件,而現(xiàn)在你應(yīng)該感到自豪,因?yàn)槟闶悄切┛梢栽诓坏?分鐘內(nèi)構(gòu)建它的少數(shù)人之一,而且你可以在不離開 HTML 文檔的情況下完成它 。
你可以在 Codepen 上獲得實(shí)時預(yù)覽。
https://codepen.io/mbianou-bradon/pen/JjBYBdr
在文章結(jié)尾,我想提醒您,文章的創(chuàng)作不易,如果您喜歡我的分享,請別忘了點(diǎn)贊和轉(zhuǎn)發(fā),讓更多有需要的人看到。同時,如果您想獲取更多前端技術(shù)的知識,歡迎關(guān)注「前端達(dá)人」,您的支持將是我分享最大的動力。我會持續(xù)輸出更多內(nèi)容,敬請期待。
原文:https://medium.com/@mbianoubradon/how-to-build-a-fully-responsive-popular-collection-list-using-tailwindcss-1aa02034fec6
作者:Mbianou Bradon
非直接翻譯,有自行改編和添加部分,翻譯水平有限,難免有疏漏,歡迎指正
文由ScriptEcho平臺提供技術(shù)支持
項(xiàng)目地址:傳送門
本段代碼適用于需要創(chuàng)建響應(yīng)式網(wǎng)格布局的場景,例如展示產(chǎn)品列表、用戶資料或任何需要靈活排列內(nèi)容的情況。通過使用 Tailwind CSS,可以輕松創(chuàng)建可在各種設(shè)備上自適應(yīng)的卡片。
這段代碼使用 Tailwind CSS 構(gòu)建了一個響應(yīng)式卡片布局,其中包含六張卡片,每張卡片顯示一個標(biāo)題、金額和一個按鈕。卡片排列在網(wǎng)格中,根據(jù)屏幕尺寸自動調(diào)整大小和排列方式。
1. HTML 結(jié)構(gòu)
<section class="container px-5 py-24 mx-auto">
<div class="flex flex-wrap -m-4 text-center">
<!-- 卡片容器 -->
</div>
</section>
2. 卡片元素
<div class="p-4 sm:w-1/2 lg:w-1/3 w-full hover:scale-105 duration-500">
<div class="flex items-center justify-between p-4 rounded-lg bg-white shadow-indigo-50 shadow-md">
<!-- 卡片內(nèi)容 -->
</div>
</div>
3. 卡片內(nèi)容
<h2 class="text-gray-900 text-lg font-bold">Total Ballance</h2>
<h3 class="mt-2 text-xl font-bold text-yellow-500 text-left">+ 150.000 ?</h3>
<p class="text-sm font-semibold text-gray-400">Last Transaction</p>
<button class="text-sm mt-6 px-4 py-2 bg-yellow-400 text-white rounded-lg tracking-wider hover:bg-yellow-300 outline-none">Add to cart</button>
4. 響應(yīng)式設(shè)計(jì)
@media (min-width: 640px) {
.sm\:w-1/2 {
width: 50%;
}
}
@media (min-width: 1024px) {
.lg:w-1/3 {
width: 33.33%;
}
}
開發(fā)這段代碼的過程讓我加深了對 Tailwind CSS 的理解,以及如何使用它來構(gòu)建響應(yīng)式布局。通過靈活的類名和媒體查詢,Tailwind CSS 允許輕松創(chuàng)建適應(yīng)各種設(shè)備的動態(tài)界面。
未來,該卡片布局可以進(jìn)一步拓展和優(yōu)化,例如:
獲取更多Echos
本文由ScriptEcho平臺提供技術(shù)支持
項(xiàng)目地址:傳送門
微信搜索ScriptEcho了解更多
*請認(rèn)真填寫需求信息,我們會在24小時內(nèi)與您取得聯(lián)系。