简体   繁体   English

空格:nowrap使元素位于窗口之外

[英]White-space: nowrap makes elements to be outside of window

I am trying to have an UL on the right of a SPAN ( http://jsfiddle.net/Shg9L/24/ ). 我正在尝试在SPAN( http://jsfiddle.net/Shg9L/24/ )的右侧设置UL。

<div>
  <span>Categories</span>
  <ul>
    <li><a href="#">Book</a></li>
    <li><a href="#">Computer</a></li>
  </ul>
</div>

When the window is resized down the LIs should get stacked but not under the SPAN. 调整窗口大小时,LI应该堆叠,但不能在SPAN下。

The problem is when I resize the window some of the LI items on the right becomes hidden. 问题是当我调整窗口大小时,右侧的某些LI项将被隐藏。

I think it is because of "white-space: nowrap" but without it I'm not able to make it work. 我认为这是因为“空白:nowrap”,但是如果没有它,我将无法使其工作。

Can this be solved? 这可以解决吗?

Display your unordered list as a block and hide the overflow. 将您的无序列表显示为块,并隐藏溢出。 It will then take up all the available width. 然后它将占用所有可用宽度。 The list items will stack neatly in line with the edge of the unordered list when there isn't enough room: 如果没有足够的空间,则列表项将与无序列表的边缘整齐地堆叠在一起:

div ul {
    list-style: none;   
    padding: 0;
    margin: 0;
    display: block;
    overflow:hidden;
}

The white-space:nowrap on your container <div> isn't needed as far as I can see. 据我所知,不需要容器<div>white-space:nowrap You can remove it. 您可以删除它。

JSFiddle JSFiddle

http://jsfiddle.net/Shg9L/29/ http://jsfiddle.net/Shg9L/29/

removed a coupple of things and added position:absolute; 删除了一件事情,增加了位置:绝对; and inline:display-block to some elements 和inline:display-block到某些元素

div {background-color:orange;}

div span {
    background-color: #E0E0E0;      
    margin-right: 8px;
    margin-bottom: 8px;
    padding: 8px;  
}

div ul {
    position:absolute;
    top:0px;left:100px;
    list-style: none;   
    padding: 0;
    margin: 0;
    display: inline-block;
}
div ul li {
    background-color: #F0F0F0;      
    margin-right: 8px;
    margin-bottom: 8px;
    padding: 8px;  
    display:inline-block;
}

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

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