Table of Contents
Reply to the discussion (solution)
Home Web Front-end HTML Tutorial Why can it be centered horizontally and vertically in Firefox but not in Chrome? _html/css_WEB-ITnose

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

Jun 24, 2016 pm 12:26 PM

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并排显示,而是另起一行显示。并且没有居中。

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

非常感谢,谢谢你耐心的解答!
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? Apr 05, 2025 am 06:15 AM

To achieve the effect of scattering and enlarging the surrounding images after clicking on the image, many web designs need to achieve an interactive effect: click on a certain image to make the surrounding...

HTML, CSS, and JavaScript: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

How to distinguish between closing a browser tab and closing the entire browser using JavaScript? How to distinguish between closing a browser tab and closing the entire browser using JavaScript? Apr 04, 2025 pm 10:21 PM

How to distinguish between closing tabs and closing entire browser using JavaScript on your browser? During the daily use of the browser, users may...

See all articles