【问题标题】:CSS - child not inheriting the width of a parent in liquid layoutCSS - 孩子不继承液体布局中父母的宽度
【发布时间】:2012-02-13 03:04:20
【问题描述】:

全部,

这似乎是与 Firefox 相关的问题(没有尝试过 Opera 和 IE)。

我有 'ul' 孩子,它们在液体布局中被赋予 100% 的宽度。父 'li' 本身的宽度是其父 'ul' 的 33%。

Chrome 似乎正确显示了子 'ul' 的宽度,即它的 100% 等于它的父 'li' 宽度的 33%。但是,Firefox 会将 'ul' 子项的宽度拉伸到整个视口宽度。

Jsfiddle:http://jsfiddle.net/g4VAd/2/

            <div id="filemenu">                     <!-- right tabs menu -->
        <ul id="fm_ul">
            <li class="filetabs">File
                <ul>
                    <li class="m_items">Open</li>
                    <li class="m_items">Save</li>
                    <li class="m_items">Exit</li>
                </ul></li><li class="filetabs">Edit
                <ul>
                    <li class="m_items">Undo</li>
                    <li class="m_items">Redo</li>
                </ul></li><li class="filetabs">Settings
                <ul>
                    <li class="m_items" id="frontView"><span>Front View</span></li>
                    <li class="m_items"id="backView"><span>Back View</span></li>
                </ul></li>
        </ul>
    </div>                    <!-- close -> 'filemenu' div -->

CSS:

    #filemenu {
    float: right;
    width: 37%;
    height: 100%;
}
#fm_ul {
    float: right;
    display: table;
    width: 100%;
    height: 100%;
    cursor: pointer;
    -moz-border-radius: 6px;
    -webkit-border-radius: 6px;
    border-radius: 6px;                 /* Opera & IE9 donot have prefixes, directly support border-radius */
    -moz-box-shadow: 0 1px 2px rgba(0,0,0,0.1);
    -webkit-box-shadow: 0 1px 2px rgba(0,0,0,0.1);
    box-shadow: 0 1px 2px rgba(0,0,0,0.1);                 /* Opera & IE9 donot have prefixes, directly support border-radius */
    background: rgb(232,232,232); /* Old browsers */
    background: -moz-linear-gradient(top, rgb(232,232,232) 0%, rgb(252,252,252) 100%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgb(232,232,232)), color-stop(100%,rgb(252,252,252))); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(top, rgb(232,232,232) 0%,rgb(252,252,252) 100%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(top, rgb(232,232,232) 0%,rgb(252,252,252) 100%); /* Opera 11.10+ */
    background: -ms-linear-gradient(top, rgb(232,232,232) 0%,rgb(252,252,252) 100%); /* IE10+ */
    background: linear-gradient(top, rgb(232,232,232) 0%,rgb(252,252,252) 100%); /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#e8e8e8', endColorstr='#fcfcfc',GradientType=0 ); /* IE6-8 */
}
    .filetabs {
        display: table-cell;
        position: relative;
        width: 33%;
        height: 100%;
        color: rgb(125,125,125);
        font-size: 0.8em;
line-height: 25px;
        text-align: center;
        text-decoration: none;
        -moz-text-shadow: 0 1px 2px rgba(150,150,150,0.4);
        -webkit-text-shadow: 0 1px 2px rgba(150,150,150,0.4);
        text-shadow: 0 1px 2px rgba(150,150,150,0.4);    
        vertical-align: middle;
        list-style-type: none;
    }
        .filetabs:first-child {
            -webkit-border-top-left-radius: 6px;
            -webkit-border-bottom-left-radius: 6px;
            -moz-border-radius-topleft: 6px;
            -moz-border-radius-bottomleft: 6px;
            border-top-left-radius: 6px;
            border-bottom-left-radius: 6px;        
        }
        .filetabs:last-child {
            -webkit-border-top-right-radius: 6px;
            -webkit-border-bottom-right-radius: 6px;
            -moz-border-radius-topright: 6px;
            -moz-border-radius-bottomright: 6px;
            border-top-right-radius: 6px;
            border-bottom-right-radius: 6px;        
        }
        .filetabs:hover {
            color: rgb(150,150,150);
            -moz-text-shadow: 0 1px 0 rgba(255,255,255,0.95);
            -webkit-text-shadow: 0 1px 0 rgba(255,255,255,0.95);
            text-shadow: 0 1px 0 rgba(255,255,255,0.95);
        }
        .filetabs ul {
            display: none;
            float: left;
            position: absolute;
            width: 100%;
            opacity: 0;
            -webkit-transition: 0.35s linear opacity;
            -moz-transition: 0.35s linear opacity;
            -o-transition: 0.35s linear opacity;
            transition: 0.35s linear opacity;
            z-index: 100;
        }
            .m_items {
                width: 100%;
                list-style: none;
                padding-top: 1px;
                background-color: rgba(1,1,1,0.8);
                -moz-box-shadow: 2px 0 2px rgba(1,1,1,0.2);
                -webkit-box-shadow: 2px 0 2px rgba(1,1,1,0.2);
                box-shadow: 2px 0 2px rgba(1,1,1,0.2);
            }
                .m_items span {
                    font-size: 0.8em;
                    cursor: default;
                }
                .m_items:first-child {                    /* for borders */
                    -moz-border-radius: 4px 4px 0 0;
                    -webkit-border-radius: 4px 4px 0 0;
                    border-radius: 4px 4px 0 0;
                }
                .m_items:last-child {                    /* for borders */
                    padding-bottom: 5px;
                    -moz-border-radius: 0 0 4px 4px;
                    -webkit-border-radius: 0 0 4px 4px;
                    border-radius: 0 0 4px 4px;
                }

【问题讨论】:

  • 它在 Safari 中也不能正常工作。我现在正在寻找问题。

标签: html css width liquid-layout


【解决方案1】:

问题似乎出在display: table-cell。我尝试将其替换为 display: inline-block;float: left; 并且子菜单开始与两者一起运行。

我还建议您查看这篇文章以使用 inline-block 进行垂直居中:http://css-tricks.com/centering-in-the-unknown

【讨论】:

  • 谢谢@joshnh。我需要“表格单元格”,以便“李”的文本位于我的液体布局的中间(垂直和水平)。
  • 那为什么不使用 display: inline-block 呢?它允许垂直和水平居中。我什至写了一篇关于你为什么应该这样做的文章:joshnh.com/2012/02/…
  • 我无法将元素与内联块垂直对齐。阅读您的文章,但垂直对齐在流式布局中不起作用。
  • 是的,我没有意识到你正在使用流体高度,我很抱歉。不过,您仍然应该能够使用 inline-block,请参阅此处以获取解决方案:css-tricks.com/centering-in-the-unknown
  • 成功了!文章底部的读者提示,他们创建了幽灵元素(或伪元素),解决了这个问题。谢谢你的文章参考。约什请您将其作为单独的评论发布或添加对顶部回复的引用,以便我可以选择正确的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-17
  • 2017-07-02
  • 1970-01-01
相关资源
最近更新 更多