與Flexbox的做到這一點的唯一方法是在箱子設置爲justify-content: space-between;
。然後給頂部的文字10px的的左緣:
#container {
border: 1px solid gray;
}
#container span{
\t margin-left:10px;
}
.outerBlock {
border: 1px solid red;
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.block {
width: 150px;
height: 150px;
background-color: #cccccc;
margin: 10px;
}
<div id="container">
<span>Aligned left to first block</span>
<div class="outerBlock">
<div class="block">1</div>
<div class="block">2</div>
<div class="block">3</div>
<div class="block">4</div>
<div class="block">5</div>
</div>
</div>
UPDATE:用jQuery的定位
var el = $('.outerBlock > .block');
var os = el.offset();
$('.blockText').css("margin-left",os.left - parseInt(el.css('margin-left')));
*{
box-sizing:border-box;
}
#container {
border: 1px solid gray;
}
.outerText {
border: 1px solid red;
}
.outerBlock {
border: 1px solid red;
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.block {
width: 150px;
height: 150px;
background-color: #cccccc;
margin: 10px;
}
.blockText {
width: 150px;
max-height: 20px;
margin: 0 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="container">
<div class="outerText">
<div class="blockText">News</div>
</div>
<div class="outerBlock">
<div class="block">1</div>
<div class="block">2</div>
<div class="block">3</div>
<div class="block">4</div>
<div class="block">5</div>
</div>
</div>
可能的複製[中心柔性容器,但對齊左側彈性項目](http:// stackoverflow。 com/questions/32802202/center-flex-container-but-align-left-flex-items) –