Modulares CSS mit PHP
- Modular CSS means splitting your styles into individual, reusable modules.
- Each module serves a specific purpose and can be used independently.
- You can easily adjust which CSS modules are included based on your needs.
How is the system structured?
CSS file structure:
config.css.php - Contains all variables and configuration settings
base.css.php - Basic styles for HTML elements
main.css.php - Collects all styles and generates the final CSS
components/* - Reusable UI components
layout/* - Layout-specific styles
pages/* - Page-specific styles
$cssModules = [
['base', 'comment', 'Basic styles for the page'],
['base', 'styles', renderBodyStyle()],
['h2', 'comment', 'Heading level 2 styles'],
['h2', 'styles', renderH2Style()]
];
- You can split your CSS into as many files as you need and structure it clearly.
- Each CSS section is defined in a function, making it easy to assemble the final CSS.
function renderH2Style() {
return [
'h2' => [
'margin-top' => '30px'
]
];
}
What else is important?
- Reusability: A module can be used in multiple projects.
- Clarity: Comments and structure make the code easier to understand.
- Maintainability: Changes can be made in a targeted way.
- Choose meaningful module names.
- Describe each module with a comment.
- Use helper functions for recurring styles.