使用CSS创建倾斜背景


倾斜背景是一种设计效果,它在网页或元素的背景上 tạo ra một kiểu dáng chéo hoặc nghiêng. 此效果可以使用CSS转换来倾斜容器元素,以及其他CSS属性,如background-color渐变和图像来创建所需的视觉效果。

方法一:使用CSS中的transform属性

算法

  • 将文档标题设置为“倾斜背景”。

  • 定义CSS变量用于倾斜角度和背景颜色

  • 使用CSS transform属性设置transform属性来倾斜背景

  • 使用CSS background属性设置线性渐变背景

  • 设置倾斜背景的填充和文本颜色

  • 定义倾斜背景内h1p元素的样式

  • 添加一个带有类名“skewed-background”的div元素作为倾斜背景

  • 在div内添加一个h1元素,文本为“欢迎来到Tutorialspoint”

  • 在div内添加一个p元素,文本为“这是一个使用CSS创建倾斜背景的示例”

示例

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>Skewed Background</title>
   <!-- Define CSS variables for skew angle and background colors -->
   <style>
      :root {
         --skew-angle: -10deg;
         --bg-color-1: #e0c3fc;
         --bg-color-2: #8ec5fc;
      }
      /* Skew the background using the CSS transform property */
      .skewed-background {
         transform: skewY(var(--skew-angle));
         /* Set a linear gradient background using the CSS background property */
         background: linear-gradient(110deg, var(--bg-color-1) 0%, var(--bg-color-2) 100%);
         padding: 50px;
         color: #000000;
      }
      /* Set styles for the h1 and p elements inside the skewed background */
      .skewed-background h1 {
         font-size: 36px;
         margin: 0;
      }
      .skewed-background p {
         font-size: 18px;
         margin: 20px 0 0;
      }
   </style>
</head>
<body>
   <!-- Add a div element with the class "skewed-background" for the skewed background -->
   <div class="skewed-background">
      <h1>Welcome to Tutorialspoint</h1>
      <p>This is an example of a skewed background using CSS</p>
   </div>
</body>
</html>

方法二:使用CSS中的clip-path属性

算法

  • 声明文档标题

  • 将body设置为无边距和填充。

  • 在body内创建一个类名为“skewed-background”的div。

  • 使用“vh”单位将div的高度设置为完整的视口高度。

  • 使用“linear-gradient”属性创建一个角度为110度的线性渐变背景。

  • 使用“clip-path”属性创建一个多边形形状,使背景看起来倾斜。

  • 向div添加填充,以在内容和倾斜背景边缘之间提供间距。

  • 使用“display: flex”、“justify-content: center”和“align-items: center”水平和垂直居中内容。

  • 添加标题和段落。

示例

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>Skewed Background using clip-path</title>
   <!-- Define CSS styles for the skewed background -->
   <style>
      body {
         margin: 0;
         padding: 0;
      }
      .skewed-background {
         height: 100vh;
         background: linear-gradient(110deg, #e0c3fc 0%, #8ec5fc 100%);
         clip-path: polygon(0 0, 100% 0, 100% 90%, 0 100%); /* Use clip-path to create a skewed shape */
         padding: 50px;
         color: #000;
         display: flex;
         justify-content: center;
         align-items: center;
         flex-direction: column;
      }
      .skewed-background h1 {
         font-size: 36px;
         margin: 0;
      }
      .skewed-background p {
         font-size: 18px;
         margin: 20px 0 0;
      }
   </style>
</head>
<body>
   <!-- Create a div element with the class "skewed-background" to hold the content of the skewed background -->
   <div class="skewed-background">
      <h1>Welcome to Tutorialspoint</h1>
      <p>This is an example of a skewed background using clip-path property in CSS.</p>
   </div>
</body>
</html>

方法三:使用CSS网格

算法

  • 使用CSS Grid定义一个包含2列1行的网格容器。

  • 将容器的高度设置为100vh,并隐藏任何溢出的内容。

  • 创建内容div和背景div作为容器中的两个网格项。

  • 将内容div定位在第一列和第一行,并将其背景颜色设置为白色,并添加文本填充。

  • 将背景div定位在第一列和第二列以及第一行,并将其水平倾斜-12deg。

  • 将倾斜div的背景设置为从#e0c3fc到#8ec5fc的线性渐变,并将其放置在内容div之后,z-index为-1。

  • 向内容div添加文本,包括标题和段落,以显示在倾斜背景之上。

示例

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>Skewed Background with CSS Grid</title>
   <style>
      /* Set margin and padding to zero */
      body {
         margin: 0;
         padding: 0;
      }
      /* Create a container using CSS Grid */
      .container {
         display: grid;
         grid-template-columns: 1fr 1fr;
         grid-template-rows: 1fr;
         grid-gap: 0;
         height: 100vh;
         overflow: hidden;
      }
      /* Set properties for the content */
      .container .content {
         grid-column: 1/2;
         grid-row: 1/2;
         background: white;
         padding: 20px;
         z-index: 1;
         position: relative;
      }
      /* Set properties for the background */
      .container .background {
         grid-column: 1/3;
         grid-row: 1/2;
         transform: skewX(-12deg);
         background: linear-gradient(110deg, #e0c3fc 0%, #8ec5fc 100%);
         z-index: -1;
         position: relative;
      }
   </style>
</head>
<body>
   <!-- Create the container and the content and background divs -->
   <div class="container">
      <div class="content">
         <h1>Welcome to Tutorialspoint</h1>
         <p>This is an example of a skewed background using CSS Grid</p>
      </div>
      <div class="background"></div>
   </div>
</body>
</html>

方法四:使用SVG

算法

  • 创建一个类名为“skewed-background”的div元素来包含SVG元素

  • 设置“skewed-background”类的CSS属性来设置其高度、宽度、位置和溢出

  • 在div内添加一个SVG元素,viewBox属性设置为“0 0 500 100”,preserveAspectRatio设置为“none”

  • 在SVG元素内添加一个path元素,其“d”属性设置为倾斜形状四个点的坐标

  • 设置“skewed-background svg”类的CSS属性,将SVG元素定位到div元素的左下角,宽度和高度为100%

  • 设置“skewed-background path”类的CSS属性,将path的填充颜色设置为#e0c3fc

  • 在div下方添加标题和段落元素以显示一些文本。

示例

<!DOCTYPE html>
<html>
<head>
   <title>Skewed Background using SVG</title>
   <style>
      /* Style for the parent container */
      .skewed-background {
         height: 300px;
         width: 100%;
         position: relative;
         overflow: hidden;
      }
      /* Style for the SVG element */
      .skewed-background svg {
         position: absolute;
         bottom: 0;
         left: 0;
         right: 0;
         width: 100%;
         height: 100%;
      }
      /* Style for the path element */
      .skewed-background path {
         fill: #e0c3fc;
      }
   </style>
</head>
<body>
   <!-- The container for the SVG element -->
   <div class="skewed-background">
      <!-- The SVG element that creates the skewed background -->
      <svg viewBox="0 0 500 100" preserveAspectRatio="none">
         <!-- The path element that creates the skewed shape -->
         <path d="M 0 0 L 500 0 L 500 100 L 0 50 Z" />
      </svg>
   </div>
   <h1>Welcome to Tutorialspoint</h1>
   <p>This is an example of a skewed background using SVG.</p>
</body>
</html>

结论

但是,使用clip-path方法可能难以创建更复杂的形状。网格间隙和溢出属性的使用会影响布局,可能需要调整才能达到预期的效果。以这种方式使用SVG可能会导致比其他方法更多的代码,并可能影响性能。

更新于:2023年11月12日

2K+ 次浏览

开启你的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.