2017-10-18 101 views
-3

我把圖像放在DIV中。我希望所有的三張圖片是他們的DIV邊框內,但它只是似乎不工作:我的圖像不保留綁定到他們的DIV

#container { 
 
    margin-right: 10%; 
 
    margin-left: 10%; 
 
    border-style: solid; 
 
    border-width: 5px; 
 
    border-color: orange; 
 
    background-color: yellow; 
 
} 
 

 
h1 { 
 
    text-align: center; 
 
} 
 

 
#original, 
 
#alike1, 
 
#alike2 { 
 
    margin: 10px; 
 
    border-style: solid; 
 
    border-width: 5px; 
 
    border-color: orange; 
 
    background-color: rgb(0, 200, 255); 
 
} 
 

 
img.pic2, 
 
img.pic3 { 
 
    float: right; 
 
    clear: both; 
 
    overflow: hidden; 
 
    margin: 10px; 
 
} 
 

 
#pic1 { 
 
    float: right; 
 
    clear: both; 
 
    overflow: hidden; 
 
    margin-right: 20px; 
 
    margin-top: 40px; 
 
}
<div id="container"> 
 

 
    <h1> Trump Hair </h1> 
 

 
    <div id="original"> 
 
    <h2> Original </h2> 
 
    <p> The Donald 
 
     <div> <img id=pic1 height="100" alt="Don" src="http://lorempixel.com/100/100/people"> </div> 
 
    </p> 
 
    <p> This is the original Trump hair. It is found often in nature. 
 
     <p> 
 
    </div> 
 

 
    <div id="alike1"> 
 
    <h2> Look alike #1</h2> 
 
    <p>Corn Silk <img class="pic2" height="100" alt="Corn" src="http://lorempixel.com/100/100/cats"></p> 
 
    <p>There have been many cases of corn silk that appear like Trump's hair. 
 
     <p> 
 
    </div> 
 

 
    <div id="alike2"> 
 
    <h2> Look alike #1 </h2> 
 
    <p> Llama Hair <img class="pic3" alt="llama" height="100" src="http://lorempixel.com/100/100/abstract"> </p> 
 
    <p> There are many animals that have Trump hair. This llama is looking very stylish. </p> 
 
    </div> 
 

 
</div>

我怎樣才能解決呢?我希望所有的三張圖片是他們的DIV邊框內,但現在看起來是這樣的:

as you can see the images flow out of the divs

1

+0

它是如何不工作?你能提供一個截圖,並且也發佈你的html嗎? –

+1

請閱讀[問],以及如何創建[mcve]。 – CBroe

+0

對不起CBroe,只是有點匆忙。 –

回答

0

的問題是,圖像是浮動的。當你浮動一個元素時,它會從頁面的正常流程中取出(就像你絕對或固定的位置時一樣)。

一種可能的解決方案是使盒子長到適合其中的圖像。您可以實現通過添加overflow:autoheight:auto,然後將文本框將增長到包括它的極限範圍內的所有浮動元素:

#container { 
 
    margin-right: 10%; 
 
    margin-left: 10%; 
 
    border-style: solid; 
 
    border-width: 5px; 
 
    border-color: orange; 
 
    background-color: yellow; 
 
} 
 

 
h1 { 
 
    text-align: center; 
 
} 
 

 
#original, 
 
#alike1, 
 
#alike2 { 
 
    margin: 10px; 
 
    border-style: solid; 
 
    border-width: 5px; 
 
    border-color: orange; 
 
    background-color: rgb(0, 200, 255); 
 
    /* add the changes here */ 
 
    height: auto; 
 
    overflow: auto; 
 
} 
 

 
img.pic2, 
 
img.pic3 { 
 
    float: right; 
 
    clear: both; 
 
    overflow: hidden; 
 
    margin: 10px; 
 
} 
 

 
#pic1 { 
 
    float: right; 
 
    clear: both; 
 
    overflow: hidden; 
 
    margin-right: 20px; 
 
    margin-top: 40px; 
 
}
<div id="container"> 
 

 
    <h1> Trump Hair </h1> 
 

 
    <div id="original"> 
 
    <h2> Original </h2> 
 
    <p> The Donald 
 
     <div> <img id=pic1 height="100" alt="Don" src="http://lorempixel.com/100/100/people"> </div> 
 
    </p> 
 
    <p> This is the original Trump hair. It is found often in nature. 
 
     <p> 
 
    </div> 
 

 
    <div id="alike1"> 
 
    <h2> Look alike #1</h2> 
 
    <p>Corn Silk <img class="pic2" height="100" alt="Corn" src="http://lorempixel.com/100/100/cats"></p> 
 
    <p>There have been many cases of corn silk that appear like Trump's hair. 
 
     <p> 
 
    </div> 
 

 
    <div id="alike2"> 
 
    <h2> Look alike #1 </h2> 
 
    <p> Llama Hair <img class="pic3" alt="llama" height="100" src="http://lorempixel.com/100/100/abstract"> </p> 
 
    <p> There are many animals that have Trump hair. This llama is looking very stylish. </p> 
 
    </div> 
 

 
</div>