【问题标题】:CSS - Rounded concave corners for compound layoutCSS - 用于复合布局的圆角凹角
【发布时间】:2016-12-26 09:35:22
【问题描述】:

首先,我知道有类似的问题可用(例如。Create concave corners in css),但它们并没有真正涵盖这种情况。

这与单个单元格/div 元素无关。

我有三个块,里面会有一些文本内容:

  1. 顶部中间居中的块(窄)
  2. middle-middle block(全屏)
  3. 中下居中的块(窄)

基本上类似于十字架(已删除文本):

外角 (8) 是直截了当的,但我怎样才能实现那些内角 (4)?

【问题讨论】:

  • 到目前为止您尝试过什么?您需要发布您的代码,以便我们为您提供帮助。
  • 你为什么不只使用背景图片。

标签: html css rounded-corners


【解决方案1】:

看下面的代码,也许它需要一些调整,但想法是你使用pseudo-elements 来制作那些内部边框

如果这是你想要的,请告诉我

.colored {
  background:yellow;
  border:5px solid green;
  width:100px;
  height:100px;
  box-sizing:border-box;
  position:relative;
}
#content {
  width:300px;
position:relative;
background:#000;
}
.top,.bottom { position:relative;margin:0 auto;clear:both}
.top { border-bottom:none}
.bottom { border-top:none}
.left { border-right:none}
.right { border-left:none;}
.colored.center { border:none;}
.left,.center,.right { float:left;}

.top { border-top-left-radius:10px;border-top-right-radius:10px;}
.bottom { border-bottom-left-radius:10px;border-bottom-right-radius:10px;}
.right { border-bottom-right-radius:10px;border-top-right-radius:10px;}
.left { border-bottom-left-radius:10px;border-top-left-radius:10px;}

.top:before {
  position:absolute;
  height:100%;
  width:100%;

  left:-100%;
  top:5px;
  content:"";
  border-bottom-right-radius:10px;
  border-right:5px solid green;
  border-bottom:5px solid green;
  z-index:9999;
  box-sizing:border-box;
  
}
.top:after {
  position:absolute;
  height:100%;
  width:100%;

  right:-100%;
  top:5px;
  content:"";
  border-bottom-left-radius:10px;
  border-left:5px solid green;
  border-bottom:5px solid green;
  z-index:9999;
  box-sizing:border-box;
  
}


.bottom:before {
  position:absolute;
  height:100%;
  width:100%;

  left:-100%;
  bottom:5px;
  content:"";
  border-top-right-radius:10px;
  border-right:5px solid green;
  border-top:5px solid green;
  z-index:9999;
  box-sizing:border-box;
  
}
.bottom:after {
  position:absolute;
  height:100%;
  width:100%;

  right:-100%;
  bottom:5px;
  content:"";
  border-top-left-radius:10px;
  border-left:5px solid green;
  border-top:5px solid green;
  z-index:9999;
  box-sizing:border-box;
  
}
<div id="content">


<div class="top colored">

</div>
<div class="left colored">

</div>
<div class="center colored">

</div>

<div class="right colored">

</div>

<div class="bottom colored">

</div>
</div>

【讨论】:

  • 看起来或多或少是我想要的方式,但在代码中我希望只有一个“屏幕宽度”div 而不是三个(左、中、右)——这可能吗?此外,内角似乎并不完美
  • 我认为不可能只有一个 div。边框太多了,你需要很多元素来制作这些边框。不仅是内部的,而且所有的............关于角落不完全适合的事实,这就是为什么我在回答中说:`也许它需要一些调整`。为了让它们完美契合,4 个 div 的边框需要更短,但没有 border-height 属性。我给你的代码,我认为,这是你可以用 CSS 做的最好的事情
【解决方案2】:

只有三个 div 的变化,有点 hacky 但很实用。使用伪元素、变换和内框阴影。

div {
  background-color: #000;
  min-height: 100px;
}
.center {
  width: 100px;
  margin: 0 auto;
}
.rounded {
  border-radius: 20px;
  border: 5px solid red;
}
.conc {
  position: relative;
}
.conc::before,
.conc::after {
  content: '';
  position: absolute;
  border: 5px solid red;
  border-radius: 20px;
  width: 25px;
  height: 25px;
  background-color: trnaspanret;
  border-color: red transparent transparent;
  z-index: 3;
  box-shadow: white 0px 0px 0px 20px inset
}
.conc.bottom {
  margin-bottom: -5px;
  border-bottom: 0;
  border-radius: 20px 20px 0 0
}
.conc.top {
  margin-top: -5px;
  border-top: 0;
  border-radius: 0 0 20px 20px
}
.conc::before {
  left: -35px;
}
.conc::after {
  right: -35px;
}
.conc.top::before,
.conc.top::after {
  top: 0px;
}
.conc.bottom::before,
.conc.bottom::after {
  bottom: 0px;
}
.conc.bottom::before {
  transform: rotate(135deg)
}
.conc.bottom::after {
  transform: rotate(-135deg)
}
.conc.top::before {
  transform: rotate(45deg)
}
.conc.top::after {
  transform: rotate(-45deg)
}
.centerblinders {
  position: relative;
}
.centerblinders::before,
.centerblinders::after {
  content: '';
  position: absolute;
  width: 130px;
  height: 20px;
  background-color: #000;
  left: 50%;
  transform: translatex(-50%);
  z-index: 2;
}
.centerblinders::before {
  top: -15px;
}
.centerblinders::after {
  bottom: -15px;
}
<div class="rounded center conc bottom"></div>
<div class="rounded centerblinders"></div>
<div class="rounded center conc top"></div>

【讨论】:

    猜你喜欢
    • 2014-01-25
    • 1970-01-01
    • 1970-01-01
    • 2014-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多