0

0

What is Graceful Degradation in CSS?

PHPz

PHPz

发布时间:2023-08-19 12:13:18

|

646人浏览过

|

来源于tutorialspoint

转载

what is graceful degradation in css?

什么是优雅降级?

If you are an experienced web developer, you may have heard the graceful degradation word before. Before we learn about the graceful degradation in web development, let’s break down the word. The meaning of graceful is elegant or beautiful, and degradation is breaking or falling down. So, the overall meaning of the graceful degradation word is that it makes the feature elegant while it breaks.

Developers use the graceful degradation term in web development. It provides various techniques so that any website or application can work correctly in less capable browsers.

For example, modern browsers support advanced CSS and JavaScript features but are not supported by older browsers or older versions of the browsers. In such cases, developers need to ensure that users can access the website in older browsers with a good experience.

Different Techniques for the Graceful Degradation

在上面的部分中,我们了解了什么是优雅降级以及为什么开发人员应该确保它。现在,我们将通过示例学习不同的优雅降级技术。

立即学习前端免费学习笔记(深入)”;

Progressive Enhancement

在这种技术中,开发人员需要将代码分解成不同的包,并逐个加载每个包。因此,成功加载网页的HTML,然后加载每个浏览器都支持的普通CSS。

At last, load the advanced CSS features, and if the browser supports that feature, it will be applied to the HTML element. Otherwise, the HTML content of the web page remains accessible. So, in this way, even if a modern browser doesn’t support some features, it can still render the HTML content properly.

Feature Detection

在这种方法中,我们检查浏览器是否支持特定的JavaScript功能。如果是,网站允许用户使用该功能来相应地样式化HTML内容。否则,我们可以显示一些错误消息或对HTML内容应用不同的样式。

让我们通过下面的示例来理解。

Example

在下面的示例中,我们创建了一个div元素并给了它一个'id'为'element'。同时,我们在CSS中定义了'class'为'container'的类,并将一些CSS属性包含在其中。

在JavaScript中,每当浏览器加载时,我们通过id访问div元素,并检查div元素是否包含'classList'属性。如果是,我们将'container'类名添加到数组中。否则,我们只是将类名连接到类名字符串中。

慧中标AI标书
慧中标AI标书

慧中标AI标书是一款AI智能辅助写标书工具。

下载

所以,我们在这里检测了div元素是否支持classList类,根据这一点,我们使用了不同的技术来向div元素添加类名。



   


   

Using the feature detection technique for the graceful degradation in the web development

添加备选选项

优雅降级的另一种技术是添加备用选项。在这种技术中,如果浏览器不支持任何CSS,我们使用另一种CSS来完美显示HTML内容在Web浏览器中。

使用下面的示例,让我们了解如何将回退选项添加到网页中。

示例(为CSS渐变添加回退选项)

In the example below, we have created the card div element and used the line-gradient() CSS function to set the background gradient. Also, we have written the fallback CSS if the browser doesn’t support the linear-gradient() CSS function.

In the output, users can observe that either it shows the gradient or background color.



   


   

Using the fallback options for the gradient for the graceful degradation in the web development

This is a card element

Example (Adding the fallback option for CSS animation)

In the example below, we added the CSS animation's fallback option. Here, we have created three div elements and added the ‘bounce’ animation in all elements. The ‘bounce’ animation moves the div upside from its position and sets it back to its initial position.

In JavaScript, we create a new div element and check if its style contains the ‘animation’ property. If yes, the animation will apply automatically. Otherwise, we need to add a ‘no_animation’ class to every div element using JavaScript, which sets ‘animation: none’.



   


   

Using the fallback options for the animation for the graceful degradation in the web development

div1
div2
div3

Users learned about various graceful degradation techniques in this tutorial. All techniques make the HTML content of web pages attractive, even if browsers are not supporting some features.

优雅降级的最佳技术是设置备选方案。开发人员应仅使用标准的HTML和CSS属性,以确保在旧版浏览器中实现优雅降级。

However, graceful degradation is costly to maintain as developers require to add fallback options for multiple features. Still, it gives a smooth web experience to visitors visiting from any web browser.

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
php远程文件教程合集
php远程文件教程合集

本专题整合了php远程文件相关教程,阅读专题下面的文章了解更多详细内容。

21

2026.01.22

PHP后端开发相关内容汇总
PHP后端开发相关内容汇总

本专题整合了PHP后端开发相关内容,阅读专题下面的文章了解更多详细内容。

14

2026.01.22

php会话教程合集
php会话教程合集

本专题整合了php会话教程相关合集,阅读专题下面的文章了解更多详细内容。

8

2026.01.22

宝塔PHP8.4相关教程汇总
宝塔PHP8.4相关教程汇总

本专题整合了宝塔PHP8.4相关教程,阅读专题下面的文章了解更多详细内容。

7

2026.01.22

PHP特殊符号教程合集
PHP特殊符号教程合集

本专题整合了PHP特殊符号相关处理方法,阅读专题下面的文章了解更多详细内容。

6

2026.01.22

PHP探针相关教程合集
PHP探针相关教程合集

本专题整合了PHP探针相关教程,阅读专题下面的文章了解更多详细内容。

6

2026.01.22

菜鸟裹裹入口以及教程汇总
菜鸟裹裹入口以及教程汇总

本专题整合了菜鸟裹裹入口地址及教程分享,阅读专题下面的文章了解更多详细内容。

20

2026.01.22

Golang 性能分析与pprof调优实战
Golang 性能分析与pprof调优实战

本专题系统讲解 Golang 应用的性能分析与调优方法,重点覆盖 pprof 的使用方式,包括 CPU、内存、阻塞与 goroutine 分析,火焰图解读,常见性能瓶颈定位思路,以及在真实项目中进行针对性优化的实践技巧。通过案例讲解,帮助开发者掌握 用数据驱动的方式持续提升 Go 程序性能与稳定性。

9

2026.01.22

html编辑相关教程合集
html编辑相关教程合集

本专题整合了html编辑相关教程合集,阅读专题下面的文章了解更多详细内容。

106

2026.01.21

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号