105

画面全体を半透明の div でカバーしようとしています。私はこれを試しました:

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

ただし、それは画面全体をカバーするのではなく、div 内の領域のみをカバーします。

4

9 に答える 9

204

を追加し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 トリックには、フルスクリーン プロパティに関する興味深い記事もあります。

編集:
この回答に出くわしたので、いくつか追加したいと思いました。コメントで述べたダニエル・アレン・ラングドン
の ように、カバーが画面の一番上と左にくっつくことを確認してください.top:0; left:0;

一部の要素をカバーの上部に配置する (すべてをカバーしないようにする) 場合は、 を追加しz-indexます。数値が高いほど、より多くのレベルをカバーします。

于 2013-09-03T09:38:26.903 に答える
23

親要素も設定する必要があり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

于 2013-09-03T09:37:54.143 に答える
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/

于 2013-09-10T07:26:54.013 に答える
2
#dimScreen{
 position:fixed;
 top:0px;
 left:0px;
 width:100%;
 height:100%;
}
于 2013-09-03T09:40:52.173 に答える
1

これを試して

#dimScreen {
    width: 100%;
    height: 100%;
    background:rgba(255,255,255,0.5);
    position: fixed;
    top: 0;
    left: 0;
}
于 2013-09-03T09:38:42.190 に答える
1

css-reset を適用して、このようにすべてのマージンとパディングをリセットします

/* 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;
}
于 2013-09-03T09:39:11.893 に答える
0

body の margin と padding を 0 に設定してみてください。

<body style="margin: 0 0 0 0; padding: 0 0 0 0;">
于 2014-10-31T11:56:46.160 に答える
0

html タグと body タグ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/

于 2013-09-03T09:51:35.047 に答える