搜索
首页web前端html教程求大神来看为li元素设置相同的padding为何padding-bottom和padding-right为多出一部分_html/css_WEB-ITnose

代码如下

<!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>


回复讨论(解决方案)


哪里有问题

看了半天没看懂你问的是什么


哪里有问题


ul上下的padding是一样的,但是下面的空白明显大于上面

看了半天没看懂你问的是什么


ul上下的padding一样,但是下面的空白明显大于上面

这是 offsetHeight 的高度定义 和 li 的 box-sizing 是 content-box 相矛盾造成的

The offsetHeight property returns the viewable height of an element in pixels, including padding, border and scrollbar
就是说 offsetHeight 包含了 内容, padding-top, padding-bottom, border-top, border-bottom, 以及涉及 scroollbar 的高度
box-sizing: content-box 指定元素的高度只包含内容, 而不含其他.
你的代码中, li 的高度之后被重设, 并且总是比期望的要多出 padding-top + padding-bottom 的和.
而之后,  padding-top 和 padding-bottom 并没有变化, 都是你指定的 10px, 这多出的部分被加到了 content 的高度上, 但content 的字体并没有变大, 而且文字又没有被处理成在 content 中垂直居中, 所以看起来上下空白就不一样了

所以可以加入以下 CSS, 在 scroollbar 不存在的情况下, 就可以解决了

li{    box-sizing: 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.



这个定义表明 box-sizing 的默认值是 content-box; box-sizing 的值将影响 css 中 height  的定义-- 垂直方向上的 padding 和 border 究竟会不会包含在 height 中

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.



这个定义表明 box-sizing 的默认值是 content-box; box-sizing 的值将影响 css 中 height  的定义-- 垂直方向上的 padding 和 border 究竟会不会包含在 height 中

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.



这个定义表明 box-sizing 的默认值是 content-box; box-sizing 的值将影响 css 中 height  的定义-- 垂直方向上的 padding 和 border 究竟会不会包含在 height 中
但是如果 出现了滚动条,页面就会变成这个样子

看了半天没看懂你问的是什么


ul上下的padding一样,但是下面的空白明显大于上面 你这里又给他加了高度
//运动            var iheight=oli.offsetHeight;            alert(iheight);            oli.style.height="0";            move(oli,{height:iheight});

但是如果 出现了滚动条,页面就会变成这个样子



我分别在 Chrome 42, IE8, Firefox 39.0 测试, 只发现 Firefox 存在这个问题

下面多余的部分不全是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>

但是如果 出现了滚动条,页面就会变成这个样子



仅在 Firefox 中发现这个问题

原因已经证实:  

当 div2 出现垂直滚动条时, 将压缩 ul 和 li 的宽度, 而每一个 li 内容没有发生变化, 当宽度减小时, 其中文字占用的行数就有可能增加, 这样就需要更大的 height 才能容纳, 此时 Chrome, IE 都忽略了 height 被设成的固定的值, 而是将它重新计算并调整;
但 Firefox 的处理方式却不一样, 它没有忽略  height 那个已被设定的固定的值, 而是维持那个值, 不做重新计算并调整, 所以就造成最后文本超出边界的问题.    而最新插入的 li 是在插入的时候(无论有(已有/将有)无滚动条)自动计算出的 height, 所以唯一最新的 li 不会存在文本超出边界的问题.

解决方案:

在 每一次插入新的 li 之前, 将上一个 li 的 height 恢复为浏览器自动计算

经测试, Chrome 42, IE8, Firefox 39.0 都没有问题

<!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>


看了半天没看懂你问的是什么


ul上下的padding一样,但是下面的空白明显大于上面 你这里又给他加了高度
//运动            var iheight=oli.offsetHeight;            alert(iheight);            oli.style.height="0";            move(oli,{height:iheight});
这儿是为了实现一个滑动效果,和padding没关系吧,何况最终的高度并没有改变


但是如果 出现了滚动条,页面就会变成这个样子



我分别在 Chrome 42, IE8, Firefox 39.0 测试, 只发现 Firefox 存在这个问题
chrome43,IE11也存在问题

下面多余的部分不全是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
谢谢,问题解决了!
声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
HTML与CSS vs. JavaScript:比较概述HTML与CSS vs. JavaScript:比较概述Apr 16, 2025 am 12:04 AM

HTML、CSS和JavaScript在网页开发中的角色分别是:HTML负责内容结构,CSS负责样式,JavaScript负责动态行为。1.HTML通过标签定义网页结构和内容,确保语义化。2.CSS通过选择器和属性控制网页样式,使其美观易读。3.JavaScript通过脚本控制网页行为,实现动态和交互功能。

HTML:是编程语言还是其他?HTML:是编程语言还是其他?Apr 15, 2025 am 12:13 AM

HTMLISNOTAPROGRAMMENGUAGE; ITISAMARKUMARKUPLAGUAGE.1)htmlStructures andFormatSwebContentusingtags.2)itworkswithcsssforstylingandjavascript for Interactivity,增强WebevebDevelopment。

HTML:建立网页的结构HTML:建立网页的结构Apr 14, 2025 am 12:14 AM

HTML是构建网页结构的基石。1.HTML定义内容结构和语义,使用、、等标签。2.提供语义化标记,如、、等,提升SEO效果。3.通过标签实现用户交互,需注意表单验证。4.使用、等高级元素结合JavaScript实现动态效果。5.常见错误包括标签未闭合和属性值未加引号,需使用验证工具。6.优化策略包括减少HTTP请求、压缩HTML、使用语义化标签等。

从文本到网站:HTML的力量从文本到网站:HTML的力量Apr 13, 2025 am 12:07 AM

HTML是一种用于构建网页的语言,通过标签和属性定义网页结构和内容。1)HTML通过标签组织文档结构,如、。2)浏览器解析HTML构建DOM并渲染网页。3)HTML5的新特性如、、增强了多媒体功能。4)常见错误包括标签未闭合和属性值未加引号。5)优化建议包括使用语义化标签和减少文件大小。

了解HTML,CSS和JavaScript:初学者指南了解HTML,CSS和JavaScript:初学者指南Apr 12, 2025 am 12:02 AM

WebDevelovermentReliesonHtml,CSS和JavaScript:1)HTMLStructuresContent,2)CSSStyleSIT和3)JavaScriptAddSstractivity,形成thebasisofmodernWebemodernWebExexperiences。

HTML的角色:构建Web内容HTML的角色:构建Web内容Apr 11, 2025 am 12:12 AM

HTML的作用是通过标签和属性定义网页的结构和内容。1.HTML通过到、等标签组织内容,使其易于阅读和理解。2.使用语义化标签如、等增强可访问性和SEO。3.优化HTML代码可以提高网页加载速度和用户体验。

HTML和代码:仔细观察术语HTML和代码:仔细观察术语Apr 10, 2025 am 09:28 AM

htmlisaspecifictypefodyfocusedonstructuringwebcontent,而“代码” badlyLyCludEslanguagesLikeLikejavascriptandPytyPythonForFunctionality.1)htmldefineswebpagertuctureduseTags.2)“代码”代码“ code” code code code codeSpassSesseseseseseseseAwiderRangeLangeLangeforLageforLogageforLogicIctInterract

HTML,CSS和JavaScript:Web开发人员的基本工具HTML,CSS和JavaScript:Web开发人员的基本工具Apr 09, 2025 am 12:12 AM

HTML、CSS和JavaScript是Web开发的三大支柱。1.HTML定义网页结构,使用标签如、等。2.CSS控制网页样式,使用选择器和属性如color、font-size等。3.JavaScript实现动态效果和交互,通过事件监听和DOM操作。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
4 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
4 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
1 个月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它们
1 个月前By尊渡假赌尊渡假赌尊渡假赌

热工具

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器