솔루션을 현재의 HTML 태그와 태그에 의해 지적은 다음과 같이 수행 할 수있는 기반 만 CSS 사용 :
이 working Fiddle 예를 참조를!
HTML
<div id="block">
<div id="content">Some content goes here.</div>
<div id="image">
<img src="path_to_image" />
</div>
</div>
CSS
#block {
width: 348px;
display: table; /* set the main wrapper to display as a table */
}
#content {
width: 164px;
padding: 20px;
}
#image {
width: 144px;
display: table-cell; /* set the inner wrapper to display as a cell */
vertical-align: middle; /* tell to vertically align the contents */
}
는 기술과 충돌 사용중인 일부 CSS position
선언을 제거 할 필요가 있었다. 그러나 CSS없이 정확히 같은 레이아웃을 구현할 수 있으므로 CSS vertical-align:middle
이 예상대로 작동 할 수 있습니다.
당신의 존재 CSS 선언 중 하나를 제거하고 정확히 같은 목표 달성이없는 마크 업에
JQuery와 솔루션 :
이 working Fiddle 예를 참조를!
jQuery를
#image
내부 img
에 도착하고, 그 높이를 수집하여 두 나누어 얻어진 값과 음으로 마진을 적용한다.
$(function() {
var $target = $('#image').find('img');
$target.css({
"margin-top" : "-" + ($target.height()/2) + "px" // adjust the top margin
});
});
CSS
#block {
width: 348px;
position: relative;
}
#content {
width: 164px;
padding: 20px;
margin-right: 144px;
}
#image {
width: 144px;
position: absolute;
right: 0;
top: 0; /* fit to the top */
bottom: 0; /* fit to the bottom */
/*overflow:hidden;*/ /* optional if the img is bigger that this container */
}
#image img {
position: absolute; /* remove element from the document flow */
top: 50%; /* move it down by 50% of its parent height */
}
HTML
<div id="block">
<div id="image">
<img src="path_to_image" />
</div>
<div id="content">Some content goes here.</div>
</div>
현재 마크 업을 보존하고 몇 가지 추가 CSS가 작동하도록 추가되었습니다. jQuery 부분은 가능한 한 간단하게 두십시오!
CSS 기반 솔루션을 사용하여이를 수행 할 수 있지만 적절한 방법은 HTML 마크 업에 따라 다릅니다. 문제에 대해 관련 HTML/CSS를 제공 할 수 없습니까? – Zuul
불행히도, 그것은 단지 일반적인 질문입니다. 이것은 개인 클라이언트이며, 내용과 디자인은 공개 될 때까지는 기밀입니다. 마크 업을 제공 할 수 있으면 좋을 듯 싶습니다. 나는 일반적인 코드를 함께 던질 것이다. – Chad