闽公网安备 35020302035485号
<div className="bg-white rounded-lg shadow-md p-6 m-4"> <h2 className="text-xl font-bold mb-2">Card Title</h2> <p className="text-gray-700">Card content goes here.</p> </div>这种方式初看起来确实简洁高效,但随着项目的推进,问题逐渐显现。
<div className="flex flex-col md:flex-row items-center justify-between p-4 bg-white rounded-lg shadow-md hover:shadow-lg transition-shadow duration-300 ease-in-out">
<div className="flex items-center mb-4 md:mb-0">
<img className="w-10 h-10 rounded-full mr-4" src="/avatar.jpg" alt="User avatar" />
<div>
<h3 className="text-lg font-semibold text-gray-800">John Doe</h3>
<p className="text-sm text-gray-600">Software Developer</p>
</div>
</div>
<button className="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600 transition-colors duration-300 ease-in-out">
Follow
</button>
</div>
这种代码不仅难以阅读,更糟糕的是,它开始影响应用的性能。
const Card = ({ title, content }) => (
<div className={styles.card}>
<h2 className={styles.title}>{title}</h2>
<p className={styles.content}>{content}</p>
</div>
);
对应的SASS文件:.card {
background-color: white;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
padding: 1.5rem;
margin: 1rem;
/* 堆代码 duidaima.com */
.title {
font-size: 1.25rem;
font-weight: bold;
margin-bottom: 0.5rem;
}
.content {
color: #4a5568;
}
}
这种方式不仅提高了代码的可读性,也大大降低了CSS的体积,提升了应用性能。灵活选择技术栈:对于大型应用,SASS配合CSS Modules可能是更好的选择。