html li padding,求大神来看为li元素设置相同的padding为何padding-bottom和padding-right为多出一部分_html/css_WEB-ITnose...

代码如下 无标题文档

回复讨论(解决方案)

哪里有问题

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

哪里有问题

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吧,修改如下

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

仅在 Firefox 中发现这个问题

原因已经证实:

当 div2 出现垂直滚动条时, 将压缩 ul 和 li 的宽度, 而每一个 li 内容没有发生变化, 当宽度减小时, 其中文字占用的行数就有可能增加, 这样就需要更大的 height 才能容纳, 此时 Chrome, IE 都忽略了 height 被设成的固定的值, 而是将它重新计算并调整;

但 Firefox 的处理方式却不一样, 它没有忽略 height 那个已被设定的固定的值, 而是维持那个值, 不做重新计算并调整, 所以就造成最后文本超出边界的问题. 而最新插入的 li 是在插入的时候(无论有(已有/将有)无滚动条)自动计算出的 height, 所以唯一最新的 li 不会存在文本超出边界的问题.

解决方案:

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

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

无标题文档

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

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吧,修改如下 是的,我不用offsetHeight就可以了

可以用 function getStyle(obj,attr){if(obj.currentStyle)return obj.currentStyle[attr];elsereturn getComputedStyle(obj,false)[attr];}这个函数来获取li的高度就不会包括padding

谢谢,问题解决了!

声明:本文原创发布php中文网,转载请注明出处,感谢您的尊重!如有疑问,请联系admin@php.cn处理

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值