CSS Shorthand vs Longhand Properties Explained with Simple Examples

Learn CSS shorthand and longhand properties with simple examples. This guide helps frontend developers understand how shorthand works, when to use longhand, and best practices for writing clean and maintainable CSS.

3 min read
shorthand vs longhand properties

๐ŸŽจ 1. Background

Shorthand

.box {
  background: red url(bg.png) center/cover no-repeat fixed;
}

Longhand

.box {
  background-color: red;
  background-image: url(bg.png);
  background-position: center;
  background-size: cover;
  background-repeat: no-repeat;
  background-attachment: fixed;
}

๐Ÿ–‹๏ธ 2. Font

Shorthand

.text {
  font: italic bold 16px/1.5 Arial, sans-serif;
}

Longhand

.text {
  font-style: italic;
  font-weight: bold;
  font-size: 16px;
  line-height: 1.5;
  font-family: Arial, sans-serif;
}

โœ๏ธ 3. Text Decoration

Shorthand

a {
  text-decoration: underline dotted red;
}

Longhand

a {
  text-decoration-line: underline;
  text-decoration-style: dotted;
  text-decoration-color: red;
}

๐Ÿ“ฆ 4. Margin

Shorthand

.card {
  margin: 10px 20px;
}

Longhand

.card {
  margin-top: 10px;
  margin-right: 20px;
  margin-bottom: 10px;
  margin-left: 20px;
}

๐Ÿ“ฆ 5. Padding

Shorthand

.box {
  padding: 10px 15px 20px;
}

Longhand

.box {
  padding-top: 10px;
  padding-right: 15px;
  padding-bottom: 20px;
  padding-left: 15px;
}

๐Ÿงฑ 6. Border

Shorthand

.button {
  border: 2px solid black;
}

Longhand

.button {
  border-width: 2px;
  border-style: solid;
  border-color: black;
}

๐Ÿงฑ 7. Border Radius

Shorthand

.avatar {
  border-radius: 50%;
}

Longhand

.avatar {
  border-top-left-radius: 50%;
  border-top-right-radius: 50%;
  border-bottom-right-radius: 50%;
  border-bottom-left-radius: 50%;
}

๐Ÿ“ 8. Outline

Shorthand

input:focus {
  outline: 2px dashed blue;
}

Longhand

input:focus {
  outline-width: 2px;
  outline-style: dashed;
  outline-color: blue;
}

๐Ÿ“ 9. Inset

Shorthand

.modal {
  inset: 0;
}

Longhand

.modal {
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
}

๐ŸŽž๏ธ 10. Animation

Shorthand

.loader {
  animation: spin 1s linear infinite;
}

Longhand

.loader {
  animation-name: spin;
  animation-duration: 1s;
  animation-timing-function: linear;
  animation-iteration-count: infinite;
}

๐ŸŽฅ 11. Transition

Shorthand

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

Longhand

.button {
  transition-property: all;
  transition-duration: 0.3s;
  transition-timing-function: ease-in-out;
}

๐Ÿงฉ 12. Flex

Shorthand

.item {
  flex: 1;
}

Longhand

.item {
  flex-grow: 1;
  flex-shrink: 1;
  flex-basis: 0%;
}

๐Ÿ“ 13. Flex Flow

Shorthand

.container {
  flex-flow: row wrap;
}

Longhand

.container {
  flex-direction: row;
  flex-wrap: wrap;
}

๐Ÿงฎ 14. Grid

Shorthand

.grid {
  grid: auto-flow / 1fr 1fr;
}

Longhand

.grid {
  grid-auto-flow: row;
  grid-template-columns: 1fr 1fr;
}

๐Ÿงฎ 15. Grid Area

Shorthand

.item {
  grid-area: 1 / 2 / 3 / 4;
}

Longhand

.item {
  grid-row-start: 1;
  grid-column-start: 2;
  grid-row-end: 3;
  grid-column-end: 4;
}

๐Ÿ“„ 16. List Style

Shorthand

ul {
  list-style: square inside;
}

Longhand

ul {
  list-style-type: square;
  list-style-position: inside;
}

๐Ÿ“ 17. Columns

Shorthand

.article {
  columns: 3 200px;
}

Longhand

.article {
  column-count: 3;
  column-width: 200px;
}

๐Ÿงญ 18. Place Items

Shorthand

.container {
  place-items: center;
}

Longhand

.container {
  align-items: center;
  justify-items: center;
}

๐Ÿงญ 19. Place Content

Shorthand

.container {
  place-content: space-between center;
}

Longhand

.container {
  align-content: space-between;
  justify-content: center;
}

๐Ÿงญ 20. Place Self

Shorthand

.item {
  place-self: end;
}

Longhand

.item {
  align-self: end;
  justify-self: end;
}

Final Thoughts

Shorthand CSS makes your code shorter and faster to write, but longhand CSS gives you better control. Once you understand how shorthand properties work behind the scenes, it becomes easier to choose the right one for each situation. Practicing both approaches will help you write CSS that is simple, clear, and easy to maintain.

If you enjoyed this post or have any feedback, feel free to connect with me:

  • ๐Ÿ’ผ LinkedIn โ€” Letโ€™s network and share ideas!
  • ๐Ÿ‘จโ€๐Ÿ’ป GitHub โ€” Check out more of my projects and code examples.