如何制作带有延长的尖角边框

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

How to make border with elongated sharp corners

问题

如何在CSS中创建这种类型的边框?

我尝试使用border-radius和(-)符号,但它不起作用。请帮助我。

我还尝试使用:after:before属性,但它们也不起作用。

如果您有任何想法,请帮助我解决这个问题。

英文:

如何制作带有延长的尖角边框

How to make that type of border in CSS?

I have try border-radius with (-) sign but it didn't work. Please help me.

Also İ tried to use :after :before properties but its also didn't work.

If you have any idea please help me solve this problem.

答案1

得分: 0

以下是代码部分的翻译:

  1. .box {
  2. --b: 4px; /* 边框厚度 */
  3. --c: red; /* 边框颜色 */
  4. --r: 30px; /* 半径 */
  5. margin: 40px;
  6. display: inline-block;
  7. font-size: 40px;
  8. padding: 20px;
  9. border: var(--b) solid var(--c);
  10. background: #fff;
  11. border-radius: var(--r) 0 var(--r) var(--r);
  12. position: relative;
  13. clip-path: inset(-100px 0);
  14. }
  15. .box:before {
  16. content: "";
  17. position: absolute;
  18. left: var(--r);
  19. right: calc(-1*var(--b));
  20. bottom: 100%;
  21. height: var(--r);
  22. border-right: var(--b) solid var(--c);
  23. border-bottom: var(--b) solid var(--c);
  24. border-bottom-right-radius: var(--r);
  25. box-shadow: var(--r) var(--b) 0 #fff;
  26. }
  27. .box:after {
  28. content: "";
  29. position: absolute;
  30. right: calc(-1*var(--b));
  31. bottom: calc(100% - var(--b));
  32. width: var(--b);
  33. height: var(--r);
  34. background: var(--c);
  35. }
  36. body {
  37. background: #eee
  38. }

希望这对你有帮助。如果你需要更多翻译,请随时告诉我。

英文:

Here is an idea using pseudo-elements. I considered CSS variables so you can easily adjust the shape without touching the code.

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

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

  1. .box {
  2. --b: 4px; /* border thickness */
  3. --c: red; /* border color */
  4. --r: 30px; /* radius */
  5. margin: 40px;
  6. display: inline-block;
  7. font-size: 40px;
  8. padding: 20px;
  9. border: var(--b) solid var(--c);
  10. background: #fff;
  11. border-radius: var(--r) 0 var(--r) var(--r);
  12. position: relative;
  13. clip-path: inset(-100px 0);
  14. }
  15. .box:before {
  16. content:&quot;&quot;;
  17. position: absolute;
  18. left: var(--r);
  19. right: calc(-1*var(--b));
  20. bottom: 100%;
  21. height: var(--r);
  22. border-right: var(--b) solid var(--c);
  23. border-bottom: var(--b) solid var(--c);
  24. border-bottom-right-radius: var(--r);
  25. box-shadow: var(--r) var(--b) 0 #fff;
  26. }
  27. .box:after {
  28. content:&quot;&quot;;
  29. position: absolute;
  30. right: calc(-1*var(--b));
  31. bottom: calc(100% - var(--b));
  32. width: var(--b);
  33. height: var(--r);
  34. background: var(--c);
  35. }
  36. body {
  37. background: #eee
  38. }

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

  1. &lt;div class=&quot;box&quot;&gt;
  2. some text&lt;br&gt;
  3. content
  4. &lt;/div&gt;

<!-- end snippet -->

答案2

得分: 0

  1. <!-- 开始代码片段: js 隐藏: true 控制台: false Babel: false -->
  2. <!-- 语言: lang-css -->
  3. body {
  4. display:flex;
  5. height:100vh;
  6. align-items:center;
  7. justify-content: center;
  8. background-color: #e7d39f;
  9. }
  10. div {
  11. position:relative;
  12. width: 200px;
  13. height: 80px;
  14. background: #522d5b;
  15. border-radius:25px;
  16. border-top-left-radius:0;
  17. }
  18. div:before {
  19. content: "";
  20. position:absolute;
  21. top:-40px;
  22. left:0;
  23. height:40px;
  24. width: 40px;
  25. border-bottom-left-radius: 60%;
  26. box-shadow: 0 22px 0 0 #522d5b;
  27. }
  28. <!-- 语言: lang-html -->
  29. <!DOCTYPE html>
  30. <html>
  31. <head></head>
  32. <body>
  33. <div></div>
  34. </body>
  35. </html>
  36. <!-- 结束代码片段 -->

Play with CSS raduises & box-shadow and see what happens! ; )

英文:

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

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

  1. body {
  2. display:flex;
  3. height:100vh;
  4. align-items:center;
  5. justify-content: center;
  6. background-color: #e7d39f;
  7. }
  8. div {
  9. position:relative;
  10. width: 200px;
  11. height: 80px;
  12. background: #522d5b;
  13. border-radius:25px;
  14. border-top-left-radius:0;
  15. }
  16. div:before {
  17. content: &quot;&quot;;
  18. position:absolute;
  19. top:-40px;
  20. left:0;
  21. height:40px;
  22. width: 40px;
  23. border-bottom-left-radius: 60%;
  24. box-shadow: 0 22px 0 0 #522d5b;
  25. }

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

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html&gt;
  3. &lt;head&gt;&lt;/head&gt;
  4. &lt;body&gt;
  5. &lt;div&gt;&lt;/div&gt;
  6. &lt;/body&gt;
  7. &lt;/html&gt;

<!-- end snippet -->

Play with CSS raduises & box-shadow and see what happens! ; )

答案3

得分: 0

使用CSS clip-path的示例。

通过将相同的clip-path添加到稍大的包装元素来模拟边框。

灵感来源:https://stackoverflow.com/questions/31854185/how-to-add-border-in-my-clip-path-polygon-css-style

有关SVG路径的良好解释:https://stackoverflow.com/questions/10177985/svg-rounded-corner

  1. .bubble {
  2. background: #1997d6;
  3. height: 150px;
  4. width: 220px;
  5. position: relative;
  6. clip-path: path('M0,8 h180 a40,20 0 0 0 40,-10 v110 a40,40 0 0 1 -40,40 h-135 a40,40 0 0 1 -40,-40 v-60 a40,40 0 0 1 40,-40 z');
  7. }
  8. .bubble>div {
  9. padding: 12px;
  10. box-sizing: border-box;
  11. position: absolute;
  12. height: 100%;
  13. width: 100%;
  14. transform: scale(0.96);
  15. color: #1997d6;
  16. background: white;
  17. clip-path: path('M0,8 h180 a40,20 0 0 0 40,-10 v110 a40,40 0 0 1 -40,40 h-135 a40,40 0 0 1 -40,-40 v-60 a40,40 0 0 1 40,-40 z');
  18. }
  1. <div className="bubble">
  2. <div>
  3. </div>
  4. </div>
英文:

Example using CSS clip-path.

It mimics a border by adding the same clip-path to a slightly larger wrapping element.

Inspiration: https://stackoverflow.com/questions/31854185/how-to-add-border-in-my-clip-path-polygon-css-style

Good explanation on SVG paths: https://stackoverflow.com/questions/10177985/svg-rounded-corner

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

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

  1. .bubble {
  2. background: #1997d6;
  3. height: 150px;
  4. width: 220px;
  5. position: relative;
  6. clip-path: path(&#39;M0,8 h180 a40,20 0 0 0 40,-10 v110 a40,40 0 0 1 -40,40 h-135 a40,40 0 0 1 -40,-40 v-60 a40,40 0 0 1 40,-40 z&#39;);
  7. }
  8. .bubble&gt;div {
  9. padding: 12px;
  10. box-sizing: border-box;
  11. position: absolute;
  12. height: 100%;
  13. width: 100%;
  14. transform: scale(0.96);
  15. color: #1997d6;
  16. background: white;
  17. clip-path: path(&#39;M0,8 h180 a40,20 0 0 0 40,-10 v110 a40,40 0 0 1 -40,40 h-135 a40,40 0 0 1 -40,-40 v-60 a40,40 0 0 1 40,-40 z&#39;);
  18. }

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

  1. &lt;div className=&quot;bubble&quot;&gt;
  2. &lt;div&gt;
  3. &lt;/div&gt;
  4. &lt;/div&gt;

<!-- end snippet -->

答案4

得分: 0

  1. <div class="对话框">
  2. 一些文本在这里告诉你关于生活的真相。
  3. </div>
  4. <div class="对话框 对话框--轮廓">
  5. 一些其他文本,展示生活有边界的时候。
  6. </div>
  1. body {
  2. 颜色: #3e3e3e;
  3. 边距: 4rem;
  4. 背景: #f4c1f3;
  5. }
  6. .对话框 {
  7. 层级: 10;
  8. 边距: 2rem;
  9. 背景: #4fc1f3;
  10. 填充: 1rem;
  11. 边框半径: 1rem 0 1rem 1rem;
  12. 显示: 行内块;
  13. 位置: 相对;
  14. }
  15. .对话框:after, .对话框:before {
  16. 层级: 0;
  17. 内容: "";
  18. 位置: 绝对;
  19. 顶部: 0;
  20. 变换: 平移Y(-100%);
  21. 宽度: 1rem;
  22. 高度: 1rem;
  23. }
  24. .对话框:before {
  25. 背景: 继承;
  26. 右侧: 0;
  27. }
  28. .对话框:after {
  29. 背景: #f4c1f3;
  30. 右侧: 0;
  31. 边框半径: 0 0 1rem 0;
  32. }
  33. .对话框--轮廓 {
  34. 层级: 100;
  35. 背景: #fff;
  36. 边框: 1px 实线 #4fc1f3;
  37. }
  38. .对话框--轮廓:before, .对话框--轮廓:after {
  39. 右侧: -1px;
  40. 边框: 1px 实线 透明;
  41. 右边框颜色: #4fc1f3;
  42. }
  43. .对话框--轮廓:before {
  44. 底边框样式: ;
  45. }
  46. .对话框--轮廓:after {
  47. 底边框颜色: #4fc1f3;
  48. }

在 CodePen 上查看实时代码

  1. <details>
  2. <summary>英文:</summary>
  3. ```HTML
  4. &lt;div class=&quot;dialog&quot;&gt;
  5. Some text here to tell you the truth about life.
  6. &lt;/div&gt;
  7. &lt;div class=&quot;dialog dialog--outline&quot;&gt;
  8. Some other text to show you life when it has borders.
  9. &lt;/div&gt;
  1. body {
  2. color: #3e3e3e;
  3. margin: 4rem;
  4. background: #f4c1f3;
  5. }
  6. .dialog {
  7. z-index: 10;
  8. margin: 2rem;
  9. background: #4fc1f3;
  10. padding: 1rem;
  11. border-radius: 1rem 0 1rem 1rem;
  12. display: inline-block;
  13. position: relative;
  14. }
  15. .dialog:after, .dialog:before {
  16. z-index: 0;
  17. content: &quot;&quot;;
  18. position: absolute;
  19. top: 0;
  20. transform: translateY(-100%);
  21. width: 1rem;
  22. height: 1rem;
  23. }
  24. .dialog:before {
  25. background: inherit;
  26. right: 0;
  27. }
  28. .dialog:after {
  29. background: #f4c1f3;
  30. right: 0;
  31. border-radius: 0 0 1rem 0;
  32. }
  33. .dialog--outline {
  34. z-index: 100;
  35. background: #fff;
  36. border: 1px solid #4fc1f3;
  37. }
  38. .dialog--outline:before, .dialog--outline:after {
  39. right: -1px;
  40. border: 1px solid transparent;
  41. border-right-color: #4fc1f3;
  42. }
  43. .dialog--outline:before {
  44. border-bottom-style: none;
  45. }
  46. .dialog--outline:after {
  47. border-bottom-color: #4fc1f3;
  48. }

Check the live code on pen

答案5

得分: -1

你可以通过直接在特定元素上使用clip-path来实现,或者添加一个:before、:after或子元素作为装饰元素来处理右上角。

英文:

You can do that either by using clip-path directly on that specific element or adding a :before or :after or a child element as a decoration element to deal with the right corner.

huangapple
  • 本文由 发表于 2023年7月13日 16:50:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76677537.html
匿名

发表评论

匿名网友

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

确定