how2j.cn


工具版本兼容问题
如果style.css中,<style>标签中,style属性上有冲突的样式,那么谁的优先级更高?

示例 1 : style标签与外部文件style.css样式重复   
示例 2 : style标签上的与style属性冲突   
示例 3 : !important   

示例 1 :

style标签与外部文件style.css样式重复

外部文件style.css的内容是

.p1{
color:red;
}

.span1{
color:blue;
}

同时style标签中也有一个.p1
那么优先使用: 最后出现的一个
"); window.frames["iframe_show498"].document.write(decodeHtml($("textarea#stepcodeTextarea498").val())); window.frames["iframe_show498"].document.close(); $(window.frames["iframe_show498"]).load(function(){ $("#iframe_show498").height($("#iframe_show498").contents().find("body").height()+showittryitheight); }); $("#iframe_show498").height($("#iframe_show498").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<link rel="stylesheet" type="text/css" href="http://127.0.0.1/study/style.css" /> <style> .p1{ color:green; } </style> <p class="p1">p1 颜色是绿色,优先使用靠的最后出现的</p>
<link rel="stylesheet" type="text/css" href="http://127.0.0.1/study/style.css" />

<style>
.p1{
  color:green;
}
</style>

<p class="p1">p1 颜色是绿色,优先使用靠的最后出现的</p>
"); window.frames["iframe498"].document.write(decodeHtml(code498)); window.frames["iframe498"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe498"]).load(function(){ $("#iframe498").height($("#iframe498").contents().find("body").height()+showittryitheight); }); $("#iframe498").height($("#iframe498").contents().find("body").height()+showittryitheight); alreadyWriteCode498 = code498; $("#rendering498").hide(); $("#rendered498").show(); } var tRereshRetry2DemoPanel498 = setInterval(rereshRetry2DemoPanel498,1000); var binded498 = false; $("textarea#stepcodeTextarea498").keyup(function(){ if(!binded498){ $(window).bind('beforeunload',function(){ binded498 = true; return "xxxx"; }); } var newCode = $(this).val() code498 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code498!=newCode){ // window.frames["iframe498"].document.write("
"); // window.frames["iframe498"].document.write(decodeHtml($("textarea#stepcodeTextarea498").val())); // window.frames["iframe498"].document.close(); // $(window.frames["iframe498"]).load(function(){ // $("#iframe498").height($("#iframe498").contents().find("body").height()+showittryitheight); // }); // code498 = newCode; // } }); $(".tryButton498").click(function(){ $("#tryDiv498").show(); $("#stepcodeTextarea498").focus(); $("#stepcodeTextarea498").height(200); $("#iframe498").height(0); window.frames["iframe498"].document.write("
"); window.frames["iframe498"].document.write(decodeHtml($("textarea#stepcodeTextarea498").val())); window.frames["iframe498"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe498"]).load(function(){ $("#iframe498").height($("#iframe498").contents().find("body").height()+showittryitheight); }); $("#iframe498").height($("#iframe498").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor498.focus(); editor498.setSize(null, "250"); $("#rendering498").hide(); $("#rendered498").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor498 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea498"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor498.on("change",function(doc){ if(!binded498){ $(window).bind('beforeunload',function(){ binded498 = true; return "xxxx"; }); } var newCode = doc.getValue(); code498 = newCode; $("textarea#stepcodeTextarea498").val(newCode); if(alreadyWriteCode498!=code498){ lastModifedTime498 = new Date().getTime(); $("#rendering498").show(); $("#rendered498").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor498 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor498.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv498").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果
示例 2 :

style标签上的与style属性冲突

style标签上的与style属性冲突
优先使用style属性
"); window.frames["iframe_show499"].document.write(decodeHtml($("textarea#stepcodeTextarea499").val())); window.frames["iframe_show499"].document.close(); $(window.frames["iframe_show499"]).load(function(){ $("#iframe_show499").height($("#iframe_show499").contents().find("body").height()+showittryitheight); }); $("#iframe_show499").height($("#iframe_show499").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<style> .p1{ color:green; } </style> <p class="p1" style="color:red">p1 颜色是红色,优先使用style属性</p>
<style>
.p1{
  color:green;
}
</style>

<p class="p1" style="color:red">p1 颜色是红色,优先使用style属性</p>
"); window.frames["iframe499"].document.write(decodeHtml(code499)); window.frames["iframe499"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe499"]).load(function(){ $("#iframe499").height($("#iframe499").contents().find("body").height()+showittryitheight); }); $("#iframe499").height($("#iframe499").contents().find("body").height()+showittryitheight); alreadyWriteCode499 = code499; $("#rendering499").hide(); $("#rendered499").show(); } var tRereshRetry2DemoPanel499 = setInterval(rereshRetry2DemoPanel499,1000); var binded499 = false; $("textarea#stepcodeTextarea499").keyup(function(){ if(!binded499){ $(window).bind('beforeunload',function(){ binded499 = true; return "xxxx"; }); } var newCode = $(this).val() code499 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code499!=newCode){ // window.frames["iframe499"].document.write("
"); // window.frames["iframe499"].document.write(decodeHtml($("textarea#stepcodeTextarea499").val())); // window.frames["iframe499"].document.close(); // $(window.frames["iframe499"]).load(function(){ // $("#iframe499").height($("#iframe499").contents().find("body").height()+showittryitheight); // }); // code499 = newCode; // } }); $(".tryButton499").click(function(){ $("#tryDiv499").show(); $("#stepcodeTextarea499").focus(); $("#stepcodeTextarea499").height(200); $("#iframe499").height(0); window.frames["iframe499"].document.write("
"); window.frames["iframe499"].document.write(decodeHtml($("textarea#stepcodeTextarea499").val())); window.frames["iframe499"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe499"]).load(function(){ $("#iframe499").height($("#iframe499").contents().find("body").height()+showittryitheight); }); $("#iframe499").height($("#iframe499").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor499.focus(); editor499.setSize(null, "250"); $("#rendering499").hide(); $("#rendered499").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor499 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea499"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor499.on("change",function(doc){ if(!binded499){ $(window).bind('beforeunload',function(){ binded499 = true; return "xxxx"; }); } var newCode = doc.getValue(); code499 = newCode; $("textarea#stepcodeTextarea499").val(newCode); if(alreadyWriteCode499!=code499){ lastModifedTime499 = new Date().getTime(); $("#rendering499").show(); $("#rendered499").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor499 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor499.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv499").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果
示例 3 :

!important

如果样式上增加了!important,则优先级最高,甚至高于style属性
"); window.frames["iframe_show500"].document.write(decodeHtml($("textarea#stepcodeTextarea500").val())); window.frames["iframe_show500"].document.close(); $(window.frames["iframe_show500"]).load(function(){ $("#iframe_show500").height($("#iframe_show500").contents().find("body").height()+showittryitheight); }); $("#iframe_show500").height($("#iframe_show500").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<style> .p1{ color:green !important; } </style> <p class="p1" style="color:red">p1 颜色是绿色,优先使用!important样式</p>
<style>
.p1{
  color:green !important;
}

</style>

<p class="p1" style="color:red">p1 颜色是绿色,优先使用!important样式</p>
"); window.frames["iframe500"].document.write(decodeHtml(code500)); window.frames["iframe500"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe500"]).load(function(){ $("#iframe500").height($("#iframe500").contents().find("body").height()+showittryitheight); }); $("#iframe500").height($("#iframe500").contents().find("body").height()+showittryitheight); alreadyWriteCode500 = code500; $("#rendering500").hide(); $("#rendered500").show(); } var tRereshRetry2DemoPanel500 = setInterval(rereshRetry2DemoPanel500,1000); var binded500 = false; $("textarea#stepcodeTextarea500").keyup(function(){ if(!binded500){ $(window).bind('beforeunload',function(){ binded500 = true; return "xxxx"; }); } var newCode = $(this).val() code500 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code500!=newCode){ // window.frames["iframe500"].document.write("
"); // window.frames["iframe500"].document.write(decodeHtml($("textarea#stepcodeTextarea500").val())); // window.frames["iframe500"].document.close(); // $(window.frames["iframe500"]).load(function(){ // $("#iframe500").height($("#iframe500").contents().find("body").height()+showittryitheight); // }); // code500 = newCode; // } }); $(".tryButton500").click(function(){ $("#tryDiv500").show(); $("#stepcodeTextarea500").focus(); $("#stepcodeTextarea500").height(200); $("#iframe500").height(0); window.frames["iframe500"].document.write("
"); window.frames["iframe500"].document.write(decodeHtml($("textarea#stepcodeTextarea500").val())); window.frames["iframe500"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe500"]).load(function(){ $("#iframe500").height($("#iframe500").contents().find("body").height()+showittryitheight); }); $("#iframe500").height($("#iframe500").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor500.focus(); editor500.setSize(null, "250"); $("#rendering500").hide(); $("#rendered500").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor500 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea500"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor500.on("change",function(doc){ if(!binded500){ $(window).bind('beforeunload',function(){ binded500 = true; return "xxxx"; }); } var newCode = doc.getValue(); code500 = newCode; $("textarea#stepcodeTextarea500").val(newCode); if(alreadyWriteCode500!=code500){ lastModifedTime500 = new Date().getTime(); $("#rendering500").show(); $("#rendered500").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor500 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor500.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv500").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果


HOW2J公众号,关注后实时获知布最新的教程和优惠活动,谢谢。


关于 前端部分-CSS-优先级 的提问

尽量提供截图代码异常信息,有助于分析和解决问题。 也可进本站QQ群交流: 620943819
提问尽量提供完整的代码,环境描述,越是有利于问题的重现,您的问题越能更快得到解答。
对教程中代码有疑问,请提供是哪个步骤,哪一行有疑问,这样便于快速定位问题,提高问题得到解答的速度
在已经存在的几千个提问里,有相当大的比例,是因为使用了和站长不同版本的开发环境导致的,比如 jdk, eclpise, idea, mysql,tomcat 等等软件的版本不一致。
请使用和站长一样的版本,可以节约自己大量的学习时间。 站长把教学中用的软件版本整理了,都统一放在了这里, 方便大家下载: http://how2j.cn/k/helloworld/helloworld-version/1718.html

上传截图