css控製圖片的等比縮放

來源:互聯網
上載者:User

css樣式代碼
<style type="text/css">
img {max-width:500px; max-height:500px; scale:expression((this.offsetWidth > this.offsetHeight)?(this.style.width = this.offsetWidth >= 500 ? "500px" : "auto"):(this.style.height = this.offsetHeight >= 500 ? "500px" : "auto")); display:inline !important;}
</style>
樣本中是預設網頁所有的img的標籤中的圖片都會縮放,如果想讓特定的圖片縮放只要修改下前面的css名字,然後在網頁中調用就可以了

比如你要讓一張大圖片1024*768在一個100*100的容器裡顯示,那這個時候你就需要用到等圖片比縮放這個功能了!

<style type="text/css">
body{ margin:0; padding:0;}
img{
border:none;
max-width:100px;   /* FF IE7 */
max-height:100px; /* FF IE7 */
_width:expression(this.width > 100 && this.width > this.height ? 100: auto); /* IE6 */
_height:expression(this.height > 100 ? 100 : auto); /* IE6 */
}
</style>
<body><img src="meinv.jpg" alt="" width=“1024” height=“768” /></body>


圖片等比列縮放的同時,你需要把圖片底部對齊,這時候可以用絕對位置實現!

圖片等比列縮放的同時,你需要把圖片水平置中,這時只要在父容器裡添加text-align:centre;即可!

圖片等比縮放且圖片底部對齊,這個時候還要水平置中的話,暫時能想到的是使用table!

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.