简体   繁体   English

CSS弯角

[英]Curved corners with CSS

I have a simple navigation Bar and some styling with CSS already. 我有一个简单的导航栏,并且已经使用CSS进行了一些样式设置。

The nav Bar will have a white border on along the top for the inner links. 导航栏的内部链接顶部会带有白色边框。

For the two outer links I want there to be a border on the left for the left link and on the right for the right link and also curved corners but i don't know how to focus the CSS on just these two li's. 对于两个外部链接,我希望左侧链接的左边有一个边界,右侧链接的右边有一个边界,还有弯曲的角,但是我不知道如何仅将CSS集中在这两个li上。

I tried to give the li an id of home but that didn't work 我试着给李家一个身份证,但这没用

i'v also tried putting the curved corners code in the ul and the NavBar tags. 我还尝试将弯曲的代码放在ul和NavBar标签中。 Here is wht I have tried 这是我尝试过的

<div id="NavBar">
<ul>
<li id="Home"><a href="Index.php"><strong>Home</strong></a></li>
<li><a href="AboutUs.php"><strong>About Us</strong></a></li>
<li><a href="Products.php"><strong>Products</strong></a></li>
<li><a href="Policies.php"><strong>Policies</strong></a></li>
<li id="ContactUs"><a href="ContactUs.php"><strong>Contact Us</strong></a></li> 
</ul>

And this is the CSS which i have tried to focus on the one li home. 这就是我试图集中在一位李家的CSS。

#NavBar li {
   margin:0;
   padding:0;
   list-style:none;
   float:left;
   position:relative;
   border:solid 3px #FFF;
   border-bottom:0px;
   width:20%;

   }
#NavBar li Home {
    margin:0;
    padding:0;
    list-style:none;
    float:left;
    position:relative;
    border:solid 3px #FFF;
    border-bottom:0px;
    width:20%;
    -moz-border-radius-topleft: 10px;
    -webkit-border-top-left-radius: 10px;

     }

Thanks for any help 谢谢你的帮助

Created a JSFiddle: https://jsfiddle.net/b4ejndkz/ 创建了一个JSFiddle: https ://jsfiddle.net/b4ejndkz/

If you're going to use width:20% and specify a border width , you'll need box-sizing:border-box; 如果要使用width:20% 并指定边框宽度 ,则需要box-sizing:border-box; , that way it'll take into account the border size when determining total width. ,这样在确定总宽度时会考虑边框大小。 Otherwise it'll split off into 2 lines like it is at the moment. 否则,它会像现在一样分成两行。

Then you can set a specific corner to apply a border radius on by doing: border-radius: 5px 0 0 0; 然后,您可以通过执行以下操作来设置特定的角以应用边界半径: border-radius: 5px 0 0 0; (top left, top right, bottom right, bottom left). (左上,右上,右下,左下)。

You could do it with id selectors https://jsfiddle.net/b4ejndkz/2/ ... or instead use the CSS selectors :first-child and :last-child to select your first and last elements of your list: 您可以使用id选择器https://jsfiddle.net/b4ejndkz/2/ ...来实现,也可以使用CSS选择器:first-child:last-child选择列表的第一个和最后一个元素:

#NavBar li {
   box-sizing:border-box;
   margin:0;
   padding:0;
   list-style:none;
   float:left;
   position:relative;
   border:solid 3px #FFF;
   border-bottom:0px;
   width:20%;
}

#NavBar li:first-child {
    border-radius: 5px 0 0 0;
}

#NavBar li:last-child {
    border-radius: 0 5px 0 0;
}

https://jsfiddle.net/b4ejndkz/1/ https://jsfiddle.net/b4ejndkz/1/

Use :first-child and :last-child. 使用:first-child和:last-child。

To access only the first or last element of your list, do something like this: 要仅访问列表的第一个或最后一个元素,请执行以下操作:

ul li:first-child {
    Styles for first element
}

ul li:last-child {
    Styles for last element
}

With that, you can apply the needed styles to the matching links. 这样,您可以将所需的样式应用于匹配的链接。

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

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