高于 1 行时 "more..."的 HTML DIV 或 SPAN 标记

标签 html css

是否有 divspan 标签或 CSS 会显示“更多...”(如果它们之间的文本高于 1 行)然后允许您单击更多...展开以显示全部?我有一排图标,每个图标下面有一个段落,我只需要显示一行文本(这将根据浏览器的宽度而有所不同)。

最佳答案

这可以做到 -不是很实用,因为它需要正确处理,而且“显示更多”将在文本 block 下方 - 通过使用 :checked 用于 checkbox,但是 checkbox 必须在我们要控制它的高度的段落之前,给 checkbox display:none 并为它制作 label 作为切换 Controller ,就像这样:

JS Fiddle

.para{
  max-height:20px;
  overflow:hidden; /* needed to hide the rest of the content */
  transition:all 1s;
}
.chk{
  display:none; /* hide the checkbox */
}

/* if the checkboxed is checked, we show the following .para div by
   increasing the max height property 
*/
.chk:checked ~ .para{
  max-height:500px;
  transition:all 1s;
}

/* change the text to "Show Less" if the full text is shown */
.chk:checked ~ .lbl::after{
  content:'Show Less';
}
.lbl{
  color:blue;
  text-decoration:underline;
  cursor:pointer;
}

/* by default the text is "Show More" as long as the checkbox is not checked*/
.lbl::after{
  content:'Show More';
}
<div class="container">
  <input type="checkbox" id="chkbx1" class="chk">
  <div class="para">
    Fruitcake toffee oat cake cheesecake chocolate sesame snaps. Cookie topping pie powder croissant gummies. Biscuit gingerbread toffee chocolate cake cupcake. Sesame snaps marzipan pudding chupa chups candy croissant chocolate dragée. Jelly-o bonbon gummies jelly ice cream tiramisu bonbon. Cake candy apple pie fruitcake wafer fruitcake. Tart gummies cake.
  </div>
  <label for="chkbx1" class="lbl"></label>
</div>

关于高于 1 行时 "more..."的 HTML DIV 或 SPAN 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34911565/

相关文章:

javascript - 单击放大文本区域

java - Spring Boot 休息服务 |不支持请求方法 'GET'

javascript - "Styles"选项卡在 chromium 开发者工具中不显示任何 css 规则

html - 使用第一个类型的 css 为 Bootstrap 选项卡中的第一个选项卡设置边距

CSS 相对于固定位置 - CSS transition

html - IE8 不呈现某些 HTML 名称实体

javascript - html窗口通过javascript打开关闭

html - 调整大小时干净地隐藏页面元素

html - 如何在两列之间有固定的间隔?

html - 显示图像、一些用省略号修剪的文本,然后是一个图标