简体   繁体   English

如果文本打破了框的宽度,如何将文本移动到新行?

[英]How can I move text to a new line if it breaks the width of a box?

Please take a look at this first: http://jsfiddle.net/TWbWx/2/ 请先看看这个: http//jsfiddle.net/TWbWx/2/

HTML: HTML:

<div class="box">
    <div class="price">HUF129031290310</div>
</div>

CSS: CSS:

.box {
    background-color:#00FF7F;
    height: 300px;
    width:120px;
}

What I want is when the price goes past the width of the box the numbers should go under the currency. 我想要的是当价格超过盒子的宽度时,数字应该低于货币。 How can I do this? 我怎样才能做到这一点? The first step is separating the currency and the amount in separate divs but I'm unsure where to go from there. 第一步是在单独的div中分离货币和金额,但我不确定从那里去哪里。 Any help would be great. 任何帮助都会很棒。

You could combine the use of <span> elements, which are inherently inline-block , with word-wrap:break-word . 您可以将<span>元素(本质上是inline-block )与word-wrap:break-word

HTML: HTML:

<div class="box">
    <div class="price">
        <span>HUF</span>
        <span>12944444</span>
    </div>
</div>

CSS: CSS:

.box {
    background-color:#00FF7F;
    height: 300px;
    width:120px;
    word-wrap: break-word;    
}

Try it out yourself . 自己尝试一下

I know the answer to this has already been given, but i rather thought i will go the java script way rather than depending on CSS. 我知道已经给出了答案,但我更倾向于认为我将采用java脚本方式而不是依赖于CSS。 So here's my code : 所以这是我的代码:

CSS: CSS:

.box {
    background-color:#00FF7F;
    height: 300px;
    width:120px;
    font-size:16px;
}

JS : JS:

//method to find width of text
String.prototype.width = function(font) {
  var f = font || '12px arial',
      o = $('<div>' + this + '</div>')
            .css({'position': 'absolute', 'float': 'left', 'white-space': 'nowrap', 'visibility': 'hidden', 'font': f})
            .appendTo($('body')),
      w = o.width();

  o.remove();

  return w;
}

var boxWidth = $('.box').width();
var priceFontSize = $('.price').css("font-size");
var priceWidth = $('.price').text().width($('.price').css("font-size"));
var priceText =  $('.price').text();
var matches = priceText.match(/\d+$/);
var number = matches[0];
var matchesText = priceText.match(/^\D+/);
var textSplit = matchesText[0];

//Test if width of text is greater than the box width
if(priceWidth  > boxWidth){
    $('.price').html(textSplit+'</br>'+number);
}

As you can see from the code above that, i am comparing the text width with the box width and if the condition succeeds , then i break the text as a whole and place one after another. 正如您从上面的代码中看到的那样,我将文本宽度与框宽度进行比较,如果条件成功,那么我将整个文本分解并一个接一个地放置。 Nifty little trick. 漂亮的小技巧。

Here's the working fiddle for you : http://jsfiddle.net/TWbWx/25/ 这是你的工作小提琴: http//jsfiddle.net/TWbWx/25/

http://jsfiddle.net/TWbWx/21/ http://jsfiddle.net/TWbWx/21/

HTML HTML

<div class="box">
   <div class="price">HUF</div>
   <div class="price">129031290310</div>
</div>

CSS CSS

.box {
   background-color:#00FF7F;
   height: 300px;
   width:120px;
}

.price{float:left;}

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

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