Z-Index分层问题(CSS)

时间:2014-12-26 23:48:22

标签: html css

我只是在学习/练习,所以任何帮助都会非常感激。此外,如果您在寻找时发现其他错误或效率低下,我很乐意听到它。

我有四层。

。体 。主要 。主要方 .sidebar

This本质上是我正在尝试重新创建的,除了第三层之外,一切都工作正常,或者在示例的情况下,右侧的垂直蓝色条纹延伸。我可以让它去第二层(主背景正上方)或掩盖一切(包括侧边栏),但我无法在第二层和第四层之间得到它(在示例中,浅蓝色内盒和较小的黄色边框。)提前致谢!

这是HTML和CSS。

<!doctype html>
<html>
<head>
    <link href='http://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,900' rel='stylesheet' type='text/css'>
    <link rel="stylesheet" type="text/css" href="./style.css"/>
    <title>CSS Zen Garden</title>
    <meta charset="utf-8" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<div class="main-side"></div>
<div class="main">
    <h1 class="redbox">CSS Zen Garden</h1>

    <h2>The Beauty of CSS Design</h2>
    <div class="sub-heading">
        <p class="top>">A demonstration of what can be accomplished through CSS-based design. Select any style sheet from the list to load it into this page.</p>

        <hr>

        <p class="bottom">Download the example <a href="#">HTML file</a> and <a href="#">CSS file</a>
    </div>
<div class="enlightenment">         
    <h3>The Road to Enlightenment</h3>

        <p>Littering a dark and dreary road lay the past relics of browser-specific tags, incompatible DOMs, broken CSS support, and abandoned browsers</p>

        <p>We must clear the mind of the past. Web enlightenment has been achieved thanks to the tireless efforts of folk like the W3C, WaSP, and the major browser creators.</p>

        <p>The CSS Zen Garden invites you to relax and meditate on the important lessons of the masters. Begin to see with clarity. Learn to use the time-honored techniques in new and invigorating fashion. Become one with the web.</p>
</div>

<div class="about">
    <h3>So What is This About?</h3>

        <p>There is a continuing need to show the power of CSS. The Zen Garden aims to excite, inspire, and encourage participation. To begin, view some of the existing designs in the list. Clicking on any one will load the style sheet into this very page. The HTML remains the same, the only thing that has changed is the external CSS file. Yes, really.</p>

        <p>CSS allows complete and total control over the style of a hypertext document. The only way this can be illustrated in a way that gets people excited is by demonstrating what it can truly be, once the reins are placed in the hands of those able to create beauty from structure. Designers and coders alike have contributed to the beauty of the web; we can always push it further.</p>
</div>

<div class="participation">
    <h3>Participation</h3>

        <p>Strong visual design has always been our focus. You are modifying this page, so strong CSS skills are necessary too, but the example files are commented well enough that even CSS novices can use them as starting points. Please see the CSS Resource Guide for advanced tutorials and tips on working with CSS.</p>

        <p>You may modify the style sheet in any way you wish, but not the HTML. This may seem daunting at first if you’ve never worked this way before, but follow the listed links to learn more, and use the sample files as a guide.</p>

        <p>Download the sample HTML and CSS to work on a copy locally. Once you have completed your masterpiece (and please, don’t submit half-finished work) upload your CSS file to a web server under your control. Send us a link to an archive of that file and all associated assets, and if we choose to use it we will download it and place it on our server.</p>
</div>

<div class="benefits">
    <h3>Benefits</h3>

        <p>Why participate? For recognition, inspiration, and a resource we can all refer to showing people how amazing CSS really can be. This site serves as equal parts inspiration for those working on the web today, learning tool for those who will be tomorrow, and gallery of future techniques we can all look forward to.</p>
</div>

<div class="sidebar">
    <h3>Select a Design:</h3>

        <ul>
            <li>
                <a href="/221/" class="design-name">Mid Century Modern</a> by                       <a href="http://andrewlohman.com/" class="designer-name">Andrew Lohman</a>
            </li>

            <li>
                <a href="/220/" class="design-name">Garments</a> by                     <a href="http://danielmall.com/" class="designer-name">Dan Mall</a>
            </li>                   

            <li>
                <a href="/219/" class="design-name">Steel</a> by                        <a href="http://steffen-knoeller.de" class="designer-name">Steffen Knoeller</a>
            </li>                   

            <li>
                <a href="/218/" class="design-name">Apothecary</a> by                       <a href="http://trentwalton.com" class="designer-name">Trent Walton</a>
            </li>                   

            <li>
                <a href="/217/" class="design-name">Screen Filler</a> by                        <a href="http://elliotjaystocks.com/" class="designer-name">Elliot Jay Stocks</a>
            </li>                   

            <li>
                <a href="/216/" class="design-name">Fountain Kiss</a> by                        <a href="http://jeremycarlson.com" class="designer-name">Jeremy Carlson</a>
            </li>                   

            <li>
                <a href="/215/" class="design-name">A Robot Named Jimmy</a> by                      <a href="http://meltmedia.com/" class="designer-name">meltmedia</a>
            </li>                   

            <li>
                <a href="/214/" class="design-name">Verde Moderna</a> by                        <a href="http://www.mezzoblue.com/" class="designer-name">Dave Shea</a>
            </li>                   
        </ul>

    <h3>Archives:</h3>
        <ul>
            <li>
                <a href="/217/page1">                   Next Designs <span class="indicator">&rsaquo;</span></a>
                </li>

            <li>
            <a href="http://www.mezzoblue.com/zengarden/alldesigns/" title="View every submission to the Zen Garden.">View All Designs</a>
            </li>
        </ul>

    <h3>Resources:</h3>
        <ul>
            <li>
                <a href="/217/217.css" title="View the source CSS file of the currently-viewed design.">View This Design&#8217;s CSS</a>
            </li>

            <li>
                <a href="http://www.mezzoblue.com/zengarden/resources/" title="Links to great sites with information on using CSS.">CSS Resources</a>
            </li>

            <li>
                <a href="http://www.mezzoblue.com/zengarden/faq/" title="A list of Frequently Asked Questions about the Zen Garden.">FAQ</a>
            </li>

            <li>
                <a href="http://www.mezzoblue.com/zengarden/submit/" title="Send in your own CSS file.">
                Submit a Design</a>
            </li>

            <li>
                <a href="http://www.mezzoblue.com/zengarden/translations/" title="View translated versions of this page.">Translations</a>
            </li>
        </ul>
</div>

<div class="requirements">
    <h3>Requirements</h3>

        <p>Where possible, we would like to see mostly CSS 1 & 2 usage. CSS 3 & 4 should be limited to widely-supported elements only, or strong fallbacks should be provided. The CSS Zen Garden is about functional, practical CSS and not the latest bleeding-edge tricks viewable by 2% of the browsing public. The only real requirement we have is that your CSS validates.</p>

        <p>Luckily, designing this way shows how well various browsers have implemented CSS by now. When sticking to the guidelines you should see fairly consistent results across most modern browsers. Due to the sheer number of user agents on the web these days — especially when you factor in mobile — pixel-perfect layouts may not be possible across every platform. That’s okay, but do test in as many as you can. Your design should work in at least IE9+ and the latest Chrome, Firefox, iOS and Android browsers (run by over 90% of the population).</p>

        <p>We ask that you submit original artwork. Please respect copyright laws. Please keep objectionable material to a minimum, and try to incorporate unique and interesting visual themes to your work. We’re well past the point of needing another garden-related design.</p>

        <p>This is a learning exercise as well as a demonstration. You retain full copyright on your graphics (with limited exceptions, see submission guidelines), but we ask you release your CSS under a Creative Commons license identical to the one on this site so that others may learn from your work.</p>

        <hr>
    <div class="author">
        <p>By <a href="http://www.mezzoblue.com/">Dave Shea</a>. Bandwidth graciously donated by <a href="http://www.mediatemple.net/">mediatemple</a>. Now available: <a href="http://rads.stackoverflow.com/amzn/click/0321303474">Zen Garden, the book</a>.</p>
    </author>
</div>

        <footer>
            <a href="http://validator.w3.org/check/referer" title="Check the validity of this site&#8217;s HTML" class="zen-validate-html">HTML</a>
            <a href="http://jigsaw.w3.org/css-validator/check/referer" title="Check the validity of this site&#8217;s CSS" class="zen-validate-css">CSS</a>
            <a href="http://creativecommons.org/licenses/by-nc-sa/3.0/" title="View the Creative Commons license of this site: Attribution-NonCommercial-ShareAlike." class="zen-license">CC</a>
            <a href="http://mezzoblue.com/zengarden/faq/#aaa" title="Read about the accessibility of this site" class="zen-accessibility">A11y</a>
            <a href="https://github.com/mezzoblue/csszengarden.com" title="Fork this site on Github" class="zen-github">GH</a>
        </footer>
</div>
</body>
</html>


body    {
    font-family: 'Source Sans Pro';
    line-height: 120%;
    font-size: 16px;
    font-weight: 400;
    color: #333;
    background: #daede2;
    z-index: 1;
    width: 90%;
    vertical-align: center;
}

p   {
    font-size: 20px;
    line-height: 140%;
    padding-left: 50px;
}

h1  {
    font-weight: 300;
    text-transform: uppercase;
    font-size: 30px;
    color: white;
    margin-bottom: -25px;
}

h2  {
    font-weight: 900;
    font-size: 130px;
    line-height: 100%;
    color: white;
    letter-spacing: -.025em;
    line-height: 1em;
    width: 65%;
    margin-left: 50px;
    position: relative;
}

.sub-heading    {
    text-transform: uppercase;
    width: 60%;
    margin: 0 0 50px 14px;
    position: relative;
    color: white;
    letter-spacing: .07em;
    font-size: 18px;
}

h3  {
    font-size: 30px;
    font-weight: 400;
    padding-left: 50px;
}

.main   {
    background: #77c4d3;
    position: relative;
    top: 50px;
    left: 50px;
    z-index: 2;
    height: 5000px;
}

.main-side  {
    background: #3a4453;
    position: absolute;
    left: 1020px;
    height: 2000px;
    top: -200px;
    width: 30%;
    z-index: 1;
}

.redbox {
    background: #df3c56;
    position: relative;
    top: 50px;
    left: 50px;
    padding: 25px;
    width: 224px;
    text-align: center;
}

.enlightenment  {
    background: #f2f9fb;
    padding: 50px 50px 50px 0;
    width: 58%;
    position: relative;
    top: 40px;
    margin-bottom: 150px;
}

.about  {
    color: white;
    width: 45%;
    margin-bottom: 100px;
    margin-top: 250px;
}

.about h3 {
    color: white;
}

.benefits   {
    background: #f2f9fb;
    position: relative;
    top: 225px;
    float: left;
    padding: 50px;
    width: 25%;
    left: 50px;
}

.sidebar    {
    background: #e9f198;
    position: absolute;
    top: 70px;
    right: 0px;
    width: 25%;
    font-size: 18px;
    line-height: 200%;
    letter-spacing: -.01em;
    padding-right: 40px;
    z-index: 4;
}

.sidebar a, h3  {
    color: #333;
}

.sidebar h3 {
    font-weight: 400;
    padding-left: 40px;
    margin-bottom: -20px;
}

.sidebar ul {
    list-style-type: none;
}

.participation  {
    background: #df3c56;
    float: right;
    width: 45%;
    color: white;
    position: relative;
    top: -618px;
    right: 80px;
    padding-top: 50px;
}

.participation p {
    padding: 0 50px 0 50px;
}

.participation h3 {
    color: white;
}

.requirements   {
    background: #f2f9fb;
    position: relative;
    float: right;
    top: -625px;
    width: 57%;
    padding: 50px;
}

.requirements a {
    color: #333;
    text-transform: uppercase;
}

.author {
    text-transform: uppercase;
}

footer  {
    background: #e9f198;
    position: relative;
    top: 119px;
    padding: 25px;
    font-size: 22px;
    word-spacing: 10px;
    color: #333;
    left: -440px;
    width: 80%;
}

a   {
    text-decoration: none;
    color: white;
    border-bottom: solid 1px #999;
    padding-bottom: 2px;
}

hr  {
    border: 0;
    height: 0;
    border-top: 1px solid #df3c56;
    width: 93%;
    margin: 50px 0 50px 50px;
}

2 个答案:

答案 0 :(得分:1)

您需要做的就是将.main-side移至.main,这样您就可以向其添加正确的z-index,使其重叠,就像您发布的示例一样,并调整值:

.main-side  {
  background: rgba(58,68,83, .8); //convert to rgba so you can fade the background like the example
  position: absolute;
  right: -150px; //update - change to right instead
  height: 2400px; //update
  top: -200px;
  width: 30%;
  z-index: 1;
}

我还将一些颜色转换为rgba以使其具有透明外观。

FIDDLE

答案 1 :(得分:0)

我不太确定你在问什么,但这里有一些可能会有所帮助的事情。首先,要使用黄色菜单实现它们的目的,您应该使用以下内容:

.sidebar    {
    background: #e9f198;
    position: absolute;
    top: 70px;
    right: -100px;
    width: 25%;
    font-size: 18px;
    line-height: 200%;
    letter-spacing: -.01em;
    padding-right: 40px;
    z-index: 4;
}

您会看到侧边栏现在更像他们的。

通过这两项变化,它是否更接近您所寻找的目标?

.main   {
    background: #77c4d3;
    position: relative;
    top: 50px;
    left: 50px;
    z-index: 2;
    height: 5000px;
}

.main-side  {
    background: #3a4453;
    position: absolute;
    left: 900px;
    height: 2000px;
    top: 70px;
    width: 30%;
    z-index: 1;
}

我也做了一个JSFIDDLE,你将来应该这样做这样的问题!