DEV Community

Cover image for Useful CSS Shorthands
Matt Adil
Matt Adil

Posted on

1 1

Useful CSS Shorthands

Introduction

In the world of web development, CSS shorthands are powerful tools that allow developers to write cleaner, more concise code without sacrificing functionality. Understanding and mastering CSS shorthands can greatly improve your workflow and efficiency as a front-end developer. In this article, we'll explore various CSS shorthands with examples and code snippets to help you harness their full potential.

1. Font Shorthand:

Original:

p{
  font-size: 16px;
  font-family: Arial, sans-serif;
  font-style: italic;
  font-weight: bold;
  line-height: 1.5;
  font-variant: small-caps;
}

Enter fullscreen mode Exit fullscreen mode

Shorthand:

p {
  font: italic small-caps bold 16px/1.5 Arial, sans-serif;
}


Enter fullscreen mode Exit fullscreen mode

2. Margin and Padding Shorthands:

Original:

.box{
  margin-top: 10px;
  margin-right: 20px;
  margin-bottom: 30px;
  margin-left: 40px;

  padding-top: 15px;
  padding-right: 25px;
  padding-bottom: 15px;
  padding-left: 25px;
}

Enter fullscreen mode Exit fullscreen mode

Shorthand:

.box {
  margin: 10px 20px 30px 40px; /* top, right, bottom, left */
  padding: 15px 25px; /* top/bottom, right/left */
}

Enter fullscreen mode Exit fullscreen mode

3. Background Shorthand:

Original:

.container{
  background-image: url('background.jpg');
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
  background-color: #f0f0f0;
}

Enter fullscreen mode Exit fullscreen mode

Shorthand:

.container {
  background: url('background.jpg') no-repeat center/cover #f0f0f0;
}

Enter fullscreen mode Exit fullscreen mode

4. Border Shorthand:

Original:

.element{
  border-width: 2px;
  border-style: solid;
  border-color: #000;
}

Enter fullscreen mode Exit fullscreen mode

Shorthand:

.element {
  border: 2px solid #000;
}

Enter fullscreen mode Exit fullscreen mode

5. Animation Shorthand:

Original:

.box {
  animation-name: slide;
  animation-duration: 2s;
  animation-timing-function: ease-in-out;
  animation-iteration-count: infinite;
  animation-direction: alternate;
}

@keyframes slide {
  0% { transform: translateX(0); }
  100% { transform: translateX(100px); }
}

Enter fullscreen mode Exit fullscreen mode

Shorthand:

.box {
  animation: slide 2s ease-in-out infinite alternate;
}

@keyframes slide {
  0% { transform: translateX(0); }
  100% { transform: translateX(100px); }
}
Enter fullscreen mode Exit fullscreen mode

Conclusion:

Mastering CSS shorthands is essential for any front-end developer looking to streamline their code and improve productivity. By leveraging these powerful shortcuts, you can write cleaner, more efficient CSS while still achieving the desired visual effects. Experiment with different shorthands and incorporate them into your workflow to become a more proficient web developer.

Additional Resources:

MDN Web Docs: CSS Shorthand Properties

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

AWS GenAI LIVE!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️