2017-08-31 112 views
-1

我有4列產品的頭部,描述和圖像在下面,我希望文本區域在頂部和圖像到每個排隊在底部,以便在屏幕調整大小時保持對齊。保持文本對齊的頂部和圖像對齊的相對高度div的底部

當圖像在文字環繞時上下移動 - 看起來非常不整齊,因爲它們每個都有不同的文字量。

我嘗試過使用表格和絕對位置,他們每個都會導致問題,並且無法正常工作。

這裏是我的代碼:

#product-landing .landing-row { 
 
    position: relative; 
 
    height: inherit; 
 
} 
 

 
.product-landing, 
 
.product-landing-2, 
 
.product-landing-3 { 
 
    margin-right: 2.0533%; 
 
} 
 

 
.product-landing, 
 
.product-landing-2, 
 
.product-landing-3, 
 
.product-landing-4 { 
 
    margin-left: 0px; 
 
    display: inline-block; 
 
    position: relative; 
 
    height: 100%; 
 
    vertical-align: top; 
 
    width: 22.978%; 
 
} 
 

 
.product-landing-4 { 
 
    margin-right: 0px; 
 
} 
 

 
#product-landing p { 
 
    margin-top: 0px; 
 
    margin-bottom: 5px; 
 
    clear: both; 
 
    width: 100%; 
 
} 
 

 
.product-landing .land-image { 
 
    vertical-align: bottom; 
 
} 
 

 
.product-landing img, 
 
.product-landing-2 img, 
 
.product-landing-3 img, 
 
.product-landing-4 img { 
 
    margin-top: 10px; 
 
    display: block; 
 
    max-width: 350px; 
 
    margin: 0 auto; 
 
    bottom: 0px; 
 
    width: 100%; 
 
}
<div id="product-landing"> 
 
    <div class="landing-row"> 
 
    <div class="product-landing"> 
 
     <div id="product-text"> 
 
     <div id="offer-title"><a href="/address" target="_blank">Product1</a></div> 
 
     <p> 
 
     </p> 
 
     <p><b><i>Product description</i></b></p> 
 
     </div> 
 
     <p class="land-image"> 
 
     <a href="/address" target="_blank"><img src="http://image1.jpg"></a> 
 
     </p> 
 
    </div> 
 
    <div class="product-landing-2"> 
 
     <div id="product-text"> 
 
     <div id="offer-title"><a href="/address" target="_blank">Product2</a></div> 
 
     <p> 
 
     </p> 
 
     <p><b><i>Product description</i></b></p> 
 
     </div> 
 
     <p class="land-image"> 
 
     <a href="/address" target="_blank"><img src="http://image2.jpg"></a> 
 
     </p> 
 
    </div> 
 
    <div class="product-landing-3"> 
 
     <div id="product-text"> 
 
     <div id="offer-title"><a href="/address" target="_blank">Product3</a></div> 
 
     <p> 
 
     </p> 
 
     <p><b><i>Product description</i></b></p> 
 
     </div> 
 
     <p class="land-image"> 
 
     <a href="/address" target="_blank"><img src="http://image3.jpg"></a> 
 
     </p> 
 
    </div> 
 
    <div class="product-landing-4"> 
 
     <div id="product-text"> 
 
     <div id="offer-title"><a href="/address" target="_blank">Product4</a></div> 
 
     <p> 
 
     </p> 
 
     <p><b><i>Product description</i></b></p> 
 
     </div> 
 
     <p class="land-image"> 
 
     <a href="/address" target="_blank"><img src="http://image4.jpg"></a> 
 
     </p> 
 
    </div> 
 
    </div> 
 
</div>

回答

1

您可以使用Flexbox的這個

#product-landing .landing-row { 
 
    display:flex; 
 
    flex-direction:row; 
 
    justify-content:space-between; 
 
    flex-wrap:wrap; 
 
} 
 

 
#product-landing p { 
 
    margin-top: 0px; 
 
    margin-bottom: 5px; 
 
} 
 

 
.product-landing { 
 
    display:flex; 
 
    flex-direction:column; 
 
    width: 22.978%; 
 
} 
 

 
.product-landing img { 
 
    margin-top: 10px; 
 
    display: block; 
 
    max-width: 350px; 
 
    width: 100%; 
 
} 
 

 
.product-text { 
 
    flex-grow:1; 
 
}
<div id="product-landing"> 
 
    <div class="landing-row"> 
 
    <div class="product-landing"> 
 
     <div class="product-text"> 
 
     <div class="offer-title"><a href="/address" target="_blank">Product1</a></div> 
 
     <p><b><i>Product description</i></b></p> 
 
     </div> 
 
     <p class="land-image"> 
 
     <a href="/address" target="_blank"><img src="http://via.placeholder.com/350x150"></a> 
 
     </p> 
 
    </div> 
 
    <div class="product-landing"> 
 
     <div class="product-text"> 
 
     <div class="offer-title"><a href="/address" target="_blank">Product2</a></div> 
 
     <p><b><i>Product description</i></b></p> 
 
     </div> 
 
     <p class="land-image"> 
 
     <a href="/address" target="_blank"><img src="http://via.placeholder.com/350x150"></a> 
 
     </p> 
 
    </div> 
 
    <div class="product-landing"> 
 
     <div class="product-text"> 
 
     <div class="offer-title"><a href="/address" target="_blank">Product2</a></div> 
 
     <p><b><i>Product description</i></b></p> 
 
     </div> 
 
     <p class="land-image"> 
 
     <a href="/address" target="_blank"><img src="http://via.placeholder.com/350x150"></a> 
 
     </p> 
 
    </div> 
 
    <div class="product-landing"> 
 
     <div class="product-text"> 
 
     <div class="offer-title"><a href="/address" target="_blank">Product3</a></div> 
 
     <p><b><i>Product description Product description Product description</i></b></p> 
 
     </div> 
 
     <p class="land-image"> 
 
     <a href="/address" target="_blank"><img src="http://via.placeholder.com/350x150"></a> 
 
     </p> 
 
    </div> 
 
    <div class="product-landing"> 
 
     <div class="product-text"> 
 
     <div class="offer-title"><a href="/address" target="_blank">Product4</a></div> 
 
     <p><b><i>Product description</i></b></p> 
 
     </div> 
 
     <p class="land-image"> 
 
     <a href="/address" target="_blank"><img src="http://via.placeholder.com/350x150"></a> 
 
     </p> 
 
    </div> 
 
    </div>

+0

沒有。只要文本包裝在其中一列(當屏幕被調整大小時),圖像就不會對齊。他們仍然對齊文本的底部而不是產品登陸框。 – LouiseW

+0

@LouiseW你使用的是什麼瀏覽器 - 這種方式在邊緣即罰款,即鉻和火狐 - 你是說它不能在你的代碼中工作,或者上面的代碼不工作,因爲當我運行代碼段並使用整頁並調整屏幕大小,圖像始終保持對齊。您可能會注意到我已將您的無效ID(ID必須是唯一的)更改爲類,並將類似於類的奇怪ID更改爲使用一個類。這段代碼很明顯,所以它必須是代碼中的東西 – Pete

+0

是的,我已經像在代碼中一樣將id更改爲類。我正在使用最新版本的Chrome和Firefox。我還檢查了safari,yandex,opera和IE 10以及更舊的瀏覽器,它也是如此。請記住,這裏顯示的描述文字比這裏顯示的要多得多,它在1到4行之間變化。 – LouiseW

0

想如果我可以過評論這一點;你有沒有試過使用柔性盒?
https://css-tricks.com/snippets/css/a-guide-to-flexbox/

這在相對/絕對組合失敗的情況下多次救了我。

更具體地說;

.wrapper{ 
     display:flex; 
     flex-direction:column; 
    } 
    .text-wrapper-div{ 
     align-self:flex-start; 
    } 
    .image-wrapper-div{ 
     align-self:flex-end; 
    } 

.wrapper是您的父容器,它從上到下(列)彎曲。 裏面是文本和圖像的兩個div,它們將自己對齊到頂部(flex-start)和底部(flex-end)。

看看鏈接,如果你卡住了 - 這是非常好的文件,我想!祝你好運。

+0

是的,我還是沒能得到圖像使用這種方法保持一致。它也不適用於舊版瀏覽器,並且在我測試它時看起來很糟糕。 – LouiseW

0

希望這可以幫助你,

使用Flex被認爲是最好的做法。桌子和桌子都是古老的方式,因爲我們走的很遠,新的屬性正在日復一日地發明。由於使用flex會在調整瀏覽器大小時調整內容本身,並具有自己的特質。

#product-landing .landing-row { 
 
display: flex; 
 
flex-direction: row; 
 
} 
 

 
.product-landing, 
 
.product-landing-2, 
 
.product-landing-3 { 
 
    margin-right: 2.0533%; 
 
} 
 

 
.product-landing, 
 
.product-landing-2, 
 
.product-landing-3, 
 
.product-landing-4 { 
 
    flex: 1; 
 
    text-align: center; 
 
} 
 

 

 
#product-landing p { 
 
    margin-top: 0px; 
 
    margin-bottom: 5px; 
 
    clear: both; 
 
    width: 100%; 
 
} 
 

 
.product-landing .land-image { 
 
    vertical-align: bottom; 
 
} 
 

 
.product-landing img, 
 
.product-landing-2 img, 
 
.product-landing-3 img, 
 
.product-landing-4 img { 
 
    margin-top: 10px; 
 
    display: block; 
 
    max-width: 350px; 
 
    margin: 0 auto; 
 
    bottom: 0px; 
 
    width: 100%; 
 
}
<div id="product-landing"> 
 
    <div class="landing-row"> 
 
    <div class="product-landing"> 
 
     <div id="product-text"> 
 
     <div id="offer-title"><a href="/address" target="_blank">Product1</a></div> 
 
     <p> 
 
     </p> 
 
     <p><b><i>Product description</i></b></p> 
 
     </div> 
 
     <p class="land-image"> 
 
     <a href="/address" target="_blank"><img src="http://image1.jpg"></a> 
 
     </p> 
 
    </div> 
 
    <div class="product-landing-2"> 
 
     <div id="product-text"> 
 
     <div id="offer-title"><a href="/address" target="_blank">Product2</a></div> 
 
     <p> 
 
     </p> 
 
     <p><b><i>Product description</i></b></p> 
 
     </div> 
 
     <p class="land-image"> 
 
     <a href="/address" target="_blank"><img src="http://image2.jpg"></a> 
 
     </p> 
 
    </div> 
 
    <div class="product-landing-3"> 
 
     <div id="product-text"> 
 
     <div id="offer-title"><a href="/address" target="_blank">Product3</a></div> 
 
     <p> 
 
     </p> 
 
     <p><b><i>Product description</i></b></p> 
 
     </div> 
 
     <p class="land-image"> 
 
     <a href="/address" target="_blank"><img src="http://image3.jpg"></a> 
 
     </p> 
 
    </div> 
 
    <div class="product-landing-4"> 
 
     <div id="product-text"> 
 
     <div id="offer-title"><a href="/address" target="_blank">Product4</a></div> 
 
     <p> 
 
     </p> 
 
     <p><b><i>Product description</i></b></p> 
 
     </div> 
 
     <p class="land-image"> 
 
     <a href="/address" target="_blank"><img src="http://image4.jpg"></a> 
 
     </p> 
 
    </div> 
 
    </div> 
 
</div>