HTML5新特性:范围样式<style scoped>

介绍HTML5中的范围样式(<stylescoped>)特性,该特性允许开发者将样式限制应用到特定元素及其子元素上,避免样式冲突。适用于内容合并、组件封装等场景。

原文出处:http://blog.csdn.net/hfahe/article/details/7381141
        Chromium 
最近实现了一个HTML5的新特性:范围样式,又叫做<style scoped> 。开发者可以通过为根元素设定一个添加了scoped属性的style标签,来限制样式只作用于style标签的子元素上。这会限制样式只影响style标签的父元素和它所有的后代元素。

例子

        下面是一个使用了标准样式的简单页面:

<html>
<body>
  <div>a div! <span>a span!</span></div>
    <div>
      <style>
        div { color: red; }
        span { color: green; }
      </style>
      a div! <span>a span!</span></div>
  <div>a div! <span>a span!</span></div>
</body>
</html>

        它设定的样式规则将使得所有的<div>变为红色,<span>变成绿色:

a div! a span!
a div! a span!
a div! a span!

        然而,如果我们为<style>元素设置了scoped属性

<html>
<body>
  <div>a div! <span>a span!</span></div>
    <div>
      <style scoped>
        div { color: red; }
        span { color: green; }
      </style>
      a div! <span>a span!</span></div>
  <div>a div! <span>a span!</span></div>
</body>
</html>

        那么这个样式规则限制使得它们应用于<style scoped>元素的父<div>元素及其内部的所有元素上。我们称之为范围,结果如下所示:

a div! a span!
a div! a span!
a div! a span!

        当然我们可以在任何地方使用这个标签。 所以如果你喜欢冒险,你可以在一个范围样式内添加尽可能多的范围样式来获取尽可能细的样式控制粒度。

用途

        它有什么好处?

        一种常见的用途是内容合并:当你作为一个网站的作者想嵌入来自第三方的内容(译者注:想想博客),包括它所有的样式风格,但是不想让这些样式污染页面其他无关的部分。其一个巨大的优势是可以将其他网站例如YelpTwitterEbay等的内容合并到一个单独页面,而无需使用<iframe>或者动态的编辑外部内容来隔离它们。

        如果你使用内容管理系统CMS),它会发送许多标记片段来整合成为一个最终显示的页面。所以范围样式是一个伟大的功能,可以确保每一个片段与任何其他页面上的样式相隔离。这对wiki来说也一样的有用。

        当你想在页面上展示一些漂亮的演示代码,很容易限制样式只作用于演示内容。你可以在演示随意的添加样式,而不用担心对页面上其他内容的影响

        它的另一个用途是简单的封装:例如,如果你的网页有一个侧边菜单,把指向菜单的样式封装到其中的<style scoped>段落会很有意义。这些样式规则对页面其他区域的渲染将不会有任何影响,这可以使得它可以很好地和主要内容进行分离!

        它可能最引人注目的用途之一是用在Web组件模型上。Web组件将是一个构建像滑块、菜单、日期选择器和选项卡部件等的伟大方式。通过提供范围内的样式,设计人员可以构建一个组件并且将其打包成为一个独立的单位,其他人可以使用这个组件并组合为一个富Web应用程序。 我们计划在Web组件和shadowDOM(已经可以在chrome://flags里开启实验性的“Shadow DOM”标志来启用)里大量使用范围样式。除了例如内联样式这样不好的方式,现在没有真正的好办法来确保样式限制在Web组件里,所以范围样式是一个完美的解决方案。

为什么包括父元素?

        最自然的方式需要包括父元素,以便于<style scoped>规则可以来做为整个区域设置通用背景颜色这样类似的事情。它还允许采用“防守性”的方式来书写范围样式,通过为ID或者类选择器加上前缀的方式为还不支持<style scoped>浏览器提供优雅降级。

<div id=”menu”>
  <style scoped>
    #menu .main { … }
    #menu .sub { … }
  …

        这种模仿可以实现范围样式的效果,但是因为更复杂的选择器会有一些运行时的性能损失。种做法的好处是,它采用一个优雅的降级方法让我们可以等到<style scoped>广泛支持和ID选择器可以简单地被丢弃时。

状态

        鉴于范围样式的实现是最新的,它目前被隐藏在Chrome的运行时标志里。要激活它,你需要下载版本号为19或者更高的Chrome(现在的Chrome Canary),然后在chrome://flags里找到“开启<stylescoped>”选项(靠近最后),单击启用,然后重新启动浏览器。

        目前没有已知bug,但是@keyframes@-webkit-region区域范围的版本还正在实现中。此外,@font-face被忽略掉了,因为现在有一个很好的机会来调整这个规范。

        我们鼓励每个对这个特性感兴趣的人都来尝试一下,让我们知道你的反馈:好、不好以及(可能)不足。

<template> <c-modal v-model:open="open" title="" @ok="handleOrderOk" width="1600px" :footer="null"> <div style="padding-bottom: 15px"> <a-button key="back" @click="handleCancel" v-if="isFooter">取消</a-button> <a-button key="print" @click="handlePrint">打印</a-button> </div> <div id="printJS-form"> <div v-for="(item, index) in formState" :key="index"> <div class="order-container print-container-div" > <div class="order-top widthClass"> <div class="order-top-title"> <div class="order-top-logo"><img class="logo" src="../../../assets/cimc.png" /></div> <p class="order-top-titles">Piping Welds Positive Material Identification Report</p> </div> </div> <div class="order-top-QRCode widthClass"> <img src="/static/pipe/images/seatrium.png" /> <img src="/static/pipe/images/br.png" /> </div> <div class="printable-area widthClass"> <table class="widthClass"> <tr style="height: 30px"> <td style="width: 50%;"></td> <td style="width: 25%;font-size: 16px;border-left: none !important;">Analysis Data Sheet: </td> <td style="width: 25%;font-size: 16px;border-left: none !important;">BM*:base material</td> </tr> </table> <table class="widthClass" style="font-size: 16px;font-weight: 400;"> <tr style="height: 30px;"> <td style="width: 15%;">Project</td> <td style="width: 20%;"></td> <td style="width: 15%;">Discipline</td> <td style="width: 10%;">Piping</td> <td style="width: 10%;">Insp. Date</td> <td style="width: 30%;"></td> </tr> <tr style="height: 30px;"> <td style="width: 15%;">Dep't</td> <td style="width: 20%;">CIMC QMD - YCRO</td> <td style="width: 15%;">Team</td> <td colspan="3"></td> </tr> </table> <table class="widthClass" style="font-size: 16px;font-weight: 400;"> <tr style="height: 30px;"> <td style="width: 15%;">Inspector</td> <td style="width: 20%;">Zhang Luning</td> <td style="width: 15%;">Inspection Place</td> <td style="width: 20%;">WORKSHOP CENTER</td> <td style="width: 15%;">Material</td> <td style="width: 20%;">S31803</td> </tr> <tr style="height: 30px;"> <td style="width: 15%;">Procedure No</td> <td style="width: 20%;">I-PR-3010.2S-1351-970-YCT-004</td> <td style="width: 15%;">Equip. Name</td> <td style="width: 20%;">FERITSCOPE FMP30</td> <td style="width: 15%;">Serial NO</td> <td style="width: 20%;">100020024</td> </tr> </table> <table class="widthClass" style="font-size: 16px;font-weight: 400;"> <tr style="height: 130px;"> <td style="width: 80%;"><img src="/static/pipe/images/tst.png" /></td> <td style="width: 20%;border-left: none !important;">Acceptance Criteria: a) 30% to 65% for weld metal b) 40% to 65% for HAZ c) 40% to 60% for base metal (including forgings and castings)</td> </tr> </table> <table class="widthClass" style="font-size: 16px;font-weight: 400;"> <tr style="height: 30px;"> <td style="width: 5%;">No</td> <td style="width: 5%;">Point</td> <td style="width: 10%;">Drawing No</td> <td style="width: 10%;">Joint No</td> <td style="width: 10%;">Joint </td> <td style="width: 10%;">HAZ 1/2</td> <td style="width: 10%;">BASE 1/2</td> <td style="width: 10%;">Result</td> <td style="width: 30%;">Result</td> </tr> </table> <table class="widthClass" style="font-size: 16px;font-weight: 400;"> <div v-for="(weldItem,index) in item.weldList" :key="item.id"> <tr style="min-height: 30px;" > <td style="width: 5%; text-align: center; vertical-align: middle; display: table-cell;" rowspan="3" >{{ index }}</td> <td style="width: 5%;">0°</td> <td style="width: 10%;">{{ weldItem.indexNum }}</td> <td style="width: 10%;">{{ weldItem.weldNo }}</td> <td style="width: 10%;">NA </td> <td style="width: 10%;">NA</td> <td style="width: 10%;"></td> <td style="width: 10%;">ACC</td> <td style="width: 30%;"></td> </tr> <tr style="min-height: 30px;"> <!-- <td style="width: 5%;">{{ index }}</td>--> <td style="width: 5%;">90°</td> <td style="width: 10%;">{{ weldItem.indexNum }}</td> <td style="width: 10%;">{{ weldItem.weldNo }}</td> <td style="width: 10%;">NA </td> <td style="width: 10%;">NA</td> <td style="width: 10%;"></td> <td style="width: 10%;">ACC</td> <td style="width: 30%;"></td> </tr> <tr style="min-height: 30px;"> <!-- <td style="width: 5%;">{{ index }}</td>--> <td style="width: 5%;">180°</td> <td style="width: 10%;">{{ weldItem.indexNum }}</td> <td style="width: 10%;">{{ weldItem.weldNo }}</td> <td style="width: 10%;">NA </td> <td style="width: 10%;">NA</td> <td style="width: 10%;"></td> <td style="width: 10%;">ACC</td> <td style="width: 30%;"></td> </tr> </div> </table> <table class="widthClass" style="font-size: 16px;font-weight: 400;min-height: 90px"> <tr style="height: 30px;"> <td style="width: 10%;" rowspan="2">CIMC</td> <td style="width: 10%;">DATE</td> <td style="width: 20%;">SIGNATURE</td> <td style="width: 10%;" rowspan="2">SEATRIUM </td> <td style="width: 10%;">DATE</td> <td style="width: 20%;">SIGNATURE</td> <td style="width: 10%;" rowspan="2">PETROBRAS </td> <td style="width: 10%;">DATE</td> <td style="width: 20%;">SIGNATURE</td> </tr> <tr style="min-height: 80px;"> <td style="width: 10%;"></td> <td style="width: 20%;"></td> <td style="width: 10%;"> </td> <td style="width: 20%;"></td> <td style="width: 10%;"></td> <td style="width: 20%;"></td> </tr> </table> </div> </div> <div style="page-break-after:always"></div> </div> </div> </c-modal> </template> <script setup> import {ref, reactive, onMounted} from "vue" import { print } from "@/utils/print" import * as server from "@/packages/piping/api/cpin"; import {create} from "@/utils/imgtoBase64"; const open = ref(false) const initType = ref() const totalWeight = ref(0) const formState = ref({}) const isFooter = ref(true) const orderTableState = reactive({ datasource: [] }) onMounted(() => { create("/static/mes/images/dataScreen-header-center-bg.png").then((res) => { centerImg.value = res }) }) const handleOrderOk = () => {} const init = (type, data) => { initType.value = type isFooter.value = type === "galv" console.log("data2", data) let printDatas = [] // 使用 Promise.all 等待所有异步操作完成 Promise.all(data.map(async (dataItem) => { try { const res = await server.getOrderList({ page: 0, size: 10000, orderNo: dataItem.orderNo }) let item = res.data.content[0] || {} item.weldList.forEach((weld, index) => { weld.indexNum = index + 1 }) return item } catch (error) { console.error("获取订单列表失败:", error) return {} } })).then(results => { printDatas = results formState.value = printDatas }) } const handleCancel = () => { open.value = false } const handlePrint = () => { print({ // mediaPrint:false, // noPrintSelector:'avoid-this', type: "html", printable: "printJS-form", // type: "raw-html", // printable: "test-dom", // scanStyles: false, // noHeadersAndFooters:true, maxWidth: 1500, targetStyles: ["*"], scanStyles: false, style: ` table{font-size:3px} td{font-size:10px !important} .print-container-div{ width: 1100px !important; } .widthClass{ width: 1000px; font-size:1px } .ant-descriptions-view >table{ width:100%!important; font-size:1px; } @media print { a[href]:after { content: none !important; } #footer { display: none; } } .imgInline{ display: flex;} .order-top-QRCode{float:right} .order-top-title { width: 100%; height: 50px; text-align: center; display: flex; padding-top:15px } .order-top-logo { img { margin-top:10px; width: 150px; position: absolute; left: 0; } } .order-top-titles { font-size: 25px; font-weight: bold; width: 100%; margin: 0; align:center; text-align:center; padding-top:15px; } .order-top-vice-title { width: 100%; } .logo{width:150px;height:20px} .paging{page-break-after: aways;} .order-container { width: 100%; height: 100%; position: relative; display: flex; flex-direction: column; align-items: center; justify-content: center; .order-top { width: 100%; display: flex; justify-content: center; position: relative; margin-bottom: 38px; .order-top-title { width: 45%; height: 50px; text-align: center; display: flex; .order-top-logo { img { width: 150px; position: absolute; left: 0; } } .order-top-titles { font-size: 25px; font-weight: bold; width: 100%; margin: 0; } .order-top-vice-title { width: 100%; } } } .order-top-QRCode { position: absolute; right: 0; top: 0; display: flex; align-items: center; justify-content: flex-end; } .order-mid { width: calc(100%); padding-top: 15px; } .order-mod { width: calc(100% ); .order-mod-left{ width: 49%; padding: 5px 0; border-radius: 10px; background: #eef0f3; } .order-mod-right { width: 49%; padding: 5px 0; border-radius: 10px; background: #eef0f3; } .order-mod-title { //border: 1px solid #3981ee; padding: 5px; //color: #3981ee; width: 50%; display: flex; align-items: center; justify-content: center; margin: 10px auto; background: #fff; border-radius: 7px; } .order-mod-content { display: flex; align-items: center; width: 100%; margin: 10px auto; .order-mod-content-title { width: 30%; text-align: right; padding: 5px 0; } .order-mod-content-content { flex: 1; white-space: nowrap; } } } .order-bot { width: 100%; margin-top: 10px; } .order-table { //border: 1px solid black !important; // transform: scale(0.7); //width: calc(100% / 0.7); // transform-origin: top center; } } :deep(.ant-descriptions) { .ant-descriptions-item-label { white-space: nowrap; width: 20px; border: 1px solid black !important; } .ant-descriptions-item-content { white-space: nowrap; border: 1px solid black !important; } } .ant-modal .ant-modal-body{font-size:9px} :deep(.c-table-wrap) { .ant-table-container { table { //padding-bottom: 80px; } .ant-table-cell{ border: 1px solid black !important; padding: 12px 8px; } .ant-table-cell-ellipsis { border-color: black !important; border: 1px; padding: 12px 8px; } } } .order-container { width: 100%; } thead { display: table-header-group; } .order-top, .order-mid, .order-mod, .order-bot, .order-table { page-break-inside: avoid; } td{text-align: center;border: 1px solid #000;} th{ text-align: center;border: 1px solid #000;} .ant-table-row{ page-break-inside: avoid; } td{text-align: center;border: 1px solid #000;} tr {page-break-inside: avoid;} th{ text-align: center;border: 1px solid #000;} table { border-collapse: collapse;} table tr th, table tr td { border: 1px solid #000!important; } #printJS-form table tr td[style*="border-left: none"] { border-left: none !important; } .print-container { transform: scale(0.8); transform-origin: top left; width: 100%; } //@page{size:auto} //.paging{page-break-after: always;} } ` }) } defineExpose({ init, open }) window.onbeforeprint = function () { var links = document.getElementsByTagName("a") for (var i = 0; i < links.length; i++) { links[i].setAttribute("data-href", links[i].href) links[i].removeAttribute("href") } } window.onafterprint = function () { var links = document.getElementsByTagName("a") for (var i = 0; i < links.length; i++) { links[i].href = links[i].getAttribute("data-href") links[i].removeAttribute("data-href") } } </script> <style lang="less" scoped> .order-container { width: 100%; height: 100%; position: relative; display: flex; flex-direction: column; align-items: center; justify-content: center; .order-top { width: 100%; display: flex; justify-content: center; position: relative; margin-bottom: 38px; .order-top-title { width: 45%; height: 50px; text-align: center; display: flex; .order-top-logo { img { width: 150px; position: absolute; left: 0; } } .order-top-titles { font-size: 25px; font-weight: bold; width: 100%; margin: 0; } .order-top-vice-title { width: 100%; } } } .order-top-QRCode { position: absolute; right: 0; top: 0; display: flex; align-items: center; justify-content: flex-end; } .order-mid { width: calc(100%); padding-top: 15px; } .order-mod { width: calc(100%); .order-mod-left { width: 49%; padding: 5px 0; border-radius: 10px; background: #eef0f3; } .order-mod-right { width: 49%; padding: 5px 0; border-radius: 10px; background: #eef0f3; } .order-mod-title { //border: 1px solid #3981ee; padding: 5px; //color: #3981ee; width: 50%; display: flex; align-items: center; justify-content: center; margin: 10px auto; background: #fff; border-radius: 7px; } .order-mod-content { display: flex; align-items: center; width: 100%; margin: 10px auto; .order-mod-content-title { width: 30%; text-align: right; padding: 5px 0; } .order-mod-content-content { flex: 1; white-space: nowrap; } } } .order-bot { width: 100%; margin-top: 10px; } .order-table { //border: 1px solid black !important; // transform: scale(0.7); //width: calc(100% / 0.7); // transform-origin: top center; } } :deep(.ant-descriptions) { .ant-descriptions-item-label { white-space: nowrap; width: 20px; border: 1px solid black !important; } .ant-descriptions-item-content { white-space: nowrap; border: 1px solid black !important; } } :deep(.c-table-wrap) { .ant-table-container { table { //padding-bottom: 80px; } .ant-table-cell { border: 1px solid black !important; padding: 12px 8px; } .ant-table-cell-ellipsis { border-color: black !important; border: 1px; padding: 12px 8px; } } } .order-container { width: 100%; } thead { display: table-header-group; } .order-top, .order-mid, .order-mod, .order-bot, .order-table { page-break-inside: avoid; } .order-mod-title { } td { text-align: center; border: 1px solid #000; } th { text-align: center; border: 1px solid #000; } @media print { a[href]:after { content: none !important; } #footer { display: none; } tr { page-break-inside: avoid; } } .ant-table-row { page-break-inside: avoid; } td { text-align: center; border: 1px solid #000; } tr { page-break-inside: avoid; } th { text-align: center; border: 1px solid #000; } table { border-collapse: collapse; } table tr th, table tr td { border: 1px solid #000 !important; } .ant-descriptions-item-label { white-space: nowrap !important; } .print-container { transform: scale(0.8); transform-origin: top left; width: 100%; } .numbers { font-family: "Arial", sans-serif; /* 或其他不支持自动转换的字体 */ } div { font-family: "Arial", sans-serif; /* 或其他不支持自动转换的字体 */ } .widthClass { width: 1500px !important; } .print-container-div { width: 1500px !important; } </style> 这里<td style="width: 25%;font-size: 16px;border-left: none !important;">Analysis Data Sheet: </td> 为什么border-left: none !important;没起作用
最新发布
11-01
<style scoped> * { margin: 0; padding: 0; box-sizing: border-box; } .shell { width: 1700px; background-color: transparent; margin: 0 auto; height: 210px; display: flex; justify-content: center; } .card { flex-basis: 15%; position: relative; overflow: hidden; } .card:not(:nth-child(1)) { margin-left: 0px; } .card:hover { overflow: initial; } .box { position: absolute; top: 0; left: 0; width: 100%; height: 500px; transition: all .1s cubic-bezier(0.165, 0.84, 0.44, 1); overflow: hidden; border-radius: 0px; background-image:transparent; } .box>img { object-fit: contain; width: 100%; height: 100px; overflow: hidden; transform: translate(-50%, 10%) scale(3); position: relative; z-index: -1; } .card:hover>.box img { opacity: 0; } .card:hover>.box { transform: scaleY(1.5); background-color: transparent; z-index: 2; cursor: pointer; } .card:hover h4 { transform: translate(-50%, 250%); } .card:hover .character>img { opacity: 1; } .card>.character { position: absolute; left: -250px; width: 800px; height: 800px; overflow: hidden; z-index: 3; pointer-events: none; } .character>img { width: 820px; height: 820px; object-fit: contain; opacity: 0; transition: all .3s; position: relative; z-index: -10; } </style> <template> <div> <div style="height:100%;position:fixed;z-index:-15" > <img src="../../res/W_4.jpg" style="height:100%"> </div> <div class="shell"> <div class="card"> <div class="box"> <img src="../../res/W_2.jpg" /> </div> <div class="character"> <img src="../../res/W_2.jpg" /> </div> </div> <div class="card"> <div class="box"> <img src="../../res/W_3.jpg" /> </div> <div class="character"> <img src="../../res/W_3.jpg" /> </div> </div> <div class="card"> <div class="box"> <img src="../../res/W_1.jpg" /> </div> <div class="character"> <img src="../../res/W_1.jpg" /> </div> </div> <div class="card"> <div class="box"> <img src="../../res/W_5.jpg" /> </div> <div class="character"> <img src="../../res/W_5.jpg" /> </div> </div> <div class="card"> <div class="box"> <img src="../../res/W_6.jpg" /> </div> <div class="character"> <img src="../../res/W_6.jpg" /> </div> </div> </div> </div> </template> <script setup lang="ts"> defineOptions({ name: 'W_SFQ' }); </script>
09-24
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值