为什么我的CSS不再对HTML生效?文本无法居中问题求排查
问题排查与修复方案
- 核心错误1:你的CSS代码末尾存在多余的
}语法错误,会直接导致浏览器解析CSS中断,是CSS突然失效的主要原因。 - 核心错误2:table仅设置了左右margin为auto,但未指定宽度,块级元素无明确宽度时
margin: auto的居中逻辑无法生效。
修正后的完整CSS代码
* { margin: 0; padding: 0; font-family: 'Noto Sans', sans-serif; overflow-x: hidden; } .sub-header { height: 50vh; width: 100%; background-image: linear-gradient(rgba(4, 9, 30, 0.7), rgba(4, 9, 30, 0.7)), url(assets/contact-sub.jpg); background-position: center; background-size: cover; text-align: center; color: #fff; } .sub-header h1 { margin-top: 50px; } .contact { width: 80%; margin: auto; padding-top: 50px; padding-bottom: 50px; } .reachme { width: 80%; margin: auto; padding-top: 80px; text-align: center; } .center { padding-top: 25px; margin-left: auto; margin-right: auto; width: fit-content; } table { border-spacing: 10px; } td { width: 110px; text-align: center; vertical-align: middle; } .hours { color: #808080; font-size: 14px; text-align: center; width: 80%; margin: auto; padding-top: 30px; } @media(max-width: 700px) { .sub-header { height: 35vh!important; } .reachme { padding-top: 30px; } }
操作说明
原有HTML代码无需修改,直接替换CSS即可生效,修改后请清空浏览器缓存再刷新页面,避免本地缓存的旧CSS影响效果。
内容的提问来源于stack exchange,提问作者user17171254
相关产品推荐
相关产品推荐

