【问题标题】:Curved button corners弧形按钮角
【发布时间】:2014-12-20 12:32:44
【问题描述】:

我正在寻找创建此类按钮的选项:

目前,我已经能够通过创建一个圆形遮罩来实现显示的效果,该遮罩位于徽标下方和背景下方,但位于按钮上方。但是,当试图将导航固定到顶部时,这确实会导致问题。

是否可以在没有遮罩的情况下创建这样的按钮,可能使用边框半径?

这是我目前所拥有的:

/* Navigation */

#logo {
  background-color: #f0f0f0;
  position: relative;
  height: 150px;
  width: 150px;
  margin: -172px auto;
  border: 6px solid #ffffff;
  border-radius: 200px;
  box-shadow: 2px 2px 5px 2px rgba(0, 0, 0, .04);
  z-index: 3;
}
#logo:active {
  top: 2px;
}
#logo-mark {
  margin: 0 auto;
  max-width: 96px;
  display: block;
  -webkit-transition: .25s;
  transition: .25s;
  padding: 15px 0 0 0px;
  opacity: 0.5;
  -webkit-backface-visibility: hidden;
}
#logo-mark:hover {
  opacity: 0.8;
}
#nav-mask {
  background-color: #ececec;
  position: relative;
  height: 190px;
  width: 190px;
  margin: 0 auto;
  border-radius: 200px;
  z-index: 0;
}
#nav-wrapper {
  background-color: #ececec;
  width: 100%;
  position: fixed;
}
#nav {
  margin: 0 auto;
  max-width: 400px;
  height: 90px;
}
.btn {
  background-color: #494f4f;
  height: 50px;
  width: 130px;
  margin: 20px 20px 0 20px;
  border-radius: 4px;
  -webkit-transition: .25s;
  transition: .25s;
  opacity: 0.8;
  z-index: 0;
}
.btn:hover {
  background-color: #3c4141;
  opacity: 1;
}
.btn:active {
  background-color: #3c4141;
}
.btn-left {
  text-align: left;
  float: left;
}
.btn-right {
  text-align: right;
  float: right;
}
.btn-rightmost {
  text-align: right;
}
.text-nav {
  color: #fff;
  font-style: normal;
  font-weight: 600;
  font-size: 18px;
  margin: 10px 8px 0 8px;
}
<div id="nav-wrapper">
  <div id="nav">
    <a href="about.html" class="btn btn-left">
      <h3 class="text-nav">About</h3>
    </a>
    <a href="#work" class="btn btn-right">
      <h3 class="text-nav">Work</h3>
    </a>
  </div>
</div>
<div id="nav-mask"></div>
<a href="website">
  <div id="logo">
    <img id="logo-mark" src="http://www.placehold.it/96">
  </div>
</a>

【问题讨论】:

标签: html css css-shapes


【解决方案1】:

这是我使用:before and :after pseudo elements 和所需的最低 HTML 对您的设计的看法。它使用了掩蔽概念,但不会中断。

曲线

两个按钮上的曲线是使用导航的:after 伪元素创建的。圆圈被导航上的overflow: hidden 切断。它以position: absolute 定位,不会从它的位置偏移。

导航被指定为position: relative,这样圆圈就会相对于它定位自己。

看起来像这样:

定位链接

链接被填充分开。没有伪元素掩码,它们看起来像这样:

定位标志

图像被指定为position: absolute,并以left: 50% 定位,并带有一半宽度的负边距,包括边框,使其处于死点。

标题为position: relative,以便徽标将自身定位在与其相关的位置。

垂直居中

链接使用display: inline-blockvertical-align: middle居中。这要归功于:before 伪元素垂直居中技巧。这将内联元素置于行中,如下所示:

header nav:before {
    content: '';
    height: 100%;
    display: inline-block;
    vertical-align: middle;
}

文本使用line-height: 50px在链接中居中,与链接height: 50px匹配。

内嵌间隙

通过在内联元素之间不留空格来防止内联间隙,如下所示:

  <nav><a href="#">About</a><a href="#">Work</a></nav>

Here is a write-up on preventing this gap over on CSS Tricks

完整示例

固定宽度

* {
  font-family: arial;
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
header {
  background: #EEE;
  height: 80px;
  display: block;
  padding: 0 25px;
  position: relative;
  width: 377px;
}
header nav {
  height: 100%;
  position: relative;
  overflow: hidden;
}
header nav:before {
  content: '';
  height: 100%;
  display: inline-block;
  vertical-align: middle;
}
header nav:after {
  content: '';
  position: absolute;
  top: 0;
  left: 50%;
  width: 170px;
  height: 170px;
  margin-left: -85px;
  border-radius: 50%;
  background: #EEE;
}
header nav a {
  height: 50px;
  display: inline-block;
  background: #666;
  color: #FFF;
  text-decoration: none;
  font-weight: bold;
  line-height: 50px;
  padding: 0 10px 0;
  vertical-align: middle;
}
header nav a:first-child {
  padding: 0 100px 0 20px;
  border-radius: 3px 0 0 3px;
}
header nav a:last-child {
  padding: 0 20px 0 100px;
  border-radius: 0 3px 3px 0;
}
header img {
  position: absolute;
  top: 15px;
  left: 50%;
  margin-left: -70px;
  border-radius: 50%;
  border: solid 5px #FFF;
}
<header>
  <nav><a href="#">About</a><a href="#">Work</a></nav>
  <img src="http://www.placehold.it/130" />
</header>

100% 宽度和居中

  • 由于display: block(其默认值),标题可以占据宽度的 100%,而不是宽度

  • 所有内容都以text-align: center 为中心在标题内

* {
  font-family: arial;
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
header {
  background: #EEE;
  height: 80px;
  display: block;
  padding: 0 25px;
  position: relative;
  text-align: center;
}
header nav {
  height: 100%;
  position: relative;
  overflow: hidden;
}
header nav:before {
  content: '';
  height: 100%;
  display: inline-block;
  vertical-align: middle;
}
header nav:after {
  content: '';
  position: absolute;
  top: 0;
  left: 50%;
  width: 170px;
  height: 170px;
  margin-left: -85px;
  border-radius: 50%;
  background: #EEE;
}
header nav a {
  height: 50px;
  display: inline-block;
  background: #666;
  color: #FFF;
  text-decoration: none;
  font-weight: bold;
  line-height: 50px;
  padding: 0 10px 0;
  vertical-align: middle;
}
header nav a:first-child {
  padding: 0 100px 0 20px;
  border-radius: 3px 0 0 3px;
}
header nav a:last-child {
  padding: 0 20px 0 100px;
  border-radius: 0 3px 3px 0;
}
header img {
  position: absolute;
  top: 15px;
  left: 50%;
  margin-left: -70px;
  border-radius: 50%;
  border: solid 5px #FFF;
}
<header>
  <nav><a href="#">About</a><a href="#">Work</a></nav>
  <img src="http://www.placehold.it/130" />
</header>

【讨论】:

  • 这是使用掩码完成的,但作为position: fixed 不会有任何问题,因为掩码完全包含在&lt;nav&gt; 中并且变得更像背景:)
  • 这完全可行,非常感谢!只是一个愚蠢的问题:您能否告诉我如何使导航栏覆盖页面的整个宽度并将其居中?
  • @PaulVO - 简单:D 删除标题上的宽度并给它text-align: center。我在回答中为您添加了第二个示例:)
  • 我还以某种方式设法将 CSS 放置在我的示例中两次。我删除了多余的,所以没有重复的选择器:)
  • 谢谢,帮了大忙。有什么办法可以向栏添加更多按钮?它们似乎被徽标标记所掩盖。
【解决方案2】:

*{box-sizing: border-box; padding: 0; margin: 0}

figure{
    width: 480px;
    height:320px;
    margin: 20px auto;
    position: relative;
    background: red
}

img{
    z-index:2;
    position: absolute;
    top: 40px;
    margin-left: -75px;
    left: 50%;
    width: 150px;
    height: 150px;
    border-radius: 50%;
    background: #E4E4E4;
    box-shadow: inset 0 0 14px 0 #333,0 0 0 4px white, 0 0 18px #333;
}

figcaption{
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100px;
    background: #E4E4E4;
    z-index: 1;
    box-shadow: inset 0 -3px 6px rgba(51, 51, 51, 0.34);
}
figcaption div{
    line-height: 65px;
    color: white;
    position: relative;
    overflow: hidden;
    z-index: 2;
    width: 178px;
    height: 64px;
    margin-top: 20px;
}
figcaption div:nth-of-type(1),figcaption div:nth-of-type(2){
    border-radius: 4px; 
}
figcaption div:nth-of-type(1){
    text-indent: 36px;
    margin-left: 20px;
    float: left   
}
figcaption div:nth-of-type(2){
    text-indent: 80px;
    margin-right: 20px;
    float: right  
}
figcaption div:nth-of-type(1):before,figcaption div:nth-of-type(2):before{
    z-index: -2;
    content: '';
    position: absolute;
    width: 116px;
    height: 164px;
    border-radius: 50%;
    box-shadow: 0 0 0 400px #444
}
figcaption div:nth-of-type(1):before{
    right: -61px;
    bottom: -100px
}
figcaption div:nth-of-type(2):before{
    left: -61px;
    bottom: -100px
}
figcaption a{
    font-size: 20px;
    font-weight: 900;
    z-index: 999999999999;
}
<figure>
    <img src="" />
    <figcaption>
        <div><a>About</a></div>
        <div><a>Work</a></div>
    <figcaption>   
</figure>

【讨论】:

    【解决方案3】:

    我会这样做:http://jsfiddle.net/56axt9z0/1/

    您只需重叠几个border-radius 等于其width/height 一半的div 即可获得循环div

    【讨论】:

    • 你应该在你的答案中使用你的代码。您可能还想考虑使用 Stack Overflow 中现在提供的代码 sn-p 功能。目前,如果不访问外部资源,您的答案几乎没有意义
    猜你喜欢
    • 1970-01-01
    • 2021-01-23
    • 1970-01-01
    • 2015-03-26
    • 1970-01-01
    • 1970-01-01
    • 2023-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多