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

CSS:无论是否设置宽度,如何让Tooltip段落右边缘对齐?

Fixing Tooltip Text Alignment & Wrapping Issues

Hey there! Let’s work through this Tooltip text layout problem you’re dealing with—this is a pretty common pain point, so I’ve got straightforward solutions for both scenarios you mentioned.

1. Fixing Right Edge Alignment When Using a Fixed Width

The large right gap after wrapping at "descriptions" happens because browsers won’t split long words by default unless you explicitly tell them to. Here’s how to make the text snugly fit your fixed width:

  • Use CSS hyphens: auto to enable automatic word hyphenation, which splits long words at appropriate points to fill the width without gaps.
  • Critical detail: Add a lang attribute to your tooltip element (e.g., lang="en" for English) so the browser knows how to hyphenate correctly for your language.

Example CSS:

.tooltip {
  width: 190px;
  hyphens: auto;
  -webkit-hyphens: auto; /* For older Chrome/Safari versions */
  text-align: left; /* Keep text aligned naturally */
}

For non-English text (like Chinese), swap hyphens with word-break: break-all; to handle continuous character strings cleanly.

2. Automatic Wrapping Without Setting a Fixed Width

To let the tooltip’s width adapt to its content while still wrapping long text, adjust its display behavior and add a maximum width constraint:

  • Set display: inline-block so the tooltip only takes up as much width as needed (instead of stretching full width).
  • Add max-width to prevent the tooltip from becoming too wide on extra-long content.
  • Use overflow-wrap: break-word to force long words to wrap even if they don’t have natural hyphenation points.

Example CSS:

.tooltip {
  display: inline-block;
  max-width: 200px; /* Adjust this to your preferred maximum width */
  overflow-wrap: break-word;
  hyphens: auto;
  -webkit-hyphens: auto;
}

This setup will make the tooltip’s width fit the text tightly until it hits the max-width, then wrap content neatly without extra gaps.

Quick Note for UI Library Users

If you’re using a pre-built tooltip component (like Bootstrap’s Tooltip), target the inner tooltip element instead. For Bootstrap, that’s .tooltip-inner:

.tooltip-inner {
  /* Apply the above styles here */
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 03:28:25