本文主要為大家分享三種CSS的方法 ,具有很好的參考價值,希望對大家有所協助。一起跟隨小編過來看看吧,希望能協助到大家。
CSS圖片的幾種方式
原圖:
<p class="p_bg"></p><style type="text/css"> .p_bg { width: 550px; height: 113px; background-image: url(img/bk_title_all.jpg); background-repeat: no-repeat; background-position: 0px -599px; }</style>
:
<img class="img1" src="img/bk_title_all.jpg" /> <style type="text/css"> .img1 { width: 550px; background-image: url(img/bk_title_all.jpg); clip: rect(599px 550px 712px 0px); position: absolute; margin-top: -599px; } </style
:
<p class="p_img"> <img class="img2" src="img/bk_title_all.jpg" /></p><style type="text/css"> .p_img { width: 550px; height: 113px; overflow: hidden; }.img2 { margin: -599px 0 0 0; } </style>
:
完整版Html如下:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head> <title>Untitled Page</title> <style type="text/css"> .p_bg { width: 550px; height: 113px; background-image: url(img/bk_title_all.jpg); background-repeat: no-repeat; background-position: 0px -599px; } .img1 { width: 550px; background-image: url(img/bk_title_all.jpg); clip: rect(599px 550px 712px 0px); position: absolute; margin-top: -599px; } .p_img { width: 550px; height: 113px; overflow: hidden; } .img2 { margin: -599px 0 0 0; } </style></head><body> <h1> 原圖</h1> <p style="width: 550px; height: 831px; background: url(img/bk_title_all.jpg); background-repeat: no-repeat;"> </p> <hr /> <h1> p <br /> <span style="color: Red;">使用background-position:Xpx Ypx (圖片左上方座標) 配合width和height</span> </h1> <h2> background: url(img/bk_title_all.jpg); width: 550px;<br /> width: 550px;<br /> height: 113px;<br /> background-repeat: no-repeat;<br /> background-position: 0px -599px; </h2> <p class="p_bg"> </p> <hr /> <h1> IMG <br /> <span style="color: Red;">使用clip: rect(左上Y 右下X 右下Y 左上X) 配合width和margin-top</span> </h1> <h2> img src="img/bk_title_all.jpg" <br /> clip: rect(599px 550px 712px 0px);<br /> position: absolute;<br /> width: 550px;<br /> margin-top: -599px; </h2> <img class="img1" src="img/bk_title_all.jpg" /> <br /> <br /> <h1 style="margin-top:100px;"> p+IMG <br /> <span style="color: Red;">使用margin屬性和overflow屬性控制</span> </h1> <h2> <h3>p</h3> width: 550px;<br /> height: 113px;<br /> overflow: hidden;<br /> <h3>IMG</h3> margin: -599px 0 0 0;<br /> </h2> <p class="p_img"> <img class="img2" src="img/bk_title_all.jpg" /> </p></body></html>