如何用CSS将文本定位在表格顶部边框中央并适配窗口
实现标题固定在表格顶部边框且缩放位置稳定
问题说明
需要将"Shirt order form"标题定位在表格的顶部边框上,且窗口缩放时标题与表格的相对位置保持稳定。之前使用基础定位和fixed定位搭配百分比单位时,窗口尺寸变化会导致位置偏移。
修复方案
核心思路是将标题和表格放入同一个相对定位的容器,让标题的绝对定位基于这个容器,同时利用transform实现精准居中,确保缩放时相对位置不变。另外先修正原HTML中表单与表格的嵌套错误(表单不能直接作为表格的子元素)。
修改后的HTML代码
<!DOCTYPE html> <html> <head> <title>Exercise 4</title> <link rel="stylesheet" type="text/css" href="/css/reset.css"> <link rel="stylesheet" type="text/css" href="/css/exercise4.css"> </head> <body> <!-- 新增容器包裹标题和表格 --> <div class="table-wrapper"> <header> <h4>Shirt order form</h4> </header> <form method="GET" action="http://webprojects.eecs.qmul.ac.uk/un300/week4/week4_process.php"> <table> <tr> <td> <div class="container"> <label for="choice">Shirt size</label> </div> <select name="choices"> <option value="" disabled selected>Choose size</option> <option>XS</option> <option>S</option> <option>M</option> <option>L</option> <option>XL</option> </select> <div class="container"> <label for="radio">Sleeves</label> </div> <input type="radio" name="Sleeves" value="1" checked>Short<br> <input type="radio" name="Sleeves" value="2">Long <h4>Choose a colour</h4> <input type="color" name="colorChoice"> <h4>Quantity</h4> <input type="number" min="1" max="10" name="Quantity" > <h4>Date Requested </h4> <input type="date"> </td> </tr> <tr> <td> <div class="container"> <label for="name">Name</label> </div> <input type="text" name="name"> <div class="container"> <label for="email">Email</label> </div> <input type="email" required name="email"> <div class="container"> <label for="Telephone" >Telephone</label> </div> <section id="right"> <input type="tel" id="phone" name="phone" pattern="^\d{2}-\d{3}-\d{3}-\d{4}$" required> <p>Example: 44-207-882-1234</p> </section> </td> </tr> <tr> <td> <div class="container"> <label for="address">Address</label> </div> <input type="text" name="address"> <div class="container"> <label for="city">City</label> </div> <input type="text" name="city" list="cities"> <datalist id="cities"> <option>London</option> <option>Paris</option> <option>Hong Kong</option> <option>Scotland</option> </datalist> <div class="container"> <label for="postcode">Postcode</label> </div> <input type="text" name="postcode"> </td> </tr> <tr> <td> <footer id="footer"> <label for="checkbox">Please keep me informed about future shirt designs</label> <input type="checkbox" name="checkbox"> <div class="formRow space"> <input type="submit"> <input type="reset"> </div> <p>© 2023 Shirt Order Form</p> </footer> </td> </tr> </table> </form> </div> </body> </html>
修改后的CSS代码
table, th, td { border: 1px solid black; } /* 新增包裹容器,设置相对定位作为标题的定位参考 */ .table-wrapper { position: relative; width: 75%; margin: 5vh auto 0; } table { width: 100%; background-color: rgb(121, 204, 121); border-collapse: collapse; /* 合并边框,让标题贴合更自然 */ } .formRow { display: flex; justify-content: center; } .container { display: flex; flex-direction: column; margin: 10px 0; } #right { display: flex; flex-direction: row; align-items: center; } #right p { margin-left: 10px; font-size: 0.9em; } .space { margin-top: 20px; } input[type="submit"], input[type="reset"] { background-color: #4CAF50; color: white; padding: 12px 24px; font-size: 16px; cursor: pointer; border-radius: 5px; border: none; margin: 0 15px; } body { max-width: 800px; margin: auto; padding: 0 20px; } #footer { background-color: white; padding: 20px 0; } header { position: absolute; top: 0; left: 50%; /* 水平垂直居中,刚好覆盖表格顶部边框 */ transform: translate(-50%, -50%); background-color: #333; color: #fff; text-align: center; padding: 5px 20px; border-radius: 4px; height: auto; } header h4 { margin: 5px 0; }
关键修改点
- 添加定位容器:
.table-wrapper设置为position: relative,同时宽度和表格保持一致,让标题的绝对定位基于这个容器,避免窗口缩放时偏移。 - 标题定位优化:用
top: 0+left: 50%+transform: translate(-50%, -50%)实现标题在容器顶部中心,刚好压在表格边框上,且缩放时始终保持这个相对位置。 - 修正HTML结构:将
form放在table外层,修复表单与表格的嵌套错误,符合HTML规范。 - 细节调整:表格添加
border-collapse: collapse让边框更紧凑,按钮去掉默认边框,调整间距让布局更美观。
内容的提问来源于stack exchange,提问作者Ri1jgo0l
相关产品推荐
相关产品推荐

