how2j.cn


工具版本兼容问题
相对定位
属性:position
值:relative

示例 1 : 相对定位   
示例 2 : 练习-相对定位,但是又不占用位置   
示例 3 : 答案-相对定位,但是又不占用位置   

示例 1 :

相对定位

属性:position
值: relative
与绝对定位不同的是,相对定位不会把该元素从原文档删除掉,而是在原文档的位置的基础上,移动一定的距离
"); window.frames["iframe_show487"].document.write(decodeHtml($("textarea#stepcodeTextarea487").val())); window.frames["iframe_show487"].document.close(); $(window.frames["iframe_show487"]).load(function(){ $("#iframe_show487").height($("#iframe_show487").contents().find("body").height()+showittryitheight); }); $("#iframe_show487").height($("#iframe_show487").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<style> p.r{ position: relative; left: 150px; top: 50px; } </style> <p >正常文字1</p> <p >正常文字2</p> <p class="r" >相对定位的文字3</p> <p >正常文字4</p> <p >正常文字5</p>
<style>
p.r{
  position: relative;
  left: 150px;
  top: 50px;
}
 
</style>
 
<p >正常文字1</p>
<p >正常文字2</p>
<p class="r" >相对定位的文字3</p>
<p >正常文字4</p>
<p >正常文字5</p>
"); window.frames["iframe487"].document.write(decodeHtml(code487)); window.frames["iframe487"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe487"]).load(function(){ $("#iframe487").height($("#iframe487").contents().find("body").height()+showittryitheight); }); $("#iframe487").height($("#iframe487").contents().find("body").height()+showittryitheight); alreadyWriteCode487 = code487; $("#rendering487").hide(); $("#rendered487").show(); } var tRereshRetry2DemoPanel487 = setInterval(rereshRetry2DemoPanel487,1000); var binded487 = false; $("textarea#stepcodeTextarea487").keyup(function(){ if(!binded487){ $(window).bind('beforeunload',function(){ binded487 = true; return "xxxx"; }); } var newCode = $(this).val() code487 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code487!=newCode){ // window.frames["iframe487"].document.write("
"); // window.frames["iframe487"].document.write(decodeHtml($("textarea#stepcodeTextarea487").val())); // window.frames["iframe487"].document.close(); // $(window.frames["iframe487"]).load(function(){ // $("#iframe487").height($("#iframe487").contents().find("body").height()+showittryitheight); // }); // code487 = newCode; // } }); $(".tryButton487").click(function(){ $("#tryDiv487").show(); $("#stepcodeTextarea487").focus(); $("#stepcodeTextarea487").height(200); $("#iframe487").height(0); window.frames["iframe487"].document.write("
"); window.frames["iframe487"].document.write(decodeHtml($("textarea#stepcodeTextarea487").val())); window.frames["iframe487"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe487"]).load(function(){ $("#iframe487").height($("#iframe487").contents().find("body").height()+showittryitheight); }); $("#iframe487").height($("#iframe487").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor487.focus(); editor487.setSize(null, "250"); $("#rendering487").hide(); $("#rendered487").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 editor487 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea487"), { 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); } } }); editor487.on("change",function(doc){ if(!binded487){ $(window).bind('beforeunload',function(){ binded487 = true; return "xxxx"; }); } var newCode = doc.getValue(); code487 = newCode; $("textarea#stepcodeTextarea487").val(newCode); if(alreadyWriteCode487!=code487){ lastModifedTime487 = new Date().getTime(); $("#rendering487").show(); $("#rendered487").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor487 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor487.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv487").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

练习-相对定位,但是又不占用位置

练习难度
Or  姿势不对,事倍功半! 点击查看做练习的正确姿势
相对定位的文字3 在原来的位置的基础上,向右移动150个像素,但是又不占用原来的坑。

结合相对定位和绝对定位实现这个效果
"); window.frames["iframe_show3080"].document.write(decodeHtml($("textarea#stepcodeTextarea3080").val())); window.frames["iframe_show3080"].document.close(); $(window.frames["iframe_show3080"]).load(function(){ $("#iframe_show3080").height($("#iframe_show3080").contents().find("body").height()+showittryitheight); }); $("#iframe_show3080").height($("#iframe_show3080").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
<style> div.r{ position: relative; left: 0; top: 0; } div.a{ position: absolute; left: 150px; top: 0px; } </style> <p>正常文字1</p> <p>正常文字2</p> <div class="r"> <div class="a">相对定位的文字3(不占坑)</div> </div> <p>正常文字4</p> <p>正常文字5</p>
"); window.frames["iframe3080"].document.write(decodeHtml(code3080)); window.frames["iframe3080"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe3080"]).load(function(){ $("#iframe3080").height($("#iframe3080").contents().find("body").height()+showittryitheight); }); $("#iframe3080").height($("#iframe3080").contents().find("body").height()+showittryitheight); alreadyWriteCode3080 = code3080; $("#rendering3080").hide(); $("#rendered3080").show(); } var tRereshRetry2DemoPanel3080 = setInterval(rereshRetry2DemoPanel3080,1000); var binded3080 = false; $("textarea#stepcodeTextarea3080").keyup(function(){ if(!binded3080){ $(window).bind('beforeunload',function(){ binded3080 = true; return "xxxx"; }); } var newCode = $(this).val() code3080 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code3080!=newCode){ // window.frames["iframe3080"].document.write("
"); // window.frames["iframe3080"].document.write(decodeHtml($("textarea#stepcodeTextarea3080").val())); // window.frames["iframe3080"].document.close(); // $(window.frames["iframe3080"]).load(function(){ // $("#iframe3080").height($("#iframe3080").contents().find("body").height()+showittryitheight); // }); // code3080 = newCode; // } }); $(".tryButton3080").click(function(){ $("#tryDiv3080").show(); $("#stepcodeTextarea3080").focus(); $("#stepcodeTextarea3080").height(200); $("#iframe3080").height(0); window.frames["iframe3080"].document.write("
"); window.frames["iframe3080"].document.write(decodeHtml($("textarea#stepcodeTextarea3080").val())); window.frames["iframe3080"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe3080"]).load(function(){ $("#iframe3080").height($("#iframe3080").contents().find("body").height()+showittryitheight); }); $("#iframe3080").height($("#iframe3080").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor3080.focus(); editor3080.setSize(null, "250"); $("#rendering3080").hide(); $("#rendered3080").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 editor3080 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea3080"), { 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); } } }); editor3080.on("change",function(doc){ if(!binded3080){ $(window).bind('beforeunload',function(){ binded3080 = true; return "xxxx"; }); } var newCode = doc.getValue(); code3080 = newCode; $("textarea#stepcodeTextarea3080").val(newCode); if(alreadyWriteCode3080!=code3080){ lastModifedTime3080 = new Date().getTime(); $("#rendering3080").show(); $("#rendered3080").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor3080 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor3080.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv3080").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

答案-相对定位,但是又不占用位置

在查看答案前,尽量先自己完成,碰到问题再来查看答案,收获会更多


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


关于 前端部分-CSS-相对定位 的提问

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

上传截图