Custom CSS Grid Border Using Image

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

Custom CSS Grid Border Using Image

问题

我正在为客户开发一个项目,使用 nextjs,并且设计要求我创建一个具有自定义边框的响应式 CSS 网格组件。我已经创建了包含所有内容的 CSS 网格,但是在添加设计所需的边框方面遇到了很多问题。

到目前为止,我已经尝试将边框作为背景的一部分,但在不同设备之间切换时出现了问题。我还尝试使用 ::after 伪元素,但没有取得进展。

我需要在 CSS 网格上实现以下效果的边框:

Custom CSS Grid Border Using Image

我应该如何实现这个效果?

英文:

I'm working on a project for a client using nextjs, and the design requires me to have a component with a custom border on a responsive CSS grid. I've made the CSS grid with all the content inside, however I'm having a lot of trouble adding the border that the design calls for.

So far I've tried making the border part of the background, but that gets screwy when I move between devices, I've also tried using an aftter pseudo element but I didn't get anywhere with that.

I need the border on the CSS grid to look like the image below:

Custom CSS Grid Border Using Image

How can I achieve this?

答案1

得分: 1

你可以更快地实现它,使用 border-image 属性。取一个 正方形图像形状,并放在 div 的边框上。上面链接的示例与此相关。看一下它。

或者

你可以尝试这个,不需要图片:-

.container{
   display: flex;
}

.container > .box{
   padding: 2rem 1rem;
   border: 1px solid black;
   position: relative;
}

.container > .box:nth-child(odd):before,
.container > .box:after
{
   content: '';
   display: block;
   width: 10px;
   height: 10px;
   border: 1px solid black;
   background-color: pink;
   position: absolute;
   z-index: 1;
}

.container > .box:before{
   top: -5px;
   left: -5px;
}

.container > .box:after{
   top: -5px;
   right: -5px;
}

.container > .box:nth-child(odd) > span:before,
.container > .box > span:after
{
content: '';
   display: block;
   width: 10px;
   height: 10px;
   border: 1px solid black;
   background-color: pink;
   position: absolute;
   z-index: 1;
}

.container > .box > span:before{
   bottom: -5px;
   left: -5px;
}

.container > .box > span:after{
   bottom: -5px;
   right: -5px;
}
<div class="container">
  <div class="box">
     <span>Lorem Ipsum</span>
  </div>
  <div class="box">
     <span>Ipsum Lorem</span>
  </div>
</div>
英文:

You can achieve it more quickly using border-image property. Take a square image shape and put in the border of the div. The attached linked above has a relatable example of it. Have a look at it.

OR

You can try this which doesn't require image:-

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

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

.container{
display: flex;
}
.container &gt; .box{
padding: 2rem 1rem;
border: 1px solid black;
position: relative;
}
.container &gt; .box:nth-child(odd):before,
.container &gt; .box:after
{
content: &#39;&#39;;
display: block;
width: 10px;
height: 10px;
border: 1px solid black;
background-color: pink;
position: absolute;
z-index: 1;
}
.container &gt; .box:before{
top: -5px;
left: -5px;
}
.container &gt; .box:after{
top: -5px;
right: -5px;
}
.container &gt; .box:nth-child(odd) &gt; span:before,
.container &gt; .box &gt; span:after
{
content: &#39;&#39;;
display: block;
width: 10px;
height: 10px;
border: 1px solid black;
background-color: pink;
position: absolute;
z-index: 1;
}
.container &gt; .box &gt; span:before{
bottom: -5px;
left: -5px;
}
.container &gt; .box &gt; span:after{
bottom: -5px;
right: -5px;
}

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

&lt;div class=&quot;container&quot;&gt;
&lt;div class=&quot;box&quot;&gt;
&lt;span&gt;Lorem Ipsum&lt;/span&gt;
&lt;/div&gt;
&lt;div class=&quot;box&quot;&gt;
&lt;span&gt;Ipsum Lorem&lt;/span&gt;
&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 1

如果不需要透明度,您可以尝试使用与 border-image 结合的渐变。

.box {
  display: grid;
  grid-template-columns: repeat(3,1fr);
  height: 300px;
  margin: 30px;
  /* 使用锥形渐变模拟边框 */
  background: 
   conic-gradient(from 90deg at 1px 1px,#0000 25%,#7a97fb 0)
   0 0/calc((100% - 1px)/3) calc((100% - 1px)/2)
}
.box div {
  position: relative;
}
.box div:before,
.box div:after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  /* 在角落创建具有 14px 大小的 4 个正方形 */
  border-image: 
   linear-gradient(#7a97fb 0 0) 50%/
    14px/7px; /* 宽度 / (宽度/2) */
}
.box div:after {
  /* 在上面的正方形之上创建 4 个具有 12px 大小的正方形,每边留出 1px 的边框 */
  border-image: 
   /* 这里的颜色必须与背景颜色匹配 */
   linear-gradient(#ebf0f3 0 0) 50%/
    12px/6px; 
}

body {
 background: #ebf0f3;
}
<div class="box">
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
</div>
英文:

If transparency is not needed you can try to use gradients combined with border-image

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

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

.box {
display: grid;
grid-template-columns: repeat(3,1fr);
height: 300px;
margin: 30px;
/* simulate the border using a grid made with conic-gradient*/
background: 
conic-gradient(from 90deg at 1px 1px,#0000 25%,#7a97fb 0)
0 0/calc((100% - 1px)/3) calc((100% - 1px)/2)
}
.box div {
position: relative;
}
.box div:before,
.box div:after {
content:&quot;&quot;;
position: absolute;
inset: 0;
pointer-events: none;
/* create 4 squares on the corners with 14px size */
border-image: 
linear-gradient(#7a97fb 0 0) 50%/
14px/7px; /* width / (width/2) */
}
.box div:after {
/* create 4 squares on the corners above the previous ones with 12px size
leaving 1px on each side for the border you need 
*/
border-image: 
/* the color here must match the background color */
linear-gradient(#ebf0f3 0 0) 50%/
12px/6px; 
}
body {
background: #ebf0f3;
}

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

&lt;div class=&quot;box&quot;&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

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

发表评论

匿名网友

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

确定