20 Basic CSS Tips for Designers – Hongkiat

Please enable JavaScript in your browser to enjoy a better experience.

10 Common CSS Mistakes Developers Often Make

10 Common CSS Mistakes Developers Often Make

How to avoid these common pitfalls to write cleaner, more efficient CSS code. Read more

1. Use CSS Reset

CSS Reset helps to reduce browser inconsistencies by providing a clean slate for styling elements. For example, you can use the following CSS reset by Eric Meyer:

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary,
time, mark, audio, video { margin: 0; padding: 0; border: 0; font-size: 100%; font: inherit; vertical-align: baseline;
}
2. Use Shorthand Properties

Shorthand properties can help you write cleaner code and save time. For example, instead of writing:

margin-top: 10px;
margin-right: 20px;
margin-bottom: 10px;
margin-left: 20px;

You can write:

margin: 10px 20px;
3. Use CSS Variables

CSS variables, also known as custom properties, allow you to store specific values to reuse throughout your CSS.

Example:

:root { --main-color: #c06c84;
} body { background-color: var(--main-color);
}
4. Use Flexbox and Grid for Layouts

Flexbox and Grid are powerful layout systems in CSS. They can make creating complex layouts easier. For example, to create a simple flex container:

.container { display: flex;
}
5. Use Media Queries for Responsive Design

Media queries allow you to apply different styles for different devices or screen widths.

Example:

@media (max-width: 600px) { body { background-color: lightblue; }
}
6. Use the Cascade to Your Advantage

CSS stands for Cascading Style Sheets, and understanding how the cascade works can help you write more efficient code. For example, you can set global styles and then override them for specific elements or components.

7. Understand Specificity

CSS specificity determines which CSS rule is applied by the browsers. It’s calculated based on the number of ID selectors, class selectors, and type selectors. For example, an ID selector has a higher specificity than class selectors and type selectors.

8. Use Pseudo-classes and Pseudo-elements

Pseudo-classes and pseudo-elements allow you to style specific parts of an element. For example, you can style the hover state of a button:

button:hover { background-color: blue;
}
9. Use CSS Functions

CSS functions can be used to make your code more dynamic. For example, the calc() function can be used to calculate values:

.element { width: calc(100% - 80px);
}
10. Use Comments to Organize Your Code

Comments can help you and others understand your code better. They can be used to section off parts of your CSS for easier navigation.

Example:

/* This is a comment */
body { background-color: white;
}
11. Use the Box Model

The CSS box model is the foundation of layout design in CSS. It consists of margins, borders, padding, and the actual content.

Example:

.box { width: 300px; border: 15px solid green; padding: 50px; margin: 20px;
}
12. Understand Positioning

CSS positioning properties (static, relative, fixed, absolute, and sticky) allow you to control the layout of elements. For example, to position an element relative to its normal position:

.element { position: relative; top: 20px; left: 30px;
}
13. Use em and rem Units

em and rem are scalable units in CSS. em is relative to the font-size of its closest parent, while rem is relative to the root element’s font-size.

Example:

.element { font-size: 2em; /* 2 times the font-size of the parent element */
}
14. Use :before and :after Pseudo-elements

These pseudo-elements can be used to insert content before or after an element’s content.

Example:

.element:before { content: "Before";
} .element:after { content: "After";
}
15. Use Vendor Prefixes for Cross-Browser Compatibility

Vendor prefixes ensure that CSS properties work across different browsers.

Example:

.element { -webkit-transform: rotate(30deg); -moz-transform: rotate(30deg); -ms-transform: rotate(30deg); transform: rotate(30deg);
}
16. Use transition for Smooth Animations

The transition property can animate changes in CSS properties over a specified duration.

Example:

.element { transition: background-color 0.5s ease;
} .element:hover { background-color: red;
}
17. Use rgba for Transparency

The rgba function can be used to set colors with alpha transparency.

Example:

.element { background-color: rgba(255, 0, 0, 0.5); /* semi-transparent red */
}
18. Use transform for 2D and 3D Transformations

The transform property can be used to rotate, scale, skew, or translate an element.

Example:

.element { transform: rotate(30deg);
}
19. Use @import to Include External CSS

The @import rule allows you to include external CSS files.

Example:

@import url("styles.css");
20. Use !important Sparingly

The !important rule overrides other declarations, but it can make debugging difficult because it breaks the natural cascading in your stylesheets. Use it sparingly and only when necessary.

Example:

.element { color: red !important;
}