Why can it be centered horizontally and vertically in Firefox but not in Chrome? _html/css_WEB-ITnose

WBOY
Release: 2016-06-24 12:26:15
Original
1394 people have browsed it

This post was last edited by ppsharp on 2013-12-31 00:23:46

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!

Note: javascript:DrawImage(this, 300, 300) is a function that reduces and enlarges in equal proportions.

<!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>
Copy after login


Reply to the discussion (solution)

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}
Copy after login
Copy after login
Copy after login

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}
Copy after login
Copy after login
Copy after login


The function should be fine , as follows:
//图片按比例缩放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+"&times;"+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+"&times;"+image.height;     }}  ImgD.style.display = "table-cell";}
Copy after login

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}
Copy after login
Copy after login
Copy after login


This js function does not exist Control css styles.

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;}
Copy after login

ImgD.style.display = "table-cell"; No more

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>
Copy after login


Here, the inner div is displayed in the center.

If you add display:tabel-cell inside, try it, it will be on the left. . .

It may not be the most fundamental reason for this situation, because this attribute has not been used much. . .

You can search to see if there are any articles on this topic~~

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>
Copy after login
Copy after login


你在浏览器调试工具中可以查看每个标签的CSS样式,应该可以看到:
1:最外层的div,设置了text-align,会被所有的子元素继承了,但是它的子元素中,块级元素div,并没有居中。
2:第二层的div,同样继承了text-align,它的子元素中,有两个img标签,第一个img没有设置display属性,那么其默认属性就是行内元素,这个img居中了;第二个img设置了table-cell属性,被当做了块级元素,那么这个img的前后,就会有换行符,所以,这个元素并没有能和第一个img并排显示,而是另起一行显示。并且没有居中。

这样应该可以大概明白了吧?

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>
Copy after login
Copy after login


你在浏览器调试工具中可以查看每个标签的CSS样式,应该可以看到:
1:最外层的div,设置了text-align,会被所有的子元素继承了,但是它的子元素中,块级元素div,并没有居中。
2:第二层的div,同样继承了text-align,它的子元素中,有两个img标签,第一个img没有设置display属性,那么其默认属性就是行内元素,这个img居中了;第二个img设置了table-cell属性,被当做了块级元素,那么这个img的前后,就会有换行符,所以,这个元素并没有能和第一个img并排显示,而是另起一行显示。并且没有居中。

这样应该可以大概明白了吧?

非常感谢,谢谢你耐心的解答!
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!