Rounded skewed top shape in css

对着背影说爱祢 提交于 2019-12-12 05:56:07

问题


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 the div
  • The transform-origin is set to 0 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!