admin管理员组文章数量:1399887
I have a CSS drop down menu with rounded corners that works fine. The issue I'm tackling is that when I hover over menu items and they get highlighted, I need the first and last item to also have rounded corners. Right now, when the menu opens up, it looks fine but when I hover over an item, it doesn't have rounded corners. This is not an issue for items that are in the middle but the first and last items don't look quite right -- see image below:
Here's my code:
/* The container <div> - needed to position the dropdown content */
.dropdown {
position: relative;
display: inline-block;
cursor: pointer;
}
/* Dropdown Content (Hidden by Default) */
.dropdown-content {
display: none;
position: absolute;
border-width: 0;
border-radius: 0.35rem;
background-color: #DD4746;
min-width: 10rem;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
right: 3rem;
}
/* Links inside the dropdown */
.dropdown-content a {
color: #FFF;
padding: 8px 16px;
font-size: 0.9rem;
text-decoration: none;
display: block;
&:hover {
background-color: #333;
}
}
.show-dropdown {
display: block;
}
.avatar {
display: flex;
background-color: #333;
width: 40;
height: 40;
border-width: 2px;
border-color: #333;
border-style: solid;
border-radius: 50%;
overflow: hidden;
justify-content: center;
margin-left: 200px;
img: {
width: 100%;
}
}
<div class="dropdown">
<div class="avatar">
<img src=":w-40,h-40,fo-face/sam_large.jpg" />
</div>
<div class="dropdown-content show-dropdown">
<a href="#">Profile</a>
<a href="#">Settings</a>
<a href="#">Logout</a>
</div>
</div>
I have a CSS drop down menu with rounded corners that works fine. The issue I'm tackling is that when I hover over menu items and they get highlighted, I need the first and last item to also have rounded corners. Right now, when the menu opens up, it looks fine but when I hover over an item, it doesn't have rounded corners. This is not an issue for items that are in the middle but the first and last items don't look quite right -- see image below:
Here's my code:
/* The container <div> - needed to position the dropdown content */
.dropdown {
position: relative;
display: inline-block;
cursor: pointer;
}
/* Dropdown Content (Hidden by Default) */
.dropdown-content {
display: none;
position: absolute;
border-width: 0;
border-radius: 0.35rem;
background-color: #DD4746;
min-width: 10rem;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
right: 3rem;
}
/* Links inside the dropdown */
.dropdown-content a {
color: #FFF;
padding: 8px 16px;
font-size: 0.9rem;
text-decoration: none;
display: block;
&:hover {
background-color: #333;
}
}
.show-dropdown {
display: block;
}
.avatar {
display: flex;
background-color: #333;
width: 40;
height: 40;
border-width: 2px;
border-color: #333;
border-style: solid;
border-radius: 50%;
overflow: hidden;
justify-content: center;
margin-left: 200px;
img: {
width: 100%;
}
}
<div class="dropdown">
<div class="avatar">
<img src="https://ik.imagekit.io/ingrid/members/tr:w-40,h-40,fo-face/sam_large.jpg" />
</div>
<div class="dropdown-content show-dropdown">
<a href="#">Profile</a>
<a href="#">Settings</a>
<a href="#">Logout</a>
</div>
</div>
Share
Improve this question
asked Mar 25 at 19:53
SamSam
30.6k76 gold badges252 silver badges464 bronze badges
2
|
3 Answers
Reset to default 3Don't put the radius/background on the container, put it on the links as required.
.dropdown-content a:first-child {
border-radius: .35rem .35rem 0 0;
}
.dropdown-content a:last-child {
border-radius: 0 0 .35rem .35rem;
}
/* The container <div> - needed to position the dropdown content */
.dropdown {
position: relative;
display: inline-block;
cursor: pointer;
}
/* Dropdown Content (Hidden by Default) */
.dropdown-content {
display: none;
position: absolute;
border-width: 0;
min-width: 10rem;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
right: 3rem;
}
/* Links inside the dropdown */
.dropdown-content a {
color: #FFF;
padding: 8px 16px;
font-size: 0.9rem;
background-color: #DD4746;
text-decoration: none;
display: block;
&:hover {
background-color: #333;
}
}
.dropdown-content a:first-child {
border-radius: .35rem .35rem 0 0;
}
.dropdown-content a:last-child {
border-radius: 0 0 .35rem .35rem;
}
.show-dropdown {
display: block;
}
.avatar {
display: flex;
background-color: #333;
width: 40;
height: 40;
border-width: 2px;
border-color: #333;
border-style: solid;
border-radius: 50%;
overflow: hidden;
justify-content: center;
margin-left: 200px;
img: {
width: 100%;
}
}
<div class="dropdown">
<div class="avatar">
<img src="https://ik.imagekit.io/ingrid/members/tr:w-40,h-40,fo-face/sam_large.jpg" />
</div>
<div class="dropdown-content show-dropdown">
<a href="#">Profile</a>
<a href="#">Settings</a>
<a href="#">Logout</a>
</div>
</div>
Just as stated, you can add border radius to first and last element of the dropdown container:
/* The container <div> - needed to position the dropdown content */
.dropdown {
position: relative;
display: inline-block;
cursor: pointer;
}
/* Dropdown Content (Hidden by Default) */
.dropdown-content {
display: none;
position: absolute;
border-width: 0;
border-radius: 0.35rem;
background-color: #DD4746;
min-width: 10rem;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
right: 3rem;
}
.dropdown-content a:first-child {
border-radius: 0.35rem 0.35rem 0 0 ;
}
.dropdown-content a:last-child {
border-radius: 0 0 0.35rem 0.35rem;
}
/* Links inside the dropdown */
.dropdown-content a {
color: #FFF;
padding: 8px 16px;
font-size: 0.9rem;
text-decoration: none;
display: block;
&:hover {
background-color: #333;
}
}
.show-dropdown {
display: block;
}
.avatar {
display: flex;
background-color: #333;
width: 40;
height: 40;
border-width: 2px;
border-color: #333;
border-style: solid;
border-radius: 50%;
overflow: hidden;
justify-content: center;
margin-left: 200px;
img: {
width: 100%;
}
}
<div class="dropdown">
<div class="avatar">
<img src="https://ik.imagekit.io/ingrid/members/tr:w-40,h-40,fo-face/sam_large.jpg" />
</div>
<div class="dropdown-content show-dropdown">
<a href="#">Profile</a>
<a href="#">Settings</a>
<a href="#">Logout</a>
</div>
</div>
Another option is to add overflow: clip;
to .dropdown-content
so the items won't overflow the container:
/* The container <div> - needed to position the dropdown content */
.dropdown {
position: relative;
display: inline-block;
cursor: pointer;
}
/* Dropdown Content (Hidden by Default) */
.dropdown-content {
display: none;
position: absolute;
border-width: 0;
border-radius: 0.35rem;
background-color: #DD4746;
min-width: 10rem;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
right: 3rem;
overflow: clip;
}
/* Links inside the dropdown */
.dropdown-content a {
color: #FFF;
padding: 8px 16px;
font-size: 0.9rem;
text-decoration: none;
display: block;
&:hover {
background-color: #333;
}
}
.show-dropdown {
display: block;
}
.avatar {
display: flex;
background-color: #333;
width: 40;
height: 40;
border-width: 2px;
border-color: #333;
border-style: solid;
border-radius: 50%;
overflow: hidden;
justify-content: center;
margin-left: 200px;
img: {
width: 100%;
}
}
<div class="dropdown">
<div class="avatar">
<img src="https://ik.imagekit.io/ingrid/members/tr:w-40,h-40,fo-face/sam_large.jpg" />
</div>
<div class="dropdown-content show-dropdown">
<a href="#">Profile</a>
<a href="#">Settings</a>
<a href="#">Logout</a>
</div>
</div>
本文标签: htmlBorder radius for first and last items in CSS drop down menuStack Overflow
版权声明:本文标题:html - Border radius for first and last items in CSS drop down menu - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744172066a2593829.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
border-radius: inherit;
to.dropdown-content a
works for your case? – cmolina Commented Mar 25 at 20:05