简体   繁体   English

css溢出可见和z-index

[英]css overflow visible and z-index

I have the following HTML and CSS: 我有以下HTML和CSS:

<style>
 .field { display: inline-block; width: 100px; overflow: hidden; white-space: nowrap; margin-left: 15px; }
 .field:hover { overflow: visible; z-index: 100; }
</style>

<span style="display: block; white-space: nowrap;"> 
 <span class="field"> field 1 - Some long text in here that gets cut off.</span>
 <span class="field"> field 2 - Some long text in here that gets cut off.</span>
</span>

Preview: http://jsfiddle.net/RpLQk/ 预览: http//jsfiddle.net/RpLQk/

If you hover over field 1, the overflow text is shown, but since the background-color is transparent the text of field 2 bleeds through, and it looks terrible. 如果将鼠标悬停在字段1上,则会显示溢出文本,但由于背景颜色是透明的,因此字段2的文本会渗透,并且看起来很糟糕。 I need field 1 to cover field 2 during hover. 在悬停期间我需要字段1来覆盖字段2。

I've tried setting the background-color, but the color is only applied to the original element size, not the newly displayed overflow. 我已尝试设置背景颜色,但颜色仅应用于原始元素大小,而不是新显示的溢出。 I've tried z-index: 1000 and position: relative, to no avail. 我试过z-index:1000和position:relative,无济于事。

Also, I need this to work in IE 9. 另外,我需要这个在IE 9中工作。

Any help is appreciated. 任何帮助表示赞赏。

The issue is that you're trying to get the background for .field to expand further than 100px, but the width is set to 100px . 问题是你试图让.field的背景扩展超过100px,但宽度设置为100px Adding an inner span will allow you to expand the background with the text. 添加内部跨度将允许您使用文本扩展背景。

Try this: 尝试这个:

HTML: HTML:

<div class="wrapper"> 
    <span class="field"><span>field 1 - Some long text in here that gets cut off.</span></span>
    <span class="field"><span>field 2 - Some long text in here that gets cut off.</span></span>
</div>

No reason to have <span style="display: block"> , should use <div> instead. 没有理由使用<span style="display: block"> ,而应该使用<div>

CSS: CSS:

body { background: #ccc; }
.wrapper { white-space: nowrap; }
.wrapper .field { 
    width: 100px;
    overflow: hidden;
    display: inline-block; }
.wrapper .field:hover { 
    position: relative;    
    overflow: visible; }
.wrapper .field span { background: #fff; }

Preview: http://jsfiddle.net/Wexcode/Vm4Xg/ 预览: http//jsfiddle.net/Wexcode/Vm4Xg/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM