如何实现HTML列表项文本的完全两端对齐?
实现HTML有序列表文本完全两端对齐的方法
要让列表项文本实现完全两端对齐,借助CSS的text-align属性即可实现,以下是两种常用实现方式:
方式一:内联样式(直接给单个列表项设置)
直接在每个<li>标签中添加style属性,通过text-align: justify实现两端对齐;若需要最后一行也保持两端对齐,可额外添加text-align-last: justify:
<p style="text-align: center;"><strong>Attention Restoration Theory</strong></p> <ol> <li style="text-align: justify; text-align-last: justify;">Faszination: Die Natur weckt Aufmerksamkeit, die nicht anstrengt, sondern regeneriert.</li> <li style="text-align: justify; text-align-last: justify;">Weg sein: Die Natur ermöglicht einen Abstand zum Alltag.</li> <li style="text-align: justify; text-align-last: justify;">Ausdehnung: Die Natur ermöglicht das Gefühl, sich mit dieser verbunden zu fühlen.</li> <li style="text-align: justify; text-align-last: justify;">Kompatibilität: Die Natur bietet einer Person die Möglichkeit, zu tun, was ihren Bedürfnissen entspricht.</li> </ol>
方式二:内部样式表(批量设置所有列表项)
如果列表项数量较多,推荐用内部样式表统一配置,代码更简洁易维护:
<style> ol li { /* 让文本除最后一行外两端对齐 */ text-align: justify; /* 可选:强制最后一行也保持两端对齐 */ text-align-last: justify; } </style> <p style="text-align: center;"><strong>Attention Restoration Theory</strong></p> <ol> <li>Faszination: Die Natur weckt Aufmerksamkeit, die nicht anstrengt, sondern regeneriert.</li> <li>Weg sein: Die Natur ermöglicht einen Abstand zum Alltag.</li> <li>Ausdehnung: Die Natur ermöglicht das Gefühl, sich mit dieser verbunden zu fühlen.</li> <li>Kompatibilität: Die Natur bietet einer Person die Möglichkeit, zu tun, was ihren Bedürfnissen entspricht.</li> </ol>
补充说明
text-align: justify:默认效果是文本除最后一行外,左右两端完全对齐;text-align-last: justify:强制列表项的最后一行也保持两端对齐,该属性兼容绝大多数现代浏览器;- 若不需要最后一行对齐,仅保留
text-align: justify即可。
内容的提问来源于stack exchange,提问作者WF51
相关产品推荐
相关产品推荐

