如何在CSS中使矩形外边缘变曲?

huangapple go评论67阅读模式
英文:

How to curve the outside of a rectangle in CSS?

问题

我有一个使用SAAS CMS的网站,所以无法更改HTML结构,但我可以添加一些自定义CSS。我希望页眉和页脚具有向上和向下的曲线。这是否可以仅通过CSS实现?我尝试使用border-radius,但似乎只适用于完美的圆形,而不适用于复杂的形状。

使用Bootstrap,这是我的HTML:

<!-- bootstrap nav -->
<nav class="navbar navbar-top navbar-expand-md">
  <a class="navbar-brand" href="#">Brand</a>
  <ul class="navbar-nav">
    <li class="nav-item"><a href="#" class="nav-link">Home</a></li>
    <li class="nav-item"><a href="#" class="nav-link">Link</a></li>
    <li class="nav-item"><a href="#" class="nav-link">Link</a></li>
    <li class="nav-item"><a href="#" class="nav-link">More</a></li>
    <li class="nav-item"><a href="#" class="nav-link">Options</a></li>
  </ul>
</nav>
<div class="container">
  <div class="main-content">
    <p>主要元素在这里</p>
    <!-- 主要元素放在这里 -->
  </div>
</div>
<!-- /bootstrap nav -->
<nav class="navbar navbar-bottom navbar-expand-md fixed-bottom">
  <a class="navbar-brand" href="#">Brand</a>
  <ul class="navbar-nav">
    <li class="nav-item"><a href="#" class="nav-link">Home</a></li>
    <li class="nav-item"><a href="#" class="nav-link">Link</a></li>
    <li class="nav-item"><a href="#" class="nav-link">Link</a></li>
    <li class="nav-item"><a href="#" class="nav-link">More</a></li>
    <li class="nav-item"><a href="#" class="nav-link">Options</a></li>
  </ul>
</nav>

你可以查看我在这里创建的 codeply

如何在CSS中使矩形外边缘变曲?

英文:

I have a website which uses a SAAS CMS, so I cannot change the HTML structure, but I do have the ability to add some custom css. I want the header and footer to have upward and downward curves. Can this be achieved in CSS alone? I have tried using border-radius but it seems only good for perfect circles, not obscure shapes.

I made a codeply for it here.

Using Bootstrap, here's my HTML:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.navbar-top {
background-color: blue;
}
.navbar-bottom {
background-color: red;
}

<!-- language: lang-html -->

&lt;!-- bootstrap nav --&gt;
&lt;nav class=&quot;navbar navbar-top navbar-expand-md&quot;&gt;
&lt;a class=&quot;navbar-brand&quot; href=&quot;#&quot;&gt;Brand&lt;/a&gt;
&lt;ul class=&quot;navbar-nav&quot;&gt;
&lt;li class=&quot;nav-item&quot;&gt;&lt;a href=&quot;#&quot; class=&quot;nav-link&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;
&lt;li class=&quot;nav-item&quot;&gt;&lt;a href=&quot;#&quot; class=&quot;nav-link&quot;&gt;Link&lt;/a&gt;&lt;/li&gt;
&lt;li class=&quot;nav-item&quot;&gt;&lt;a href=&quot;#&quot; class=&quot;nav-link&quot;&gt;Link&lt;/a&gt;&lt;/li&gt;
&lt;li class=&quot;nav-item&quot;&gt;&lt;a href=&quot;#&quot; class=&quot;nav-link&quot;&gt;More&lt;/a&gt;&lt;/li&gt;
&lt;li class=&quot;nav-item&quot;&gt;&lt;a href=&quot;#&quot; class=&quot;nav-link&quot;&gt;Options&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/nav&gt;
&lt;div class=&quot;container&quot;&gt;
&lt;div class=&quot;main-content&quot;&gt;
&lt;p&gt;main elements are here&lt;/p&gt;
&lt;!-- main elements go here--&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;!-- /bootstrap nav --&gt;
&lt;nav class=&quot;navbar navbar-bottom navbar-expand-md fixed-bottom&quot;&gt;
&lt;a class=&quot;navbar-brand&quot; href=&quot;#&quot;&gt;Brand&lt;/a&gt;
&lt;ul class=&quot;navbar-nav&quot;&gt;
&lt;li class=&quot;nav-item&quot;&gt;&lt;a href=&quot;#&quot; class=&quot;nav-link&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;
&lt;li class=&quot;nav-item&quot;&gt;&lt;a href=&quot;#&quot; class=&quot;nav-link&quot;&gt;Link&lt;/a&gt;&lt;/li&gt;
&lt;li class=&quot;nav-item&quot;&gt;&lt;a href=&quot;#&quot; class=&quot;nav-link&quot;&gt;Link&lt;/a&gt;&lt;/li&gt;
&lt;li class=&quot;nav-item&quot;&gt;&lt;a href=&quot;#&quot; class=&quot;nav-link&quot;&gt;More&lt;/a&gt;&lt;/li&gt;
&lt;li class=&quot;nav-item&quot;&gt;&lt;a href=&quot;#&quot; class=&quot;nav-link&quot;&gt;Options&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/nav&gt;

<!-- end snippet -->

如何在CSS中使矩形外边缘变曲?

答案1

得分: 3

为了实现这个效果,请尝试使用以下代码:

.navbar {
  justify-content: flex-start;
}

.navbar-nav {
  flex-direction: row !important;
}

.navbar-top {
  background-color: blue;
}

.navbar-bottom {
  background-color: red;
}

.main {
  border-top-left-radius: 10%;
  border-bottom-right-radius: 10%;
  background: #fff;
  position: relative;
  min-height: calc(100vh - 112px);
  padding: 20px;
}

.main:before {
  position: absolute;
  content: '';
  top: 0;
  left: 0;
  background-color: blue;
  width: 100%;
  height: 50%;
  z-index: -1;
}

.main:after {
  position: absolute;
  content: '';
  bottom: 0;
  right: 0;
  background-color: red;
  width: 100%;
  height: 50%;
  z-index: -1;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.0.2/css/bootstrap.min.css" rel="stylesheet" />
<!-- bootstrap nav -->
<nav class="navbar navbar-top navbar-expand-md">
  <a class="navbar-brand" href="#">Brand</a>
  <ul class="navbar-nav">
    <li class="nav-item"><a href="#" class="nav-link">Home</a></li>
    <li class="nav-item"><a href="#" class="nav-link">Link</a></li>
    <li class="nav-item"><a href="#" class="nav-link">Link</a></li>
    <li class="nav-item"><a href="#" class="nav-link">More</a></li>
    <li class="nav-item"><a href="#" class="nav-link">Options</a></li>
  </ul>
</nav>
<main class="main">
  <div class="container">
    <div class="main-content">
      <p>main elements are here</p>
      <!-- main elements go here-->
    </div>
  </div>
</main>
<!-- /bootstrap nav -->
<nav class="navbar navbar-bottom navbar-expand-md fixed-bottom">
  <a class="navbar-brand" href="#">Brand</a>
  <ul class="navbar-nav">
    <li class="nav-item"><a href="#" class="nav-link">Home</a></li>
    <li class="nav-item"><a href="#" class="nav-link">Link</a></li>
    <li class="nav-item"><a href="#" class="nav-link">Link</a></li>
    <li class="nav-item"><a href="#" class="nav-link">More</a></li>
    <li class="nav-item"><a href="#" class="nav-link">Options</a></li>
  </ul>
</nav>
英文:

To achieve this, try using this code:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.navbar {
justify-content: flex-start;
}
.navbar-nav {
flex-direction: row !important;
}
.navbar-top {
background-color: blue;
}
.navbar-bottom {
background-color: red;
}
.main {
border-top-left-radius: 10%;
border-bottom-right-radius: 10%;
background: #fff;
position: relative;
min-height: calc(100vh - 112px);
padding: 20px;
}
.main:before {
position: absolute;
content: &#39;&#39;;
top: 0;
left: 0;
background-color: blue;
width: 100%;
height: 50%;
z-index: -1;
}
.main:after {
position: absolute;
content: &#39;&#39;;
bottom: 0;
right: 0;
background-color: red;
width: 100%;
height: 50%;
z-index: -1;
}

<!-- language: lang-html -->

&lt;link href=&quot;https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.0.2/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; /&gt;
&lt;!-- bootstrap nav --&gt;
&lt;nav class=&quot;navbar navbar-top navbar-expand-md&quot;&gt;
&lt;a class=&quot;navbar-brand&quot; href=&quot;#&quot;&gt;Brand&lt;/a&gt;
&lt;ul class=&quot;navbar-nav&quot;&gt;
&lt;li class=&quot;nav-item&quot;&gt;&lt;a href=&quot;#&quot; class=&quot;nav-link&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;
&lt;li class=&quot;nav-item&quot;&gt;&lt;a href=&quot;#&quot; class=&quot;nav-link&quot;&gt;Link&lt;/a&gt;&lt;/li&gt;
&lt;li class=&quot;nav-item&quot;&gt;&lt;a href=&quot;#&quot; class=&quot;nav-link&quot;&gt;Link&lt;/a&gt;&lt;/li&gt;
&lt;li class=&quot;nav-item&quot;&gt;&lt;a href=&quot;#&quot; class=&quot;nav-link&quot;&gt;More&lt;/a&gt;&lt;/li&gt;
&lt;li class=&quot;nav-item&quot;&gt;&lt;a href=&quot;#&quot; class=&quot;nav-link&quot;&gt;Options&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/nav&gt;
&lt;main class=&quot;main&quot;&gt;
&lt;div class=&quot;container&quot;&gt;
&lt;div class=&quot;main-content&quot;&gt;
&lt;p&gt;main elements are here&lt;/p&gt;
&lt;!-- main elements go here--&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/main&gt;
&lt;!-- /bootstrap nav --&gt;
&lt;nav class=&quot;navbar navbar-bottom navbar-expand-md fixed-bottom&quot;&gt;
&lt;a class=&quot;navbar-brand&quot; href=&quot;#&quot;&gt;Brand&lt;/a&gt;
&lt;ul class=&quot;navbar-nav&quot;&gt;
&lt;li class=&quot;nav-item&quot;&gt;&lt;a href=&quot;#&quot; class=&quot;nav-link&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;
&lt;li class=&quot;nav-item&quot;&gt;&lt;a href=&quot;#&quot; class=&quot;nav-link&quot;&gt;Link&lt;/a&gt;&lt;/li&gt;
&lt;li class=&quot;nav-item&quot;&gt;&lt;a href=&quot;#&quot; class=&quot;nav-link&quot;&gt;Link&lt;/a&gt;&lt;/li&gt;
&lt;li class=&quot;nav-item&quot;&gt;&lt;a href=&quot;#&quot; class=&quot;nav-link&quot;&gt;More&lt;/a&gt;&lt;/li&gt;
&lt;li class=&quot;nav-item&quot;&gt;&lt;a href=&quot;#&quot; class=&quot;nav-link&quot;&gt;Options&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/nav&gt;

<!-- end snippet -->

答案2

得分: -1

在CSS中,要使矩形的外边缘变曲线,您可以使用border-radius属性。border-radius属性允许您为元素设置圆角,从而使矩形的外边缘变成曲线。

英文:

To curve the outside of a rectangle in CSS, you can use the border-radius property. The border-radius property allows you to set rounded corners for an element, thereby curving the outer edges of the rectangle.

huangapple
  • 本文由 发表于 2023年6月29日 22:48:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76582198.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定