【问题标题】:Prevent floating UL and LI elements underneath a header防止在标题下浮动 UL 和 LI 元素
【发布时间】:2020-07-08 15:19:54
【问题描述】:

下面是标题内的菜单。 ul 和 li 元素是浮动的,现在浮动在标题下方,我试图通过 clear:both 来防止这种情况。但是,这似乎不起作用,所以我想知道......有什么问题?

html:

<header>
    <ul>
        <li><a href='#'>Item 1</a></li>
        <li><a href='#'>Item 2</a></li>
        <li><a href='#'>Item 3</a></li>
        <li><a href='#'>Item 4</a></li>
    </ul>
    <div class='clear'/>
</header>

css:

header {
    background: #888;
    height: 20px;
    padding: 10px;
}

ul{
  margin: 18px 0;
  padding: 0;
  list-style: none;
}

ul li{
  margin: 0 10px 0 0;
  padding: 0;
  display: block;
  float: left;
  width: 80px;
  height: 20px;
  border: 1px solid #000;
  background: red;
}

ul li a{
  display:block;
  width: 100%;
  height: 100%;
  text-decoration: none;
  float: left;
  text-align: center;
  padding-top: 2px;
}

ul li a:hover{
  color: #fff;
  background-color: #000;
}​

.clear {
    clear:both;
}

【问题讨论】:

  • 你想在这里完成什么?那个 div 是空的,那么把它放在 header 里有什么意义呢?
  • 问题是lis 也是浮动的。
  • 不是一个答案,但我总是觉得在容器上使用 overflow: hidden hack 感觉更好。
  • 您可能想在这里放弃float 的概念。有一种很好的方法可以让display: inline-block 工作(即使在旧的 IE 浏览器中)并通过更好、更简单的代码实现您想要的。 @PeeHaa 是对的——如果你要漂浮,overflow: hidden 是一个更好的方法来强制容器包裹漂浮物。
  • @PeeHaa - 这很老套。完全同意。这就是为什么我们都喜欢旧的 IE 浏览器......

标签: html css css-float


【解决方案1】:

您可以使用 clearfix:

.clearfix:after {
  clear:both;
  content:".";
  display:block;
  height:0;
  line-height:0;
  visibility:hidden;
}

并将其应用于 ul 和标题。

【讨论】:

    【解决方案2】:

    更好的 css 适合您的情况(在 FF、Chrome、IE6+ 中测试)

    header {
        display: block; /* Necessary for older browsers to render this html5 element properly */
        background: #888;  
        height: 20px;  
        padding: 10px;  
    }
    
    ul {  
        margin: 18px 0;  
        padding: 0;  
        list-style: none;  
    }
    
    ul li {
        margin: 0 10px 0 0; /* at least some margin-right is necessary to make inline-block work in IE */
        padding: 0 5px; /* you don't have to set a width any longer, so let padding define spacing */
        display: inline-block;
        zoom: 1; /* part 1 of the IE hack to make inline-block work */
        *display: inline; /* part 2 of the IE hack to make inline-block work */
       /* height and line-height not necessary - driven by a element below */
        border: 1px solid #000;
        background: red;
    }
    
    ul li a {
        display:block;
        height: 20px;
        line-height: 20px; /* this will vertically center the text in the li */
        text-decoration: none;
        text-align: center;
    }
    
    ul li a:hover{
        color: #fff;
        background-color: #000;
    }​
    

    【讨论】:

      【解决方案3】:

      与其使用 cleardiv,不如尝试使用 html5boilerplate 中的标准 clearfix?

      这是修改后的标记:

        <header class="clearfix">
            <ul>
                <li><a href='#'>Item 1</a></li>
                <li><a href='#'>Item 2</a></li>
                <li><a href='#'>Item 3</a></li>
                <li><a href='#'>Item 4</a></li>
            </ul>
        </header>
      

      这是要添加的css:

      .clearfix:before,
      .clearfix:after {
          content: " "; /* 1 */
          display: table; /* 2 */
      }
      
      .clearfix:after {
          clear: both;
      }
      
      
      .clearfix {
          *zoom: 1;
      }
      

      来自文档:

      .clearfix

      将 .clearfix 添加到元素将确保它始终完全包含其浮动子元素。多年来,clearfix hack 出现了许多变体,还有其他 hack 也可以帮助您包含浮动的孩子,但 HTML5 Boilerplate 目前使用的是 micro clearfix。

      【讨论】:

        【解决方案4】:

        您不能将/&gt; 用于 div。将其从&lt;div class='clear'/&gt; 更改为&lt;div class="clear"&gt;&lt;/div&gt;

        或者尝试在当前的下面添加另一个 li 并赋予它 clear 类。

        <header>
            <ul>
                <li><a href='#'>Item 1</a></li>
                <li><a href='#'>Item 2</a></li>
                <li><a href='#'>Item 3</a></li>
                <li><a href='#'>Item 4</a></li>
                <li class='clear'></li>
            </ul>
        </header>
        

        【讨论】:

        • 如果您删除了关于自动关闭divs 的错误陈述,我将删除我的反对票。
        • Div 不是有效的自闭合标签 Gareth。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-05-27
        • 2022-12-05
        • 1970-01-01
        • 2022-08-17
        • 2011-08-22
        • 2012-08-12
        • 2020-02-01
        相关资源
        最近更新 更多