【问题标题】:Having trouble with 0 height div, but no floats. What can I do?0 高度 div 有问题,但没有浮动。我能做些什么?
【发布时间】:2013-11-21 10:53:44
【问题描述】:

http://www.walkerspencer.com/chrhsweb/max

所以我目前正在做一个高中项目,我完全被难住了。我的 div 之一,文章,没有表现出我想要的样子。它的高度为 0。现在,经过大量谷歌搜索和研究,这似乎是一个常见问题……对于包含浮动元素的容器。我的文章不包含浮动 div。我什至尝试从我的#buttons div 的 css 中删除浮动,但它没有任何效果。我还尝试了大多数建议的浮动修复:clearfix 方法,clear:both。在这一点上,我只需要一个外部意见。我在我的代码中找不到任何重大错误(除了普遍的混乱和对语义元素的滥用)。预期的行为是,随着浏览器调整大小,部分会缩放,同时保持 16:9 的比例,并且具有填充整个文章的黑色透明背景。我也可以将背景应用于文章,但文章也没有高度。如果您只是想批评我的不良代码习惯和格式,那也没关系:)。图像的缺失应该不重要,尽管它们的比例为 16:9,在添加任何 jquery 之前我遇到了这个问题。太感谢了。抱歉,如果我的帖子或我的提问方式有问题,这是我第一次在这里提问。

HTML:

<head>
    <meta charset='utf-8'/>
    <title>design</title>
    <link rel="stylesheet" type="text/css" href="reset.css"/>
    <link rel="stylesheet" type="text/css" href="style.css"/>
    <link href='http://fonts.googleapis.com/css?family=Lato:100' rel='stylesheet' type='text/css'>
    <script type="text/javascript" src="js/jquery.js"></script>
    <script type="text/javascript" src="js/jquery.cycle.all.js"></script>


    <script type="text/javascript">
    $(document).ready(function() {

        $("#images").cycle({
        containerResize: false,
        slideResize: false,
        fit: 1
        });

        $('article').cycle({ 

        activePagerClass: 'activeSlide',
        containerResize: false,
        slideResize: false,
        fit: 1,
        timeout: 0, 
        speed:   300, 
        startingSlide:0,
        pager: "#buttons",
        pagerAnchorBuilder: function(idx, slide) { 
        return '#buttons li:eq(' + idx + ')';}
        });


        $('#right').click(function() {
            $('article').cycle("next"); 
            return false; 
        }); 
        $('#left').click(function() { 
            $('article').cycle("prev"); 
            return false; 
        }); 
    });
    </script>
</head> 

<body>


    <div id="container">


            <div id="arrows">
                <div id="left">&#60;</div><div id="right">&#62;</div>
            </div>
            <nav>

                <ul id="buttons">
                    <li><a href="#"></a>
                    <li><a href="#"></a>
                    <li><a href="#"></a>
                    <li><a href="#"></a>
                    <li><a href="#"></a>
                </ul>

            </nav>
            <div class="fix"></div>
            <article>
                <section id="images">
                <img src="images/alaska.jpg" width="100%" height="auto"/>
                <img src="images/field.jpg" width="100%" height="auto"/>
                <img src="images/sunset.jpg" width="100%" height="auto"/>
                </section >
                <section id="about">
                <p>
                    my name is <span style="color:crimson">max</span>.<br>
                    i'm a senior in high school.<br>
                    i love <span style="color:yellow">code</span><br>
                    <span style="color:#a45bc4">&amp;</span><br>
                    i love <span style="color:lightgreen">design</span>.<br>
                    <span style="color:#a45bc4">welcome to my site.</span>
                </p>
                </section >
                <section id="work">

                </section >

                <section id="contact">

                </section>
                <section id="place">

                </section>

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

CSS:

body{
    background-image:url("images/clouds.jpg");
    background-size:cover;
    background-color:white;
    background-repeat:no-repeat;
    height:100%

}

#container{
    width:70%;
    min-height:103px;
    margin:0 auto;
    min-width:182px;
}

#arrows{
    max-width: 140px;
    height: 70px;
    background: #f7f7f7;
    -moz-border-radius: 70px 70px 0 0;
    -webkit-border-radius: 70px 70px 0 0;
    border-radius: 70px 70px 0 0;
    margin:auto;
    position:relative;
    top:25px;
    z-index:100;
    border-top:1px solid;
    border-color:#cccccc;
    font-family:"Lato";
    text-align:center;
    vertical-align:middle;
    line-height:60px;
    font-size:68px;
    color:#cfcfcf;
    font-weight:100;
    font-stretch:ultra-condensed;

}

#right:hover, #left:hover{
    color:#a45bc4;
    text-decoration:none;
}
#right, #left{
    display:inline;
    color:#cfcfcf;
    text-decoration:none;
    cursor:pointer;
    -webkit-user-select: none;       
    -moz-user-select: none;
    -ms-user-select: none;

    -o-user-select: none;
    user-select: none;

}
nav{
    width:calc(100%-2px);
    border-left:1px solid #cfcfcf;
    border-right:1px solid #cfcfcf;
    height:35px;
    background-color:#f7f7f7;
    min-width:182px;


}
#buttons{
    position:relative;

    float:right;
    margin-right:6px;
    z-index:100;
}
.fix{
    clear:both;
}
.current{
    padding-right:0px;
}

#buttons li{

    display:inline-block;
    height:26px;
    width:26px;
    background-image:url("images/navc.jpg");
    background-repeat:no-repeat;
    background-position:center;
    background-size:24px;
    margin-left:8px;
    margin-top:4px;
    vertical-align:middle;
    cursor:pointer;
}
#buttons li:hover{
    opacity:.8;

}
#buttons a{
    color:black;
    text-decoration:none;
}
#buttons li.activeSlide{
    background-image:url("images/violetc.jpg");
    height:26px;
    width:26px;
    background-size:26px;
    vertical-align:middle;
}


article{
    width:100%;
    min-width:184px;
    height:100%;

}


section{
    height:100%;
    width:100%;
    margin:0 auto;
    text-align:center;
    vertical-align:middle;
    color:white;
    font-weight:100;
    font-size:36px;
    font-family:"Lato";
    background-color:rgba(0,0,0,.4);
}
p{

}
#about{

}

.option img{
    opacity:.6
}


.option:hover{
    opacity:.8;

}
#bottom{
    border-left:1px solid #cfcfcf;
    border-right:1px solid #cfcfcf;
    border-bottom:1px solid #cfcfcf;
    height:30px;
    background-color:#f7f7f7;
    min-width:182px;
    margin-top:56.25%;


}


@media all and (max-width:690px){
    #buttons{
    margin-top:2px;
    width:98%;
    text-align:center;
    }
    nav{position:relative;
    text-align:center;}
}

【问题讨论】:

  • 抱歉,我无法重现该问题。可以发jsfiddle来演示吗?
  • jsbin.com 或 fiddle 肯定会有所帮助。
  • 我很想,但我不确定我会放在那里...
  • 这里是该站点的副本:请注意,当您检查元素时,文章高度为 0px。 link
  • article 元素的高度为 0,因为它在文档流中不包含子元素。浮动并不是唯一将孩子从文档流中带走的东西,绝对定位孩子也可以做到这一点。如果您没有在子 section 元素上添加绝对位置的内联样式,那么它与您正在使用的幻灯片脚本有关。

标签: css html layout


【解决方案1】:

如果您保留幻灯片插件,并且它是导致绝对定位的孩子的原因(您没有这样做),那么您唯一的选择是强制 article 元素的高度,如果你想用它来做某事。

您可以为其应用一个类/ID(如果您正在使用与此样式无关的其他 article 元素,并且您每页只使用一次,则使用一个 ID,或者如果您想在同一页面上的其他地方重新使用它,使用一个类。)所以你会有类似的东西:

article.slideshow-container { height: 498px; }

然后您就可以随心所欲地使用它了。虽然这是一个固定高度,所以如果图像高度发生变化,您会再次失败。

【讨论】:

    【解决方案2】:

    问题肯定出在您对 jQuery Cycle 插件的实现上。正如 setek 所提到的,幻灯片使您的部分 div 位置绝对,这会将文章元素的高度折叠为 0。您可以将以下内容添加到循环脚本中以动态调整每个幻灯片/部分的高度:

        before : function(currSlideElement, nextSlideElement){
          $('article').css('height', $(nextSlideElement).height()+'px');
        },
    

    您还应该清理您的代码(这有点乱)并尝试逐步完成。我在这里为您创建了一个工作示例:

    http://jsfiddle.net/BCyD8/

    您将不得不努力获得正确的间距。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-20
      • 2020-08-25
      • 2011-03-19
      相关资源
      最近更新 更多