The code is as follows. The image can be centered horizontally or vertically in Firefox, but it can only be centered vertically in Chrome. , cannot be centered horizontally. I've been working on it for a long time but can't find a solution. Please help me. Thank you!
<!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><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Test</title><script src="lib/js.js" type="text/javascript"></script><style type="text/css">#a{ width: 300px; height: 300px; margin: 10px; display: table-cell; border: 1px solid red; text-align: center; vertical-align: middle; overflow: hidden;}#b{ width: 300px; height: 300px; padding: 2px; display: table-cell; border: 1px solid #DDDDDD; vertical-align: middle; text-align: center;}</style></head><body><div id="a" > <a href="#"><img src="1.jpg" onload="javascript:DrawImage(this, 300, 300)" /></a></div><div id="b"> <img src="1.jpg" onload="javascript:DrawImage(this, 300, 300);" /></div></body></html>
This should be centered. The author can try to give the width of the img
There may be something wrong with your js function. .
After testing, there is no problem under chrome 25.0.1364.172 m. I directly used css to control the length and width
#a img ,#b img{width:200px;height:200px}
Your js function is as follows Question. .
After testing, there is no problem under chrome 25.0.1364.172 m. I directly use css to control the length and width
#a img ,#b img{width:200px;height:200px}
//图片按比例缩放var flag=false;function DrawImage(ImgD, w, h){var image=new Image();var iwidth = w; //定义允许图片宽度,当宽度大于这个值时等比例缩小var iheight = h; //定义允许图片高度,当宽度大于这个值时等比例缩小image.src=ImgD.src;if(image.width>0 && image.height>0){ //假如图片长宽都不为零flag=true; if(image.height/image.width>= iheight/iwidth){ //通过正弦值判断图片缩放后是否偏高 if(image.height>iheight){ //如果图片比设定的要高 ImgD.height=iheight; ImgD.width=(image.width*iheight)/image.height; }else{ ImgD.width=image.width; ImgD.height=image.height; } ImgD.alt=image.width+"×"+image.height; } else{ //如果图片比例 小于 设定的比例 if(image.width>iwidth){ ImgD.width=iwidth; ImgD.height=(image.height*iwidth)/image.width; }else{ ImgD.width=image.width; ImgD.height=image.height; } ImgD.alt=image.width+"×"+image.height; }} ImgD.style.display = "table-cell";}
There may be something wrong with your js function. .
After testing, there is no problem under chrome 25.0.1364.172 m, so I directly use css to control the length and width
#a img ,#b img{width:200px;height:200px}
You can test whether this is caused by the js function. Just delete the js function and see if it can be centered. You will know.
Moreover, your function is modifying the width and height, but you actually said that you do not control the CSS style. . .
You can test whether this is caused by the js function. Just delete the js function and see if it can be centered. You will know.
Moreover, your function is modifying the width and height, but you actually said that you do not control the CSS style. . .
After testing, it turns out that the problem is with this function. However, this function only modifies the height and width of the image and does not set the alignment. Why can’t the image be centered horizontally? How can this function be modified without changing its function? Thanks!
ImgD.style.display = "table-cell";
Try removing the last one.
#a>a,#b>img{ display:inline-block;}
ImgD.style.display = "table-cell";
Try removing the last one.
That’s ok, thank you!
But I still want to ask why the table cells within the table cells cannot be centered horizontally?
That is, the outer div is table-cell and the inner picture is table-cell, so it cannot be centered horizontally. Just curious.
My problem is solved, thank you everyone!
I don’t quite understand this
You can think of it this way, display is used on the tag itself, to make it have the same style as the td in the table, and usually, td They are all displayed on the left. The left side is either the left boundary of the table or the previous sibling TD. Therefore, after you set the table-cell here, the img will be on the left.
You can give a simple example:
<div style = "border:1px solid #aaa;width:500px;height:400px;"> <div style = "width:200px;height:200px;margin:0 auto;background:#eee;"> </div></div>
You understand the advantages wrongly. . .
The display attribute will only affect itself and will not be inherited by descendants
No matter how your own display is set, it will not affect the display mode of its descendants. of.
So, the reason you have here is just because img sets display=table-cell, and it has nothing to do with the parent element setting display=table-cell.
You understand the advantages wrongly. . .
The display attribute will only affect itself and will not be inherited by descendants
No matter how your own display is set, it will not affect the display mode of its descendants. of.
So, the reason you have here is just because img sets display=table-cell, and it has nothing to do with the parent element setting display=table-cell.
But setting text-align: center; after img sets table-cell does not work.
http://www.w3school.com.cn/css/pr_text_text-align.asp
text-align一般是用于处理文字的(后代会继承该属性),并且,这个是对子元素设置的效果,说是对子元素,其实也包括该元素包含的文字,但有一点是肯定,它对它本身是没有影响的。而display正好相反,display只对设置的元素本身有影响,并且后代不会继承。
一般情况下,text-align:center不会让子标签中的块级元素显示为居中的,比如,行内元素会被居中处理。这里img是属于行内元素的表现类型,所以才会居中。
display中,只有“inline”和“inline-***”开头的属性是属于行内元素的,而table开头的属性,都是被解析成块级元素的。
好像说的差不多了。。
给你个例子,还是在上面例子的基础上修改的。
<div style = "border:1px solid #aaa;width:500px;height:400px;text-align:center;"> <div style = "width:300px;height:300px;background:#eee;"> <img src = "#" style = "width:100px;height:100px;"></img> <img src = "#" style = "width:100px;height:100px;display:table-cell;"></img> </div></div>
http://www.w3school.com.cn/css/pr_text_text-align.asp
text-align一般是用于处理文字的(后代会继承该属性),并且,这个是对子元素设置的效果,说是对子元素,其实也包括该元素包含的文字,但有一点是肯定,它对它本身是没有影响的。而display正好相反,display只对设置的元素本身有影响,并且后代不会继承。
一般情况下,text-align:center不会让子标签中的块级元素显示为居中的,比如,行内元素会被居中处理。这里img是属于行内元素的表现类型,所以才会居中。
display中,只有“inline”和“inline-***”开头的属性是属于行内元素的,而table开头的属性,都是被解析成块级元素的。
好像说的差不多了。。
给你个例子,还是在上面例子的基础上修改的。
<div style = "border:1px solid #aaa;width:500px;height:400px;text-align:center;"> <div style = "width:300px;height:300px;background:#eee;"> <img src = "#" style = "width:100px;height:100px;"></img> <img src = "#" style = "width:100px;height:100px;display:table-cell;"></img> </div></div>