實際開發中,經常需要給某個容器畫一條邊框,比如給div畫一條上邊框。這裡就以「給div畫上邊框」為例,其實方法有幾種。
1.直接設置border-top:
border-top: 1px solid #eee;
2.背景圖片的方式:
background-image: url('demo.gif'); background-repeat: no-repeat; background-attachment:fixed; background-position: 0% 0%; background-size:100% 1px;
上面的CSS表示,給div設置一個背景圖片,只顯示寬度100%、高度1px,這樣也能呈現出一條線。
3.在div裡再放一個元素,hr、div、span都行,給這個元素設置樣式:
#target hr{ width: 100%; height: 1px; border: 0; background-color: #222; } <div id="target"> <hr /> </div>
4.使用偽類元素實現:
#target:before{ content: " "; background-color: #222; width: 100%; height: 3px; display: block; }
這種方式會佔用元素的內部空間,不過只用來實現一條細線效果還是很好用的。
如果你偶然看到還有其他方式,也歡迎留言告訴我。