html设置图片大小:在img标签上设置图片大小、或者使用css样式控制图片大小。操作方法如下。
设备:戴尔Inspiron15
系统:Win10
软件:visual studio code1.55.2
1、首先打开电脑之后,如下图所示,新建一个“cs”文件夹,在文件夹中存储一张图片用来做演示。
2、接着打开visual studio code点击“文件”-“打开文件夹”,选中上一步建立好的“cs”文件夹。
3、然后点击“cs”右侧的“新建文件”图标,创建一个“1.html”文件,在空白html文件中输入一个英文“!”按Tab键填充html基本代码。
4、接着在body中插入img标签,alt规定图像的替代文本,src规定显示图像的URL。
5、然后在img标签上使用width属性设置图片宽度、height属性设置图片高度。
6、最后在title标签下面插入style标签(如下图所示),接着在style标签中编辑css样式,如下图所示,img{width:100pxheight:auto},这里表示把图片设置为100px宽、auto表示高度根据图片比例自适应。设置图片大小就完成了。
html中图片以中心放大的代码如下:
<div style=" width:300px height:300pxmargin-left:auto
margin-right:auto overflow:hidden margin-top:100px">
<img id="img" onmouseover="bigger()" onmouseout="smaller()"
src="http://mat1.gtimg.com/www/images/qq2012/guanjia2.png"
style="cursor:pointerwidth:300pxheight:300px
transition:all 1s ease-out 0s perspective-origin:bottom"/>
<script type="text/javascript">
var img = document.getElementById('img')
function bigger(){
img.style.width = '400px'
img.style.height = '400px'
img.style.marginTop = "-50px"
img.style.marginLeft = "-50px"
}
function smaller(){img.style.width = '300px'
img.style.height = '300px'
img.style.marginTop = "0px"
img.style.marginLeft = "0px"
}
</script>
扩展资料:
在html中用js实现鼠标指向图片时图片放大的效果的代码如下:
<img id="img" onmouseover="bigger()" onmouseout="smaller()"
src="你的图片路径" style="width:100pxheight:100px" />
<script type="text/javascript">
var img = document.getElementById('img')
function bigger(){ img.style.width = '400px' img.style.height = '400px' }
function smaller(){ img.style.width = '100px' img.style.height = '100px' }
</script>