首页 web前端 css教程 Organize Your CSS Like a Pro: Logical Grouping of Properties

Organize Your CSS Like a Pro: Logical Grouping of Properties

Sep 19, 2024 am 06:25 AM

Organize Your CSS Like a Pro: Logical Grouping of Properties

Writing clean and well-organized CSS is important, especially for bigger projects. One way to achieve this is by grouping CSS properties in a logical way. In this article, I will show you how to organize your CSS using Logical Grouping, where positioning comes first. This will make your code easier to read and maintain.

Why Logical Grouping?

When writing CSS, we often add properties in a random order. But grouping them logically helps in these ways:

  • Readability: It’s easier to understand what each class does.
  • Consistency: Using the same order makes it easier to work with a team.
  • Maintenance: You can quickly find and update properties. Let’s first look at a bad example of CSS without logical grouping.

Bad Example: Unorganized CSS

.card {
    font-size: 16px;
    border: 1px solid #ddd;
    display: flex;
    justify-content: space-between;
    background-color: #fff;
    width: 300px;
    height: 400px;
    position: relative;
    line-height: 1.5;
    border-radius: 10px;
    padding: 20px;
    color: #333;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
    transition: transform 0.3s ease;
}
登录后复制

In this bad example, the properties are in a random order, which makes it harder to follow. There’s no clear structure, and it takes more time to find specific properties like position or background-color.
Now, let’s see how to fix this with Logical Grouping.

Four Main Groups

1. Positioning
These properties control how the element is positioned relative to other elements. Examples: position, top, right, bottom, left, and z-index.
2. Box Model
These properties control the layout, size, and spacing of elements. Examples: display, width, padding, and margin.
3. Typography and Text
These properties control the font, text size, and alignment. Examples: font-size, line-height, and text-align.
4. Visual Appearance
These properties control how an element looks. Examples: background-color, color, border, box-shadow, and transition.

Example: Flexbox Layout for a Card

Here’s how the card layout looks when we use logical grouping:

.card {
    /* Positioning */
    position: relative;
    z-index: 1;

    /* Box Model */
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    width: 300px;
    height: 400px;
    padding: 20px;

    /* Typography */
    font-size: 16px;
    line-height: 1.5;

    /* Visual Appearance */
    background-color: #fff;
    color: #333;
    border: 1px solid #ddd;
    border-radius: 10px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);

    /* Miscellaneous */
    transition: transform 0.3s ease;
}

.card:hover {
    transform: translateY(-5px);
}

登录后复制

In this good example, the properties are grouped in a clear way, making the code easier to follow and maintain.

Note: The comments in the CSS are only for explanation. Remove them in your actual code.

More Examples for Common Components

Responsive Image

.responsive-image {
    /* Positioning */
    position: relative;

    /* Box Model */
    display: block;
    width: 100%;
    max-width: 600px;
    height: auto;
    aspect-ratio: 16 / 9;

    /* Visual Appearance */
    background-color: #f0f0f0;
    border-radius: 8px;
    object-fit: cover;

    /* Miscellaneous */
    transition: transform 0.3s ease;
}
登录后复制

Button

.button-primary {
    /* Positioning */
    position: relative;

    /* Box Model */
    display: inline-block;
    padding: 10px 20px;

    /* Typography */
    font-size: 16px;
    text-align: center;

    /* Visual Appearance */
    background-color: #007BFF;
    color: white;
    border: none;
    border-radius: 5px;

    /* Miscellaneous */
    cursor: pointer;
    transition: background-color 0.3s ease;
}

.button-primary:hover {
    background-color: #0056b3;
}
登录后复制

Navigation Bar (Fixed)

.navbar {
    /* Positioning */
    position: fixed;
    top: 0;
    left: 0;
    z-index: 1000;

    /* Box Model */
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 20px;
    width: 100%;
    height: 60px;

    /* Typography */
    font-size: 18px;

    /* Visual Appearance */
    background-color: #333;
    color: white;
    border-bottom: 2px solid #555;
}
登录后复制

Here, positioning is defined first, followed by the box model, typography, and visual appearance.

Conclusion

Using Logical Grouping for your CSS properties helps you write clean and easy-to-maintain code. Placing positioning properties first makes it clearer how elements interact with each other on the page. Whether you work alone or in a team, this method will improve your CSS.
Try this approach in your next project and see how it helps!

References:
This article was inspired by Vinodan, N. (2020) 'Better ways to organise CSS properties' and my personal experience with frontend development practices.

以上是Organize Your CSS Like a Pro: Logical Grouping of Properties的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

<🎜>:泡泡胶模拟器无穷大 - 如何获取和使用皇家钥匙
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系统,解释
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆树的耳语 - 如何解锁抓钩
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

Java教程
1670
14
CakePHP 教程
1428
52
Laravel 教程
1329
25
PHP教程
1274
29
C# 教程
1256
24
静态表单提供商的比较 静态表单提供商的比较 Apr 16, 2025 am 11:20 AM

让我们尝试在这里造成一个术语:“静态表单提供商”。你带上html

使Sass更快的概念证明 使Sass更快的概念证明 Apr 16, 2025 am 10:38 AM

在一个新项目开始时,Sass汇编发生在眼睛的眨眼中。感觉很棒,尤其是当它与browsersync配对时,它重新加载

每周平台新闻:HTML加载属性,主要的ARIA规格以及从iframe转移到Shadow dom 每周平台新闻:HTML加载属性,主要的ARIA规格以及从iframe转移到Shadow dom Apr 17, 2025 am 10:55 AM

在本周的平台新闻综述中,Chrome引入了一个用于加载的新属性,Web开发人员的可访问性规范以及BBC Move

带有HTML对话框元素的一些动手 带有HTML对话框元素的一些动手 Apr 16, 2025 am 11:33 AM

这是我第一次查看HTML元素。我已经意识到了一段时间,但是尚未将其旋转。它很酷,

纸张形式 纸张形式 Apr 16, 2025 am 11:24 AM

购买或建造是技术的经典辩论。自己构建东西可能会感觉更便宜,因为您的信用卡账单上没有订单项,但是

'订阅播客”链接应在哪里? '订阅播客”链接应在哪里? Apr 16, 2025 pm 12:04 PM

有一段时间,iTunes是播客中的大狗,因此,如果您将“订阅播客”链接到喜欢:

托管您自己的非JavaScript分析的选项 托管您自己的非JavaScript分析的选项 Apr 15, 2025 am 11:09 AM

有很多分析平台可帮助您跟踪网站上的访问者和使用数据。也许最著名的是Google Analytics(广泛使用)

See all articles