使用Flexbox制作联系表单时右侧出现多余空白问题求助
Flexbox联系表单左右容器宽度不均问题解决
问题描述
我使用Flexbox制作“Contact Us”联系表单时,已将左右两侧的内层容器设置为flex:1,但表单右侧仍存在多余空白,希望通过flex:1实现左右两侧宽度均等。
原始代码
CSS代码
/* http://meyerweb.com/eric/tools/css/reset/ v2.0 | 20110126 License: none (public domain) */ html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, time, mark, audio, video { margin: 0; padding: 0; border: 0; font-size: 100%; font: inherit; vertical-align: baseline; } /* HTML5 display-role reset for older browsers */ article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section { display: block; } body { line-height: 1; } ol, ul { list-style: none; } blockquote, q { quotes: none; } blockquote:before, blockquote:after, q:before, q:after { content: ''; content: none; } table { border-collapse: collapse; border-spacing: 0; } html { -webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */ -moz-box-sizing: border-box; /* Firefox, other Gecko */ box-sizing: border-box; /* Opera/IE 8+ */ font-family: 'Roboto', sans-serif; font-weight: bold; } .contact-form { background-color: #569DAA; height: 100vh; display: flex; justify-content: center; align-items: center; } .form-body { display: flex; background-color: white; border: 1px solid #999; border-radius: 10px; padding: 10px 10px; width: 600px; } .row { display: flex; } .left-pane, .right-pane { display: flex; } .leftpane-body, .rightpane-body { display: flex; flex-direction: column; flex: 1; }
HTML代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Contact Us Form</title> <link rel="preconnect" href="https://fonts.googleapis.com"> <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300&display=swap" rel="stylesheet"> <link rel="stylesheet" href="style.css"> </head> <body> <form class="contact-form"> <div class="form-body"> <div class="row"> <div class="right-pane"> <div class="rightpane-body"> <label>Address</label> <a href="#">xxxx City</a> <label>Email</label> <a href="#">xxxxx@gmail.com</a> </div> </div> <div class="left-pane"> <div class="leftpane-body"> <h2>Get in Touch</h2> <input type="text" placeholder="Enter Your Name"> <input type="text" placeholder="Enter Your Email"> <textarea id="story" name="story" rows="5" cols="33" placeholder="Enter Your Query"></textarea> </div> </div> </div> </div> </form> </body> </html>
解决方案
问题核心是**flex:1加错了层级**:你给内层的leftpane-body/rightpane-body设置了flex:1,但它们的父容器left-pane/right-pane才是.row的直接flex子项,只有给这两个父容器设置flex:1,才能让它们平分.row的可用宽度。
修改后的CSS代码片段:
.left-pane, .right-pane { display: flex; flex: 1; /* 让左右容器平分父容器宽度 */ } .leftpane-body, .rightpane-body { display: flex; flex-direction: column; width: 100%; /* 确保内层容器占满父容器宽度 */ }
关键说明
- 给
left-pane和right-pane添加flex:1:它们作为.row的flex子元素,会自动平分.row的总宽度,实现左右均等。 - 内层容器设置
width:100%:让内容完全填充父容器,避免内部出现空白区域。
修改后,表单的左右两侧就会各占.form-body宽度的50%,解决右侧多余空白的问题。
内容的提问来源于stack exchange,提问作者DailyFun
相关产品推荐
相关产品推荐

