2014-12-28 2 views

답변

1

CSS로 처리 할 수 ​​없으므로 부모 컨테이너의 widthheight (#A)을 설정하려면 자바 스크립트가 필요합니다.

.offsetHeight.offsetWidth을 사용할 수 있습니다.

모두 반환 실제 width/height + padding-leftpadding-right/padding-toppadding-bottom + border-left-widthborder-right-width/border-top-widthborder-bottom-width. 아래의 예에서


, 나는의 알파 값 부모 컨테이너 ( #A) 하위 컨테이너와 동일한 widthbackgroundImageheight을 부여하고 하위 컨테이너의 ( #B) background 설정 '의 border들과'한 그것을 더 잘 설명하기 위해 0.5에.

var a = document.getElementById('A'); 
 
var b = document.getElementById('B'); 
 

 
a.style.height = b.offsetHeight + 'px'; 
 
a.style.width = b.offsetWidth + 'px'; 
 
a.style.backgroundImage = 'url(http://www.lorempixel.com/' + b.offsetWidth + '/' + b.offsetHeight + ')'
#A { 
 
    position: relative; 
 
} 
 
#B { 
 
    position: absolute; 
 
    background: rgba(210,180,140, 0.5); 
 
    width: 300px; 
 
    height: 100px; 
 
    border: 5px solid rgba(210,0,0, 0.5); 
 
    padding: 10px; 
 
}
<div id="A"> 
 
    <div id="B"> 
 
    </div> 
 
</div>

당신은 여기에 대한 자세한 읽을 수 있습니다 : http://kingori.co/minutae/2013/05/relative-parent-absolute-children/

+1

그래, 내가 자바 스크립트를 사용하는 것을 피하기 위해 기대했다,하지만 유일한 방법 것처럼 않습니다. 감사! – gcbenison

0

표시 옵션을 사용해 보셨습니까?

디스플레이 : 블록

코드의보다 자세한 버전을 제시해 주시겠습니까?

관련 문제