Home >Web Front-end >CSS Tutorial >How Can I Create a Slanted Background Using CSS?

How Can I Create a Slanted Background Using CSS?

DDD
DDDOriginal
2024-11-06 19:21:02921browse

How Can I Create a Slanted Background Using CSS?

Creating a Slanted Background with CSS

Problem:

You wish to create a slanted background, as illustrated in the provided image. Your current CSS implementation does not achieve the desired effect.

Solution:

To achieve a slanted background, you can utilize a pseudo element with a skew transformation:

body {
  height: 100vh;
  margin: 0;
  background: yellow;
}

body:before {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  width: 300px;
  background: #000;
  transform: skew(-30deg);
  transform-origin: top;
}

This technique creates a slanted element positioned at the top-left corner of the viewport. By controlling the width of the element and the skew angle, you can customize the slant according to your requirements.

The above is the detailed content of How Can I Create a Slanted Background Using CSS?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn