div가 컨테이너 내의 영역 대신 전체 페이지를 덮도록 허용


91

반투명 div가 전체 화면을 덮도록 만들려고합니다. 나는 이것을 시도했다 :

#dimScreen
{
    width: 100%;
    height: 100%;
    background:rgba(255,255,255,0.5);
}

그러나 그것은 전체 화면을 포함하지 않고 div 내의 영역 만 포함합니다.


4
추가position: absolute; top: 0; left: 0;
세르주 파라 스키 브에게

CSS3 또는 현재 가지고있는 확장에 대한 업데이트가 있습니까?
윌리엄 Entriken

답변:


175

추가 position:fixed. 그런 다음 스크롤 할 때도 전체 화면에 표지가 고정됩니다.
그리고 margin: 0; padding:0;덮개 주위에 약간의 공간이 없도록 추가 할 수도 있습니다 .

#dimScreen
{
    position:fixed;
    padding:0;
    margin:0;

    top:0;
    left:0;

    width: 100%;
    height: 100%;
    background:rgba(255,255,255,0.5);
}

고정 된 화면에 달라 붙지 않으면 position:absolute;

CSS Tricks 에는 전체 화면 속성에 대한 흥미로운 기사도 있습니다.

편집 :
방금이 답변을 보았으므로 몇 가지 추가하고 싶었습니다. 댓글에서 언급 한 Daniel Allen Langdon
처럼 커버는 화면의 맨 위와 왼쪽에 붙어 있습니다.top:0; left:0;

일부 요소가 표지의 맨 위에있는 경우 (모든 항목을 포함하지는 않음) z-index. 숫자가 높을수록 더 많은 레벨을 포함합니다.


1
제 경험상, 저는 또한 필요했습니다top: 0; left: 0;
Vivian River

나는 항상 이것으로 절대 위치에 어려움을 겪었지만 위치 고정을 사용하는 것이 훨씬 더 효과적이었습니다. 감사합니다
BrianLegg

2
분명히 문제가 있습니다. position : fixed 요소에 css 변환이있는 조상이 있으면 뷰포트 대신 해당 조상을 기준으로 고정됩니다. developer.mozilla.org/en-US/docs/Web/CSS/position#Values
mpoisot

18

부모 요소도 다음과 100%같이 설정해야합니다.

html, body {
    height: 100%;
}

데모 (background데모 용으로변경됨)


또한 전체 화면을 가리고 싶을 때는하려는 것 같기 dim때문에이 경우에는position: fixed;

#dimScreen {
    width: 100%;
    height: 100%;
    background:rgba(255,255,255,0.5); 
    position: fixed;
    top: 0;
    left: 0;
    z-index: 100; /* Just to keep it at the very top */
}

그렇다면 필요하지 않은 것보다 html, body {height: 100%;}

데모 2


2
부모 중 누구라도 position:relative. 이거 작동 안 할거야. position:fixed대신 제안하십시오 .
Muhammad Talha Akbar 2013 년

@MuhammadTalhaAkbar 오 이미 DT, 나는 내 대답은 삭제해야한다고 생각한다는 대답을 참조
씨 외국인

조금만 변경하면 다른 사람보다 낫습니다.
Muhammad Talha Akbar 2013 년


7

position:fixed이 방법을 사용 하면 div가 전체 가시 영역에 계속 유지됩니다.

div에 클래스를 제공 overlay하고 CSS에 다음 규칙을 만듭니다.

.overlay{
    opacity:0.8;
    background-color:#ccc;
    position:fixed;
    width:100%;
    height:100%;
    top:0px;
    left:0px;
    z-index:1000;
}

데모 : http://www.jsfiddle.net/TtL7R/1/


2
#dimScreen{
 position:fixed;
 top:0px;
 left:0px;
 width:100%;
 height:100%;
}

1

이 시도

#dimScreen {
    width: 100%;
    height: 100%;
    background:rgba(255,255,255,0.5);
    position: fixed;
    top: 0;
    left: 0;
}

1

CSS 재설정을 적용하여 이와 같은 모든 여백과 패딩을 재설정하십시오.

/* http://meyerweb.com/eric/tools/css/reset/ 

v2.0 | 20110126 라이센스 : 없음 (퍼블릭 도메인) * /

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
display: block;
}
body {
line-height: 1;
}
ol, ul {
list-style: none;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none;
}
table {
border-collapse: collapse;
border-spacing: 0;
}

필요에 따라 다양한 CSS를 사용할 수 있습니다.

 html
 {
  margin: 0px;
 padding: 0px;
 }

body
{
margin: 0px;
padding: 0px;
}

2
이것은 어떤 관련이 있습니까?
akauppi

0

html 및 body 태그 height를로 설정하고 본문 100%주변의 여백을 제거합니다.

html, body {
    height: 100%;
    margin: 0px; /* Remove the margin around the body */
}

이제 positiondiv를 fixed다음 과 같이 설정하십시오 .

#dimScreen
{
    width: 100%;
    height: 100%;
    background:rgba(255,255,255,0.5);

    position: fixed;
    top: 0px;
    left: 0px;

    z-index: 1000; /* Now the div will be on top */
}

데모 : http://jsfiddle.net/F3LHW/


0

본문에 여백과 패딩을 0으로 설정하여 시도하십시오.

<body style="margin: 0 0 0 0; padding: 0 0 0 0;">
당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.