问题
I need to make a div that looks like this:

With text in the middle in css. I tried looking at transforms and other 3d stuff, but I couldn't figure it out, especially without ruining the text.
回答1:
You can use a skewed Y pseudo element as a background of the element. This won't affect the content :
div {
position: relative;
display: inline-block;
padding: 20px 50px;
overflow: hidden;
}
div:before {
content: '';
position: absolute;
top: 0; left: 0;
width: 100%; height: 100%;
background: #FFD505;
-webkit-transform-origin: 0 0;
-ms-transform-origin: 0 0;
transform-origin: 0 0;
-webkit-transform: skewY(5deg);
-ms-transform: skewY(5deg);
transform: skewY(5deg);
z-index: -1;
border-radius: 10px 10px 0 0;
}
<div>Some text</div>
- The
div
has overflow:hidden so that the overflowing parts of the pseudo-element are hidden. - The pseudo element has a negative
z-index
so it stacks behind the content of thediv
- The
transform-origin
is set to0 0
so that top left of the skewed pseudo element doesn't move.
回答2:
you could use a skew'ed pseudo element for this, which ensures the text won't be skewd as well:
.wrap {
height: 100px;
width: 400px;
position: relative;
overflow: hidden;
padding-top: 30%;
}
.wrap:before {
content: "";
position: absolute;
border-radius: 5px;
height: 100%;
width: 100%;
left: 0;
top: 20%;
background: tomato;
transform: skewY(5deg);
z-index: -2;
}
<div class="wrap">hello
</div>
来源:https://stackoverflow.com/questions/29073804/rounded-skewed-top-shape-in-css