ฉันกำลังพยายามสร้าง div แบบกึ่งโปร่งใสครอบคลุมทั้งหน้าจอ ฉันลองสิ่งนี้:
#dimScreen
{
width: 100%;
height: 100%;
background:rgba(255,255,255,0.5);
}
แต่ไม่ได้ครอบคลุมทั้งหน้าจอ แต่จะครอบคลุมเฉพาะพื้นที่ภายใน div
ฉันกำลังพยายามสร้าง div แบบกึ่งโปร่งใสครอบคลุมทั้งหน้าจอ ฉันลองสิ่งนี้:
#dimScreen
{
width: 100%;
height: 100%;
background:rgba(255,255,255,0.5);
}
แต่ไม่ได้ครอบคลุมทั้งหน้าจอ แต่จะครอบคลุมเฉพาะพื้นที่ภายใน div
คำตอบ:
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
แล้วเพิ่ม ยิ่งจำนวนสูงเท่าไหร่ก็ยิ่งครอบคลุมระดับมากขึ้นเท่านั้น
top: 0; left: 0;
คุณต้องตั้งค่าองค์ประกอบหลัก100%
เป็นด้วย
html, body {
height: 100%;
}
Demo (เปลี่ยน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%;}
position:relative
ของพ่อแม่มี มันจะไม่ทำงาน แนะนำposition:fixed
แทน
นี่จะทำเคล็ดลับ!
div {
height: 100vh;
width: 100vw;
}
ใช้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/
#dimScreen{
position:fixed;
top:0px;
left:0px;
width:100%;
height:100%;
}
ลองทำตามนี้
#dimScreen {
width: 100%;
height: 100%;
background:rgba(255,255,255,0.5);
position: fixed;
top: 0;
left: 0;
}
ใช้ css-reset เพื่อรีเซ็ตระยะขอบและ paddings ทั้งหมดเช่นนี้
/* 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-resets ต่างๆได้ตามต้องการปกติและใช้ใน css
html
{
margin: 0px;
padding: 0px;
}
body
{
margin: 0px;
padding: 0px;
}
ตั้งค่า HTML และร่างกายแท็กheight
ไป100%
และลบขอบรอบ ๆ ร่างกาย:
html, body {
height: 100%;
margin: 0px; /* Remove the margin around the body */
}
ตั้งค่าposition
div ของคุณเป็น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 บนตัวถัง
<body style="margin: 0 0 0 0; padding: 0 0 0 0;">
position: absolute; top: 0; left: 0;