Ripped paper effect with css mask

huangapple go评论62阅读模式
英文:

Ripped paper effect with css mask

问题

我尝试使用CSS蒙版实现一些撕裂/线条纸张的效果。实际上,这样做效果相当好。但我无法实现的是如果我还想要在顶部也有蒙版。以下是我迄今为止尝试过的内容:

mask-image: linear-gradient(white, white), url("https://www.isorauschen.at/ripped-paper.svg"), url("https://www.isorauschen.at/ripped-paper-mirrored.svg");
mask-position: bottom, top;
mask-repeat: repeat-x, repeat-x;
mask-size: 400px auto, 400px auto;

也许有更好的方法来实现这个效果。我选择了蒙版,因为它还可以裁剪所有的背景和其他线条。

<div class="lined-paper">
  <div class="lined-paper-background">
    <div class="correction-margin-left"></div>
    <div class="correction-margin-right"></div>
  </div>

  <div class="masking-tape"></div>

  <div class="lined-paper-title">
    <span>文本标题</span>
  </div>
  <div class="lined-paper-text">
    这里是文本内容。
  </div>
</div>

注意:这是您提供的CSS和HTML代码的摘录,我没有进行任何翻译或修改。

英文:

I try to achieve some ripped/lined paper effect with css mask. It works actually pretty fine and looks good. What i can't achieve is if i also want the mask at the top. So here is what i tried so far:

mask-image: linear-gradient(white, white), url(&quot;https://www.isorauschen.at/ripped-paper.svg&quot;), url(&quot;https://www.isorauschen.at/ripped-paper-mirrored.svg&quot;);
mask-position: bottom, top;
mask-repeat: repeat-x, repeat-x;
mask-size: 400px auto, 400px auto;

Maybe there is a better way to get this done. I chose mask because it also clips all the background and other lines.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

*,
*::after,
*::before {
  box-sizing: border-box;
}

.lined-paper{
	color: black;
	display: inline-block;
	line-height: calc(1.5rem + 2px);
	margin: 30px calc(1em + 5px) 0 calc(1em + 5px);
	position: relative;
  width: 400px;
}

.lined-paper-background{
	background: repeating-linear-gradient(#FBFCF8, #FBFCF8 1.5rem, #9198e5 calc(1.5rem + 1px), #9198e5 calc(1.5rem + 2px));
	background-color: #FBFCF8;
	height: 100%;
	left: 0;
	mask-composite: exclude;
	mask-composite: xor;
	mask-image: linear-gradient(white, white), url(&quot;https://www.isorauschen.at/ripped-paper.svg&quot;);
	mask-position: bottom;
	mask-repeat: repeat-x;
	mask-size: 400px auto;
	position: absolute;
	top: 0;
	width: 100%;
}

.lined-paper-title {
	align-items: end;
	background-color: #FBFCF8;
	display: flex;
	height: calc((1.5rem + 2px) * 3 - 2px);
	margin-bottom: calc(1.5rem + 4px);
	overflow: hidden;
	padding: calc(1.5rem + 2px) 3em 0 4.5em;
	position: relative;
	max-width: 100%;
}
.lined-paper-title::after,
.lined-paper-title::before {
	border-right: 1px solid pink;
	content: &quot;&quot;;
	display: block;
	height: 100%;
	left: 0;
	position: absolute;
	top: 0;
	width: 4em;
}
.lined-paper-title::before{
	border-left: 1px solid pink;
	border-right: none;
	left: auto;
	right: 0;
	width: 2em;
}

.lined-paper-title span{
	font-size: 1.5em;
	font-weight: bold;
	max-height: calc((1.5rem + 2px) * 2 - 2px);
}

.lined-paper-text {
 padding: 0 3rem 4rem 4.5rem;
 position: relative;
}

.lined-paper .masking-tape::after,
.lined-paper .masking-tape::before{
  background: rgba(255, 255, 245, 0.6);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
  content: &quot;&quot;;
  display: block;
  height: 30px;
  margin: auto;
  position: absolute;
  width: 100px;
	z-index: 3;
}

.lined-paper .masking-tape::before{
	left: -1em;
	top: calc(-1em - 5px);
	transform: rotate(-9deg);
}

.lined-paper .masking-tape::after{
	right: -1em;
	top: calc(-1em - 2px);
	transform: rotate(4deg);
}

.correction-margin-left,
.correction-margin-right{
	border-right: 1px solid pink;
	display: block;
	height: 100%;
	left: 0;
	position: absolute;
	top: 0;
	width: 4em;
}
.correction-margin-right{
	border-left: 1px solid pink;
	border-right: none;
	left: auto;
	right: 0;
	width: 2em;
	z-index: 1;
}

.correction-margin-left::after,
.correction-margin-left::before{
  background-color: white;
	border-radius: 50%;
	box-shadow: 0 0 3px rgba(0, 0, 0, 0.5) inset;
	content: &quot;&quot;;
	display: block;
	height: 20px;
	left: 50%;
	position: absolute;
	top: 20%;
	transform: translateX(-50%);
	width: 20px;
}
.correction-margin-left::before{
	bottom: 20%;
	top: auto;
}

<!-- language: lang-html -->

&lt;div class=&quot;lined-paper&quot;&gt;

  &lt;div class=&quot;lined-paper-background&quot;&gt;
    &lt;div class=&quot;correction-margin-left&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;correction-margin-right&quot;&gt;&lt;/div&gt;
  &lt;/div&gt;
  
  &lt;div class=&quot;masking-tape&quot;&gt;&lt;/div&gt;
  
  &lt;div class=&quot;lined-paper-title&quot;&gt;
    &lt;span&gt;Lorem Ipsum Lorem Ipsum&lt;/span&gt;
  &lt;/div&gt;
  &lt;div class=&quot;lined-paper-text&quot;&gt;
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 2

请更新您的代码如下。为了演示的目的,我只展示掩模代码。我还使用了一个随机的PNG图像,因为您的SNG在片段中由于一些CORS问题而无法工作。

请注意50px的使用,这是允许您控制撕裂效果大小的值。

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->
.box {
  width: 400px;
  height: 600px;
  background-color: red;
  -webkit-mask:
    url('https://i.ibb.co/7RW8C9t/top.png') /*url("https://www.isorauschen.at/ripped-paper.svg")*/
      top/auto 50px repeat-x,
    linear-gradient(white, white) no-repeat
      center/100% calc(100% - 2*50px),
    url('https://i.ibb.co/5WvbqgG/zmylJ.png') /* url("https://www.isorauschen.at/ripped-paper-mirrored.svg")*/
      bottom/auto 50px repeat-x;
}

<!-- language: lang-html -->
<div class="box"></div>

<!-- end snippet -->

以上是您要求的代码翻译部分。

英文:

Update your code like below. For the sake of the demo I am showing only the mask code. I am also using a random PNG image because your SNG won't work in the snippet due to some CORS issues.

Note the use of 50px which the value that allow you to control the size of the ripped effect

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.box{
   width: 400px;
   height: 600px;
   background-color: red;
   -webkit-mask: 
     url(&#39;https://i.ibb.co/7RW8C9t/top.png&#39;) /*url(&quot;https://www.isorauschen.at/ripped-paper.svg&quot;)*/
       top   /auto 50px repeat-x,
     linear-gradient(white, white) no-repeat
       center/100% calc(100% - 2*50px),
     url(&#39;https://i.ibb.co/5WvbqgG/zmylJ.png&#39;) /* url(&quot;https://www.isorauschen.at/ripped-paper-mirrored.svg&quot;)*/
      bottom/auto 50px repeat-x;
}

<!-- language: lang-html -->

&lt;div class=&quot;box&quot;&gt;

&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月8日 18:34:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75671911.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定