先上代碼:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style> html body { padding: 0; margin: 0 } </style> </head> <body> <div > <div style="width: 30%;background:blue;height: 500px;display: inline-block;font-size: 15px"></div> <div style="width:30%;;height: 500px;background: green;display: inline-block;font-size: 15px; "> </div> <div style="width:30%;background: red;height: 500px;display: inline-block;font-size: 15px"></div> </div> </body> </html>
運行后效果如下:
圖上可以看出,這里存在兩個問題:1、div之間有間隙 2、如果在中間添加內容,會出現詭異效果:
怎么解決?
1、解決間隙,把父元素的font-size的設置為0
2、把加內容的的div增加樣式:vertical-align:top
注意:不要忘了把中間div加font-size:15px