Table of Contents
Solution to the overflow of ellipsis in a single line of text, causing redundant background color
Home Web Front-end CSS Tutorial How to solve the problem of unnecessary background color when single line of text with background color overflows ellipsis?

How to solve the problem of unnecessary background color when single line of text with background color overflows ellipsis?

Apr 05, 2025 pm 11:18 PM
css Solution overflow

How to solve the problem of unnecessary background color when single line of text with background color overflows ellipsis?

Solution to the overflow of ellipsis in a single line of text, causing redundant background color

In CSS, when using text-overflow: ellipsis to deal with single-line text overflow, if the text has a background color, the background color residue of the last character will often occur. This is because text-overflow: ellipsis acts on the elements within the line, while the background color is applied to the text element itself, resulting in the background color being visible after truncation.

The following figure shows an example of the problem:

How to solve the problem of unnecessary background color when single line of text with background color overflows ellipsis?

Here are the CSS and HTML codes for the problem:

 .oneline {
  width: 640rpx;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  text {
    background-color: #999999;
    padding: 4rpx 8rpx;
    margin-right: 12rpx;
  }
}
Copy after login
<view class="oneline"><text wx:for="{{flavorsarr}}" wx:key="index"> {{item.label}}</text></view>
Copy after login

The solution is to modify the display method of text element to inline-block . inline-block element can be processed as a whole block, avoiding the background color residue problem caused by simple in-line element truncation.

The modified CSS code is as follows:

 .oneline {
  width: 640rpx;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  text {
    display: inline-block; /* is modified to inline-block */
    background-color: #999999;
    padding: 4rpx 8rpx;
    margin-right: 12rpx;
  }
}
Copy after login

By setting text element to inline-block , text truncation will be based on the entire element, effectively avoiding the appearance of unnecessary background colors and ensuring better visual effect of text overflowing ellipsis.

The above is the detailed content of How to solve the problem of unnecessary background color when single line of text with background color overflows ellipsis?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Clair Obscur: Expedition 33 - How To Get Perfect Chroma Catalysts
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1677
14
PHP Tutorial
1278
29
C# Tutorial
1257
24
Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

Solve caching issues in Craft CMS: Using wiejeben/craft-laravel-mix plug-in Solve caching issues in Craft CMS: Using wiejeben/craft-laravel-mix plug-in Apr 18, 2025 am 09:24 AM

When developing websites using CraftCMS, you often encounter resource file caching problems, especially when you frequently update CSS and JavaScript files, old versions of files may still be cached by the browser, causing users to not see the latest changes in time. This problem not only affects the user experience, but also increases the difficulty of development and debugging. Recently, I encountered similar troubles in my project, and after some exploration, I found the plugin wiejeben/craft-laravel-mix, which perfectly solved my caching problem.

HTML vs. CSS and JavaScript: Comparing Web Technologies HTML vs. CSS and JavaScript: Comparing Web Technologies Apr 23, 2025 am 12:05 AM

HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

Laravel8 optimization points Laravel8 optimization points Apr 18, 2025 pm 12:24 PM

Laravel 8 provides the following options for performance optimization: Cache configuration: Use Redis to cache drivers, cache facades, cache views, and page snippets. Database optimization: establish indexing, use query scope, and use Eloquent relationships. JavaScript and CSS optimization: Use version control, merge and shrink assets, use CDN. Code optimization: Use Composer installation package, use Laravel helper functions, and follow PSR standards. Monitoring and analysis: Use Laravel Scout, use Telescope, monitor application metrics.

What should I do if the Redis cache of OAuth2Authorization object fails in Spring Boot? What should I do if the Redis cache of OAuth2Authorization object fails in Spring Boot? Apr 19, 2025 pm 08:03 PM

In SpringBoot, use Redis to cache OAuth2Authorization object. In SpringBoot application, use SpringSecurityOAuth2AuthorizationServer...

How to import the source code of wordpress How to import the source code of wordpress Apr 20, 2025 am 11:24 AM

Importing WordPress source code requires the following steps: Create a sub-theme for theme modification. Import the source code and overwrite the files in the sub-topic. Activate the sub-theme to make it effective. Test the changes to make sure everything works.

How to simplify CMS development with Composer: Practical application of the Lebenlabs/SimpleCMS library How to simplify CMS development with Composer: Practical application of the Lebenlabs/SimpleCMS library Apr 18, 2025 am 09:45 AM

When developing a new content management system (CMS), I encountered a common but difficult problem: how to quickly build a fully functional CMS without adding too much complexity. There are many ready-made CMS solutions available on the market, but they are often too large and complex to configure and can be a burden for small projects. After some exploration, I discovered the lebenlabs/simplecms library, which provides a simple and efficient solution through Composer.

See all articles