How to set the image in the DIV to be vertically centered in CSS_html/css_WEB-ITnose

WBOY
Release: 2016-06-24 12:27:59
Original
1302 people have browsed it

The use of DIV for positioning is now more and more widely used.
When I was using DIV to position pictures recently, I found that DIV cannot directly set the vertical alignment of the picture.
I was really troubled for a while, and then I found many solutions on the Internet.
But it didn’t seem to work, and then I discovered Mencius Chapter E (I was super surprised at the breadth of knowledge in Mencius Chapter E)
In a post to answer other people’s questions
there is a solution that looks very authentic. As follows

< div id ="Layer1"
style ="position:absolute;
width:500px;
height:305px;
z-index:1 ;
background-color: #999999;
border: 1px none #000000;
padding-top:expression(this.style.top (this.offsetHeight-mxh.offsetHeight)/2)"
align =center >
< img src ="http://sz.luohuedu.net/xml/Images/logo.gif" id =mxh >


After using it, I found that it still failed.
Then I checked the information and found that there are two problems in his method.

What this.style.top takes out is not a number but a string, such as 50px, so there is no way to operate in expression. The correct method should be the this.style.pixelTop property, so that the numbers are taken out. After the picture is placed in a DIV, the calculation of its top attribute value no longer starts from the upper left corner of the window, but from the upper left corner of the DIV container. Therefore, the top padding distance of the DIV does not need to be added to the top value of the DIV.

Based on the above two points, I made slight modifications to the method in Chapter E of Mencius and got the following method. I've tested it successfully.
The whole process took a lot of time, so I will post it here to share with everyone, so as to save everyone from making further detours, especially those who have not used expression yet.

< div id ="Layer1"
style ="
position:absolute;
left:261px;
top:50px;
wid th:251px;
Height:105px;
z-index:1;
overflow:hidden;
border: 1px solid #0066CC; expression
padding-top:((this.offsetHeight-myI mage.offsetHeight)/ 2);
" "
align ="center" >
< img src ="12.gif" width ="70" height ="76" align ="absmiddle" id ="myImage" >


There is a key point to remind everyone that the picture must be given an ID before it can be calculated. Remember~

Thank you Mencius Chapter E for his erudition, hehehe
I am once again amazed by the breadth of knowledge in Mencius Chapter E.

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