You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

input文本溢出显示省略号后,悬停无法展示完整文本求助

解决Input元素悬停显示完整截断文本的问题

先指出你代码里的两个关键问题:

  1. <input> 标签不支持 for 属性,这个属性属于 <label> 元素,用来关联对应的表单控件,你的HTML写法存在语义错误。
  2. 直接给input设置 overflow: visible 无效——因为input是替换元素,浏览器对这类元素的溢出渲染有特殊规则,普通overflow样式无法覆盖其默认行为。

下面给你两种可行的解决方案:

方案一:利用title属性(最简单)

直接给input添加title属性,将其值设为input的内容,鼠标悬停时浏览器会自动弹出完整文本的tooltip,无需调整复杂样式。

修改后的代码:

HTML

<div class="tasks">
<div class="task1">
    <input type="checkbox" id="button1"/>
    <input class="task-text" value="这里是一段超长的测试文本,长度超过280px会被截断显示省略号" title="这里是一段超长的测试文本,长度超过280px会被截断显示省略号">
    <button class="material-symbols-outlined star" style="font-size:20px;">star</button>
    <button type="button" class="material-symbols-outlined delete" style="font-size:20px;">delete</button>
</div>

CSS

.task-text {
    border: none;
    border-radius: 5px;
    background-color: var(--bgTasks-color);
    outline: none;
    color: var(--text-color);
    font-size: 16px;
    width: 280px;
    overflow: hidden;
    white-space: nowrap; 
    text-overflow: ellipsis;
    cursor: pointer;
}

如果是动态生成的内容,用JS同步value和title:

const taskInput = document.querySelector('.task-text');
taskInput.title = taskInput.value;
// 若内容可编辑,监听输入事件实时同步
taskInput.addEventListener('input', () => {
    taskInput.title = taskInput.value;
});

方案二:用容器包裹实现宽幅展开效果

如果不想用tooltip,希望文本直接展开显示,可以给input套一个容器,通过容器控制截断逻辑,hover时让input自动撑开宽度:

修改后的代码:

HTML

<div class="tasks">
<div class="task1">
    <input type="checkbox" id="button1"/>
    <div class="task-text-container">
        <input class="task-text" value="这里是一段超长的测试文本,长度超过280px会被截断显示省略号">
    </div>
    <button class="material-symbols-outlined star" style="font-size:20px;">star</button>
    <button type="button" class="material-symbols-outlined delete" style="font-size:20px;">delete</button>
</div>

CSS

.task-text-container {
    width: 280px;
    overflow: hidden;
    white-space: nowrap;
    display: inline-block;
    position: relative;
}

.task-text {
    border: none;
    border-radius: 5px;
    background-color: var(--bgTasks-color);
    outline: none;
    color: var(--text-color);
    font-size: 16px;
    width: 100%;
    cursor: pointer;
    white-space: nowrap;
}

.task-text-container:hover .task-text {
    width: auto;
    position: absolute;
    background-color: var(--bgTasks-color);
    z-index: 1;
    /* 可选:添加阴影增强层级感 */
    box-shadow: 0 0 3px rgba(0,0,0,0.2);
}

这个方案的核心是用容器模拟文本截断效果,hover时让input脱离文档流(absolute)并自动适配内容宽度,避免打乱原有布局。

内容的提问来源于stack exchange,提问作者Msamericana1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 03:52:09