简体   繁体   English

Chrome导航列表在CSS / HTML中带有媒体查询的定位错误

[英]Chrome navigation list positioning bug with media queries in CSS/HTML

This might be a Chrome/webkit rendering bug, but I want to check if I'm doing something wrong first. 这可能是Chrome / webkit渲染错误,但我想先检查是否做错了什么。

I have a simple UL/LI navigation, each LI is inline-block to appear horizontally. 我有一个简单的UL / LI导航,每个LI都是内联块,可以水平出现。 The UL is text-align: right. UL是text-align:正确。 The 2nd to last element is positioned absolute to the top right corner of the page. 倒数第二个元素绝对位于页面的右上角。 The last element is positioned relative to push it down a little bit. 最后一个元素的位置相对于将其向下推一点。

For mobile devices (max-width 400px) there is a media query to reset elements into a standard top-down stacked list, 100px wide. 对于移动设备(最大宽度为400px),有一个媒体查询可将元素重置为100px宽的标准自上而下的堆叠列表。

If you load the page in the wide screen width, everything is fine. 如果以宽屏幕宽度加载页面,则一切正常。 If you load the page in the mobile screen width, everything is fine. 如果以移动屏幕宽度加载页面,则一切正常。 But if you start in the mobile screen width, and extend your width, Chrome will render the list elements in a broken way : element Five will appear on the next line , even though there is plenty of room to fit next to the other elements. 但是,如果您从移动屏幕的宽度开始,然后再扩展宽度,Chrome会以一种折断的方式呈现列表元素:元素5将出现在下一行 ,即使其他元素旁边有足够的空间容纳这些元素。

HTML: HTML:

<ul>
    <li>li One</li>
    <li>li Two</li>
    <li>li Three</li>
    <li class="abs">li Four</li>
    <li class="last">li Five</li><!-- This element should always appear to the right of "Three" in the wide screen width -->
</ul>

CSS: CSS:

ul {
    background: lightblue;
    padding: 20px 0;
    text-align: right;
}

li {
    display: inline-block;
    padding: 5px;
    background: blue;
    color: white;
    min-width: 50px;
    margin-right: 10px;
}

li.abs {
    position: absolute;
    top: 0px;
    right: 0px;    
    z-index: 9;
}

.last {
    position: relative;
    top: 10px;
    margin: 8px;
}

@media (max-width: 400px) {
    ul li {
        display: block;
        width: 100px;
    }

    li.abs {
        position: static;   
    }

    .last {
        position: static;
        margin: 0;
    }
}

Expected Output: 预期产量:

在此处输入图片说明

Actual Output (after resizing window): 实际输出(调整窗口大小后):

在此处输入图片说明

Firefox has no problems at all. Firefox完全没有问题。 This is Chrome 31. Simplified JSFiddle Test Case: http://jsfiddle.net/ZE3Kk/4/ 这是Chrome31。简化的JSFiddle测试案例: http//jsfiddle.net/ZE3Kk/4/

try to use float: right for your li's: 尝试使用float:适合您的li:

li {
display: block;
padding: 5px;
background: blue;
float: right;
color: white;
min-width: 50px;
margin-right: 10px;
}

and just switch all the elements so they will have the order that you need, and it will work after resizing 并切换所有元素,以便它们具有您需要的顺序,并且在调整大小后将可以使用

here is the example with float, now just re-arrange the menu items http://jsfiddle.net/ZE3Kk/6/ 这是float的示例,现在只需重新排列菜单项http://jsfiddle.net/ZE3Kk/6/

I found a workaround here: http://jsfiddle.net/ZE3Kk/8/ 我在这里找到了解决方法: http : //jsfiddle.net/ZE3Kk/8/

li.abs div {
    position: absolute;
    top: 0px;
    right: 0px;    
    width: 60px;
    height: 25px;
    background: red;
    z-index: 9;
}

Solution was to insert a DIV inside the LI, keep the LI position standard, but absolutely position the DIV. 解决方法是在LI内插入DIV,保持LI的位置标准,但绝对将DIV定位。 By doing this, Chrome does not break the elements when changing media queries. 这样一来,Chrome在更改媒体查询时不会破坏元素。

Update : Found a related Chromium bug that demonstrates this problem in a different way -> https://code.google.com/p/chromium/issues/detail?id=147449 更新 :发现了一个相关的Chromium错误,该错误以不同的方式演示了此问题-> https://code.google.com/p/chromium/issues/detail?id=147449

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM