国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > web前端 > htmlcss > CSS控制网页中图片的最大宽度和高度

CSS控制网页中图片的最大宽度和高度

来源:程序员人生   发布时间:2013-11-22 19:42:31 阅读次数:3199次
<html>
<head>
<style type="text/css">
<!--
img.pic{
max-width:300;
max-height:100px;
/*由于IE6.0以及以前版本的IE不支持上边两个属性,所以加上以下两条语句.这里要说明的是expression只有IE支持*/
width: expression(this.width > 300 && this.width / 300 >= this.height / 100 ? 300 : true);
height: expression(this.height > 100 && this.width / 300 < this.height / 100 ? 100 : true);
}
//-->
</style>

</head>
<body>
<div id="pic">
</div>
<img class="pic" src="Blue hills.jpg" />
</body>
</html>

由于上边的代码中,设置最大的长宽是直接用数字的,这样再修改的时候十分不方便,所以做了以下修改:
<html>
<head>
<script type="text/javascript">
<!--
Object.MAX_WIDTH = 400;
Object.MAX_HEIGHT = 300;
-->
</script>
<style type="text/css">
<!--
img.pic{
max-width:300px;
max-height:100px;
width: expression(this.width > Object.MAX_WIDTH && this.width / Object.MAX_WIDTH >= this.height / Object.MAX_HEIGHT ? Object.MAX_WIDTH : true);
height: expression(this.height > Object.MAX_HEIGHT && this.width / Object.MAX_WIDTH < this.height / Object.MAX_HEIGHT ? Object.MAX_HEIGHT : true);
}
//-->
</style>

</head>
<body>
<div id="pic">
</div>
<img class="pic" src="Blue hills.jpg" />
</body>
</html>
这样的话,只需要修改Object.MAX_WIDTH和Object.MAX_HEIGHT这两个类变量就可以很方便的修改最大的长宽.

连续的字母或其他特殊字符导致不能自动换行,解决的办法是使用如下方法

其实 CSS 里面有一个 style,叫做 word-wrap。把它设为 break-word,然后把单元格宽度定一下,就没有必要在程序里面进行换行了。如下所示。
table style="table-layout: fixed; word-break: break-all; word-wrap: break-word; overflow-x: hidden" width="500"
不过注意,在 Mozilla 浏览器里面这个用法似乎不起作用。
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生