Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

html - Add rounded borders to selected corners of an element

How could I go about constructing something like this with pure CSS?

SS

This is how far I've gotten so far: Fiddle

I'm struggling with how to get that rounded corner there, even if I continue to add additional spans.


CODE:

body {
  background: #000;
}

.container {
  position: relative;
  width: 300px;
  height: 150px;
  margin: 10% auto;
}

.top-right {
  position: absolute;
  top: -10px;
  right: 0;
  width: 50px;
  height: 1px;
  background: white;
  border-radius: 5px;
}

.box {
  width: 100%;
  height: 100%;
  background: red;
  border-radius: 15px;
  display: flex;
  align-items: center;
  justify-content: center;
}

h3 {
  color: white;
}
<div class="container">
  <span class="top-right"></span>
  <div class="box">
    <h3>Content</h3>
  </div>
</div>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you can achieve that by using pseudo elements ::before/::after in .box using the properties border and border-radius

body {
  background: #000;
}
.container {
  width: 300px;
  height: 150px;
  margin: 3% auto 0 /* changed for demo */
}
h3 {
  color: white;
}
.box {
  width: 100%;
  height: 100%;
  background: red;
  border-radius: 15px;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}
.box::before,
.box::after {
  content: "";
  position: absolute;
  border: solid white;
  width: 50px;
  height: 50px;
}
.box::before {
  top: -15px;
  left: -15px;
  border-radius: 15px 0; /* top-left */
  border-width: 5px 0 0 5px;
}
.box::after {
  bottom: -15px;
  right: -15px;
  border-radius: 0 0 15px; /* bottom-right */
  border-width: 0 5px 5px 0;
}
<div class="container">
  <div class="box">
    <h3>Content</h3>
  </div>
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...