[英]Keeping divs from overlapping on resize
這些div的圖像帶有重疊的文本,看起來像我想要的全尺寸。
但是,當我縮小到較小的移動設備尺寸時,文本將移到圖像頂部,並且div重疊。
我該如何做,以便在調整大小時將文本移到圖像的底部,但最重要的是,容器不會重疊?
.container { position: relative; font-family: Arial; clear:both; } .text-block { clear:both; position: absolute; bottom: 30px; right: 10px; background-color: black; color: white; padding-left: 20px; padding-right: 20px; }
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> .container { position: relative; font-family: Arial; clear:both; } .text-block { clear:both; position: absolute; bottom: 30px; right: 10px; background-color: black; color: white; padding-left: 20px; padding-right: 20px; } </style> </head> <body> <h2>Image Text Blocks</h2> <p>How to place text blocks over an image:</p> <div class="container"> <img src="https://via.placeholder.com/250x150" alt="Nature" style="width:90%;"> <div class="text-block"> <h4>Nature</h4> <p>What a beautiful sunrise</p> </div> </div> <div class="container"> <img src="https://via.placeholder.com/250x150" alt="Nature" style="width:90%;"> <div class="text-block"> <h4>Nature</h4> <p>What a beautiful sunrise</p> </div> </div> </body> </html>
允許像小視窗一樣自然地堆疊塊級元素( .text-blocks
)。 然后,使用媒體查詢在您首選的斷點之后應用絕對定位。
.container { font-family: Arial; } .container img { display: block; width: 100%; } .text-block { background-color: black; color: white; padding-left: 20px; padding-right: 20px; } @media ( min-width: 48em ) { .container { position: relative; } .container img { width: 90%; } .text-block { position: absolute; bottom: 30px; right: 10px; } }
<h2>Image Text Blocks</h2> <p>How to place text blocks over an image:</p> <div class="container"> <img src="https://via.placeholder.com/250x150" alt="Nature"> <div class="text-block"> <h4>Nature</h4> <p>What a beautiful sunrise</p> </div> </div> <div class="container"> <img src="https://via.placeholder.com/250x150" alt="Nature"> <div class="text-block"> <h4>Nature</h4> <p>What a beautiful sunrise</p> </div> </div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.