2016-10-04 84 views
2

我有一個圖像和一些文本在p標籤和鏈接垂直對齊。當我浮動圖像時,它可以很好地工作。但是當我在p標記和鏈接中使用填充時,它不能完美地填充填充。


click here to see the image I need look like


here is a fiddle圖像和多行文本和鏈接垂直對齊

.wrapper { 
 
    font-family: sans-serif; 
 
    width: 400px; 
 
    margin-top: 30px; 
 
    margin-bottom: 30px; 
 
    clear:both 
 
} 
 

 
img { 
 
    float: left; 
 
} 
 
p.text { 
 
    padding-left: 30px; 
 
    display: block; 
 
    padding-bottom: 22px; 
 
    color: #222222; 
 
} 
 

 
a.link { 
 
    text-decoration: none; 
 
    padding-left: 30px; 
 
    text-transform: uppercase; 
 
    line-height: 22px; 
 
    color: #777777; 
 
} 
 

 
a.date { 
 
    color: #e10622; 
 
    text-decoration: none; 
 
}
<div class="wrapper"> 
 
    <img src="https://placehold.it/100x100" alt=""> 
 
    <p class="text">Posted By: Simon | <a href="#" class="date"> 26 July 2016</a></p> 
 
    <a href="#" class="link">Days are all happiness is key 
 
and Free</a> 
 
</div> 
 
<div class="wrapper"> 
 
    <img src="https://placehold.it/100x100" alt=""> 
 
    <p class="text">Posted By: Simon | <a href="#" class="date"> 26 July 2016</a></p> 
 
    <a href="#" class="link">Days are all happiness is key 
 
and Free</a> 
 
</div> 
 
<div class="wrapper"> 
 
    <img src="https://placehold.it/100x100" alt=""> 
 
    <p class="text">Posted By: Simon | <a href="#" class="date"> 26 July 2016</a></p> 
 
    <a href="#" class="link">Days are all happiness is key 
 
and Free</a> 
 
</div> 
 

 

 
<br> 
 
<br> 
 
<br> 
 

 
<a href="http://prntscr.com/cpusej" style="text-align:center; display: block; padding: 10px; background: #f44; font-size: 18px">Please see here what I need</a>




在此先感謝。 :)

回答

1

您需要將display: block;添加到您的a.link類樣式中。另外,在圖像上設置margin-right: 30px;,然後從p.texta.link元素中刪除padding-left: 30px;樣式。最終CSS如下:

.wrapper { 
    font-family: sans-serif; 
    width: 400px; 
    margin-top: 30px; 
    margin-bottom: 30px; 
} 

img { 
    float: left; 
    margin-right: 30px; 
} 
p.text { 
    display: block; 
    padding-bottom: 22px; 
    color: #222222; 
} 

a.link { 
    text-decoration: none; 
    text-transform: uppercase; 
    line-height: 22px; 
    color: #777777; 
    display: block; 
} 
+0

它解決了我的問題:)。但我很想知道爲什麼我必須在img中提供保證金。文本有什麼問題? –

+0

有兩個原因。首先,你只需要在一個地方而不是兩個地方設置/更新它。其次,由於圖像上的浮動,它旁邊元素(即'p.text'和'a.link'元素)上的任何填充或邊距都會完全忽略圖像。由於您的圖片大小爲100px,因此您的元素在觸碰填充/邊距邊界之前會碰到圖片。你可以將'margin-left'或'padding-left'設置爲'130px',這在技術上是可行的,但將它放在圖像上看起來更清晰。 – manwill

+0

非常感謝。抱歉打擾你。因爲我是一個新的學習者,它肯定會對我有所幫助。 祝你有美好的一天:) –

0

我有一個快速解決方案。看起來你需要在文本週圍添加一個容器,然後添加你想要的填充。請參閱下面的快速說明。

以下內容添加到你的CSS:

div.textcontainer{ 
padding-left:130px 
} 

還要去掉填充左:從p.text 30PX,如下圖所示:

p.text { 
display: block; 
padding-bottom: 22px; 
color: #222222; 
} 

,最後添加一個容器中的文本如下:

<div class="textcontainer"> 
//Insert coding you wish 
</div> 

這是解決方案的快速解決方案。理想情況下,你會想使用引導或其他CSS框架的工作來設計這種流體。我希望這對你有用。

+0

它工作正常,但對我來說並不完美。因爲我必須在幾個地方使用它。而且圖像尺寸在任何地方都不一樣。所以我不能使用這個。感謝你的回答。 :) –