Css网格 - 内容内联,隐藏溢出

时间:2017-05-29 01:05:09

标签: html css

这是我的HTML代码

  <div class="longtext">
    <div class="image">
      <img src="https://c1.staticflickr.com/8/7610/17149522281_3b6ae4c948_b.jpg" alt="">
    </div>
    <div class="text">
      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
    </div>
    <div class="buy">
      <a href="#">$ 3053</a>
    </div>
  </div>

我想实现内联显示所有内容,就像这样。

enter image description here

图片已定义尺寸,。text和.buy宽度未知。 .buy必须始终以单行显示。 .text也必须显示在一行上,但不必显示整个文本。

这是我的css

.longtext {
  border: 1px solid red;
  overflow: hidden;
}
.longtext > * {
  float: left;
}
.longtext img {
  height: 60px;
  width: 60px;
}
.buy {
  float: right;
  border: 1px solid blue;
}
.text {
  border: 1px solid black;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

Codepen

2 个答案:

答案 0 :(得分:2)

我会在父级align-items上使用flex-grow: 1来垂直对齐它们。将.text添加到flex: 1 0 0;(或简称为overflow/ellipsis),以便填充可用空间,并将p添加到white-space: nowrap。然后你可以从其他一切中删除浮动。同时将.buy添加到$元素,以便.longtext { border: 1px solid red; display: flex; align-items: center; } .longtext img { height: 60px; width: 60px; vertical-align: top; } .buy { border: 1px solid blue; } .text { border: 1px solid black; overflow: hidden; } .text p { text-overflow: ellipsis; overflow: hidden; } .buy, .text { white-space: nowrap; }和数字之间的空格不会中断。

 <div class="longtext">
    <div class="image">
      <img src="https://c1.staticflickr.com/8/7610/17149522281_3b6ae4c948_b.jpg" alt="">
    </div>
    <div class="text">
      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
    </div>
    <div class="buy">
      <a href="#">$ 3053</a>
    </div>
  </div>
var c = Colors.Aqua;
var newColor =  new Color {A = 50, R=c.R, G=c.G,B=c.B};

答案 1 :(得分:0)

&#13;
&#13;
.longtext {
  border: 1px solid red;
  display: flex;
}

.longtext img {
  height: 60px;
  width: 60px;
}

.longtext div{
  vertical-align: top;
}

.buy {
  border: 1px solid blue;
}

.text {
  border: 1px solid black;
  overflow: hidden;
}

.text p {
  text-overflow: ellipsis;
  overflow: hidden;
}

.buy, .text {
  white-space: nowrap;
}
&#13;
<div class="longtext">
    <div class="image">
      <img src="https://c1.staticflickr.com/8/7610/17149522281_3b6ae4c948_b.jpg" alt="">
    </div>
    <div class="text">
      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
    </div>
    <div class="buy">
      <a href="#">$ 30530000</a>
    </div>
  </div>
  
  <div class="longtext">
    <div class="image">
      <img src="https://c1.staticflickr.com/8/7610/17149522281_3b6ae4c948_b.jpg" alt="">
    </div>
    <div class="text">
      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
    </div>
    <div class="buy">
      <a href="#">$ 30500</a>
    </div>
  </div>
&#13;
&#13;
&#13;