div元素需前置换行才可正常渲染 Alan Turing文本换行inline属性无效如何解决
修复方案
核心问题源于浏览器内置元素的默认样式干扰,以及绝对定位元素未指定垂直偏移导致的排版异常,按以下方式调整代码即可:
1. 补充CSS样式
在原有CSS基础上新增/调整以下规则:
.inf{ position: relative; display: inline-block; /* 把内联样式移到这里,为规范写法 */ } /* 清除按钮默认样式,强制内部文本不换行 */ .inf .author { display: inline-block; margin: 0; border: none; background: transparent; white-space: nowrap; cursor: pointer; } .inf ul{ font-size: 13px; margin-top: 10px; margin: 0; /* 清除ul默认边距 */ position: absolute; top: 100%; /* 明确指定相对于inf底部定位,避免挤压按钮 */ left: 0px; width: max-content; /* 下拉框宽度适配内容,不强制换行,兼容要求高可替换为固定宽度值 */ display: flex; justify-content: space-around; align-items: center; background: white; padding: 10px 15px; list-style: none; border-radius: 5px; opacity: 0; overflow:hidden; pointer-events: none; transform: translateY(-10px); transition: all 0.4s ease; } /* 清除li内p标签的默认边距,避免撑开下拉框 */ .inf li p { margin: 0; } .inf li , .products li { width: 100%; height: 100%; display: flex; justify-content: center; align-items: center; transition:0.5s; } .inf a , .products a { color: black; text-decoration: none; } .inf button:hover + ul{ opacity: 1; pointer-events: all; transform: translateY(0px); }
2. 调整HTML代码
移除inf元素的冗余内联样式即可:
<div class='inf'> <button class="author">Alan Turing</button> <ul> <li> <p>bla bla bla</p> </li> </ul> </div>
如果需要inf容器和前置内容完全不换行,给inf的直接父元素添加white-space: nowrap;属性即可。
内容的提问来源于stack exchange,提问作者Plămădeală Max
相关产品推荐
相关产品推荐

