代码之家  ›  专栏  ›  技术社区  ›  J86

跨设备的响应文本

  •  -1
  • J86  · 技术社区  · 9 年前

    我正在我的歌词网站(我的一个附带项目)的艺术家名单页面上工作。以下是我目前拥有的:

    artists list

    代码

    <div class="row artists">
      <div class="col-md-2 col-6">
        <div class="artist-card">
          <div class="artist-image">
            <img src="http://placehold.it/200x200" alt="Rihanna" />
          </div>
          <div class="artist-info">
            <div class="artist-name">
              <h2>Rihanna</h2>
            </div>
          </div>
        </div>
      </div><!-- .artist-card -->
      <!-- more artist cards go here -->
    </div><!-- .artists -->
    

    以下是Sass风格:

    $artist-card-background-color: #bdbbb0;
    
    .artist-card {
      position: relative;
      background-color: $artist-card-background-color;
      border: none;
      border-radius: 2px;
      margin-bottom: 15px;
      cursor: pointer;
    
      .artist-image {
        img {
          width: 100%;
        }
      }
    
      .artist-info {
        position: absolute;
        bottom: 0;
        left: 0;
        width: 100%;
        background-color: rgba(0, 0, 0, 0.8);
    
        .artist-name {
          h2 {
            margin: 0;
            color: #fff;
            padding: 10px 15px;
          }
        }
      }
    }
    

    我还创建了一个 Codepen 让你们可以实验这些卡片,以及它们当前在不同视口中的行为。

    我可以用什么最干净的方式使标题易读(可读),但无论视口大小如何,标题的高度都相同?

    1 回复  |  直到 9 年前
        1
  •  2
  •   Mattias H    9 年前

    备选方案1

    如果文本超过宽度,则截断文本。

        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    

    备选方案2

    使用类似 http://fittextjs.com/ 或 http://simplefocus.com/flowtype 根据字体长度动态更改字体大小。

    备选方案3

    结合上述2种方法,仅当文本变小无法阅读时才截断文本。