Home > Web Front-end > CSS Tutorial > How to Create a Responsive Slanted Edge for a Div Without Obscuring Underlying Images?

How to Create a Responsive Slanted Edge for a Div Without Obscuring Underlying Images?

Mary-Kate Olsen
Release: 2024-11-19 01:40:02
Original
494 people have browsed it

How to Create a Responsive Slanted Edge for a Div Without Obscuring Underlying Images?

Create a Responsive Slanted Edge for a Div

Problem:

Creating a slanted edge for a div without using borders that will obscure underlying images.

Solution:

Slanted Pseudo Element:

Utilize a skewed pseudo element to create the slanted background while preserving the text content above it.

Transform Origin:

Set the transform origin of the pseudo element to the desired anchor point for the slant (right bottom corner in this case).

Skew Transformation:

Apply a skew transformation to the pseudo element to angle it accordingly (e.g., -45 degrees).

Overflow:

Use overflow: hidden; to hide any overflowing parts of the pseudo element.

Negative Z-Index:

Position the pseudo element behind the div content using a negative z-index.

Example Code:

div {
  position: relative;
  display: inline-block;
  padding: 1em 5em 1em 1em;
  overflow: hidden;
  color: #fff;
}

div:after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: #000;
  -webkit-transform-origin: 100% 0;
  -ms-transform-origin: 100% 0;
  transform-origin: 100% 0;
  -webkit-transform: skew(-45deg);
  -ms-transform: skew(-45deg);
  transform: skew(-45deg);
  z-index: -1;
}

body {
  background: url('https://farm3.staticflickr.com/2878/10944255073_973d2cd25c.jpg');
  background-size: cover;
}
Copy after login

Result:

This solution enables you to create responsive slanted edges for divs that won't affect their text content.

The above is the detailed content of How to Create a Responsive Slanted Edge for a Div Without Obscuring Underlying Images?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template