定位绝对,负右移,滚动条移除

时间:2012-07-26 08:20:51

标签: html css positioning

在窗口调整大小时,绝对定位的DIV移动到HTML的右边界之外 - 因此会出现水平滚动条。但我需要在中心固定宽度列上滚动条 。任何消息?如何正确评论悄悄地进入HTML标签右边界?

请参阅http://jsfiddle.net/9y5BW/1/

<!DOCTYPE HTML>
<html>
<head><title>Absolute position right scrollbar removing</title></head>

<body>
  <style>
    body {line-height: 1.5em;}
    p {margin: 0;}
    .page {position: relative; width: 400px; margin: 0 auto; background-color: #ccc;}
    .comment-container {position: relative; width: 100%; height: 0; top: -1px;}
    .comment {position: absolute; width: 200px; background-color: #eee; top: -1.5em; border-top: 1px solid #aaa;}
  </style>

    <div class="page">
    <p>
        Text and images here. Scrollbar should appear when window size is less than 400px.
    </p>
    <div class="comment-container">
      <div class="comment" style="right: -200px;">
        Some outside comment on the right. Horizontal scrolbar on html-tag appears on window squeezing.
        Is there any way to remove the scrollbar when this element goes outside?
      </div>
    </div>
    <p>
        More text, text and text goes here.
    </p>
    <div class="comment-container">
      <div class="comment" style="left: -200px;">
        Some outside comment on the left. No scrollbar on window resizing.
      </div>
    </div>
    </div>

</body>
</html>

2 个答案:

答案 0 :(得分:1)

您将所有内容放入具有隐藏溢出的容器中,然后将右列right: -100px放置如下:

<!DOCTYPE html>
<html>
<head>
<style type="text/css">
html,
body { 
    margin: 0;
    padding: 0;
    text-align: center;
}

body {
    overflow: auto;
}
#container {
    min-width: 960px;
    zoom: 1; /*For ie6*/
    position: relative; /*For ie6/7*/
    overflow: hidden;
    margin: 0 auto;
}

#main {
    background: #cea;
    width: 960px;
    margin: 0 auto;
    height: 700px;
    position: relative;
    top: 0;
}

#right,
#left {
    position: absolute;
    height: 100px;
    width: 100px;
    top: 0;
    z-index: 100;
}

#right { 
    background: #797;
    right: -100px;
}

#left {
    background: #590;
    left: -100px;
}      
</style>
</head>
<body>
    <div id="container">
        <div id="main">
            <div id="left">left</div>
            <div id="right">right</div>
        </div>
    </div>
</body>
</html>

答案 1 :(得分:0)

如果在正确的div / wrappers上使用overflow: hidden,您可以决定哪些内容不会触发滚动条。