文本在弹性块中未垂直对齐
Text not aligning vertically in a flex block
.account-select-block {
display: flex;
width: 100%;
height: 50px;
line-height: 50px;
}
.account-select-icon {
flex: 0;
font-size: 20px;
}
.account-select-item {
flex: 1;
font-size: 14px;
border: 1px solid #D8D8D8;
background: #eee;
cursor: pointer;
transition: border 0.1s ease;
text-align: left;
}
.account-select-remove {
flex: 0;
font-size: 20px;
background-color: #ff5200;
color: #fff;
}
<div class="account-select-block">
<div class="account-select-icon">
<i class="fa fa-user-circle" aria-hidden="true"></i>
</div>
<div class="account-select-item">
<h3>Account Name</h3>
</div>
<div class="account-select-remove">
<i class="fa fa-times" aria-hidden="true"></i>
</div>
</div>
最终图标垂直对齐但中间块中的文本没有
我做错了什么?
Fiddle: https://jsfiddle.net/7su954x3/
您需要删除标题的边距:
.account-select-block {
display: flex;
width: 100%;
height: 50px;
line-height: 50px;
}
.account-select-icon {
flex: 0;
font-size: 20px;
}
.account-select-item {
flex: 1;
font-size: 14px;
border: 1px solid #D8D8D8;
background: #eee;
cursor: pointer;
transition: border 0.1s ease;
text-align: left;
}
.account-select-remove {
flex: 0;
font-size: 20px;
background-color: #ff5200;
color: #fff;
}
/* add this */
.account-select-item h3 {
margin: 0;
}
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
<div class="account-select-block">
<div class="account-select-icon">
<i class="fa fa-user-circle" aria-hidden="true"></i>
</div>
<div class="account-select-item">
<h3>Account Name</h3>
</div>
<div class="account-select-remove">
<i class="fa fa-times" aria-hidden="true"></i>
</div>
</div>
没有意识到您手动将行高设置为垂直居中,如果您不想那样做,那么您将不得不使每个 child 也弯曲并向它们应用对齐项目:
.account-select-block {
display: flex;
width: 100%;
height: 50px;
}
.account-select-icon {
display:flex;
align-items:center;
font-size: 20px;
}
.account-select-item {
display:flex;
align-items:center;
flex-grow:1;
font-size: 14px;
border: 1px solid #D8D8D8;
background: #eee;
cursor: pointer;
transition: border 0.1s ease;
text-align: left;
}
.account-select-remove {
display:flex;
align-items:center;
font-size: 20px;
background-color: #ff5200;
color: #fff;
}
/* add this */
.account-select-item h3 {
margin: 0;
}
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
<div class="account-select-block">
<div class="account-select-icon">
<i class="fa fa-user-circle" aria-hidden="true"></i>
</div>
<div class="account-select-item">
<h3>Account Name</h3>
</div>
<div class="account-select-remove">
<i class="fa fa-times" aria-hidden="true"></i>
</div>
</div>
试试这个
.account-select-item {
flex: 1;
font-size: 14px;
border: 1px solid #D8D8D8;
background: #eee;
cursor: pointer;
transition: border 0.1s ease;
text-align: left;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;
}
在您的问题中,您询问了如何在 flexbox 中对齐。但是在您的代码中 .account-select-item
不是 flexbox。
相反,您似乎使用了使用行高的旧样式 'hack'。如果您决定更改 .account-select-block
的高度或行高,这将分崩离析。
.account-select-block {
display: flex;
width: 100%;
height: 50px;
/* line-height: 50px; */
}
.account-select-icon {
flex: 0;
font-size: 20px;
}
.account-select-item {
display: flex; /* NEW */
flex: 1;
align-items: center; /* NEW */
font-size: 14px;
border: 1px solid #D8D8D8;
background: #eee;
cursor: pointer;
transition: border 0.1s ease;
text-align: left;
}
.account-select-remove {
flex: 0;
font-size: 20px;
background-color: #ff5200;
color: #fff;
}
<div class="account-select-block">
<div class="account-select-icon">
<i class="fa fa-user-circle" aria-hidden="true"></i>
</div>
<div class="account-select-item">
<h3>Account Name</h3>
</div>
<div class="account-select-remove">
<i class="fa fa-times" aria-hidden="true"></i>
</div>
</div>
.account-select-block {
display: flex;
width: 100%;
height: 50px;
line-height: 50px;
}
.account-select-icon {
flex: 0;
font-size: 20px;
}
.account-select-item {
flex: 1;
font-size: 14px;
border: 1px solid #D8D8D8;
background: #eee;
cursor: pointer;
transition: border 0.1s ease;
text-align: left;
}
.account-select-remove {
flex: 0;
font-size: 20px;
background-color: #ff5200;
color: #fff;
}
<div class="account-select-block">
<div class="account-select-icon">
<i class="fa fa-user-circle" aria-hidden="true"></i>
</div>
<div class="account-select-item">
<h3>Account Name</h3>
</div>
<div class="account-select-remove">
<i class="fa fa-times" aria-hidden="true"></i>
</div>
</div>
最终图标垂直对齐但中间块中的文本没有
我做错了什么?
Fiddle: https://jsfiddle.net/7su954x3/
您需要删除标题的边距:
.account-select-block {
display: flex;
width: 100%;
height: 50px;
line-height: 50px;
}
.account-select-icon {
flex: 0;
font-size: 20px;
}
.account-select-item {
flex: 1;
font-size: 14px;
border: 1px solid #D8D8D8;
background: #eee;
cursor: pointer;
transition: border 0.1s ease;
text-align: left;
}
.account-select-remove {
flex: 0;
font-size: 20px;
background-color: #ff5200;
color: #fff;
}
/* add this */
.account-select-item h3 {
margin: 0;
}
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
<div class="account-select-block">
<div class="account-select-icon">
<i class="fa fa-user-circle" aria-hidden="true"></i>
</div>
<div class="account-select-item">
<h3>Account Name</h3>
</div>
<div class="account-select-remove">
<i class="fa fa-times" aria-hidden="true"></i>
</div>
</div>
没有意识到您手动将行高设置为垂直居中,如果您不想那样做,那么您将不得不使每个 child 也弯曲并向它们应用对齐项目:
.account-select-block {
display: flex;
width: 100%;
height: 50px;
}
.account-select-icon {
display:flex;
align-items:center;
font-size: 20px;
}
.account-select-item {
display:flex;
align-items:center;
flex-grow:1;
font-size: 14px;
border: 1px solid #D8D8D8;
background: #eee;
cursor: pointer;
transition: border 0.1s ease;
text-align: left;
}
.account-select-remove {
display:flex;
align-items:center;
font-size: 20px;
background-color: #ff5200;
color: #fff;
}
/* add this */
.account-select-item h3 {
margin: 0;
}
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
<div class="account-select-block">
<div class="account-select-icon">
<i class="fa fa-user-circle" aria-hidden="true"></i>
</div>
<div class="account-select-item">
<h3>Account Name</h3>
</div>
<div class="account-select-remove">
<i class="fa fa-times" aria-hidden="true"></i>
</div>
</div>
试试这个
.account-select-item {
flex: 1;
font-size: 14px;
border: 1px solid #D8D8D8;
background: #eee;
cursor: pointer;
transition: border 0.1s ease;
text-align: left;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;
}
在您的问题中,您询问了如何在 flexbox 中对齐。但是在您的代码中 .account-select-item
不是 flexbox。
相反,您似乎使用了使用行高的旧样式 'hack'。如果您决定更改 .account-select-block
的高度或行高,这将分崩离析。
.account-select-block {
display: flex;
width: 100%;
height: 50px;
/* line-height: 50px; */
}
.account-select-icon {
flex: 0;
font-size: 20px;
}
.account-select-item {
display: flex; /* NEW */
flex: 1;
align-items: center; /* NEW */
font-size: 14px;
border: 1px solid #D8D8D8;
background: #eee;
cursor: pointer;
transition: border 0.1s ease;
text-align: left;
}
.account-select-remove {
flex: 0;
font-size: 20px;
background-color: #ff5200;
color: #fff;
}
<div class="account-select-block">
<div class="account-select-icon">
<i class="fa fa-user-circle" aria-hidden="true"></i>
</div>
<div class="account-select-item">
<h3>Account Name</h3>
</div>
<div class="account-select-remove">
<i class="fa fa-times" aria-hidden="true"></i>
</div>
</div>