使用 text-overflow: ellipsis溢出文本显示省略号时碰到的小问题_html/css_WEB-ITnose
本人刚刚实习,第一次写东西,希望大家多多鼓励。
项目中需要实现标题超过一定长度以省略号的形式显示,不是什么难的问题。可是我不想用js实现,就百度了发现text-overflow: ellipsis;(其实很久就有了,不过之前都不知道)这个样式可以实现这种效果,而且所有主流浏览器都支持 text-overflow 属性。
于是就用了如下代码
.ellipsis{ width:300px; white-space: nowrap; word-break: break-all; overflow: hidden; text-overflow: ellipsis;}
可是效果出现了一点问题
这个问题让我很费解,后来我试试加了一个float:left;居然好了
我想text-overflow和float应该没有关系吧,我又试了试display:block,display: inline-block发现也行,
我又去百度了一下发现使用text-overflow: ellipsis时必须得给作用的元素设置宽度,而行内元素直接设置宽度是不可以的,而我是用的标签,之所以我float:left可以实现效果是因为行内元素在使用了float后会变成块元素,如果我加了display:block效果是一样的,所以碰到这种情况只要将要显示的元素设置成块元素就可以了。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

The article discusses the <iframe> tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit
