Custom CSS by Luis Arenas v1.1.0
Custom CSS by Luis Arenas v1.1.0
v 1.1.0
Author: Luis Arenas.
Creating a custom CSS for your product is a critical step in developing a consistent and
brand-aligned user interface. This article will guide you through the process of creating a
custom CSS from scratch, focusing on implementing a dark theme, utilizing custom
variables, leveraging grid and flex systems, defining classes for components, and
adhering to good practices and useful tricks.
1. Base Colors: Start by defining a color palette that includes primary, secondary, and
accent colors. For a dark theme, choose colors that reduce eye strain and provide
good readability.
2. Contrast Ratios: Ensure text and background colors have a sufficient contrast ratio
to enhance readability. Tools like WebAIM's Contrast Checker can be useful.
4. Overridable: Make it easy to switch between dark and light themes, allowing users to
choose based on their preference.
:root {
--primary-color: #2979ff;
--secondary-color: #ffc107;
--font-size-normal: 16px;
--spacing-unit: 8px;
}
2. Usage: Use these variables throughout your CSS to maintain consistency and
facilitate changes.
body {
color: var(--primary-color);
font-size: var(--font-size-normal);
}
1. CSS Grid: Use Grid for complex, two-dimensional layouts. Define grid containers and
grid items, leveraging properties like grid-template-columns , grid-template-rows , and
grid-gap .
2. Flexbox: Use Flexbox for one-dimensional layouts. It's great for aligning items
horizontally or vertically, making it ideal for headers, footers, or sections where the
size of items might vary.
3. Responsive Design: Utilize media queries to adapt your grid and flex layouts to
different screen sizes, enhancing mobile responsiveness.
3. State Classes: Define classes for different states (e.g., active, disabled) to easily
toggle UI changes.
:root {
--primary-color: #007bff;
--primary-color-hover: #0056b3;
--secondary-color: #6c757d;
--secondary-color-hover: #545b62;
--text-color: #ffffff;
}
Here, we've defined two sets of colors for primary and secondary buttons, along with a
text color.
.button {
padding: 10px 20px;
font-size: 16px;
border-radius: 5px;
border: none;
cursor: pointer;
color: var(--text-color);
transition: background-color 0.3s;
}
This class sets up padding, font size, border radius, and other properties that are
common across all button types.
Extending the Base Class for Button Variants
Next, we extend the base class for specific button variants, like primary and secondary
buttons, using the defined color variables:
Primary Button
.button-primary {
background-color: var(--primary-color);
}
.button-primary:hover {
background-color: var(--primary-color-hover);
}
The .button-primary class modifies the background color for primary buttons and
changes it on hover, utilizing the color variables.
Secondary Button
.button-secondary {
background-color: var(--secondary-color);
}
.button-secondary:hover {
background-color: var(--secondary-color-hover);
}
Each button has two classes: the base .button class and a variant class ( .button-primary
or .button-secondary ) that applies the specific styles for that button type.
Good Practices
1. Maintainability: Write clean, concise, and well-commented CSS. Use a preprocessor
like Sass or LESS for better structure and reusability.
2. Performance: Optimize your CSS for performance. Minimize the use of expensive
properties like box-shadow and border-radius on large areas.
3. Cross-Browser Compatibility: Test your CSS across different browsers and devices
to ensure consistent user experiences.
1. CSS Variables in Media Queries: Use CSS variables within media queries to adjust
styles dynamically based on screen size.
3. Fallbacks: Provide fallback values for older browsers that may not support certain
CSS features.
4. DevTools: Use browser developer tools to debug and optimize your CSS, observing
how changes affect the layout and performance in real-time.
Conclusion
Creating a custom CSS for your product is an iterative and detailed process. By focusing
on implementing a user-friendly dark theme, utilizing CSS variables and component-
based classes, you can create a flexible and maintainable styling system for your
components. This approach not only keeps your styles consistent across the application
but also makes it easier to update colors or other style properties across all components
by changing a single value. Leveraging grid and flex systems for responsive layouts,
organizing your styles with component-based classes, and adhering to good practices,
you can create a robust and maintainable CSS framework that aligns with your product's
identity and provides a seamless user experience.