简体   繁体   English

带圆角的菜单

[英]Menu with rounded corners

i'm trying to create a menubar with rounded corners, but when i add the float:left to the li elements, the rounded corners dissapear... 我正在尝试创建一个带圆角的菜单栏,但是当我添加float:leftli元素,圆角消失了......

This is my code: 这是我的代码:

<ul>
  <li>jkfasdf</li>
  <li>jkfasdf</li>
  <li>jkfasdf</li>
</ul>

ul{
  background: red;
  border-top-left-radius: 5px;
  border-bottom-left-radius: 5px;
  border-top-right-radius: 5px;
  border-bottom-left-radius: 5px;
}

li {
  padding: 5px;
  #float: left; #toggle this
}

add overflow:hidden to ul 's css: 添加overflow:hiddenul的css:

ul{
  background: red;
  border-top-left-radius: 5px;
  border-bottom-left-radius: 5px;
  border-top-right-radius: 5px;
  border-bottom-right-radius: 5px;
  overflow:hidden;
}

Currently, when you add a float:left; 目前,当你添加一个float:left; ul became 0px height. ul变为0px高度。 That why it looks like rounded corners disappear. 这就是圆角看起来消失的原因。

overflow:hidden is one of the fixes for such issues. overflow:hidden是针对此类问题的解决方案之一。 See demo here: http://jsfiddle.net/2x3Vm/ 在此处查看演示: http : //jsfiddle.net/2x3Vm/

Quickest way is simply to just set your ul to float left 最快的方法就是将ul设置为向左浮动

ul{
  background: red;
  border-top-left-radius: 5px;
  border-bottom-left-radius: 5px;
  border-top-right-radius: 5px;
  border-bottom-left-radius: 5px;
  float:left;
}

Your problem is when you float you list elements inside your ul loses context of width. 您的问题是,当您浮动时,您在ul中列出的元素会丢失width的上下文。 Another way would be to clear your list 另一种方法是清除您的列表

ul{
  background: red;
  border-top-left-radius: 5px;
  border-bottom-left-radius: 5px;
  border-top-right-radius: 5px;
  border-bottom-left-radius: 5px;

}

li {
  padding: 5px;
  float: left; 
}
.clear{clear:both;}

<ul>
  <li>jkfasdf</li>
  <li>jkfasdf</li>
  <li>jkfasdf</li>
  <li class="clear"></li>
</ul>

If all you want to do is show your LI's next to eachother, display: inline-block; 如果您要做的就是将您的LI彼此相邻显示,则显示:inline-block; might actually work better than floating the elements. 实际上可能比浮动元素更好。

The solution for this problem is the common clearfix . 解决此问题的方法是使用通用的clearfix

Here's a fiddle that fixes it: http://jsfiddle.net/5mxJA/ . 这是修复它的小提琴: http : //jsfiddle.net/5mxJA/ Just add the clearfix class to your ul . 只需将clearfix类添加到您的ul

That's because you have no clear to the . 那是因为你不清楚。 Add this to your CSS: 将其添加到您的CSS:

.clearfix { *zoom: 1; }
.clearfix:after { 
width: 100%; 
content: ''; 
font-size: 0; 
line-height: 0; 
text-indent: -4000px; 
clear: both; 
display:block; 
}

and then add class .clearfix to your <ul> : 然后将class .clearfix添加到您的<ul>

<ul class="clearfix">
  <li>jkfasdf</li>
  <li>jkfasdf</li>
  <li>jkfasdf</li>
</ul>

you must use clear div. 你必须使用明确的div。

<ul>
  <li>jkfasdf</li>
  <li>jkfasdf</li>
  <li>jkfasdf</li>
  <li class="clear"></li>
</ul>

and

ul{
  background: red;
  border-top-left-radius: 5px;
  border-bottom-left-radius: 5px;
  border-top-right-radius: 5px;
  border-bottom-left-radius: 5px;
}

li {
  margin: 5px;
  padding: 5px;
  float: left;
  background: blue;
}
li.clear{
  width: 0;
  height: 0;
  float: none;
  clear: both;
  margin: 0;
  padding: 0;
}

parents ignore height of children that has float in style. 父母不理会飘浮的孩子的身高。

for more information about float see this link . 有关浮动的更多信息,请参阅此链接

for more information about clear see this link . 有关清除的更多信息,请参阅此链接

you can see one sample here . 你可以在这里看到一个样本。

As the others have said, the float voids the UL's height. 正如其他人所说,浮子会使UL的高度无效。

Rather than a clearfix, I'd set a default height for the UL: 我没有为clearfix设置默认高度:

ul{
  list-style: none;
  background: red;
  border-top-left-radius: 5px;
  border-bottom-left-radius: 5px;
  border-top-right-radius: 5px;
  border-bottom-left-radius: 5px;
  height: 30px;
}

li {
  padding: 5px;
  float: left;
}

I used the code in one of my sample projects, and it gave me a warning of using < border-bottom-left-radius: 5px; 我在我的一个示例项目中使用了代码,它给了我一个使用<border-bottom-left-radius:5px; twice >. 两次>。

You forgot to add in bottom-right-radius, so that may have impacted your code. 您忘记添加右下半径,因此可能会影响您的代码。

Thanks 谢谢

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

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