热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

css溢出隐藏增加容器的高度-cssoverflowhiddenincreasesheightofcontainer

Pleasehavealookatthisfiddle-http:jsfiddle.netZ27hC请看看这个小提琴-http:jsfiddle.netZ27hC

Please have a look at this fiddle - http://jsfiddle.net/Z27hC/

请看看这个小提琴 - http://jsfiddle.net/Z27hC/

var cOntainer= document.createElement('span');
container.style.display = 'inline-block';
container.style.marginTop = '10px';
container.style.marginLeft = '50px';
container.style.background = 'lightblue';
document.body.appendChild(container);

var cell = document.createElement('span');
cell.style.display = 'inline-block';
cell.style.border = ' 2px solid black';
cell.style.width = '200px';
cell.style.height = '16px';
cell.style.overflow = 'hidden';
container.appendChild(cell);

var text_node = document.createTextNode('hallo');
cell.appendChild(text_node);

I have a container, containing a cell, containing a text node. The cell has a fixed width.

我有一个容器,包含一个包含文本节点的单元格。单元具有固定的宽度。

If the text passed to the text node exceeds the width, I want it to be truncated, so I set the cell to 'overflow: hidden'.

如果传递给文本节点的文本超出宽度,我希望它被截断,所以我将单元格设置为'overflow:hidden'。

It works, but it causes the height of the cell to increase by 3px. The cell has a border, but the increased height appears below the border, not inside it.

它有效,但它会使细胞的高度增加3px。单元格有边框,但增加的高度显示在边框下方,而不是在边框内。

As I have many cells in a spreadsheet style, it messes up the layout.

由于我在电子表格样式中有许多单元格,因此会混淆布局。

I have tested this on IE8 and Chrome, with the same result.

我在IE8和Chrome上测试了这个,结果相同。

Any of the following solutions would be ok -

任何以下解决方案都可以 -

  • prevent the increased height from occurring
  • 防止高度增加
  • keep the increased height inside the border
  • 保持边界内增加的高度
  • another way to truncate the text
  • 截断文本的另一种方法

As requested, here is a new fiddle that shows a more complete example.

根据要求,这是一个新的小提琴,显示了一个更完整的例子。

http://jsfiddle.net/frankmillman/fA3wy/

http://jsfiddle.net/frankmillman/fA3wy/

I hope it is self-explanatory. Let me know if you need anything explained in more detail.

我希望这是不言自明的。如果您需要更详细的解释,请告诉我。

Here is (hopefully) my final fiddle -

这是(希望)我最后的小提琴 -

http://jsfiddle.net/frankmillman/RZQQ8/

http://jsfiddle.net/frankmillman/RZQQ8/

It incorporates ideas from all responders, and it now works as I want.

它融合了所有响应者的想法,现在可以按照我的意愿运作。

There are two main changes.

主要有两个变化。

The first one was inspired by Mathias' table solution. Instead of an intermediate container, containing a blank row and a data row, one of which was hidden and one shown, I now just have alternating blank and data rows in the top-level container. I don't think it affected my layout problem, but it cut out a layer and simplified the code.

第一个是Mathias的表解决方案的灵感来源。而不是包含空行和数据行的中间容器,其中一个是隐藏的,一个是显示的,我现在只在顶层容器中有交替的空白和数据行。我认为它不会影响我的布局问题,但它会删除一个图层并简化代码。

The second change, suggested by all the responders, actually fixed the problem. Instead of having elements of type 'span' with display 'inline-block', I now use 'div', and a careful mix of 'float left' and 'float right' to achieve the layout I want.

所有响应者建议的第二个变化实际上解决了这个问题。我没有使用显示'内联块'的'span'类型的元素,而是使用'div',并小心地混合'float left'和'float right'来实现我想要的布局。

Many thanks to all.

非常感谢大家。

4 个解决方案

#1


25  

Let me explain to you why this is happening.

让我向你解释为什么会这样。

According to CSS 2.1 Specs,

根据CSS 2.1 Specs,

The baseline of an 'inline-block' is the baseline of its last line box in the normal flow, unless it has either no in-flow line boxes or if its 'overflow' property has a computed value other than 'visible', in which case the baseline is the bottom margin edge.

“内联块”的基线是正常流程中其最后一个线框的基线,除非它没有流入线框或者其“溢出”属性具有“可见”以外的计算值,在哪种情况下,基线是底部边缘边缘。

To explain in simple words,

用简单的话来解释,

i) If inline-block in question has its overflow property set to visible (which is by default so no need to set though). Then its baseline would be the baseline of the containing block of the line. ii) If inline-block in question has its overflow property set to OTHER THAN visible. Then its bottom margin would be on the baseline of the line of containing box.

i)如果有问题的内联块将其溢出属性设置为可见(默认情况下不需要设置)。然后它的基线将是该线的包含块的基线。 ii)如果有问题的内联块的溢出属性设置为OTHER可见。然后它的底部边距将位于包含框的线的基线上。

So, in your case the inline-block cell has overflow:hidden (not VISIBLE), so its margin-bottom, the border of cell is at the baseline of the container element container.

因此,在您的情况下,内联块单元格具有溢出:隐藏(不可见),因此其边距底部,单元格边框位于容器元素容器的基线处。

That is why the element cell looks pushed upwards and the height of container appears increased. You can avoid that by setting cell to display:block.

这就是元素单元看起来向上推动并且容器高度增加的原因。您可以通过将单元格设置为display:block来避免这种情况。

#2


11  

The overflow-property can change the baseline of an element. To prevent this you can use vertical-align on inline-blocks.

overflow-property可以更改元素的基线。为防止这种情况,您可以在内联块上使用vertical-align。

cell.style.display = 'inline-block';
cell.style.overflow = 'hidden';
cell.style.verticalAlign = 'bottom';

http://jsfiddle.net/23e0rny9/

http://jsfiddle.net/23e0rny9/

#3


4  

Try setting line-height: 0 for the container and line-height: 20pxfor your cells

尝试设置line-height:0表示容器,行高:20px表示单元格

container.style.lineHeight = '0px';

cell.style.lineHeight = '20px';

Fiddle

小提琴

Or you can use float: left on your cells

或者你可以在你的细胞上使用float:left

cell.style.float = 'left';

Fiddle2

Fiddle2

#4


1  

use the dsiplay 'block' of cell

使用单元格的dsiplay'block'

cell.style.display = 'block';

and

cell.style.float= 'left';

to align left if you want to align

如果要对齐,则对齐左侧


推荐阅读
  • Voicewo在线语音识别转换jQuery插件的特点和示例
    本文介绍了一款名为Voicewo的在线语音识别转换jQuery插件,该插件具有快速、架构、风格、扩展和兼容等特点,适合在互联网应用中使用。同时还提供了一个快速示例供开发人员参考。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • Webpack5内置处理图片资源的配置方法
    本文介绍了在Webpack5中处理图片资源的配置方法。在Webpack4中,我们需要使用file-loader和url-loader来处理图片资源,但是在Webpack5中,这两个Loader的功能已经被内置到Webpack中,我们只需要简单配置即可实现图片资源的处理。本文还介绍了一些常用的配置方法,如匹配不同类型的图片文件、设置输出路径等。通过本文的学习,读者可以快速掌握Webpack5处理图片资源的方法。 ... [详细]
  • 本文介绍了C#中数据集DataSet对象的使用及相关方法详解,包括DataSet对象的概述、与数据关系对象的互联、Rows集合和Columns集合的组成,以及DataSet对象常用的方法之一——Merge方法的使用。通过本文的阅读,读者可以了解到DataSet对象在C#中的重要性和使用方法。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • javascript  – 概述在Firefox上无法正常工作
    我试图提出一些自定义大纲,以达到一些Web可访问性建议.但我不能用Firefox制作.这就是它在Chrome上的外观:而那个图标实际上是一个锚点.在Firefox上,它只概述了整个 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • 后台获取视图对应的字符串
    1.帮助类后台获取视图对应的字符串publicclassViewHelper{将View输出为字符串(注:不会执行对应的ac ... [详细]
  • 《数据结构》学习笔记3——串匹配算法性能评估
    本文主要讨论串匹配算法的性能评估,包括模式匹配、字符种类数量、算法复杂度等内容。通过借助C++中的头文件和库,可以实现对串的匹配操作。其中蛮力算法的复杂度为O(m*n),通过随机取出长度为m的子串作为模式P,在文本T中进行匹配,统计平均复杂度。对于成功和失败的匹配分别进行测试,分析其平均复杂度。详情请参考相关学习资源。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • 深入理解CSS中的margin属性及其应用场景
    本文主要介绍了CSS中的margin属性及其应用场景,包括垂直外边距合并、padding的使用时机、行内替换元素与费替换元素的区别、margin的基线、盒子的物理大小、显示大小、逻辑大小等知识点。通过深入理解这些概念,读者可以更好地掌握margin的用法和原理。同时,文中提供了一些相关的文档和规范供读者参考。 ... [详细]
  • JDK源码学习之HashTable(附带面试题)的学习笔记
    本文介绍了JDK源码学习之HashTable(附带面试题)的学习笔记,包括HashTable的定义、数据类型、与HashMap的关系和区别。文章提供了干货,并附带了其他相关主题的学习笔记。 ... [详细]
  • 本文介绍了GregorianCalendar类的基本信息,包括它是Calendar的子类,提供了世界上大多数国家使用的标准日历系统。默认情况下,它对应格里高利日历创立时的日期,但可以通过调用setGregorianChange()方法来更改起始日期。同时,文中还提到了GregorianCalendar类为每个日历字段使用的默认值。 ... [详细]
author-avatar
大额资金一号
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有