Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS shape before div

Tags:

html

css

I have a problem with adding an irregular shape before div.

I need to get effect as in this image:

CSS shape before div

But I need to get this effect for whole content div. Not only left or bottom. I tried to adapt css's arrow made with ​​border but it does not give me the correct effect.

Is it better to use the borders with combination of transparent color and ::before, ::after pseudo elements, or background-clip property.

My HTML structure

.wrapper::before {
  display: block;
  height: 0px;
  width: 0px;
  border-left: 5px solid transparent;
  border-right: 650px solid transparent;
  border-bottom: 50px solid black;
}
.content {
  background-color: #f2f2f2;
  box-sizing: border-box;
  color: #000;
  text-transform: uppercase;
}
<div class="wrapper">
  <div class="content">
    Content
  </div>
</div>

Is there a way to combine ::after and ::before pseudoelements to add this effect to whole div?

like image 330
housesoul Avatar asked Jun 01 '16 07:06

housesoul


People also ask

What is :: before and :: after?

The ::before selector inserts something before the content of each selected element(s). Use the content property to specify the content to insert. Use the ::after selector to insert something after the content.

What is :: before in CSS?

::before. In CSS, ::before creates a pseudo-element that is the first child of the selected element. It is often used to add cosmetic content to an element with the content property.

What is a pseudo-element in CSS?

A CSS pseudo-element is a keyword added to a selector that lets you style a specific part of the selected element(s). For example, ::first-line can be used to change the font of the first line of a paragraph. /* The first line of every <p> element. */ p::first-line { color: blue; text-transform: uppercase; }


3 Answers

Due to the irregular shape, this is going to be tricky, maybe border-image would be an easier solution. If the blue background may be based on a rectangular shape, you can easily do it with a single pseudo-element using skew and rotation transforms, e.g.

.wrapper {
  padding: 50px;
  position: relative;
}
.content {
    background-color: #f2f2f2;
    box-sizing: border-box;
    color: #000;
    text-transform: uppercase;
    width: 300px;
    height: 200px;
    position: relative;
    z-index: 3;
    text-align: center;
    line-height: 200px;
}
.wrapper::before {
  content: '';
  display: block;
  background: rgb(145,205,239);
  width: 330px;
  height: 230px;
  position: absolute;
  z-index: 1;
  margin: -15px;
  transform: rotate(-1deg) skew(-5deg, -3deg);
}
<div class="wrapper">
    <div class="content">
CONTENT
    </div>
</div>
like image 95
Paul Avatar answered Oct 24 '22 08:10

Paul


I have example for your layout, you can custom it and apply your code.

.content {
  background-color: #f2f2f2;
  box-sizing: border-box;
  color: #000;
  text-transform: uppercase;
  width: 100%;
  height: 100%;
  transform: skew(3deg, 5deg);
  -webkit-transform: skew(3deg, 5deg);
  -moz-transform: skew(3deg, 5deg);
  -o-transform: skew(3deg, 5deg);
  -ms-transform: skew(3deg, 5deg);
  position: relative;  
}
.wrapper {
  width: 400px;
  height: 222px;
  margin: 50px;
  padding: 25px 10px;
  background-color: blue;
  transform: skew(-3deg, -5deg);
  -webkit-transform: skew(-3deg, -5deg);
  -moz-transform: skew(-3deg, -5deg);
  -o-transform: skew(-3deg, -5deg);
  -ms-transform: skew(-3deg, -5deg);
  box-sizing: border-box;
}
.content-text {
  position: absolute;
  top: 50%;
  left: 50%;
  -webkit-transform: translate(-50%, -50%);
  -moz-transform: translate(-50%, -50%);
  -o-transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
}
<!DOCTYPE html>
<html lang="">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Title Page</title>
  </head>
  <body>
    <div class="wrapper">
      <div class="content">
        <p class="content-text">Content</p>
      </div>
    </div>
  </body>
</html>
like image 31
TOM Avatar answered Oct 24 '22 08:10

TOM


  1. <div class="wrapper"> is not required for this task.
  2. I've used transform: scale(1.1, 1.18) skew(-4deg,-1.5deg).

.content {
  background-color: #f2f2f2;
  color: #000;
  font-family:  Impact, "Trebuchet MS", sans-serif;
  text-transform: uppercase;

  float: left;
  margin: 40px;
  position: relative;

  width: 360px;
  height: 180px;
  line-height: 180px;
  text-align: center;
}

.content::before {
  content: '';
  background: #91cdef;
  display: block;
  width: 360px;
  height: 180px;

  position: absolute;
  transform: scale(1.1, 1.18) skew(-4deg,-1.5deg);
  z-index: -1;
}

img {
  float: left;
  margin: 19px;
}  
<div class="content">Content</div>
<div class="content">Content</div>
<div class="content">Content</div>
<div class="content">Content</div>

<img src="http://i.stack.imgur.com/9vJ92.png" alt="">
like image 1
Gleb Kemarsky Avatar answered Oct 24 '22 10:10

Gleb Kemarsky