2016-02-27 84 views
2

我想將文本與第一個左側塊對齊。該塊與中間對齊。更改屏幕/瀏覽器寬度時,應調整文本。將文本與第一個左側塊對齊

做什麼,要得到這樣的:

enter image description here

是否有可能在IE瀏覽器?

#container { 
 
    border: 1px solid gray; 
 
} 
 
.outerBlock { 
 
    border: 1px solid red; 
 
    display: flex; 
 
    flex-wrap: wrap; 
 
    justify-content: center; 
 
} 
 
.block { 
 
    width: 150px; 
 
    height: 150px; 
 
    background-color: #cccccc; 
 
    margin: 10px; 
 
}
<div id="container"> 
 
    Aligned left to first block 
 
    <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>

+0

可能的複製[中心柔性容器,但對齊左側彈性項目](http:// stackoverflow。 com/questions/32802202/center-flex-container-but-align-left-flex-items) –

回答

0

與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>

+0

我在想更通用的東西。就像這樣:http://jsfiddle.net/0h779z45/1/。但在這種情況下,我必須複製文本塊...有沒有可能沒有重複? – user3752660

+0

不,因爲'outerBlock'中的框根據框的數量和其包含的div的可用寬度由'flex'隔開。因此,第一個區塊的左側取決於這些因素。你願意使用jQuery嗎? – symlink

+0

是的。 jQuery是一個選項。 – user3752660

相關問題