html - 消除样式表的呈现阻塞资源

标签 html css styles

我已经阅读了很多关于消除 Web.dev 上的渲染阻塞资源警告的文章,但结果是一样的。

这是我在 header 标签内的 HTML 代码:

<link rel="stylesheet" href="https://example.com/assets/inc/bootstrap.min.css" media="all" async>
<link rel="stylesheet" href="https://example.com/assets/inc/fontello.css" media="all" async>
<link rel="stylesheet" href="https://example.com/assets/inc/styling.css" media="all">
<link rel="stylesheet" href="https://example.com/v2/_assets/css/bootstrap-rtl.min.css" media="all" async>
<link rel="stylesheet" href="https://example.com/v2/_assets/css/theme.css" media="all" async>

enter image description here

我还为第一个 Contentful Paint 中不需要的每个样式表链接添加了 async 属性。

最佳答案

这将减少样式表的一些加载时间。首先,以正常方式加载 bootstrap.min.css。然后,将其他 css 文件合并到一个文件中(如果不是,请先缩小)并使用此 Javascript 加载它们。

var LinkHref = 'https://www.example.com/style.css'; // href of <link>
var LinkId   = 'cssStyle1'; // unique id of <link>
if (!document.getElementById(LinkId)) {
  var LinkCss  = document.createElement('link');
  LinkCss.href = LinkHref;
  LinkCss.id   = LinkId;
  LinkCss.rel  = 'stylesheet';
  LinkCss.type = 'text/css';
  document.getElementsByTagName('head')[0].appendChild(LinkCss);
}

关于html - 消除样式表的呈现阻塞资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59753293/

相关文章:

javascript - 如何设置位于PC上的图像文件

html - float :left; vs display:inline; vs display:inline-block; vs display:table-cell;

html - 没有js的固定纵横比和固定高度的元素

html - 将此渐变图像转换为 CSS 渐变

javascript - 视差滚动背景图像自动放大和缩小

javascript设置字体粗细: want to set to bold or normal can't get it work

php - style.css 未在 wordpress 主题中加载

android - TouchableOpacity 组件设置为 position absolute not clickable in android

javascript - 当我的网站加载时,如何为我的 `<div>` 元素提供动画条目

css - 在 Safari 10(?) 中隐藏自动添加的输入(用户)按钮