Cascading style sheet

Create Diagonal Stripes in CSS

We generally encounter striped design in most websites, magazines and flyers. But a diagonal stripe in CSS? How is that even possible?

Yes, it is possible and we are not using a bitmap image or an SVG. We would just rely on CSS’s linear gradient and background-size property.

So let’s begin.

<body style="background: repeating-linear-gradient(45deg,
green 0, green 25%, #58a 0, #58a 50%);
background-size: 42.426406871px 42.426406871px;">
<p>Your text here </p>
</body>

Let’s understand how this worked out.

To understand how we were able to create the diagonal stripe, we need to remember the Pythagorean theorem we learned at school about calculating the lengths of the sides of right triangles.

The theorem states that the hypotenuse (the longest, diagonal side of the triangle) is equal to a2 + b2 where a and b are the lengths of its legs. On a 45° right triangle, both its legs are of the same length, so the formula becomes 2a2 = a 2.

In our diagonal stripes, the background size specifies the length of the hypotenuse, but the stripe width is actually the length of the leg.

This means that to get our original stripe width of 15px, we need to specify a background size of 2 × 15 2 ≈ 42 . 426406871 pixels, which we can safely round off to 42px.

Recent Posts

South Korea bans downloads of DeepSeek AI until further notice

The South Korean government announced on Monday that it had temporarily halted new downloads of…

5 days ago

Which programming language is best for software development?

As a software developer, choosing the right programming language for software development can be a…

1 week ago

What is a server farm?

A server farm, also known as a server cluster or data center, is a collection…

1 week ago

Elon Musk’s Starlink satellite internet service is awaiting authorization to begin operations in Pakistan.

Pakistan's mobile and broadband internet speeds rank in the bottom 10 percent globally, according to…

2 weeks ago

React Native Styling Guide

React Native is a popular framework for building cross-platform mobile applications using JavaScript and React.…

2 weeks ago

You can now customize your Android home screen shortcut with any widget of your choice

Google is not only passionate about developing innovative apps and services but also about finding…

2 weeks ago