Home >Web Front-end >HTML Tutorial >Please let me know why the padding-bottom and padding-right are extra when setting the same padding for the li element_html/css_WEB-ITnose
The code is as follows
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>无标题文档</title><style> body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,form,fieldset,input,textarea,p,blockquote,th,td,img {padding:0;margin:0;} #div1{ margin:10px; width:300px; height:200px; } #div1 textarea{ width:300px; height:180px; } #div2{ margin:10px auto; height:400px; width:400px; border:1px solid black; overflow:auto; } #div2 ul li{ border-bottom:1px dashed black; list-style:none; padding:10px 10px 10px 10px; }</style><script type="text/javascript"> window.onload=function(){ var adiv1=document.getElementById("div1"); var adiv2=document.getElementById("div2"); var obtn=adiv1.getElementsByTagName("input")[0]; var otxt=adiv1.getElementsByTagName("textarea")[0]; var oul=document.getElementById("ul1"); obtn.onclick=function(){ var oli=document.createElement("li"); oli.innerHTML=otxt.value; otxt.value=""; if(oul.children.length>0) oul.insertBefore(oli,oul.children[0]); else oul.appendChild(oli); //运动 var iheight=oli.offsetHeight; alert(iheight); oli.style.height="0"; move(oli,{height:iheight}); }; }; function getStyle(obj,attr){ if(obj.currentStyle) return obj.currentStyle[attr]; else return getComputedStyle(obj,false)[attr]; } function move(obj,json,fun){ clearInterval(obj.timer); obj.timer=setInterval(function(){ var astop=true; for(var attr in json){ var cur=0; if(attr=="opacity") cur=Math.round(parseFloat(getStyle(obj,attr))*100); else cur=parseInt(getStyle(obj,attr)); var speed=(json[attr]-cur)/10; speed=speed>0?Math.ceil(speed):Math.floor(speed); if(cur!=json[attr]) astop=false; if(attr=="opacity"){ if(obj.style.filter) obj.style.filter="alpha(opacity:"+(speed+cur)+")"; else obj.style.opacity=(speed+cur)/100; } else obj.style[attr]=speed+cur+"px"; } if(astop){ clearInterval(obj.timer); if(fun) fun(); } },30); }</script></head><body> <div id="div1"> <form> <textarea id="text1"></textarea> <input type="button" value="发布" id="btn"/> </form> </div> <div id="div2"> <ul id="ul1"> </ul> </div></body></html>
What’s the problem
After reading it for a long time I don’t understand what you are asking
What’s the problem
After reading for a long time, I didn’t understand what you were asking.
This is the height definition of offsetHeight and the box- of li sizing is caused by the conflict of content-box
The offsetHeight property returns the viewable height of an element in pixels, including padding, border and scrollbar
That is to say, offsetHeight contains content, padding-top, padding- bottom, border-top, border-bottom, and the height involving scrollbar
box-sizing: the height of the content-box specified element only contains the content, and not others.
In your code, after the height of li is reset, and the sum of padding-top and padding-bottom is always more than expected.
After that, padding-top and padding-bottom have not changed, they are both the 10px you specified, and this extra Part is added to the height of content, but the font of content does not become larger, and the text is not processed to be vertically centered in content, so it looks like the top and bottom spaces are different
So you can add the following CSS, when scrollbar does not exist, it can be solved
li{ box-sizing: border-box;}
The definition of box-sizing on W3C is quoted
3.1. box-sizing property
Name: box-sizing
Value: content-box | border-box
Initial: content-box
content-box
This is the behavior of width and height as specified by CSS2.1. The specified width and height (and respective min/max properties) apply to the width and height respectively of the content box of the element. The padding and border of the element are laid out and drawn outside the specified width and height .
border-box
Length and percentages values for width and height (and respective min/max properties) on this element determine the border box of the element. That is, any padding or border specified on the element is laid out and drawn inside this specified width and height. The content width and height are calculated by subtracting the border and padding widths of the relevant sides from the specified width and height properties. As the content width and height cannot be be negative ([ CSS21], section 10.2), this computation is floored at 0. Used values, as exposed for instance through getComputedStyle(), also refer to the border box.
W3C 上 box-sizing 的定义引用
3.1. box-sizing property
Name: box-sizing
Value: content-box | border-box
Initial: content-box
content-box
This is the behavior of width and height as specified by CSS2.1. The specified width and height (and respective min/max properties) apply to the width and height respectively of the content box of the element. The padding and border of the element are laid out and drawn outside the specified width and height.
border-box
Length and percentages values for width and height (and respective min/max properties) on this element determine the border box of the element. That is, any padding or border specified on the element is laid out and drawn inside this specified width and height. The content width and height are calculated by subtracting the border and padding widths of the respective sides from the specified width and height properties. As the content width and height cannot be negative ([CSS21], section 10.2), this computation is floored at 0. Used values, as exposed for instance through getComputedStyle(), also refer to the border box.
W3C 上 box-sizing 的定义引用
3.1. box-sizing property
Name: box-sizing
Value: content-box | border-box
Initial: content-box
content-box
This is the behavior of width and height as specified by CSS2.1. The specified width and height (and respective min/max properties) apply to the width and height respectively of the content box of the element. The padding and border of the element are laid out and drawn outside the specified width and height.
border-box
Length and percentages values for width and height (and respective min/max properties) on this element determine the border box of the element. That is, any padding or border specified on the element is laid out and drawn inside this specified width and height. The content width and height are calculated by subtracting the border and padding widths of the respective sides from the specified width and height properties. As the content width and height cannot be negative ([CSS21], section 10.2), this computation is floored at 0. Used values, as exposed for instance through getComputedStyle(), also refer to the border box.
看了半天没看懂你问的是什么
//运动 var iheight=oli.offsetHeight; alert(iheight); oli.style.height="0"; move(oli,{height:iheight});
但是如果 出现了滚动条,页面就会变成这个样子
下面多余的部分不全是padding,还有一部分是li的高。
oli.offsetHeight获取的值是包括padding在内的,但是oli.style.height这样赋值的高是不包括padding的。
最简单的解决方法就是减掉padding,即var iheight=oli.offsetHeight-20;
但这样解决效果不太好,因为oli.style.height=“0”;时其实padding部分还是看的见的。
要效果好,也要简单就用jquery吧,修改如下
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script><!--+++++++++++++加载jquery++++++++++++++--><script type="text/javascript"> window.onload=function(){ var adiv1=document.getElementById("div1"); var adiv2=document.getElementById("div2"); var obtn=adiv1.getElementsByTagName("input")[0]; var otxt=adiv1.getElementsByTagName("textarea")[0]; var oul=document.getElementById("ul1"); obtn.onclick=function(){ var oli=document.createElement("li"); oli.style.display="none";//+++++++++++++++++++++先隐藏++++++++++++++++++++ oli.innerHTML=otxt.value; otxt.value=""; if(oul.children.length>0) oul.insertBefore(oli,oul.children[0]); else oul.appendChild(oli); $(oli).slideDown(800);//++++++++++++++++用jquery方法展开li++++++++++++++++++++ /*-------------------------------这段不需要了------------------------------------------ //运动 var iheight=oli.offsetHeight-20; alert(iheight); oli.style.height="0"; move(oli,{height:iheight}); */ }; };</script>
但是如果 出现了滚动条,页面就会变成这个样子
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>无标题文档</title><style> body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,form,fieldset,input,textarea,p,blockquote,th,td,img {padding:0;margin:0;} #div1{ margin:10px; width:300px; height:200px; } #div1 textarea{ width:300px; height:180px; } #div2{ margin:10px auto; height:400px; width:400px; border:1px solid black; overflow:auto; } #div2 ul li{ border-bottom:1px dashed black; list-style:none; padding:10px 10px 10px 10px; box-sizing: border-box; }</style><script type="text/javascript"> window.onload=function(){ var adiv1=document.getElementById("div1"); var adiv2=document.getElementById("div2"); var obtn=adiv1.getElementsByTagName("input")[0]; var otxt=adiv1.getElementsByTagName("textarea")[0]; var oul=document.getElementById("ul1"); obtn.onclick=function(){ var oli=document.createElement("li"); oli.innerHTML=otxt.value; otxt.value=""; if(oul.children.length>0) { oul.children[0].style.height = 'auto'; // 将上一个 li 的 height 恢复为浏览器自动计算 oul.insertBefore(oli,oul.children[0]); } else oul.appendChild(oli); //运动 var iheight=oli.offsetHeight; alert(iheight); oli.style.height="0"; move(oli,{height:iheight}); }; }; function getStyle(obj,attr){ if(obj.currentStyle) return obj.currentStyle[attr]; else return getComputedStyle(obj,false)[attr]; } function move(obj,json,fun){ clearInterval(obj.timer); obj.timer=setInterval(function(){ var astop=true; for(var attr in json){ var cur=0; if(attr=="opacity") cur=Math.round(parseFloat(getStyle(obj,attr))*100); else cur=parseInt(getStyle(obj,attr)); var speed=(json[attr]-cur)/10; speed=speed>0?Math.ceil(speed):Math.floor(speed); if(cur!=json[attr]) astop=false; if(attr=="opacity"){ if(obj.style.filter) obj.style.filter="alpha(opacity:"+(speed+cur)+")"; else obj.style.opacity=(speed+cur)/100; } else obj.style[attr]=speed+cur+"px"; } if(astop){ clearInterval(obj.timer); if(fun) fun(); } },30); }</script></head><body> <div id="div1"> <form> <textarea id="text1"></textarea> <input type="button" value="发布" id="btn"/> </form> </div> <div id="div2"> <ul id="ul1"> </ul> </div></body></html>
看了半天没看懂你问的是什么
//运动 var iheight=oli.offsetHeight; alert(iheight); oli.style.height="0"; move(oli,{height:iheight});这儿是为了实现一个滑动效果,和padding没关系吧,何况最终的高度并没有改变
但是如果 出现了滚动条,页面就会变成这个样子
下面多余的部分不全是padding,还有一部分是li的高。
oli.offsetHeight获取的值是包括padding在内的,但是oli.style.height这样赋值的高是不包括padding的。
最简单的解决方法就是减掉padding,即var iheight=oli.offsetHeight-20;
但这样解决效果不太好,因为oli.style.height=“0”;时其实padding部分还是看的见的。
要效果好,也要简单就用jquery吧,修改如下
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script><!--+++++++++++++加载jquery++++++++++++++--><script type="text/javascript"> window.onload=function(){ var adiv1=document.getElementById("div1"); var adiv2=document.getElementById("div2"); var obtn=adiv1.getElementsByTagName("input")[0]; var otxt=adiv1.getElementsByTagName("textarea")[0]; var oul=document.getElementById("ul1"); obtn.onclick=function(){ var oli=document.createElement("li"); oli.style.display="none";//+++++++++++++++++++++先隐藏++++++++++++++++++++ oli.innerHTML=otxt.value; otxt.value=""; if(oul.children.length>0) oul.insertBefore(oli,oul.children[0]); else oul.appendChild(oli); $(oli).slideDown(800);//++++++++++++++++用jquery方法展开li++++++++++++++++++++ /*-------------------------------这段不需要了------------------------------------------ //运动 var iheight=oli.offsetHeight-20; alert(iheight); oli.style.height="0"; move(oli,{height:iheight}); */ }; };</script>是的,我不用offsetHeight就可以了
function getStyle(obj,attr){ if(obj.currentStyle) return obj.currentStyle[attr]; else return getComputedStyle(obj,false)[attr]; }这个函数来获取li的高度就不会包括padding