how2j.cn


工具版本兼容问题
如果把所有的css都写在html文件里面,一旦样式比较多的时候,就会显得不易维护

这个时候就会选择把所有的css内容,放在一个独立文件里

然后在html中引用该文件

通常这个文件会被命名为style.css

示例 1 : 直接在html里面写样式   
示例 2 : 把样式代码写在style.css,并在html中包含它   
示例 3 : css是本地文件 如何包含   

示例 1 :

直接在html里面写样式

样式代码写在style标签里
"); window.frames["iframe_show497"].document.write(decodeHtml($("textarea#stepcodeTextarea497").val())); window.frames["iframe_show497"].document.close(); $(window.frames["iframe_show497"]).load(function(){ $("#iframe_show497").height($("#iframe_show497").contents().find("body").height()+showittryitheight); }); $("#iframe_show497").height($("#iframe_show497").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<style> .p1{ color:red; } .span1{ color:blue; } </style> <p class="p1">红色</p> <span class="span1">蓝色</span>
<style>
.p1{
  color:red;
}

.span1{
  color:blue;
}
</style>

<p class="p1">红色</p>

<span class="span1">蓝色</span>
"); window.frames["iframe497"].document.write(decodeHtml(code497)); window.frames["iframe497"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe497"]).load(function(){ $("#iframe497").height($("#iframe497").contents().find("body").height()+showittryitheight); }); $("#iframe497").height($("#iframe497").contents().find("body").height()+showittryitheight); alreadyWriteCode497 = code497; $("#rendering497").hide(); $("#rendered497").show(); } var tRereshRetry2DemoPanel497 = setInterval(rereshRetry2DemoPanel497,1000); var binded497 = false; $("textarea#stepcodeTextarea497").keyup(function(){ if(!binded497){ $(window).bind('beforeunload',function(){ binded497 = true; return "xxxx"; }); } var newCode = $(this).val() code497 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code497!=newCode){ // window.frames["iframe497"].document.write("
"); // window.frames["iframe497"].document.write(decodeHtml($("textarea#stepcodeTextarea497").val())); // window.frames["iframe497"].document.close(); // $(window.frames["iframe497"]).load(function(){ // $("#iframe497").height($("#iframe497").contents().find("body").height()+showittryitheight); // }); // code497 = newCode; // } }); $(".tryButton497").click(function(){ $("#tryDiv497").show(); $("#stepcodeTextarea497").focus(); $("#stepcodeTextarea497").height(200); $("#iframe497").height(0); window.frames["iframe497"].document.write("
"); window.frames["iframe497"].document.write(decodeHtml($("textarea#stepcodeTextarea497").val())); window.frames["iframe497"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe497"]).load(function(){ $("#iframe497").height($("#iframe497").contents().find("body").height()+showittryitheight); }); $("#iframe497").height($("#iframe497").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor497.focus(); editor497.setSize(null, "250"); $("#rendering497").hide(); $("#rendered497").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 editor497 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea497"), { 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); } } }); editor497.on("change",function(doc){ if(!binded497){ $(window).bind('beforeunload',function(){ binded497 = true; return "xxxx"; }); } var newCode = doc.getValue(); code497 = newCode; $("textarea#stepcodeTextarea497").val(newCode); if(alreadyWriteCode497!=code497){ lastModifedTime497 = new Date().getTime(); $("#rendering497").show(); $("#rendered497").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor497 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor497.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv497").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

把样式代码写在style.css,并在html中包含它

创建一个文件叫style.css
其内容为

.p1{
color:red;
}

.span1{
color:blue;
}

然后在html中包含该文件

<link rel="stylesheet" type="text/css" href="/study/style.css" />


注:style.css文件里,就不要再使用style标签了
"); window.frames["iframe_show496"].document.write(decodeHtml($("textarea#stepcodeTextarea496").val())); window.frames["iframe_show496"].document.close(); $(window.frames["iframe_show496"]).load(function(){ $("#iframe_show496").height($("#iframe_show496").contents().find("body").height()+showittryitheight); }); $("#iframe_show496").height($("#iframe_show496").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<link rel="stylesheet" type="text/css" href="http://127.0.0.1/study/style.css" /> <p class="p1">红色</p> <span class="span1">蓝色</span>
<link rel="stylesheet" type="text/css" href="http://127.0.0.1/study/style.css" />

<p class="p1">红色</p>

<span class="span1">蓝色</span>
"); window.frames["iframe496"].document.write(decodeHtml(code496)); window.frames["iframe496"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe496"]).load(function(){ $("#iframe496").height($("#iframe496").contents().find("body").height()+showittryitheight); }); $("#iframe496").height($("#iframe496").contents().find("body").height()+showittryitheight); alreadyWriteCode496 = code496; $("#rendering496").hide(); $("#rendered496").show(); } var tRereshRetry2DemoPanel496 = setInterval(rereshRetry2DemoPanel496,1000); var binded496 = false; $("textarea#stepcodeTextarea496").keyup(function(){ if(!binded496){ $(window).bind('beforeunload',function(){ binded496 = true; return "xxxx"; }); } var newCode = $(this).val() code496 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code496!=newCode){ // window.frames["iframe496"].document.write("
"); // window.frames["iframe496"].document.write(decodeHtml($("textarea#stepcodeTextarea496").val())); // window.frames["iframe496"].document.close(); // $(window.frames["iframe496"]).load(function(){ // $("#iframe496").height($("#iframe496").contents().find("body").height()+showittryitheight); // }); // code496 = newCode; // } }); $(".tryButton496").click(function(){ $("#tryDiv496").show(); $("#stepcodeTextarea496").focus(); $("#stepcodeTextarea496").height(200); $("#iframe496").height(0); window.frames["iframe496"].document.write("
"); window.frames["iframe496"].document.write(decodeHtml($("textarea#stepcodeTextarea496").val())); window.frames["iframe496"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe496"]).load(function(){ $("#iframe496").height($("#iframe496").contents().find("body").height()+showittryitheight); }); $("#iframe496").height($("#iframe496").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor496.focus(); editor496.setSize(null, "250"); $("#rendering496").hide(); $("#rendered496").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 editor496 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea496"), { 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); } } }); editor496.on("change",function(doc){ if(!binded496){ $(window).bind('beforeunload',function(){ binded496 = true; return "xxxx"; }); } var newCode = doc.getValue(); code496 = newCode; $("textarea#stepcodeTextarea496").val(newCode); if(alreadyWriteCode496!=code496){ lastModifedTime496 = new Date().getTime(); $("#rendering496").show(); $("#rendered496").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor496 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor496.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv496").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

css是本地文件 如何包含

在测试的时候,大家写的css文件都是放在本地的,比如d:/style.css
这时就应该写成
href="file://d:/style.css"
css是本地文件 如何包含


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


关于 前端部分-CSS-css文件 的提问

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

上传截图