简体   繁体   English

没有空格时断字

[英]Break word when no space

I've got the following div structure in my html layout. 我的html布局中有以下div结构。

<div class='main-container'>
    <h5 class='hotel-name'></h5>
    <div class='hotel-price'></div>
</div>

and the css is as following for each element. 每个元素的css如下。

.main-container {
    position: relative;
    border-bottom: 1px solid #EBEBEB;
    height: 55px;
}

.hotel-name {
    font-size: 13px;
    font-weight: bold;
    position: relative;
    padding: 10px 0 0 20px;
    display: inline-block;
    white-space: nowrap;
}

.hotel-price {
    display: inline;
    float: right;
    font-size: 100%;
    font-weight: bold;
    height: 55px;
    padding: 6px 25px;
    border-top: none;
    border-right: 1px solid #EBEBEB;
    border-bottom: 1px solid #EBEBEB;
    border-left: 1px solid #EBEBEB;
    -webkit-backface-visibility: hidden;
    -webkit-perspective: 1000;
    color: #3CA7B4;
}

I've not defined any width to any element. 我尚未为任何元素定义任何宽度。 The content in each element is drawn dynamically. 每个元素中的内容都是动态绘制的。 for example, the following image shows how the structure looks after all the data is loaded. 例如,下图显示了加载所有数据后结构的外观。

在此处输入图片说明

but my problem is there are some occasions where the name is long and the price just drops down. 但我的问题是,在某些情况下,名称很长,而价格却下降了。

example image of the issue. 问题的示例图片。

在此处输入图片说明

How can I fix this issue. 我该如何解决此问题。 I'm building this layout using bootstrap 3.1 and my custom css. 我正在使用bootstrap 3.1和我的自定义css构建此布局。

Can you update your CSS? 您可以更新CSS吗?

.hotel-name {
    font-size: 13px;
    font-weight: bold;
    position: relative;
    padding: 10px 0 0 20px;
    display: inline-block;
    white-space: nowrap;
    word-wrap: break-word;      // <- New Line
}

Here's the reference on MDN 这是MDN上的参考

试试这个,使用clearfix类

<p class="clearfix"></p>

Here is a working example using Flex box. 这是一个使用Flex框的工作示例。 Flexbox is the new standard of designing and aligning. Flexbox是设计和对齐的新标准。

http://jsfiddle.net/maxspan/t4QuH/ http://jsfiddle.net/maxspan/t4QuH/

    #mainDiv{
    display:flex;
    flex-wrap: nowrap;
    width: 700px;
    background-color: lightgray;
}
#innerDiv{
    box-shadow:box-shadow: 2px 2px 2px #888888;
    width:200px;
    height:200px;
    border:1px;
    background-color: green;
    padding:12px;
}

Using float is not a good idea if you just want the price to stay in the right lower corner. 如果只希望价格保持在右下角,则使用float并不是一个好主意。 position:absolute div in a position:relative div will help you better position your price tag. 位置:绝对div位置:相对div将帮助您更好地定位价格标签。

Example: 例:

.main-container {
    position: relative;
    border-bottom: 1px solid #EBEBEB;
    height: 55px;
}

.hotel-price {
    display: inline;
// ========== Here is the change =================
    position: absolute;
    bottom: 0px;
    right: 0px;
// ===============================================
    font-size: 100%;
    font-weight: bold;
    height: 55px;
    padding: 6px 25px;
    border-top: none;
    border-right: 1px solid #EBEBEB;
    border-bottom: 1px solid #EBEBEB;
    border-left: 1px solid #EBEBEB;
    -webkit-backface-visibility: hidden;
    -webkit-perspective: 1000;
    color: #3CA7B4;
}

You can try with display:table-cell css property 您可以尝试使用display:table-cell css属性

.hotel-name {
    font-size: 13px;
    font-weight: bold;
    position: relative;
    padding: 10px 0 0 20px;
    display: table-cell;
    white-space: nowrap;
}

.hotel-price {
    display:  table-cell;
    font-size: 100%;
    font-weight: bold;
    height: 55px;
    padding: 6px 25px;
    border-top: none;
    border-right: 1px solid #EBEBEB;
    border-bottom: 1px solid #EBEBEB;
    border-left: 1px solid #EBEBEB;
    -webkit-backface-visibility: hidden;
    -webkit-perspective: 1000;
    color: #3CA7B4;
}

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

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