【问题标题】:Margin on a div with width 100% height 100% inside another div with width 100% height 100%宽度为 100% 高度为 100% 的 div 上的边距在宽度为 100% 高度为 100% 的另一个 div 内
【发布时间】:2013-04-04 16:45:15
【问题描述】:

我正在构建一个以视频为背景的网站。我希望所有 div 居中且响应迅速,这就是为什么我使用 100% 的宽度和高度。然后,我有一个覆盖 div,使用 jquery 在点击时淡入淡出。我的问题是,在这个 div 淡入之后,我似乎无法在它周围获得均匀的边距。我想要边距的 div 的 id 为“info”。

我的 html 看起来像这样:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8"/>
        <meta name="description" content=""/>
        <meta name="keywords"content=""/>
        <title></title>
        <link href="css/main.css" rel="stylesheet" type="text/css"/>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script>
$(document).ready(function(){
  $("#btn").click(function(){
    $("#info").fadeToggle("slow");
  });
});
</script>
    </head>
    <header>
        <div id="nav">
            <p><a id="btn" href="#">+</a></p>
        </div>
    </header>
    <body>
        <div id="container">
            <div id="info">
            </div>
        </div>
    </body>
    <video id="video_background" src="vid/147000037.mp4" autoplay>
</html>

还有我的 CSS:

* {
    margin: 0;
    padding: 0;
}


body {
    font-family: "HelveticaNeue-UltraLight","HelveticaNeue-Light","Helvetica Neue",Helvetica,Arial,sans-serif;

}

header {
    z-index: 999;
    display: block;
    height: auto;
    width: 100%;
    position: fixed;
}

header a {
    text-decoration: none;
    font-size: 1.8em;
    color: #000000;
}

#nav {
    position: relative;
    float: right;
    top: 15px;
    right: 20px;
    color: #000000;
}


#container {
    height: 100%;
    width: 100%;
    display: block;
}

#video_background {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  z-index: -1000;
}

#info {
    height: 100%;
    width: 100%;
    background: rgba(255,255,255,0.97);
    z-index: 900;
    display: none;
    position: fixed;
    margin: 10px;
    vertical-align: center;
}

【问题讨论】:

    标签: html css margin centering


    【解决方案1】:

    由于您的信息框具有固定的定位,您可以在 CSS 中省略高度/宽度和边距,并使用偏移量来创建具有良好边距的响应式容器。只需将您的 #info css 更改为以下内容:

    #info {
       /* omit height & width */
       background: #bada55; /* Just because white on white is tough to see */
       z-index: 900;
       display: none;
       position: fixed;
       /* omit margin and use according offsets */
       top:15px;left:15px;right:15px;bottom:15px;
       vertical-align: center;
    }
    

    working here

    【讨论】:

    • 工作就像一个魅力。谢谢克里斯托夫!
    • @user2273444 为什么你不接受这个答案是正确的?这是在 S.O. 上说“谢谢”的一种方式