【问题标题】:Hide text that is outside div隐藏 div 之外的文本
【发布时间】:2017-08-11 04:07:46
【问题描述】:

我正在尝试创建上面的设计。这是我到目前为止所取得的成就,但我使用的是固定高度。

https://jsfiddle.net/iDaniel19/b0LL5wj8/1/

<div class="container">
<div class="row event">
    <div class="col-xs-3 col-sm-2 col-md-1 event-date">
        <h3>9</h3>
        <h3>May</h3>
    </div>
    <div class="col-xs-9 col-sm-10 col-md-1 no-padding-left event-info">
        <div class="event-info-title">
            <h4>Title</h4>
        </div>
        <div class="event-info-tl">
            <div class="event-info-tl-text">
                <p><span class="glyphicon glyphicon-time" aria-hidden="true"></span> Time</p>
                <p><span class="fa fa-map-marker" aria-hidden="true"></span>Post code</p>
            </div>
            <div class="event-info-tl-icon">
                <p class="event-icon"><span class="fa fa-chevron-right" aria-hidden="true"></span></p>
                <div style="clear: both;"></div>
            </div>
        </div>
    </div>
</div>

如何确保文字保持在一行而不显示在外面?

例如在上图中,底线(时间和位置)不适合移动设备,并且会在容器外中断

这是为该图像编码的正确方法吗?

【问题讨论】:

    标签: html css twitter-bootstrap twitter-bootstrap-3


    【解决方案1】:

    updated fiddle

    尝试按以下方式进行操作 - 它不会中断小型设备,而是会创建一个新行。

    .body {
        font-family: 'Open Sans';
    }
    
    
    
    .no-padding-left {
        padding-left:0;
    }
    
    .float-right {
        float:right;
    }
    .event{
        height:70px;
    }
    .event-date {
        border-right: 1px solid black;
        text-align: center;
    }
    
    .event-date > h3 {
        font-weight: bold;
        text-transform: uppercase;
        padding:0;
        margin:0;
        line-height: 34px;
    }
    
    .event-info-title {
        height: 35px;
    }
    
    .event-info-title > h4{
        padding: 0 0 0 5px;
        font-weight: 600;
        letter-spacing: 2px;
        text-transform: uppercase;
        margin:0;
        top:25%;
        position: relative;
    }
    
    .event-info-tl {
        height: 35px;
        border-top: 1px solid black;
    }
    .event-info-tl > div {
        display: inline-block;
    }
    
    .event-info-tl-text {
        top:25%;
        position: relative;
    }
    
    .event-info-tl-text > p {
        display: inline-block;
        margin: 0;
    }
    .event-info-tl-text > p:first-child{
        padding-left: 5px;
    }
    .event-info-tl-icon {
        float:right;
        margin:0;
    }
    .event-icon {
        padding:5px;
        font-size: 20px;
        margin:0;
    }
    
    @media (max-width: 576px) {
        .container {
            padding:0;
        }
    }
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <!-- Latest compiled and minified JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
    <div class="container">
        <div class="row event">
            <div class="col-xs-3 col-sm-2 col-md-1 event-date">
                <h3>9</h3>
                <h3>May</h3>
            </div>
            <div class="col-xs-9 col-sm-10 col-md-1 no-padding-left event-info">
                <div class="event-info-title">
                    <h4>Title</h4>
                </div>
                <div class="event-info-tl ">
                    <div class="event-info-tl-text ">
                    <div class="col-xs-12 col-sm-6">
                    
                   
                        <p><span class="glyphicon glyphicon-time" aria-hidden="true"></span> Time</p>
                         </div>
                         <div class="col-xs-12 col-sm-6">
                        <p><span class="fa fa-map-marker" aria-hidden="true"></span>Post code</p>
                         </div>
                    </div>
                    <div class="event-info-tl-icon">
                        <p class="event-icon"><span class="fa fa-chevron-right" aria-hidden="true"></span></p>
                        <div style="clear: both;"></div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    【讨论】:

    • 我试图避免这种情况。如果你inspect element 你会看到文本超出了容器的高度。
    • 您可以增加容器的高度或使用媒体查询并在小型设备中使用较小的字体。
    【解决方案2】:

    您可以使用顶部/底部填充在标题/时间/邮政编码文本上方/下方创建空间并移除固定高度。绝对定位右箭头。并在容器上使用 white-space: nowrap 来显示您不想中断的 2 行文本。

    .body {
      font-family: 'Open Sans';
    }
    
    .no-padding-left {
      padding-left: 0!important;
    }
    
    .float-right {
      float: right;
    }
    
    .event-date {
      border-right: 1px solid black;
      text-align: center;
    }
    
    .event-date > h3 {
      font-weight: bold;
      text-transform: uppercase;
      padding: 0;
      margin: 0;
      line-height: 34px;
    }
    
    .event-info-title,
    .event-info-tl {
      padding: .5em 0;
    }
    
    .event-info-title > h4 {
      padding: 0 0 0 5px;
      font-weight: 600;
      letter-spacing: 2px;
      text-transform: uppercase;
      margin: 0;
      top: 25%;
      position: relative;
    }
    
    .event-info-tl {
      border-top: 1px solid black;
      position: relative;
      overflow: hidden;
    }
    
    .event-info-tl > div {
      display: inline-block;
    }
    
    .event-info-tl-text {
      top: 25%;
      position: relative;
      white-space: nowrap;
    }
    
    .event-info-tl-text > p {
      display: inline-block;
      margin: 0;
    }
    
    .event-info-tl-text > p:first-child {
      padding-left: 5px;
    }
    
    .event-info-tl-icon {
      position: absolute;
      top: .5em;
      right: 0;
    }
    
    .event-icon {
      font-size: 20px;
      margin: 0;
    }
    
    @media (max-width: 576px) {
      .container {
        padding: 0;
      }
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
    <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
    
    <div class="container">
        <div class="row event">
            <div class="col-xs-3 col-sm-2 col-md-1 event-date">
                <h3>9</h3>
                <h3>May</h3>
            </div>
            <div class="col-xs-9 col-sm-10 col-md-1 no-padding-left event-info">
                <div class="event-info-title">
                    <h4>Title</h4>
                </div>
                <div class="event-info-tl">
                    <div class="event-info-tl-text">
                        <p><span class="glyphicon glyphicon-time" aria-hidden="true"></span> Time</p>
                        <p><span class="fa fa-map-marker" aria-hidden="true"></span> Post code</p>
                    </div>
                    <div class="event-info-tl-icon">
                        <p class="event-icon"><span class="fa fa-chevron-right" aria-hidden="true"></span></p>
                        <div style="clear: both;"></div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    【讨论】:

    • 在你的情况下,如果我添加很多文本而不是 post code,它会在箭头下方永远向右移动。
    • @BogdanDaniel 你希望发生什么?您说您希望所有内容都集中在一条线上。
    • 但不要离开 div。我想我用text-overflow: ellipsis; 解决了它
    • @BogdanDaniel 是的,只需修改溢出属性。更新了我的答案,将overflow: hidden 添加到.event-info-tl
    【解决方案3】:

    使用overflow 隐藏它怎么样?我添加了最后这两个类:

    .event-info-tl-text{width:90%; line-height: 1.1em; height: 1.1em; overflow:hidden;white-space: nowrap;}
    .event-info-tl-icon{width:5%}
    

    这里有一个fiddle

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-09
      • 1970-01-01
      • 2018-12-07
      • 2014-08-30
      • 2014-09-08
      • 2014-09-07
      • 2013-03-04
      • 2013-06-03
      相关资源
      最近更新 更多