简体   繁体   English

在DIV中对齐3个文本(左,中和右)

[英]Aligning 3 texts (left, center and right) in a DIV

I am trying to make a sort of progress bar that gets filled, with text inside that is on the left, in the center, and SOMETIMES on the right. 我正在尝试制作一种填充了进度条的程序,其内部的文本在左侧,中间,而SOMETIMES在右侧。

I got it (almost) working, my only issue so far is that sometimes the text in the middle gets too long, and so it gets spanned out of the div. 我(几乎)使它工作了,到目前为止,我唯一的问题是有时中间的文本太长,因此它超出了div。 Meaning it wraps around, and takes sort of 2 lines, but there is still place in the main div. 意思是它回绕,需要2条线,但是在主div中仍然存在。

This is the code, maybe someone can help me fix this and improve it a little: 这是代码,也许有人可以帮助我解决此问题并进行一些改进:

<div class="progress progressSize">
    <div style="width: 50%;" class="progressFill"></div>
    <div class="progressText">
        <span class="leftText">Left Text</span>
        <span class="centerText">Center text that gets too long</span>
        <span class="rightText">Right Text</span>
    </div>
</div>

And for the CSS: 对于CSS:

.progress {
    border: 1px solid #004b91;  
    background-color: #FFFFFF;
    position: relative;
}

.progressSize {
    width: 500px;
    height: 20px;
}

.progressFill {
    background-color: #EAF3FE;
    height: 100%;
    position: absolute;
}

.progressText {
    padding-left: 10px;
    padding-right: 10px;
    padding-top: 2px;
    position: relative;
}

.leftText {
    float: left;
    width: 33%;
}

.centerText {
    float: left;
    text-align: center;
    width: 33%;
}

.rightText {
    float: right;
    text-align: right;
}

So my issue is with the centerText. 所以我的问题是centerText。 The text in the middle is too big, so it spans 2 lines, but it's not big enough to fill the whole bar. 中间的文本太大,因此横跨两行,但不足以填满整个栏。 Because I reserve 33% for each: left, center and right text, the center text is placed in the middle but it has like a "bound". 因为我为左,中间和右文本各保留33%的内容,所以将中心文本放在中间,但它就像一个“绑定”。

I am not sure how to fix that. 我不确定该如何解决。 Could anyone please help me? 谁能帮我吗?

Thank you, 谢谢,

Rudy 鲁迪

An alternative to using float for this issue is to use absolute and relative positioning, I've found-- I'm in a situation where I need to do this without float and this has helped me solve my issue. 我发现使用浮点数的另一种方法是使用绝对和相对定位,我处于一种需要不使用浮点数的情况,这已经帮助我解决了问题。

http://www.bennadel.com/blog/2541-most-css-floats-can-be-replaced-with-relative-and-absolute-positioning.htm http://www.bennadel.com/blog/2541-most-css-floats-can-be-replaced-with-relative-and-absolute-positioning.htm

Maybe overflow:hidden in combination with height:1em would help? 也许overflow:hidden结合height:1em会有所帮助? This will crop the text that is too long. 这将裁切太长的文本。

.centerText {
  float: left;
  text-align: center;
  width: 33%;
  overflow:hidden;
  height: 1em;
}

Your procressSize CSS should be: 您的procressSize CSS应该是:

.progressSize {
    width: 500px;
    min-height:20px;
    height: auto;
    overflow:auto;
}

This will increase the height of the progress div to contain the text 这将增加进度div的高度以包含文本

Edit but if you want the div height to remain the same, don't have the width:33% for the centerText div and keep the progressSize CSS the way i have mentioned 编辑,但如果您希望div高度保持不变,请不要为width设置width:33%的centerText div,并按照我前面提到的方式保留progressSize CSS

.progressSize {
    width: 500px;
    min-height:20px;
    height: auto;
    overflow:auto;
}

.centerText {
    float: left;
    text-align: center;
    min-width: 33%; // initially, width:33%
    height:auto;
}

The min-width is just a minimum-width so that the div does not shrink below 33% (But it will not work in IE6) 最小宽度只是最小宽度,因此div不会缩小到33%以下(但在IE6中将无法使用)

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

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