Table of Contents
知识储备
效果实现
Home Web Front-end HTML Tutorial 4-3 视觉效果 毛玻璃效果_html/css_WEB-ITnose

4-3 视觉效果 毛玻璃效果_html/css_WEB-ITnose

Jun 24, 2016 am 11:20 AM

知识储备

1.filter:blur() CSS3模糊滤镜


这个效果很有趣哦。先来看看效果图吧

毛玻璃效果

效果实现

我们先来看看html结构吧,本书作者采用了blockquote结构来展示,因为对其不是特别熟悉,于是用p标签来代替

示例代码:html

<div class="mytest">     <p>"The only way to get rid of a temptation is yield to it.Resist it,and your soul grows sick with longing for the things it has forbidden to itself."             <br/>Oscar Wilde,The Picture of Dorian Gray      </p></div>
Copy after login

css因为是背景图片(无法撑开容器),必须设置其容器大小

.test{     position: relative;    width: 400px;    height: 250px;   margin: 0 auto;  background: url("锥头螳螂.jpg") center;  background-size: cover;}
Copy after login

设置文字p容器(这里我称呼它为玻璃) 垂直/水平 居中

.test p{    width: 330px;    padding: 15px;   //设置文字左右/垂直居中  position: absolute;  left: 50%;    top: 50%;   transform: translate(-50%,-50%);   margin: 0;   //取消p标签的margin  background: hsla(0,100%,50%,.6);  //设置其背景色  color: white;   border-radius: 3px;   overflow: hidden;  //下面具体分析其作用}
Copy after login

设置伪元素来模拟毛玻璃的效果

.test p::before{     position: absolute;    left: 0;  top: 0;  bottom: 0;  right: 0;      content: '';    background: url("锥头螳螂.jpg");   background-size: cover;     margin: -20px;   //!!这个后面会说明   z-index: -1; //!!这个后面会重点说明   -webkit-filter: blur(20px);   filter: blur(20px);}
Copy after login

于是乎,看完一大堆代码还是很难受的。我们整理一下思路

1.我们设置了父容器来显示图片

2.我们设置了p标签,并将玻璃先生(p标签)水平/垂直居中,并给玻璃先生一点半透明的背景色效果:

半透明背景

3.上面的效果还不是我们想要的,上面的半透明色影响到了我们去阅读文字于是我们设置了一个伪元素来制造一点的效果我们先来加个滤镜,并设置和div容器一样的背景图片

background: url("锥头螳螂.jpg") no-repeat center;-webkit-filter: blur(5px);filter: blur(5px);
Copy after login

其次设置z-index=-1,防止覆盖文字

但是,还有一点瑕疵---模糊程度越到边缘越不明显

观察看到,模糊程度越到边缘越不明显

于是作者非常巧妙地利用

margin: -20px;
Copy after login

来扩大模糊半径,同时做到和背景图片的重合这里很重要,所以请注意看!!!!

我们先来解释模糊半径

扩大的模糊半径(超出了父容器)

于是在玻璃先生(p标签)的样式里设置

overflow:hidden
Copy after login

再看看效果

理想的模糊效果

那么我们来说第二点,这里需要一点点计算,也是这个效果的约束之一我们去掉滤镜,来看看伪元素背景图和div容器背景图是否重合

明显不重合的背景

虽然模糊后,我们不易察觉,但有时我们需要一个微弱的模糊滤镜的时候很容易被察觉,来查查原因!思考一番,发现伪元素和div背景大小不一致,背景图缩放不同,产生了错位。了解原因后,我们总结一下:

1.我们需要让div容器和伪元素宽度一致2.我们不希望div容器因为让图片产生横向的缩放

计算一下:伪元素宽度330px+2x内距15px+2x伪元素外距20px = 400px,正好是div容器宽度,我们的错位也消失了。(这里margin起到了调节的效果)

4.我们加回滤镜,效果已经很逼真了,但是貌似我们的毛玻璃和背景色融在了一起,所以我们在玻璃先生(p标签)中加了背景色(这里的颜色需要与背景色有较大的色差才明显)

background: hsla(274, 100%, 90%, 0.2);
Copy after login

并在我们的伪元素中加入上一节学到的滤镜

mix-blend-mode: luminosity;
Copy after login

大功告成,我们实现了效果

可以尝试一下不同的滤镜效果哦

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

What is the viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

The article discusses the &lt;iframe&gt; tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

See all articles