简体   繁体   English

CSS3全宽梯形/多边形文字?

[英]CSS3 Full Width Trapezoid / Polygon with text?

I'm trying to redo a client site that's currently not responsive and throughout the site she has long images that are trapezoids with text inside. 我正在尝试重做一个当前无响应的客户端站点,并且在整个站点中,她的长图像都是梯形的,内部带有文本。 Of course, on devices, you can barely read it. 当然,在设备上,您几乎看不到它。

在此处输入图片说明

So I'm trying to turn it into CSS using shapes. 因此,我正在尝试使用形状将其转换为CSS。 Tried a bunch of examples but nothing working at the moment. 尝试了很多示例,但目前没有任何效果。 I think the difference is the examples seem to use hard width numbers instead of 100% for fluid width. 我认为区别在于示例似乎使用硬宽度数字而不是100%作为流体宽度。 I have a pen here: https://codepen.io/anon/pen/KmgoqE and here's the code I'm playing with as I post this (still playing, of course): 我在这里有一支笔: https : //codepen.io/anon/pen/KmgoqE ,这是我发布此代码时正在使用的代码(当然仍在播放):

h2.test-text {
  background: #000;
  color: #FFF;
  padding: 5px;
  font-size: 30px;
  line-height: 1;
  width: 100%;
  text-align: center;
  position: relative;
}

h2.test-text:before {
  content: "";
  position: absolute;
  border: none;
  top: -4%;
  bottom: -11%;
  left: -3%;
  right: -3%;
  z-index: -1;
  -webkit-transform: perspective(50em) rotateX(-30deg);
  transform: perspective(50em) rotateX(-30deg)
}

You have already good answers 您已经有了很好的答案

To give another try. 再试一次。 I have opted to fix your current attempt. 我选择修复您当前的尝试。

Basically the problem is that the background should be on the pseudo instead of on the base 基本上问题是背景应该在伪的而不是基础的

 h2.test-text { color: #FFF; padding: 5px; font-size: 30px; line-height: 1; width: 100%; text-align: center; position: relative; } h2.test-text:before { content: ""; position: absolute; border: none; top: -0px; bottom: -50%; left: 0px; right: 0px; z-index: -1; background: #000; transform: perspective(20em) rotateX(-45deg); transform-origin: top; } 
 <h2 class="test-text">Check out what our Clients are Saying</h2> 

And now a fancy efect 现在看中了效果

 h2.test-text { color: #FFF; padding: 5px; font-size: 30px; line-height: 1; width: 100%; text-align: center; position: relative; perspective: 20em; animation: tilt 2s infinite alternate linear; } h2.test-text:before { content: ""; position: absolute; border: none; top: -0px; bottom: -50%; left: 0px; right: 0px; z-index: -1; background: #000; transform: rotateX(-45deg); transform-origin: top; } @keyframes tilt { from {perspective-origin: left} to {perspective-origin: right} } 
 <h2 class="test-text">Check out what our Clients are Saying</h2> 

You can achieve this effect by using the the common transparent border trick to achieve css triangles. 您可以通过使用常见的透明边框技巧来实现css三角形来实现此效果。 Just instead of even borders and only one set to non-transparent you use different border sizes and two colors. 只是使用了不同的边框大小和两种颜色,而不是均匀的边框,并且只有一种设置为非透明的。 I colored the right edge differently so it's easier to see what's going on. 我对右边缘进行了不同的着色,因此更容易看到正在发生的情况。

 h2.test-text { background: #bada55; color: #FFF; font-size: 30px; padding: 5px; line-height: 1; width: 80%; text-align: center; position: relative; margin:40px; } h2.test-text:before, h2.test-text:after { content:"";position:absolute;top:0;width:0;height:0; border-style:solid; border-width:20px 15px; } h2.test-text:before{ left: -30px; border-color: #bada55 #bada55 transparent transparent; } h2.test-text:after { right: -30px; border-color:blue transparent transparent red; } 
 <h2 class="test-text">Whatever somebody says&hellip;</h2> 

By using pseudo elements, and skew them, you can achieve that. 通过使用伪元素并将其倾斜,您可以实现。

This one works if the line breaks up to 3 lines, and if you need more, a media query will fix that. 如果该行最多分为3行,则此方法有效,如果需要更多行,则可以通过媒体查询来解决。

 h2.test-text { background: #000; color: #FFF; padding: 5px; font-size: 30px; width: calc(100% - 120px); margin: 0 auto; text-align: center; position: relative; } h2.test-text:before, h2.test-text:after { content: ""; position: absolute; top: 0; height: 100%; width: 70px; background: inherit; z-index: -1; } h2.test-text:before { left: -35px; transform: skewX(30deg) } h2.test-text:after { right: -35px; transform: skewX(-30deg) } h2.test-text.nr2 { margin-top: 20px; width: calc(60% - 100px); } 
 <h2 class="test-text">Check out what our Clients are Saying</h2> <h2 class="test-text nr2">Check out what our Clients are Saying</h2> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM