源代碼
點擊運行
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>教程</title> <style> div { border: 3px solid #4CAF50; padding: 5px; } .img1 { float: right; } .clearfix { overflow: auto; } .img2 { float: right; } </style> </head> <body> <p>以下實例圖在父元素中溢出,很不美觀:</p> <div> <img class="img1" src="/images/logo.png" alt="Pineapple" width="170" height="170"> 教程 - 學的不僅是技術,更是夢想!!!</div> <p style="clear:right">在父元素上通過添加 clearfix 類,併設置 overflow: auto; 來解決該問題:</p> <div class="clearfix"> <img class="img2" src="/images/logo.png" alt="Pineapple" width="170" height="170"> 教程 - 學的不僅是技術,更是夢想!!!</div> </body> </html>
運行結果