2016-12-05 89 views
2

我不知道如果我這個得太多,但我有一個網站,我有一個組織像這樣的部分:從父div浮動文本在兩個並排子div上?

<div id="parent"> 
<p>Some Text</p> 
    <div id="child1"></div> 
    <div id="child2"></div> 
</div> 

#child1#child2沒有內容,但只有背景圖像是大小相同。我想要做的是將文本集中浮動在兩個div元素上。

在這個頁面上,由於background-image大小不能設置容器的大小,我使用一個快速的JS解決方案來根據圖像的高寬比來設置div的高度。這是在這裏工作,但我不知道如何將文本放在子div上,而不是在它們之前。

編輯:這是一個示例圖像,兩種顏色代表兩個不同的背景圖像。 enter image description here

+3

可以添加撥弄你的標記和CSS – Geeky

+2

你能提供一些圖片,展示你的最終輸出應該是什麼樣子?這並不清楚。 – Dekel

+0

@Dekel剛剛做到了。 – AKor

回答

0

您可以顯示圖像作爲inline-block所以他們會在同一行並使用text-align: center居中的#parent DIV裏面所有的內容。

#parent{ 
 
    text-align: center; 
 
} 
 

 
.image{ 
 
    height: 100px; 
 
    width: 100px; 
 
    background-size: cover; 
 
    display: inline-block; 
 
} 
 

 
#child1{ 
 
    background-image: url("https://pstbqpil.files.wordpress.com/2012/03/seagull-in-the-foreground-robert-moses-bridge-in-the-background-vicki-jauron.jpg"); 
 
} 
 

 
#child2{ 
 
    background-image: url("http://betterphotography.in/wp-content/uploads/2012/05/890362_56513892.jpg"); 
 
}
<div id="parent"> 
 
<p>Some Text</p> 
 
    <div id="child1" class="image"></div> 
 
    <div id="child2" class="image"></div> 
 
</div>

0

這裏是一個選項,您可以使用:

#parent { 
 
    width: 400px; 
 
    height: 400px; 
 
    position: relative; 
 
} 
 
#child1 { 
 
    background: url(https://dummyimage.com/200x400/F00/FFF); 
 
    width: 50%; 
 
    height: 100%; 
 
    position: absolute; 
 
    top: 0; 
 
    left: 0; 
 
} 
 
#child2 { 
 
    background: url(https://dummyimage.com/200x400/0F0/FFF); 
 
    width: 50%; 
 
    height: 100%; 
 
    position: absolute; 
 
    top: 0; 
 
    right: 0; 
 
} 
 
#parent p { 
 
    position: absolute; 
 
    top: 50%; 
 
    left: 50%; 
 
    transform: translate(-50%, -50%); 
 
    z-index: 1; 
 
    background: white; 
 
    margin: 0; 
 
    padding: 0; 
 
}
<div id="parent"> 
 
<p>Some Text</p> 
 
    <div id="child1"></div> 
 
    <div id="child2"></div> 
 
</div>

0

下面的演示使用的位置絕對方法。

<div id="parent"> 
    <p class="text">Some Text</p> 
    <div id="child1"></div> 
    <div id="child2"></div> 
</div> 

所有東西都包裝在Parent div中,所以你可以隨意移動它。

只需用CSS中的URL鏈接替換背景顏色即可。

希望這是你正在尋找的! :)

https://jsfiddle.net/xf87jgLq/45/