DEV Community

Pandeyashish17
Pandeyashish17

Posted on • Updated on

Mastering CSS: Essential Skills and Techniques

CSS (Cascading Style Sheets) is a stylesheet language used to describe the look and formatting of a document written in HTML. It is a widely used language on the web and is an essential tool for web developers and designers. While it is relatively easy to learn and use, there are many advanced techniques and tricks that can help you create more dynamic and stylish websites. In this article, we will go over some of the most useful CSS tricks that you can use in your projects.

1) Using CSS Variables
CSS variables, also known as custom properties, are a relatively new feature that allows you to define variables in your CSS code. These variables can then be used throughout your stylesheet, making it easier to maintain and update your styles. For example, you could define a variable for your brand's primary color and use it throughout your stylesheet instead of hardcoding the color value every time. This way, if you need to change the color, you only need to update the value of the variable in one place.

To define a CSS variable, you can use the -- prefix followed by the name of the variable. For example:

:root {
  --primary-color: #00b8d4;
}
Enter fullscreen mode Exit fullscreen mode

To use the variable, you can use the var() function. For example:

body {
  color: var(--primary-color);
}
Enter fullscreen mode Exit fullscreen mode

CSS variables are a powerful tool that can help you create more modular and maintainable stylesheets.

**2) Using the CSS calc() Function
**The calc() function is a powerful tool that allows you to perform mathematical calculations in your CSS. This can be useful for calculating dimensions, positions, and other values that depend on other values. For example, you could use the calc() function to set the width of an element to be the width of its parent element minus 20 pixels:

div {
  width: calc(100% - 20px);
}

Enter fullscreen mode Exit fullscreen mode

The calc() function can also be used with variables. For example:

:root {
  --sidebar-width: 250px;
}

main {
  width: calc(100% - var(--sidebar-width));
}
Enter fullscreen mode Exit fullscreen mode

The calc() function is a useful tool for creating more flexible and dynamic layouts.

3) Using the :not() Pseudo-Class
The :not() pseudo-class is a useful tool for selecting elements that do not match a particular selector. For example, you could use it to select all elements that are not checked checkboxes:

input[type="checkbox"]:not(:checked) {
  /* styles for unchecked checkboxes */
}
Enter fullscreen mode Exit fullscreen mode

You can also use the :not() pseudo-class with other pseudo-classes or attributes. For example, you could use it to select all links that do not have the .active class:

a:not(.active) {
  /* styles for inactive links */
}
Enter fullscreen mode Exit fullscreen mode

The :not() pseudo-class is a powerful tool for fine-tuning your selections and applying styles to specific elements.

4) Using the :empty Pseudo-Class
The :empty pseudo-class allows you to select elements that do not have any children. This can be useful for applying styles to empty containers or hiding them if they are not needed. For example, you could use it to hide an empty list:

ul:empty {
  display: none;
}
Enter fullscreen mode Exit fullscreen mode

You can also use the :empty pseudo-class in combination with the :not() pseudo-class to select elements that have children. For example, you could use it to select all non-empty list items:

li:not(:empty) {
  /* styles for non-empty list items */
}
Enter fullscreen mode Exit fullscreen mode

5) Using the :root Pseudo-Class
The :root pseudo-class allows you to target the root element of the document, which is the html element. This can be useful for defining global styles or setting variables that can be used throughout the document. For example, you could use it to set the default font size for the document:

:root {
  font-size: 16px;
}
Enter fullscreen mode Exit fullscreen mode

You can also use the :root pseudo-class in combination with the var() function to set and use global variables. For example:

:root {
  --default-font-size: 16px;
}

body {
  font-size: var(--default-font-size);
}
Enter fullscreen mode Exit fullscreen mode

6) Using the rem Unit
The rem unit is a relative unit that is based on the root element's font size. It is a useful tool for creating scalable and responsive layouts. For example, you could use it to set the font size of all headings to be 1.5 times the root element's font size:

h1 {
  font-size: 1.5rem;
}

h2 {
  font-size: 1.3rem;
}

h3 {
  font-size: 1.2rem;
}
Enter fullscreen mode Exit fullscreen mode

You can also use the rem unit in combination with the calc() function to create more dynamic layouts. For example, you could use it to set the width of an element to be 80% of the viewport width minus 2 times the root element's font size:

div {
  width: calc(80% - 2rem);
}
Enter fullscreen mode Exit fullscreen mode

7) Using the @media Rule
The @media rule allows you to apply styles based on the conditions of the device, such as the screen width or resolution. This is a powerful tool for creating responsive layouts that adjust to different screen sizes. For example, you could use it to apply a different layout for mobile devices:

@media (max-width: 768px) {
  /* styles for mobile devices */
}
Enter fullscreen mode Exit fullscreen mode

You can also use the @media rule in combination with the rem unit to create scalable layouts that adjust to different screen sizes. For example, you could use it to set the font size of the body to be 16 pixels on desktop devices and 14 pixels on mobile devices:

@media (min-width: 769px) {
  body {
    font-size: 16px;
  }
}

@media (max-width: 768px) {
  body {
    font-size: 14px;
  }
}
Enter fullscreen mode Exit fullscreen mode

8) Using the flex Display Property
The flex display property is a powerful tool for creating flexible and responsive layouts. It allows you to specify how elements should be positioned within a container, and how they should adjust to different screen sizes and devices.

To use the flex property, you need to set the display property of the container element to flex and specify the flex-direction, justify-content, and align-items properties as needed. For example, you could use it to create a horizontal navigation bar:

nav {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
}
Enter fullscreen mode Exit fullscreen mode

You can also use the flex property in combination with the @media rule to create responsive layouts that adjust to different screen sizes. For example, you could use it to create a vertical navigation bar on mobile devices:

@media (max-width: 768px) {
  nav {
    flex-direction: column;
  }
}
Enter fullscreen mode Exit fullscreen mode

9) Using the transition Property
The transition property allows you to specify the transition effect that should be applied to an element when its state changes. This can be useful for creating smooth and subtle animations on hover or when an element is clicked. For example, you could use it to fade in a menu when it is hovered over:

.menu {
  transition: opacity 0.3s ease-in-out;
  opacity: 0;
}

.menu:hover {
  opacity: 1;
}
Enter fullscreen mode Exit fullscreen mode

You can also use the transition property in combination with the transform property to create more advanced animations. For example, you could use it to rotate an element when it is clicked:

.button {
  transition: transform 0.3s ease-in-out;
}

.button:active {
  transform: rotate(45deg);
}
Enter fullscreen mode Exit fullscreen mode

10) Using the @keyframes Rule
The @keyframes rule allows you to define custom animations that can be applied to elements using the animation property. This is a powerful tool for creating advanced and complex animations.

To use the @keyframes rule, you need to define the keyframes of the animation using percentages or the from and to keywords. You can then specify the properties that should be animated and their values at each keyframe. For example, you could use it to create a simple fade-in animation:

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

.element {
  animation: fadeIn 0.3s ease-in-out;
}
Enter fullscreen mode Exit fullscreen mode

You can also use the @keyframes rule in combination with the animation-delay property to create more advanced animations. For example, you could use it to create a bouncing animation:

@keyframes bounce {
  0%, 20%, 50%, 80%, 100% {
    transform: translateY(0);
  }
  40% {
    transform: translateY(-30px);
  }
  60% {
    transform: translateY(-15px);
  }
}
Enter fullscreen mode Exit fullscreen mode

11) Using the opacity Property
The opacity property allows you to specify the transparency of an element. This can be useful for creating subtle hover effects or for hiding elements temporarily. For example, you could use it to fade out an element when it is clicked:

.element {
  transition: opacity 0.3s ease-in-out;
}

.element:active {
  opacity: 0;
}
Enter fullscreen mode Exit fullscreen mode

You can also use the opacity property in combination with the pointer-events property to create transparent elements that are still clickable. For example, you could use it to create a semi-transparent overlay:

.overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease-in-out;
}

.overlay:target {
  opacity: 1;
  pointer-events: all;
}
Enter fullscreen mode Exit fullscreen mode

12) Using the box-shadow Property
The box-shadow property allows you to add a shadow to an element. This can be useful for creating depth and dimension in your designs. For example, you could use it to add a subtle shadow to a button:

button {
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
Enter fullscreen mode Exit fullscreen mode

You can also use the box-shadow property in combination with the :hover pseudo-class to create hover effects. For example, you could use it to create a button that becomes raised on hover:

button {
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  transition: box-shadow 0.3s ease-in-out;
}

button:hover {
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
Enter fullscreen mode Exit fullscreen mode

13) Using the text-shadow Property
The text-shadow property allows you to add a shadow to text. This can be useful for creating subtle text effects or for making text more legible on certain backgrounds. For example, you could use it to add a subtle shadow to headings:

h1 {
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
Enter fullscreen mode Exit fullscreen mode

You can also use the text-shadow property in combination with the :hover pseudo-class to create hover effects. For example, you could use it to create a glowing effect on hover:

h1 {
  transition: text-shadow 0.3s ease-in-out;
}

h1:hover {
  text-shadow: 0 0 8px rgba(255, 255, 255, 0.5);
}
Enter fullscreen mode Exit fullscreen mode

14) Using the :before and :after Pseudo-Elements
The :before and :after pseudo-elements allow you to insert content before or after an element's content. This can be useful for adding icons, decorative elements, or other types of content to your HTML without having to add extra elements to the DOM.

To use the :before and :after pseudo-elements, you need to use the content property to specify the content that you want to insert. For example, you could use it to add a decorative element before a heading:

h1:before {
  content: "";
  display: inline-block;
  width: 10px;
  height: 10px;
  background-color: red;
  margin-right: 10px;
}

Enter fullscreen mode Exit fullscreen mode

You can also use the :before and :after pseudo-elements in combination with the position, top, left, and transform properties to create more advanced designs. For example, you could use it to create a tool tip:

.tooltip {
  position: relative;
}

.tooltip:before {
  content: attr(data-tooltip);
  position: absolute;
  top: calc(-100% - 10px);
  left: 0;
  padding: 10px;
  background-color: rgba(0, 0, 0, 0.8);
  color: white;
  font-size: 12px;
  white-space: nowrap;
  transform: translateX(-50%);
  visibility: hidden;
  pointer-events: none;
}

.tooltip:hover:before {
  visibility: visible;
}
Enter fullscreen mode Exit fullscreen mode

Conclusion
CSS is a powerful tool for styling and formatting web documents. There are many advanced techniques and tricks that can help you create more dynamic and stylish websites. By using CSS variables, the calc() function, pseudo-classes and pseudo-elements, the rem unit, the @media rule, the flex display property, the transition property, the @keyframes rule, the opacity property, the box-shadow and text-shadow properties, the position property, and the :before and :after pseudo-elements, you can create more flexible and responsive layouts, add animations and transitions, and create more advanced designs.

Top comments (0)