html 使用Flexbox在表单右侧留空

wqlqzqxt  于 2023-04-18  发布在  其他
关注(0)|答案(1)|浏览(123)

我正在使用flexbox创建一个“联系我们”表单,但我面临一个问题,即表单的右侧有过多的空间,尽管将左侧和右侧都设置为flex: 1
代码如下:

/* 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;
}
<!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.使左右两侧的大小相等

tkqqtvp1

tkqqtvp11#

这个问题与flexbox知识差距有关。要在水平方向均匀地间隔flexbox子级,您可以在.row类上使用justify-content: space-around;。此外,您需要确保.row与父级的宽度相同。您不应该在.left-pane, .right-pane上使用display: flex;

.row {
    display: flex;
    justify-content: space-around;
    width: 100%;
}

我建议在这里进一步阅读:https://developer.mozilla.org/en-US/docs/Learn/CSS/CSS_layout/Flexbox#horizontal_and_vertical_alignment

相关问题