2

我有一个稍微不寻常的要求,我真的很挣扎。我创建了一个图像来说明我正在尝试做的事情:

在此处输入图像描述

基本上这是一个由 3 个区域组成的 DIV - 顶部、中间和底部。所有 3 个区域都有背景图像,我需要中间区域(包括其边框的蓝色位)来扩展内容。所以内容越多,中间区域就越大。问题是内容实际上需要覆盖整个 DIV 而不仅仅是中间区域。

这就是我目前所拥有的:

HTML:

<div id="panel">
    <div id="top"></div>

    <div id="middle">
        <div id="content">
            Lorem ipsum, etc...
        </div>
    </div>

    <div id="bottom"></div>
</div>

CSS:

#panel {
    float: left;
    width: 300px;
    position: relative;
}

#top {
    position: absolute;
    left: 0;
    top: 0;
    height: 100px;
    width: 100%;
    background: url(top.png) no-repeat;
}

#middle {
    float: left;
    margin-top: 100px;
    width: 100%;
    min-height: 200px;
    background: url(middle.png) repeat-y;
}

#bottom {
    position: absolute;
    left: 0;
    bottom: 0;
    height: 100px;
    width: 100%;
    background: url(bottom.png) no-repeat;
}

这实际上不起作用,并且可能是不正确的方法。有人有什么想法吗?

4

10 回答 10

3

内容永远不会覆盖所有内容,因为它位于中间面板内,并且中间面板的位置永远不会占据整个#panel- 将 移动#content#panel元素中......

<div id="panel">
    <div id="top"></div>
    <div id="middle"></div>
    <div id="bottom"></div>
    <div id="content">
        Lorem ipsum, etc...
    </div>
</div>

#middle使用position: absolute;like#top和定位#bottom

#panel {
    height: 400px; /* or 100% or whatever */
    width: 300px;
    position: relative;
}

#top,
#middle,
#bottom {
    left: 0;
    position: absolute;
    width: 100%;
}

#top {
    height: 100px;
    background: url(top.png) no-repeat;
    top: 0;
}

#middle {
    bottom: 100px;
    top: 100px;
    background: url(middle.png) repeat-y;
}

#bottom {
    bottom: 0;
    height: 100px;
    background: url(bottom.png) no-repeat;
}

#content {
/* any padding or anything */
    position: relative;
}

http://jsfiddle.net/trolleymusic/yqure/

于 2013-05-23T23:39:35.803 回答
3

我会这样做:

http://jsfiddle.net/5dvgk/5/

html:

    <div id="middle">
        <div id="content">
            Lorem ipsum, etc...<br/>
            Lorem ipsum, etc...<br/>
            Lorem ipsum, etc...<br/>
            Lorem ipsum, etc...<br/>
            Lorem ipsum, etc...<br/>
            Lorem ipsum, etc...<br/>
            Lorem ipsum, etc...<br/>
            Lorem ipsum, etc...<br/>
            Lorem ipsum, etc...<br/>
            Lorem ipsum, etc...<br/>
        </div>
    </div>

    <div id="bottom"></div>
</div>

CSS:

#panel {
    width: 330px;
    position: relative;
    background: url(http://oi44.tinypic.com/acoeo.jpg);
}

#top {
    position: absolute;
    left: 0;
    top: 0;
    z-index: 0;
    height: 170px;
    width: 100%;
    background: url(http://oi43.tinypic.com/2zedqqc.jpg)
}

#middle {   
    position: relative;
    z-index: 1;
    min-height: 300px;
    padding-left: 50px;
    padding-top: 70px;
    padding-bottom: 70px;
}

#bottom {
    position: absolute;
    left: 0;
    bottom: 0;
    z-index: 0;
    height: 170px;
    width: 100%;
    background: url(http://oi43.tinypic.com/34i0jeu.jpg);
}
于 2013-05-24T00:04:21.900 回答
1

In most newer browsers which support parts of the CSS3 specification you can do this quite easily using CSS3 multiple backgrounds, if you need to support older browsers, then your current approach isn't too bad, you would need to absolutely position your top and bottom divs as you have already done, and then ensure the top and bottom div's render behind the div in the middle by setting the position property (z-index doesn't work if position is not set) and then specifically setting their z-index.

Here's a jsFiddle demonstrating both approaches side by side. The one on the left is using CSS3 multiple backgrounds, the other one is a fixed up version of the approach you had going already.

HTML

<div id="css3-multiple-backgrounds">Content goes here..</div>

<div id="any-browser-not-using-css3">
    <div class="top"></div>
    <div>Content goes here..</div>
    <div class="bottom"></div>
</div>

CSS

#css3-multiple-backgrounds {
    background: url(https://dl.dropboxusercontent.com/u/6928212/dots.png) left top no-repeat,        
                url(https://dl.dropboxusercontent.com/u/6928212/dots.png) left bottom no-repeat,
                url(https://dl.dropboxusercontent.com/u/6928212/bg.png) center center repeat-y;
}
#any-browser-not-using-css3 {
    background: url(https://dl.dropboxusercontent.com/u/6928212/bg.png) center center repeat-y;
    position: relative;
}
#any-browser-not-using-css3 > div {
    position: relative;
    z-index: 2;
}
#any-browser-not-using-css3 > div[class] {
    position:absolute;
    background: url(https://dl.dropboxusercontent.com/u/6928212/dots.png) left top no-repeat;
    width: 100%;
    height: 50px;
    left: 0px;
    z-index: 1;
}
#any-browser-not-using-css3 > div.top {
    top: 0px;
}
#any-browser-not-using-css3 > div.bottom {
    bottom: 0px;
}
于 2013-05-24T00:32:56.950 回答
1

您将需要添加一个与我称之为背景区域的内容区域分开的内容区域

这是一个小提琴http://jsfiddle.net/5dvgk/2/

做这样的事情:

<div class="panel">
    <div class="background">
        <div class="top">
        </div>
        <div class="middle">
        </div>
        <div class="bottom">
        </div>
    </div>
    <div class="content">
        Your content goes here
    </div>
</div>

你的CSS会是这样的

.panel {
    float: left;
    width: 300px;
    position: relative;
}
.background {
    width:100%;
    height:100%;
    z-index: 1;
}
.top {
    position: absolute;
    left: 0;
    top: 0;
    height: 100px;
    width: 100%;
    background: url(top.png) no-repeat;
    z-index: 2;
}
.middle {
    position:absolute;
    top:0;
    left: 0;
    height:100%;
    width:100%;
    z-index: 1;
}
.bottom {
    position: absolute;
    left: 0;
    bottom: 0;
    height: 100px;
    width: 100%;
    background: url(bottom.png) no-repeat;
    z-index: 2;
}
.content {
    float: left;
    width: 300px;
    position: relative;
    z-index: 4;
}
于 2013-05-23T23:35:25.723 回答
1

正如您从其他答案中看到的那样,实际上有多种方法可以做到这一点。

我决定使用纯 CSS 和一些带有 position:relative 的元素“滑行”。此外,除非中间内容框的背景图像在内容区域中是透明的,否则此方法将不起作用。

我还删除了一些不必要的浮动和绝对位置,因为这些框可以相互堆叠:

看看这个小提琴,我在 IE9/FF/Chrome 中测试过它,但我确信它至少可以工作到 IE6,因为我以前使用过这种方法。 http://jsfiddle.net/shayl/KwWjt/1/

在此处输入图像描述

我必须对您的中间内容框以及您希望它的外观做出假设,但我使用了背景图像。


CSS

            #panel {
                width: 234px;
            }

            #top {
                height:49px;
                background: url(http://s7.postimg.org/f1usobsnf/top.gif) no-repeat;
                z-index:1;
            }

            #middle {
                position:relative;
                top:-20px;
                width: 134px;
                min-height: 200px;
                padding:0 50px 0 50px;
                background: url(http://s23.postimg.org/gups2v0wb/mid.gif) repeat-y;
                z-index:2;
            }

            #bottom {
                position:relative;
                top:-30px;
                height:49px;
                width: 100%;
                background: url(http://s12.postimg.org/s78huz5u5/bottom.gif) no-repeat;
                z-index:1;
            }


HTML

            <div id="panel">
                <div id="top"></div>

                <div id="middle">
                    <div id="content">
                        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla quis condimentum lacus. Duis tempor bibendum ante, non luctus leo mollis vitae. Nunc in augue massa, ut facilisis velit. Etiam in magna lacus, in lacinia lectus. Nulla facilisi. Aenean rutrum, magna sed molestie condimentum, magna arcu ornare nisl, id luctus turpis felis ornare lorem. Aenean placerat erat in nisi convallis feugiat. Ut sodales tincidunt tellus, nec eleifend ligula posuere nec. Proin sit amet quam quam, mollis laoreet diam. Sed volutpat libero et velit commodo laoreet. Vestibulum eros dui, hendrerit molestie sodales eget, ullamcorper sed enim. Integer tempus, eros at dapibus ultricies, mauris risus sagittis metus, quis consequat massa mi quis felis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Etiam sagittis blandit odio, sed vulputate quam egestas id. 
                    </div>
                </div>

                <div id="bottom"></div>
            </div>
于 2013-05-23T23:52:43.280 回答
1

上面的很多选项都需要相当复杂的 HTML / CSS。这是一个只需要两个DIV标签和一些 CSS 的示例。

HTML:

<div class="border">
  <div class="content">
    Lorem ipsum, etc...<br/>
    Lorem ipsum, etc...<br/>
    Lorem ipsum, etc...<br/>
    Lorem ipsum, etc...<br/>
    Lorem ipsum, etc...<br/>
    Lorem ipsum, etc...<br/>
    Lorem ipsum, etc...<br/>
    Lorem ipsum, etc...<br/>
    Lorem ipsum, etc...<br/>
    Lorem ipsum, etc...<br/>
  </div>
</div>

CSS:

.border { 
    position: relative; 
    width: 234px;
    background: #fff url(http://i41.tinypic.com/axzj0o.jpg) top center repeat-y;
}
.border:before, .border:after {
    position: absolute; 
    content: '';
    display: inline-block;
    height: 53px; width: 234px;
}
.border:before {
    top: 0; left: 0;
    background: transparent url(http://i41.tinypic.com/2jgwn6.jpg) top center no-repeat;
}
.border:after {
    bottom: 0; left: 0;
    background: transparent url(http://i43.tinypic.com/vfiiag.jpg) bottom center no-repeat;
}
.border > .content { 
    background: transparent;
    position: relative;
    padding: 13px;
    z-index: 1;
}

该解决方案仅通过 3 个重叠图形来工作,如下所示:

  1. 背景

    背景

  2. 最佳

    最佳

  3. 底部

    底部

背景图像应用于外部DIV, .border, 并且允许repeat-y(沿y轴垂直重复)。Top 图像应用于:before伪元素,Bottom 图像应用于:after伪图像。

接下来,.content DIV:before元素:after都被position编辑,以便它们可以z-index应用。因为:before:afterposition: absolute,它们不再占用流中的空间 - 因此它们不会影响.content彼此的定位。

这意味着.contentinner-DIV将在其父级中自行流动,填充到.border.

但是,它仍然需要被赋予 az-index: 1才能出现在:beforeand:after元素的前面——这样就完成了效果。

如您所见,我冒昧地对您的特定图像进行了Photoshop处理,以便您可以看到它的工作原理。这是结果的屏幕截图:

结果

这是一个jsFiddle,它演示了它的工作原理

于 2013-05-24T03:14:14.660 回答
0

看到这个:jsFiddle

您可以根据需要调整margin和设置display: inline-block; width: --px;

编辑

再次阅读问题后,我想出了这个:http: //jsfiddle.net/5dvgk/7/

于 2013-05-24T00:49:59.757 回答
0

如果您愿意尝试 css3,您可以将多个背景应用于同一个标签。尽管您必须小心,因为使用此方法拉伸具有明显特征(如上面的星星)的图像可能会失真。

我玩了一个例子。虽然它似乎没有给我确切的我正在寻找的东西,但我接近你所说的。这对于基于梯度且没有明显特征的图像非常有用。但是,任何拉伸过远的图像总是会导致像素化。我见过的大多数 css 都创建了一个 1px 宽的“这么多”长度的渐变。像这样的拉伸元素更难看到像素化。

<html>
<head>
 <style>
#container {
background: url(red.jpg) no-repeat, 
            url(checkered.jpg) no-repeat, 
            url(purple.jpg) no-repeat;

background-position: 0 0 , 0 20%, 0 100%;
background-size: 100% 20%, 100% 80%, 100% 20%;
}
 </style>
</head>
<body>
    <div id="container">
        <div>Lorem Ipsum</div>
        <div>Lorem Ipsum</div>
        <div>Lorem Ipsum</div>
        <div>Lorem Ipsum</div>
        <div>Lorem Ipsum</div>
        <div>Lorem Ipsum</div>
        <div>Lorem Ipsum</div>
        <div>Lorem Ipsum</div>
        <div>Lorem Ipsum</div>
        <div>Lorem Ipsum</div>
        <div>Lorem Ipsum</div>
        <div>Lorem Ipsum</div>
        <div>Lorem Ipsum</div>
        <div>Lorem Ipsum</div>

    </div>
</body>
</html>
于 2013-05-24T00:22:57.410 回答
0

我不知道这是否可以用纯 CSS 精确完成,但我会这样做:

设置您的 3 个区域,并添加一个 push :

<div id="container">
  <div id="top"></div>
  <div id="content"></div>
  <div id="push"></div>
  <div id="bottom"></div>
</div>

从流中删除内容:

#content { position: absolute; }

并计算推高:

h = $("#content").height() * 0.5;
$("#push").css( { "height": h } );

看到这个小提琴:http: //jsfiddle.net/RM9NS/1/

于 2013-05-23T23:41:36.567 回答
-2

您为什么不尝试将三个图像附加到一个图像文件中。这样你只需要一个 div 而不需要复杂的样式。

于 2013-05-23T23:33:45.297 回答