使用纯CSS从右侧截取文本

时间:2018-12-28 12:55:58

标签: css css3

我正在尝试实现右上边框切断文本但没有使文本靠近的效果。 如图所示,任何人都可以帮我解决这个问题。 Sample Image

2 个答案:

答案 0 :(得分:0)

.wrapper{
  background: #E2E2E2;
  display: inline-block;
  font-size: 25px;
  position:relative;
  border: 1px solid gray;
}
.wrapper::after{
  content: " ";

  position:absolute;
  top:0;
  right:0;
  width: 10px;
  display: block;
  border-right: 15px solid white;
  border-bottom: 30px solid transparent;
}
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <div class="wrapper">Your text</div>
</body>
</html>

其他方式:

1)使用png图像并使用绝对位置

2)注释中Paulie_D建议的剪切路径

答案 1 :(得分:0)

A-当按钮的宽度固定时:(其背景颜色可能是透明的): 您可以将三个div放在另一个div中:

  • 第二个将旋转并具有嵌入式阴影框
  • 第三个div将与第二个div相反的方向

所有框都将溢出设置为隐藏:

div.outer_box{
    position: absolute;
    top: 10px;
    left: 10px;
    background-color: rgba(0,0,0,0.1);
    width: 160px;
    height: 35px;
    overflow: hidden;
}
div.containter{
    position: absolute;
    overflow: hidden;
    box-shadow: inset -5px 0px 12px 0px rgba(0,0,0,0.3);
    border: 0px;
    padding: 5px;
    top: -51px;
    width: 140px;
    height: 160px;
    transform: rotate(-30deg);
}
div.containter2{
    position: absolute;
    left: 0px;
    top:120px;
    width: 380px;
    transform: rotate(30deg);
    padding: 2px;
}
<html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <title>Document</title>
    </head>
    <body>
        <div class="outer_box">
            <div class="containter">
                <div class="containter2">
                    <span style="color:red; font-weight:bold; font-size: 22px;">
                        <span style="color:red;">OUR</span>
                        <span style="color:darkblue;">BRANDS</span>
                    </span>
                </div>
            </div>
        </div>
    </body>
</html>

要在Safari或IE上工作:请在以下行进行旋转

-ms-transform: rotate(20deg); /* IE 9 */
-webkit-transform: rotate(20deg); /* Safari */
transform: rotate(20deg);

以及阴影

-webkit-box-shadow: inset -15px 0px 22px 0px rgba(0,0,0,0.75);
-moz-box-shadow: inset -15px 0px 22px 0px rgba(0,0,0,0.75);
box-shadow: inset -15px 0px 22px 0px rgba(0,0,0,0.75);

B-当按钮的宽度可以变化时: 如果此按钮具有纯色(非透明)背景色,则还可以创建“ shader-div”并将其向右移:

div.outer_box{
    position: absolute;
    top: 10px;
    left: 10px;
    background-color: rgb(222,222,222);
    width: 160px;
    height: 35px;
    overflow: hidden;
}
div.outer_box:hover{
    width: 220px;
}
div.text_container{
    position: absolute;
    left: 0px;
    top:0px;
    width: 380px;
    padding: 5px;
}
div.right_cut_off{
    position: absolute;
    box-shadow: -5px 0px 12px 0px rgba(0,0,0,0.2);
    background-color: rgb(222,222,222);
    top: -60px;
    right: -10px;
    width: 25px;
    height: 140px;
    transform: rotate(-30deg);
}
<html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <title>Document</title>
    </head>
    <body>
        <div class="outer_box">
            <div class="text_container">
                <span style="color:red; font-weight:bold; font-size: 22px;">
                    <span style="color:red;">OUR</span>
                    <span style="color:darkblue;">BRANDSXXXXXXXXXXXX</span>
                </span>
            </div>
            <div class="right_cut_off"></div>
        </div>
    </body>
</html>