끝 - < 스니핏 >;!
(그리고 최고의 크로스 브라우저) 방식으로 사용할 수 있는 유일한 그러니까말이야 인라인 블록 '도우미' 와 '높이: 100% '와' 수직 정렬: 중간 '의 두 요소가.
그래서 해결책은 있다.
<! - begin 스니핏: js 숨기십시오: 참 - >.
.frame {
height: 25px; /* Equals maximum image height */
width: 160px;
border: 1px solid red;
white-space: nowrap; /* This is required unless you put the helper span closely near the img */
text-align: center;
margin: 1em 0;
}
.helper {
display: inline-block;
height: 100%;
vertical-align: middle;
}
img {
background: #3A6F9A;
vertical-align: middle;
max-height: 25px;
max-width: 160px;
}
<div class="frame">
<span class="helper"></span><img src="http://jsfiddle.net/img/logo.png" height=250px />
</div>
<div class="frame">
<span class="helper"></span><img src="http://jsfiddle.net/img/logo.png" height=25px />
</div>
<div class="frame">
<span class="helper"></span><img src="http://jsfiddle.net/img/logo.png" height=23px />
</div>
<div class="frame">
<span class="helper"></span><img src="http://jsfiddle.net/img/logo.png" height=21px />
</div>
<div class="frame">
<span class="helper"></span><img src="http://jsfiddle.net/img/logo.png" height=19px />
</div>
<div class="frame">
<span class="helper"></span>
<img src="http://jsfiddle.net/img/logo.png" height=17px />
</div>
<div class="frame">
<span class="helper"></span>
<img src="http://jsfiddle.net/img/logo.png" height=15px />
</div>
<div class="frame">
<span class="helper"></span>
<img src="http://jsfiddle.net/img/logo.png" height=13px />
</div>
<div class="frame">
<span class="helper"></span>
<img src="http://jsfiddle.net/img/logo.png" height=11px />
</div>
<div class="frame">
<span class="helper"></span>
<img src="http://jsfiddle.net/img/logo.png" height=9px />
</div>
<div class="frame">
<span class="helper"></span>
<img src="http://jsfiddle.net/img/logo.png" height=7px />
</div>
<div class="frame">
<span class="helper"></span>
<img src="http://jsfiddle.net/img/logo.png" height=5px />
</div>
<div class="frame">
<span class="helper"></span>
<img src="http://jsfiddle.net/img/logo.png" height=3px />
</div>
끝 - < 스니핏 >;!
이 솔루션은 전에 () '을 (를)', '와' 표현식에서는 internet& nbsp, 탐색기: http://jsfiddle.net/kizu/4RPFa/4571/
<! - begin 스니핏: js 숨기십시오: 참 - >.
.frame {
height: 25px; /* Equals maximum image height */
width: 160px;
border: 1px solid red;
white-space: nowrap;
text-align: center;
margin: 1em 0;
}
.frame:before,
.frame_before {
content: "";
display: inline-block;
height: 100%;
vertical-align: middle;
}
img {
background: #3A6F9A;
vertical-align: middle;
max-height: 25px;
max-width: 160px;
}
/* Move this to conditional comments */
.frame {
list-style:none;
behavior: expression(
function(t){
t.insertAdjacentHTML('afterBegin','<span class="frame_before"></span>');
t.runtimeStyle.behavior = 'none';
}(this)
);
}
<div class="frame"><img src="http://jsfiddle.net/img/logo.png" height=250px /></div>
<div class="frame"><img src="http://jsfiddle.net/img/logo.png" height=25px /></div>
<div class="frame"><img src="http://jsfiddle.net/img/logo.png" height=23px /></div>
<div class="frame"><img src="http://jsfiddle.net/img/logo.png" height=21px /></div>
<div class="frame"><img src="http://jsfiddle.net/img/logo.png" height=19px /></div>
<div class="frame"><img src="http://jsfiddle.net/img/logo.png" height=17px /></div>
<div class="frame"><img src="http://jsfiddle.net/img/logo.png" height=15px /></div>
<div class="frame"><img src="http://jsfiddle.net/img/logo.png" height=13px /></div>
<div class="frame"><img src="http://jsfiddle.net/img/logo.png" height=11px /></div>
<div class="frame"><img src="http://jsfiddle.net/img/logo.png" height=9px /></div>
<div class="frame"><img src="http://jsfiddle.net/img/logo.png" height=7px /></div>
<div class="frame"><img src="http://jsfiddle.net/img/logo.png" height=5px /></div>
<div class="frame"><img src="http://jsfiddle.net/img/logo.png" height=3px /></div>
끝 - < 스니핏 >;!
How it works.
이 유용할 수 있습니다.
div {
position: relative;
width: 200px;
height: 200px;
}
img {
position: absolute;
top: 0;
bottom: 0;
margin: auto;
}
.image {
min-height: 50px
}
참조: http://www.student.oulu.fi/ ~ laurirai/www/css/middle/
데모: html, css, 출력입니다 https://jsbin.com/lidebapomi/edit?
[미리봅니다] (https://i.imgur.com/Fgh89yb.png)!
< hr/>;
HTML:
<div class="frame">
<img src="foo"/>
</div>
CSS.
.frame {
height: 160px; /* Can be anything */
width: 160px; /* Can be anything */
position: relative;
}
img {
max-height: 100%;
max-width: 100%;
width: auto;
height: auto;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}
A 순결케 CSS 해결책:
<! - begin 스니핏: js 숨기십시오: 거짓값 콘솔: 진정한 바벨. > 거짓값 -;
.frame {
margin: 1em 0;
height: 35px;
width: 160px;
border: 1px solid red;
position: relative;
}
img {
max-height: 25px;
max-width: 160px;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
background: #3A6F9A;
}
<div class=frame>
<img src=" /embedded/result,js,html,css/">
끝 - < 스니핏 >;!
주요 항목
// position: relative; - in .frame holds the absolute element within the frame
// top: 0; bottom: 0; left: 0; right: 0; - this is key for centering a component
// margin: auto; - centers the image horizontally & vertically
좀 더 현대적인 위한 경우, 기존 브라우저 솔루션을 지원할 필요가 없습니다, 이 작업을 수행할 수 있습니다.
<! - begin 스니핏: js 숨기십시오: 거짓값 콘솔: 진정한 바벨. > 거짓값 -;
.frame {
display: flex;
/**
Uncomment 'justify-content' below to center horizontally.
✪ Read below for a better way to center vertically and horizontally.
**/
/* justify-content: center; */
align-items: center;
}
img {
height: auto;
/**
✪ To center this image both vertically and horizontally,
in the .frame rule above comment the 'justify-content'
and 'align-items' declarations,
then uncomment 'margin: auto;' below.
**/
/* margin: auto; */
}
/* Styling stuff not needed for demo */
.frame {
max-width: 900px;
height: 200px;
margin: auto;
background: #222;
}
p {
max-width: 900px;
margin: 20px auto 0;
}
img {
width: 150px;
}
<div class="frame">
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/9988/hand-pointing.png">
</div>
끝 - < 스니핏 >;!
또한 플렉상자 정확한 결과를 얻기 위해 사용할 수 있습니다.
<! - begin 스니핏: js 숨기십시오: 거짓값 콘솔: 거짓값 바벨. > 거짓값 -;
.parent {
align-items: center; /* For vertical align */
background: red;
display: flex;
height: 250px;
/* justify-content: center; <- for horizontal align */
width: 250px;
}
<div class="parent">
<img class="child" src="https://cdn2.iconfinder.com/data/icons/social-icons-circular-black/512/stackoverflow-128.png" />
</div>
끝 - < 스니핏 >;!
아래 코드를 실행할 수 있습니다.
<! - begin 스니핏: js 숨기십시오: 거짓값 콘솔: 진정한 바벨. > 거짓값 -;
.frame{
display: flex;
justify-content: center;
align-items: center;
width: 100%;
}
<div class="frame" style="height: 25px;">
<img src=" /embedded/result,js,html,css/">
끝 - < 스니핏 >;!
내가 재거됨 이미지 요소를 모두 갖춘 it 배경으로 설정하고 div 학번 오프라임 '
적어도 이 Internet&;;; 빨리 빨리, 빨리 대해 과태료 작동됨 Explorer& Firefox& &, 크롬, 6, 8, nbsp. 13.
이 솔루션은 확인해봤는데, 큰 이미지를 높이 25 명이 넘는 픽셀입니다 줄어들 것으로 작동하지 않습니다. 특성 '설정' 이라는 좁히어 배경 크기 요소의 크기를 않는 것은, 빨리, 빨리, 빨리 explorer& 충돌합니다 internet& 있는 css& 3;; 7 홍보하십시오.
나라면 어떤 브라우저를 사용할 수 있는 가장 좋은 방법은 우선 순위 및 디자인 너회가 조언을 명령복구 브라우저, 또는 좀 서버측 코드를 크기조정할 you& 이미지를 사용할 경우, d # 39 이 솔루션을.
.frame {
height: 25px; /* Equals maximum image height */
line-height: 25px;
width: 60px;
border: px solid red;
text-align: center;
margin: em 0;
position: relative; /* Changes here... */
}
img {
background: #3A6F9A;
max-height: 25px;
max-width: 60px;
top: 50%; /* Here.. */
left: 50%; /* Here... */
position: absolute; /* And here */
}
< br/>;
$("img").each(function(){
this.style.marginTop = $(this).height() / -2 + "px";
})
.frame {
height: 35px; /* Equals maximum image height */
width: 160px;
border: 1px solid red;
text-align: center;
margin: 1em 0;
display: table-cell;
vertical-align: middle;
}
img {
background: #3A6F9A;
display: block;
max-height: 35px;
max-width: 160px;
}
주요 속성은 '디스플레이: 표에서 오프라임 ',' 는 '. '' 로 표시되는 데이비드프라임 인라인 대체하십시오 안에 마무리 할 수 있도록 블록 요소.
이 작품은 오페라, 파이어폭스, 크롬, 사파리, 빨리, 빨리 Explorer& 있는 Internet&;; 8 (이상).
업데이트
빨리, 빨리 Explorer& Internet&;; 7 의 CSS 표현식에서는 추가해야 합니다.
*:first-child+html img {
position: relative;
top: expression((this.parentNode.clientHeight-this.clientHeight)/2+"px");
}
하지만, 익스플로러, 파이어폭스, 크롬, i am not 확신하는 Internet& 빨리 아래에 있는 경우 (img) ',' 다음 '에서' div 컨테이너인 css 컨텐트입니다 노력해야 합니다. 적어도 가져다줄래요 작동하잖아 뭐.
div.img-container {
display: table-cell;
vertical-align: middle;
height: 450px;
width: 490px;
}
div.img-container img {
max-height: 450px;
max-width: 490px;
}
이 솔루션을 통해 시도하시겠습니까 순결케 CSS
어쩌면 가장 중요한 문제가 HTML. 프랑스 파리 ~ 'c' 인용 # 39, re you& 사용하지 않는 정의할 때 &. '높이' 의 이미지를 너회의 HTML.
CSS.
.frame {
height: 25px; /* Equals maximum image height */
width: 160px;
border: 1px solid red;
position: relative;
margin: 1em 0;
top: 50%;
text-align: center;
line-height: 24px;
margin-bottom: 20px;
}
img {
background: #3A6F9A;
vertical-align: middle;
line-height: 0;
margin: 0 auto;
max-height: 25px;
}
Img '태그' 를 중심으로 일을 it& # 39 의 공간 '에서' 3 대 2 로 픽셀입니다 것가운데 픽셀입니다. 이젠 ',' s 라인 높이 감소 및 it& # 39, 작업 중.
CSS.
.frame {
height: 25px; /* Equals maximum image height */
width: 160px;
border: 1px solid red;
margin: 1em 0;
text-align: center;
line-height: 22px;
*:first-child+html line-height:24px; /* For Internet Explorer 7 */
}
img {
background: #3A6F9A;
vertical-align: middle;
line-height: 0;
max-height: 25px;
max-width: 160px;
}
@media screen and (-webkit-min-device-pixel-ratio:0) {
.frame {
line-height:20px; /* WebKit browsers */
}
이 '속성' 라인 높이 렌더링됩니다 브라우저마다 다르게. 그래서 우리는 우리가 정의하십시오 '속성' 라인 높이 다른 브라우저.
이걸 예: http://jsfiddle.net/sandeep/4be8t/11/
예를 들어 '이것' 라인 높이 차이가 약 다른 브라우저: , https://stackoverflow.com/questions/7229568/input-height-differences-in-firefox-and-chrome
내 해결책: [
<div class="container">
<div class="frame">
<img src="http://jsfiddle.net/img/logo.png" class="img" alt="" />
</div>
</div>
.container {
display: table;
float: left;
border: solid black 1px;
margin: 2px;
padding: 0;
background-color: black;
width: 150px;
height: 150px;
}
.frame {
display: table-cell;
text-align: center;
vertical-align: middle;
border-width: 0;
}
.img {
max-width: 150px;
max-height: 150px;
vertical-align: middle;
}
테이블로 / 표에서 표시하여 해결해야 할 경우도 있습니다. 예를 들어, 빠른 타이틀 화면이. W3 컴포지션이 권장됨 방식으로 있다. 내가 선택한 이 링크를 추천합니까 불렀으매 [블록 또는 이미지 중심으로] [1] 출처: W3C.org.
여기에 사용된 팁을 다음과 같습니다.
<! - begin 스니핏: js 숨기십시오: 거짓값 콘솔: 진정한 바벨. > 거짓값 -;
.container {
position: absolute;
display: table;
width: 100%;
height: 100%;
}
.content {
display: table-cell;
vertical-align: middle;
}
<div class="container">
<div class="content">
<h1 style="text-align:center">Peace in the world</h1>
</div>
</div>
끝 - < 스니핏 >;!
[1]: https://www.w3.org/Style/Examples/007/center.en.html # 블록
개인적으로 알고 이를 위해 사용하는 대한 사실 그렇지 않다.
쉽게 사용할 수 있는 날.
img {
vertical-align: middle;
display: inline-block;
position: relative;
}
업무용 구글 크롬 작동하잖아 잘 았겠습니다. 이 중 한 명, 시도하시겠습니까 다른 브라우저.