网站首页 > 技术文章 正文
超出隐藏是指当某个元素内的内容,超出该元素的边界时,如何隐藏这些内容。
overflow
overflow属性用于设置元素内容溢出时的处理方式。它有以下几个可能的取值:
visible:默认值,不进行任何处理,超出部分直接显示在边界外。
hidden:隐藏溢出的内容。
scroll:添加滚动条以便浏览溢出内容。
auto:如果需要,会添加滚动条以便浏览溢出内容。
如果要实现超出隐藏,我们就要用到overflow属性,将其设置为hidden或者scroll。
text-overflow
text-overflow属性用于控制文本的溢出方式。它有以下几个可能的取值:
clip:默认值,不显示文本溢出的内容,直接截断。
ellipsis:显示一个省略号以指示文本溢出。
当我们想要控制文本的溢出时,可以使用text-overflow属性。
white-space
white-space属性用于指定元素内的空白符(空格、换行符等)如何处理。它有以下几个可能的取值:
normal:默认值,忽略多余的空白符,将连续的空白符合并为一个空格。
nowrap:不换行,每行的文本会持续向右溢出。
pre:保留所有的空白符,包括换行符和连续的空格。
pre-wrap:保留所有的空白符,但允许换行。
pre-line:合并多余的空白符,但保留换行符。
通过设置white-space属性,我们可以控制元素内文本的换行方式,进而影响元素的大小和溢出状态。
超出隐藏示例
下面定义了一个名为 hidden-ellipsis 的 class,当文本超出容器指定的宽度时,内容会被截断,并显示省略号。
<style type="text/css">
.hidden-ellipsis {
/* 隐藏溢出的内容 */
overflow: hidden;
/* 用省略号表示内容有溢出 */
text-overflow: ellipsis;
/* 强制文本不换行 */
white-space: nowrap;
}
</style>
title属性显示
CSS
<style type="text/css">
.table {
width: 500px;
margin:0 auto;
}
.table th,
.table td {
max-width: 260px;
position: relative;
}
.table th {
background-color: #783d9f;
color: #ffffff;
position: relative;
}
.table th,
.table>tbody>tr>td:first-child {
text-align: center;
}
</style>
HTML
<tr>
<td style="width:80px;">1</td>
<td style="width:160px;">title属性显示</td>
<td style="width:260px;">
<div class="hidden-ellipsis"
title="这是一段很的长文字,我的父容器设置了宽度,并且设置了超出后需要隐藏内容的样式,现在您可以看到我的渲染效果了。"
>这是一段很的长文字,我的父容器设置了宽度,并且设置了超出后需要隐藏内容的样式,现在您可以看到我的渲染效果了。
</div>
</td>
</tr>
单行显示
CSS
<style type="text/css">
.table td .move-show:hover {
position: absolute;
z-index: 999;
background-color: #fff;
border: 1px solid #ddd;
padding:5px;
margin-top:-6px;
margin-left:-6px;
border-radius:4px;
box-shadow: 0 0 10px 0 #999;
}
</style>
HTML
<tr>
<td style="width:80px;">2</td>
<td style="width:160px;">单行显示</td>
<td style="width:260px;">
<div class="hidden-ellipsis move-show">这是一段很的长文字,我的父容器设置了宽度,并且设置了超出后需要隐藏内容的样式,现在您可以看到我的渲染效果了。</div>
</td>
</tr>
多行显示
CSS
<style type="text/css">
.table td .move-show:hover {
position: absolute;
z-index: 999;
background-color: #fff;
border: 1px solid #ddd;
padding: 5px;
margin-top: -6px;
margin-left: -6px;
border-radius: 4px;
box-shadow: 0 0 10px 0 #999;
}
.table td .move-show-rows:hover {
width: 100%;
white-space: normal;
}
</style>
HTML
<tr>
<td style="width:80px;">3</td>
<td style="width:160px;">多行显示</td>
<td style="width:260px;">
<div class="hidden-ellipsis move-show move-show-rows">
这是一段很的长文字,我的父容器设置了宽度,并且设置了超出后需要隐藏内容的样式,现在您可以看到我的渲染效果了。</div>
</td>
</tr>
弹窗显示
HTML
<tr>
<td style="width:80px;">4</td>
<td style="width:160px;">弹窗显示</td>
<td style="width:260px;">
<div class="hidden-ellipsis move-show-tips">
这是一段很的长文字,我的父容器设置了宽度,并且设置了超出后需要隐藏内容的样式,现在您可以看到我的渲染效果了。</div>
</td>
</tr>
JavaScript
let layerTipsIndex;
$('.move-show-tips').hover(function(e){
layerTipsIndex = layer.tips(e.target.innerHTML, e.target, { time: 10000 });
}, function(e){
layer.close(layerTipsIndex);
})
我们搭配 layer 的 tips ,当鼠标移动到被隐藏的单元格时,显示完整内容。
完整代码示例
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>demo</title>
<link href="/cdn/libs/twitter-bootstrap/5.3.0/css/bootstrap.min.css" type="text/css" rel="stylesheet" />
<style type="text/css">
.hidden-ellipsis {
/* 强制文本不换行 */
white-space: nowrap;
/* 隐藏溢出的内容 */
overflow: hidden;
/* 用省略号表示内容有溢出 */
text-overflow: ellipsis;
}
.table {
width: 500px;
margin: 0 auto;
}
.table th,
.table td {
max-width: 260px;
position: relative;
}
.table th {
background-color: #783d9f;
color: #ffffff;
position: relative;
}
.table th,
.table>tbody>tr>td:first-child {
text-align: center;
}
.table td .move-show:hover {
position: absolute;
z-index: 999;
background-color: #fff;
border: 1px solid #ddd;
padding: 5px;
margin-top: -6px;
margin-left: -6px;
border-radius: 4px;
box-shadow: 0 0 10px 0 #999;
}
.table td .move-show-rows:hover {
width: 100%;
white-space: normal;
}
</style>
</head>
<body>
<script src="/cdn/libs/jquery/1.12.2/jquery.min.js" type="application/javascript"></script>
<script src="/cdn/libs/twitter-bootstrap/5.3.0/js/bootstrap.bundle.min.js" type="application/javascript"></script>
<script src="/cdn/libs/layer/3.5.1/layer.min.js" type="application/javascript"></script>
<div class="container mt-5">
<table class="table table-bordered">
<thead>
<tr>
<th>num</th>
<th>name</th>
<th>remark</th>
</tr>
</thead>
<tbody>
<tr>
<td style="width:80px;">1</td>
<td style="width:160px;">title属性显示</td>
<td style="width:260px;">
<div class="hidden-ellipsis" title="这是一段很的长文字,我的父容器设置了宽度,并且设置了超出后需要隐藏内容的样式,现在您可以看到我的渲染效果了。">
这是一段很的长文字,我的父容器设置了宽度,并且设置了超出后需要隐藏内容的样式,现在您可以看到我的渲染效果了。</div>
</td>
</tr>
<tr>
<td style="width:80px;">2</td>
<td style="width:160px;">单行显示</td>
<td style="width:260px;">
<div class="hidden-ellipsis move-show">这是一段很的长文字,我的父容器设置了宽度,并且设置了超出后需要隐藏内容的样式,现在您可以看到我的渲染效果了。
</div>
</td>
</tr>
<tr>
<td style="width:80px;">3</td>
<td style="width:160px;">多行显示</td>
<td style="width:260px;">
<div class="hidden-ellipsis move-show move-show-rows">
这是一段很的长文字,我的父容器设置了宽度,并且设置了超出后需要隐藏内容的样式,现在您可以看到我的渲染效果了。</div>
</td>
</tr>
<tr>
<td style="width:80px;">4</td>
<td style="width:160px;">弹窗显示</td>
<td style="width:260px;">
<div class="hidden-ellipsis move-show-tips">
这是一段很的长文字,我的父容器设置了宽度,并且设置了超出后需要隐藏内容的样式,现在您可以看到我的渲染效果了。</div>
</td>
</tr>
</tbody>
</table>
</div>
<script type="text/javascript">
let layerTipsIndex;
$('.move-show-tips').hover(function (e) {
layerTipsIndex = layer.tips(e.target.innerHTML, e.target, { time: 10000 });
}, function (e) {
layer.close(layerTipsIndex);
})
</script>
</body>
</html>
希望本文能够对您有所帮助,感谢您的阅读!
人人为我,我为人人,谢谢您的浏览,我们一起加油吧。
- 上一篇: 如何让*符号在文本框内垂直居中显示
- 下一篇: 众泰报节气门前漏气 节气门前漏气有什么症状
猜你喜欢
- 2024-12-27 汽车ABS泵体出现问题不报警,越想越害怕
- 2024-12-27 织梦DEDECMS限制标题长度后鼠标移到标题显示完整的title
- 2024-12-27 工业屏:千万不要以为电脑显示啥样,工业屏就啥样,注意色差。
- 2024-12-27 宝马320三元催化器前端烧得通红,故障灯报警
- 2024-12-27 修复Mac系统图片预览缩略图不生效的问题
- 2024-12-27 前端经验-如何在p元素中展示固定行数的文字,超出部分显示省略号
- 2024-12-27 基于 Vue+D3.js 可视化柱状动态排名
- 2024-12-27 众泰报节气门前漏气 节气门前漏气有什么症状
- 2024-12-27 如何让*符号在文本框内垂直居中显示
- 2024-12-27 关于家庭供水和增压泵的几个问题 家庭水管增压泵
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- 前端设计模式 (75)
- 前端性能优化 (51)
- 前端模板 (66)
- 前端跨域 (52)
- 前端缓存 (63)
- 前端react (48)
- 前端aes加密 (58)
- 前端脚手架 (56)
- 前端md5加密 (49)
- 前端路由 (55)
- 前端数组 (65)
- 前端定时器 (47)
- Oracle RAC (73)
- oracle恢复 (76)
- oracle 删除表 (48)
- oracle 用户名 (74)
- oracle 工具 (55)
- oracle 内存 (50)
- oracle 导出表 (57)
- oracle 中文 (51)
- oracle链接 (47)
- oracle的函数 (57)
- mac oracle (47)
- 前端调试 (52)
- 前端登录页面 (48)
本文暂时没有评论,来添加一个吧(●'◡'●)