html - 相对于居中内容 div 的固定位置 div?

标签 html css

我是 CSS 初学者。 我想要一个包含主要内容的半透明居中 div。它下面应该是一个包含目录的固定 div。

下面是我的尝试。这适用于特定的浏览器大小。但是当浏览器窗口的大小发生变化时,目录也会移动。

我希望目录与主 div 保持固定距离。

jsFiddle link

有了这个窗口大小,一切看起来都很好: with this window size everything looks ok

减小窗口大小将 toc 移动到内容 div 下: decreasing the window size moves toc under content div

html

<html>
<head>
    <title>Testpage</title>
    <link rel='stylesheet' href='css/testpage.css'>
</head>
<body>
    <div id="contenttable">
        <h1>Contents</h1>
        <a href="#">Content 01</a><br>
    </div>
    <div id="content">  
        some text
    </div>
</body>
</html>

CSS:

#content{
    height: 1000px;
    width:  320px;
    position: relative;
    top: 50px;
    left: 50%;
    margin-left: -160px;
    background-color: cyan;
        }

#contenttable{
    padding: 12px;
    width:100%;
    height:200px;
    position: fixed;
    background-color: yellow;
    top: 125px;
    left: 6%;
}

#contenttable a{
    position: relative;
    top: 0px;
    left: 66%;
}

#contenttable h1{

    position: relative;
    top: 0px;
    left: 66%;
}

最佳答案

您可以使用内部 div absolute定位在 fixed TOC 内,并设置其位置。

使用 CSS3 Calc 为您的主要内容制定正确的位置。

使用 opacity 实现透明,避免设置主要内容 div 的 height 进行自动溢出处理。

演示:http://jsfiddle.net/vMAQz/1/

CSS

#contenttable {
  padding: 12px;
  width:100%;
  height:200px;
  position: fixed;
  background-color: yellow;
  top: 125px;
}
#innerContent {
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  width: 100px;
  padding: 30px;
}
#content {
  padding: 10px;
  opacity: 0.8;
  width: 320px;
  position: relative;
  top: 50px;
  left: calc(100% - 480px);
  background-color: cyan;
}

HTML

<div id="contenttable">
  <div id="innerContent">
    <h1>Contents</h1>
    <a href="#">Content 01</a>
    <br/>
  </div>
</div>
<div id="content">
   some text
</div>      

关于html - 相对于居中内容 div 的固定位置 div?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14468629/

相关文章:

javascript - 我想为我的插件添加一个简单的 jquery slider

javascript - 如何在我的 jQuery 导航菜单中正确定位箭头?

html - 容器内列的全宽背景 [Bootstrap 3]

CSS "see-through"背景 - 疯狂导航菜单问题

html - 如何将水平表格单元格和其中的内容居中居中

javascript - 使用 JavaScript 的 dataTable 中的 Colspan

javascript - 我的 HTML 按钮将我重定向到这个奇怪的 "Index of C:\"内容。我如何解决它?

html - Bootstrap - 如何在与 form-control-lg 高度相同的情况下实现具有更大高度的自定义选择?

CSS 过渡

jquery - 使用 Timer JQuery 更改背景颜色 CSS