使用writing-mode: vertical-rl实现垂直按钮时Safari移动端文字截断问题
问题根因
Safari 浏览器对 writing-mode: vertical-rl 垂直书写模式的元素尺寸计算存在原生兼容缺陷,当垂直文本被 flex、grid 容器包裹时,无法正确计算容器的自适应宽高,最终导致文本溢出、被容器截断。
修复步骤
你可以按照以下规则修改CSS代码解决问题:
- 为垂直文本元素添加
-webkit-writing-mode前缀属性适配 Safari 内核,同时添加text-orientation: mixed适配多语种的字符朝向 - 调整文本按钮的布局规则,将
display: grid替换为display: flex避免布局冲突,显式指定width: fit-content开启宽高自适应 - 移除外层容器不必要的尺寸限制,修正固定定位下的尺寸计算逻辑
修复后完整CSS代码
body { height: 100%; width: 100%; } .wrapper.center-right { cursor: pointer; position: fixed; height: auto; width: auto; margin: 0; padding: 0; display: flex; z-index: 2147483647; background: red; top: 50%; flex-direction: column; right: 0; transform: translate(0, -50%); transform-origin: right top; } .wrapper.center-right .text-button { width: fit-content; min-width: auto; display: flex; align-items: center; justify-content: center; } .wrapper.center-right .text-button .text { writing-mode: vertical-rl; -webkit-writing-mode: vertical-rl; text-orientation: mixed; padding: 8px 4px; margin: 0; max-height: 150px; overflow-y: auto; } .text-button, .close-btn { border: solid 2px red; outline: none; box-sizing: border-box; } .text-button:focus, .close-btn:focus { color: blue; outline: none; border: blue solid 2px; } .text-button:focus .button-text { color: blue; }
修复效果说明
修改后代码可同时兼容 Chrome、Safari 桌面端及移动端,动态长度的多语种垂直文本可正确撑开容器,不会出现溢出截断问题,横向展示模式也可通过切换书写模式属性正常适配。
内容的提问来源于stack exchange,提问作者Megi Ben Nun
相关产品推荐
相关产品推荐

