简体   繁体   English

Flexbox-将项目左对齐并居中

[英]Flexbox - align items left and centered

在此处输入图片说明 JS Fiddle JS小提琴

 body { color: #fff; background-color: #f78e2a; text-align: center; } * { box-sizing: border-box; } h1 { font-size: 20px; text-align: center; } span { font-size: 14px; text-align: left; } .container { display: flex; flex-direction: column; padding: 5px 0; } .box7, .box8 { flex-basis: 100%; width: 100%; justify-content: center; } .box { height: 50px; width: calc(100% - 20px); display: flex; justify-content: center; align-items: center; font-size: 1.2em; flex: 1; } input { background-color: #f9a558; border: 1px solid #fff; height: 25px; width: 100%; flex: 1; } .company, .comments { display: flex; margin: 5px 5px; flex-grow: 3; width 100%; } .company { margin-bottom: 3%; } @media (min-width: 426px) { .container { flex-direction: row; flex-wrap: wrap; align-self: flex-start; } .box { flex-basis: 45%; } } 
 <div class="container"> <div class="box box1"><span>First Name <input type="text"></span></div> <div class="box box2"><span>Last Name <input type="text"></span></div> <div class="box box3"><span>Email <input type="text"></span></div> <div class="box box4"><span>Phone <input type="text"></span></div> <div class="box box1"><span>City <input type="text"></span></div> <div class="box box2"><span>State/Province <input type="text"></span></div> </div> <div class="container"> <div class=" company"><span>Company <input class="company" type="text"></span></div> </div> <div class="container"> <div class="comments"><span>Comments <input class="comments" type="text"></span></div> </div> 

I am attempting to center the boxes within my form as demonstrated in the attached picture. 我正尝试将表格中的框居中,如所附图片所示。 How can I get the form boxes to align left and appear side by? 如何获得表格框以使其左对齐并并排出现? As you can see in my jsfiddle, the from boxes are centered, however they do not align correctly nor do they appear side by side. 正如您在我的jsfiddle中看到的那样,“发件人”框居中,但是它们不能正确对齐,也不会并排出现。

Try below CSS I think this is for helpfull: 请尝试以下CSS,我认为这对您有所帮助:

See Fiddle Demo 参见小提琴演示

 body { color: #fff; background-color: #f78e2a; text-align: center; } * { box-sizing: border-box; } h1 { font-size: 20px; text-align: center; } span { font-size: 14px; text-align: left; float: left; width: 100%; } input[type="text"] { float: left; width: 98%; padding: 10px; } .container { display: flex; flex-direction: column; padding: 5px 0; } .box7, .box8 { flex-basis: 100%; width: 100%; justify-content: center; } .box { height: 50px; /* width: calc(100% - 20px); */ /* display: flex; */ /* justify-content: center; */ /* align-items: center; */ /* font-size: 1.2em; */ /* flex: 1; */ float: left; width: 48%; margin: 2% 1%; } (index):38 input { background-color: #f9a558; border: 1px solid #fff; height: 25px; width: 100%; flex: 1; } .company, .comments { display: flex; margin: 5px 5px; flex-grow: 3; width 100%; } .company { margin-bottom: 3%; } @media (min-width: 426px) { .container { flex-direction: row; flex-wrap: wrap; align-self: flex-start; } } 
 <div class="container"> <div class="box box1"><span>First Name <input type="text"></span></div> <div class="box box2"><span>Last Name <input type="text"></span></div> <div class="box box3"><span>Email <input type="text"></span></div> <div class="box box4"><span>Phone <input type="text"></span></div> <div class="box box1"><span>City <input type="text"></span></div> <div class="box box2"><span>State/Province <input type="text"></span></div> </div> <div class="container"> <div class=" company"><span>Company <input class="company" type="text"></span></div> </div> <div class="container"> <div class="comments"><span>Comments <input class="comments" type="text"></span></div> </div> 

CSS: CSS:

span {
    font-size: 14px;
    text-align: left;
    float: left;
    width: 100%;
}

input[type="text"] {
    float: left;
    width: 100%;
    padding: 10px;
}

.box {
    height: 50px;
    float: left;
    width: 48%;
    margin: 2% 1%;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM