简体   繁体   English

CSS float:left和溢出:可见的截断文本

[英]CSS float:left and overflow:visible cutting off text

I'm currently trying to make a color gradient in javascript with numerical values in some of the divs to represent as the scale. 我目前正在尝试在javascript中使用一些div中的数字值来表示颜色渐变,以表示比例。 However, I've noticed with larger values, the numbers get cut off due to the float:left. 但是,我注意到使用较大的值时,由于float:left而使数字被截断。 I've also tried used display:inline-block, but it seems that has weird positioning and leaves gaps between them. 我也尝试过使用display:inline-block,但是看起来位置很奇怪,并且在它们之间留有空隙。 I need the divs flush together, but just have the text overflow ontop of the following div. 我需要将div齐平,但是在下面的div上放文本溢出。 Is there a way to do this? 有没有办法做到这一点?

Both examples I mentioned are here: 我提到的两个示例都在这里:

http://jsfiddle.net/y3LTZ/3/ http://jsfiddle.net/y3LTZ/3/

<div style="overflow: visible; width: 600px; height: 30px;white-space: nowrap;">
    <div style="width:20px;height:100%;overflow: visible;display:inline-block;background-color:red;">texts</div>
    <div style="width:20px;height:100%;overflow: visible;display:inline-block;background-color:green;"></div>
    <div style="width:20px;height:100%;overflow: visible;display:inline-block;background-color:red;">texts</div>
    <div style="width:20px;height:100%;overflow: visible;display:inline-block;background-color:green;"></div>
    <div style="width:20px;height:100%;overflow: visible;display:inline-block;background-color:red;">texts</div>
</div>

and

<div style="overflow: visible; width: 600px; height: 30px;white-space: nowrap;">
    <div style="width: 20px;height:100%;float:left;background-color:red;">texts</div>
    <div style="width: 20px;height:100%;float:left;background-color:green;"></div>
    <div style="width: 20px;height:100%;float:left;background-color:red;">texts</div>
    <div style="width: 20px;height:100%;float:left;background-color:green;"></div>
    <div style="width: 20px;height:100%;float:left;background-color:red;">texts</div>
</div>

Thanks! 谢谢!

DEMO 演示

using inline-block 使用内联块

HTML 的HTML

<div class="wrapper">
    <div class="single-block red">texts</div>
    <div class="single-block green"></div>
    <div class="single-block red">texts</div>
    <div class="single-block green"></div>
    <div class="single-block red">texts</div>
</div>

CSS 的CSS

.wrapper{
    width: 600px; 
    height: 30px;
    white-space: nowrap;
    font-size:0;
}
.single-block{
   width:20px;
   height:100%;
   display:inline-block;
   vertical-align:bottom;
    font-size:16px;
}
.red{
    background-color:red;
    position:relative;
}
.green{
    background-color:green;
}

If you want the text overlap the following div, you have to give them position:absolute; 如果希望文本与以下div重叠,则必须给它们position:absolute;

<div style="position: relative; overflow: visible; width: 600px; height: 30px;white-space: nowrap;">
    <div class="column" style="background-color:red;">
        <div class="textontop">texts</div>
    </div>
    <div class="column" style="background-color:green;"></div>
    <div class="column" style="background-color:red;">
        <div class="textontop">texts</div>
    </div>
    <div class="column" style="background-color:green;"></div>
    <div class="column" style="background-color:red;">
        <div class="textontop">texts</div>
    </div>
</div>

Css: CSS:

.column {
    width:20px;
    height:100%;
    overflow: visible;
    float:left;
    z-index:0;
}

.textontop {
    position:absolute;
    z-index:999;
}

Jsfiddle: http://jsfiddle.net/y3LTZ/5/ Jsfiddle: http : //jsfiddle.net/y3LTZ/5/

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

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