棘手的 CSS 布局

发布于 2024-08-24 08:57:31 字数 607 浏览 1 评论 0原文

所以我正在制作一个布局相当有问题的网站。有四个角图像TL、TR、BL和BR,用黑色块表示。深橙色区域是主要内容(宽度为960px),绿色箭头表示的外部区域是浏览器窗口。请参阅图表:

图表 http://dotcafedesigns.com/stackoverflow/problem.gif

顶部图像代表最窄的站点可能 - 它不应该允许比这个(960px)更窄,如果它比定义的区域大,那么就不应该有滚动条。底部两个图像代表浏览器的不同宽度。

左下方和右下方的黑色块(图像)应始终位于屏幕的左下方和右下方,除非宽度降至 960 像素,在这种情况下 BL 和 BR 图像应稍微伸入主要区域。如果网站缩小到(例如 200 像素),BR 图像不应仍然在右上角。

在这一点上,我并不真正关心它在 IE6 中是否准确工作(我可以让它大致工作),但我什至无法弄清楚如何在没有 Javascript 或极其实验性的 CSS 的情况下完全完成它。目前我正在使用绝对定位的 div 来完成这种工作,但工作得不太正确。

我想如果没有其他办法的话我愿意接受一点JS,但我不愿意。

回答非常感谢!

So I am making a website with quite a problematic layout. There are four corner images TL, TR, BL and BR indicated by black blocks. The dark orange area is the main content (to a width of 960px), with the outside area denoted by the green arrow as the browser window. See diagram:

Diagram http://dotcafedesigns.com/stackoverflow/problem.gif

The top image represents the site at its narrowest possible - it shouldn't be allowed to be narrower than this (960px) if it is larger than the defined area there should be no scrollbars. The bottom two images represent different widths of browser.

The bottom left and right black blocks (images) should be at the bottom left and right of the screen at all times, unless the width falls to 960px, in which case the BL and BR images should poke into the main area slightly. If the site is shrunk to, say 200px, the BR image should not still be poking in the right corner.

At this point I don't really care about it working exactly in IE6 (I can get it roughly working) but I can't even figure out how to do it fully without Javascript or extremely experimental CSS. Currently I am using absolutely positioned div's which sort of work, but don't work quite right.

I think I'd be willing to accept a bit of JS if there is no other way but I'd rather not.

Answer very appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最美不过初阳 2024-08-31 08:57:31

不需要 JavaScript。至少在大多数现代浏览器中是这样。使用简单的定位和一个 @media 查询,我成功了:

<head>
  <style type="text/css" media="screen">
    body {
      margin: 0;
      padding: 0;
      font-family: sans-serif;
      background: orange;
      text-align: center;
      color: black;
      overflow-x: hidden;
    }
    #content {
      width: 960px;
      margin: 0 auto;
      padding: 20px 0;
      min-height: 800px;
      background: #cc6600;
      z-index: 0;
    }
    .image {
      background: black;
      color: white;
      height: 60px;
      width: 100px;
      padding: 20px 0;
      z-index: 1;
      opacity: .95;
    }
    #top {
      width: 960px;
      margin: 0 auto;
      position: relative;
      overflow: visible;
    }
    #top .image {
      position: absolute;
    }
    #top .left {
      left: -80px;
    }
    #top .right {
      right: -80px;
    }
    #bottom {
      position: fixed;
      bottom: 0;
      width: 100%;
      height: 100px;
    }
    #bottom .image {
      position: absolute;
    }
    #bottom .left {
      left: 0;
    }
    #bottom .right {
      right: 0;
    }

    @media all and (max-width:1120px) {

      #container {
        width: 960px;
        margin: 0 auto;
        position: relative;
        overflow: visible;
      }
      #bottom .left {
        left: -80px;
      }
      #bottom .right {
        right: -80px;
      }    

    }
  </style>
</head>
<body>
  <div id="top">
    <div class="left image">left image</div>
    <div class="right image">right image</div>    
  </div>
  <div id="content">
    content
  </div>
  <div id="bottom">
    <div id="container">
      <div class="left image">left image</div>
      <div class="right image">right image</div>
    </div>
  </div>
</body>

这可能属于您对“极其实验性 CSS”的描述,但我认为您会对它拥有如此多的支持以及引导它是多么容易感到惊讶只需使用 JS 即可实现 - 只需在调整大小时检查浏览器宽度,并在宽度低于 1120px 时添加额外的 CSS。

No need for Javascript. At least in most modern browsers. Using simple positioning and one @media query I pulled it off:

<head>
  <style type="text/css" media="screen">
    body {
      margin: 0;
      padding: 0;
      font-family: sans-serif;
      background: orange;
      text-align: center;
      color: black;
      overflow-x: hidden;
    }
    #content {
      width: 960px;
      margin: 0 auto;
      padding: 20px 0;
      min-height: 800px;
      background: #cc6600;
      z-index: 0;
    }
    .image {
      background: black;
      color: white;
      height: 60px;
      width: 100px;
      padding: 20px 0;
      z-index: 1;
      opacity: .95;
    }
    #top {
      width: 960px;
      margin: 0 auto;
      position: relative;
      overflow: visible;
    }
    #top .image {
      position: absolute;
    }
    #top .left {
      left: -80px;
    }
    #top .right {
      right: -80px;
    }
    #bottom {
      position: fixed;
      bottom: 0;
      width: 100%;
      height: 100px;
    }
    #bottom .image {
      position: absolute;
    }
    #bottom .left {
      left: 0;
    }
    #bottom .right {
      right: 0;
    }

    @media all and (max-width:1120px) {

      #container {
        width: 960px;
        margin: 0 auto;
        position: relative;
        overflow: visible;
      }
      #bottom .left {
        left: -80px;
      }
      #bottom .right {
        right: -80px;
      }    

    }
  </style>
</head>
<body>
  <div id="top">
    <div class="left image">left image</div>
    <div class="right image">right image</div>    
  </div>
  <div id="content">
    content
  </div>
  <div id="bottom">
    <div id="container">
      <div class="left image">left image</div>
      <div class="right image">right image</div>
    </div>
  </div>
</body>

That may fall under your description of "extremely experimental CSS", but I think you'd be surprised by how much support it does have, and how easy it would be to bootstrap that with a touch of JS - simply check browser width on re-size and add the extra CSS when the width falls under 1120px.

执着的年纪 2024-08-31 08:57:31

像这样的东西吗?如果您愿意,您可以用类似的 JQuery 代码替换 JavaScript 代码,只是想说明这是如何工作的。

<html>
<head>
  <title>....</title>

  <style type="text/css">
  html
  { height: 100%; margin: 0; background: white; }
  body
  { height: 100%; width: 960px; margin: 0 auto; background: gray; overflow: hidden; }
  #main
  { margin: 0px; padding: 5px 20px; position: relative; }

  #headLeft
  { position: absolute; top: 0px; left: -80px;
    width: 100px; height: 35px; background: green; }
  #headRight
  { position: absolute; top: 0px; right: -80px;
    width: 100px; height: 35px; background: green; }
  #footLeft
  { position: absolute; bottom: 0px; left: 0px;
    width: 100px; height: 35px; background: green; }
  #footRight
  { position: absolute; bottom: 0px; right: 0px;
    width: 100px; height: 35px; background: green; }
  </style>

  <script type="text/javascript">
  function checkSize ( event )
  {
    var contentWidth = 960;
    var minOuterBoxSize = 60; // maximum 100-60=40 px will be displayed inside the main area

    if ( window.innerWidth - contentWidth < minOuterBoxSize * 2 )
    {
      var pos = ( ( minOuterBoxSize * 2 ) - window.innerWidth + contentWidth ) / 2;
      document.getElementById( 'footLeft' ).style.left = '-' + pos;
      document.getElementById( 'footRight' ).style.right = '-' + pos;
    }
    else
    {
      document.getElementById( 'footLeft' ).style.left = '0px';
      document.getElementById( 'footRight' ).style.right = '0px';
    }
  }
  </script>
</head>

<body onload="checkSize( event );" onresize="checkSize( event );">
 <div id="main">
  <div id="headLeft">TL</div>
  <div id="headRight">TR</div>

  <p>Normal content</p>
 </div>

 <div id="footLeft">BL</div>
 <div id="footRight">BR</div>
</body>
</html>

Something like this? You may replace the JavaScript code with similar JQuery code if you like, just wanted to give the idea how this could work.

<html>
<head>
  <title>....</title>

  <style type="text/css">
  html
  { height: 100%; margin: 0; background: white; }
  body
  { height: 100%; width: 960px; margin: 0 auto; background: gray; overflow: hidden; }
  #main
  { margin: 0px; padding: 5px 20px; position: relative; }

  #headLeft
  { position: absolute; top: 0px; left: -80px;
    width: 100px; height: 35px; background: green; }
  #headRight
  { position: absolute; top: 0px; right: -80px;
    width: 100px; height: 35px; background: green; }
  #footLeft
  { position: absolute; bottom: 0px; left: 0px;
    width: 100px; height: 35px; background: green; }
  #footRight
  { position: absolute; bottom: 0px; right: 0px;
    width: 100px; height: 35px; background: green; }
  </style>

  <script type="text/javascript">
  function checkSize ( event )
  {
    var contentWidth = 960;
    var minOuterBoxSize = 60; // maximum 100-60=40 px will be displayed inside the main area

    if ( window.innerWidth - contentWidth < minOuterBoxSize * 2 )
    {
      var pos = ( ( minOuterBoxSize * 2 ) - window.innerWidth + contentWidth ) / 2;
      document.getElementById( 'footLeft' ).style.left = '-' + pos;
      document.getElementById( 'footRight' ).style.right = '-' + pos;
    }
    else
    {
      document.getElementById( 'footLeft' ).style.left = '0px';
      document.getElementById( 'footRight' ).style.right = '0px';
    }
  }
  </script>
</head>

<body onload="checkSize( event );" onresize="checkSize( event );">
 <div id="main">
  <div id="headLeft">TL</div>
  <div id="headRight">TR</div>

  <p>Normal content</p>
 </div>

 <div id="footLeft">BL</div>
 <div id="footRight">BR</div>
</body>
</html>
愛上了 2024-08-31 08:57:31

唯一真正棘手的部分是底部。其余部分非常简单:

<body>
    <div id="container">
        <div id="header">
            <div class="right"></div>
            <div class="left"></div>
        </div>

        <div id="footer">
            <div class="right"></div>
            <div class="left"></div>
        </div>
    </div>
</body>

CSS:

#container {
    margin: 0 auto;
    width: 960px;
}

#header {
    position: relative;
}

#header .right, #header .left {
    position: absolute;
    width: 100px;
}

#header .right {
    left: 940px; /* leaves 20px inside the #header */
    background: url(images/header_right.gif) no-repeat;
}

#header .left {
    left: -80px; /* leaves 20px inside the #header */
    background: url(images/header_left.gif) no-repeat;
}

以及 #footer 的一些类似内容,但您需要使用一点 JavaScript 来检测窗口宽度并调整 left稍微。

The only really tricky part is the bottom. Th rest of it is pretty straightforward:

<body>
    <div id="container">
        <div id="header">
            <div class="right"></div>
            <div class="left"></div>
        </div>

        <div id="footer">
            <div class="right"></div>
            <div class="left"></div>
        </div>
    </div>
</body>

CSS:

#container {
    margin: 0 auto;
    width: 960px;
}

#header {
    position: relative;
}

#header .right, #header .left {
    position: absolute;
    width: 100px;
}

#header .right {
    left: 940px; /* leaves 20px inside the #header */
    background: url(images/header_right.gif) no-repeat;
}

#header .left {
    left: -80px; /* leaves 20px inside the #header */
    background: url(images/header_left.gif) no-repeat;
}

And some similar stuff for the #footer, but you'll need to use a little bit of javascript to detect the window width and adjust the lefts slightly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文