!important 的绝对控制样式

本文详细解析了CSS中内联样式、内嵌样式及外联样式之间的优先级关系,特别是当使用!important属性时,内嵌样式如何超越内联样式的情况,并讨论了在不同情况下如何调整样式优先级。
	<head>
          <style type="text/css">
		div{background-color: blue !important;}
	  </style>
	</head>
	<body>
	  <div style="width: 100px;height: 100px;background-color: red;"></div>
	</body>    

  一般来说内联样式的优先级权重大于内嵌样式

直接在div标签上写style属性叫做内联样式  而在head标签里定义的style标签里写的叫内嵌   而通过css文件加载的叫外联

上面的代码可以看出,使用了!important属性后,内嵌样式高于了内联样式

 

当然,如果你在内联样式又加一个

background-color: red !important;  那么内联又高于了内嵌

转载于:https://www.cnblogs.com/YKingcc/p/9959269.html

<template> <div id="printContainer"> <img id="printImage" :src="imageSrc" alt="打印图片" @load="onImageLoaded" style="max-width: 100%; max-height: 30mm;" > </div> </template> <script> export default { name: "printPage", data(){ return{ imageSrc: "", } }, methods:{ onImageLoaded() { // 图片加载完成后强制计算尺寸并打印 this.$nextTick(() => { this.wytPrintExec(); }); }, wytPrintExec() { window.print() }, } } </script> <style> /* 基础样式确保打印容器初始状态正确 */ #printContainer { display: none; /* 默认隐藏,避免影响页面 */ } @media print { /* 完全隐藏页面其他内容 */ body > *:not(#printContainer) { display: none !important; } /* 打印容器样式优化 */ #printContainer { display: block !important; position: absolute !important; left: 0 !important; top: 0 !important; width: 100% !important; height: 100% !important; padding: 0 !important; margin: 0 !important; box-sizing: border-box !important; } /* 图片尺寸严格控制 */ #printImage { max-width: 100% !important; max-height: 100% !important; margin: 0 auto !important; /* 居中显示 */ display: block !important; page-break-inside: avoid !important; /* 防止图片被分页 */ } /* 页面设置关键优化 */ @page { margin: 0 !important; size: 50mm 30mm !important; /* 严格定义页面尺寸 */ overflow: hidden !important; } /* 根元素尺寸锁定 */ html, body { width: 50mm !important; height: 30mm !important; margin: 0 !important; padding: 0 !important; overflow: hidden !important; /* 禁止内容溢出 */ } /* 移除所有可能的额外间距 */ * { box-sizing: border-box !important; line-height: 1 !important; } } </style> 预览页面没有传进来的图片 图片大小为300px*60px
最新发布
10-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值