Css grid border between

Border inside Grid Layout

Just use the background color of the grid for border color, and the property for border width. You may use grid-template-columns to do the trick. create a parent container that will hold your four images. set a background color (desire color of the border). set the padding to 0 then do the trick arrange the images by grid-template-column: auto auto; then add gap to them grid-gap: 10px; (to show the background color of the container as grid).

Border inside Grid Layout

I have a CSS grid that represents the tic-tac -toe game. I wanted to put an border only inside the grid. Today, I proceed in this way:

:root < --border: 2px dashed #393939; --symbol-color: #FF7F5B; >.grid < height: 100%; display: grid; grid-template-columns: repeat(3, calc(100%/3)); grid-template-rows: repeat(3, calc(100%/3)); >.child < display: flex; align-items: center; align-content: center; color: var(--symbol-color); font-size: 2.5rem; >.child:nth-child(1), .child:nth-child(2), .child:nth-child(3) < border-bottom: var(--border); >.child:nth-child(7), .child:nth-child(8), .child:nth-child(9) < border-top: var(--border); >.child:nth-child(1), .child:nth-child(4), .child:nth-child(7) < border-right: var(--border); >.child:nth-child(3), .child:nth-child(6), .child:nth-child(9)

Result

This solution works but I find it unattractive. Do you have an idea to refactor this solution?

Since you want a stylized border (dashed, in this case), then your approach and the approach taken in the other answers appears to be useful.

However, if you decide to use a simple, solid line border, then the approach can be simplified. Just use the background color of the grid for border color, and the grid-gap property for border width.

Читайте также:  Nginx gzip type php

One thing you can use the nth-child selector in a better way like below instead of targeting one by one.

.child:nth-child(-n+3) < border-bottom: var(--border); >.child:nth-child(3n+1) < border-right: var(--border); >.child:nth-child(3n) < border-left: var(--border); >.child:nth-child(n+7)
:root < --border: 2px dashed #393939; --symbol-color: #FF7F5B; >.grid < height: 100%; display: grid; grid-template-columns: repeat(3, calc(100%/3)); grid-template-rows: repeat(3, calc(100%/3)); >.child < display: flex; align-items: center; align-content: center; color: var(--symbol-color); font-size: 2.5rem; >.child:nth-child(-n+3) < border-bottom: var(--border); >.child:nth-child(3n+1) < border-right: var(--border); >.child:nth-child(3n) < border-left: var(--border); >.child:nth-child(n+7)

You may consider this workaround.

You may use grid-template -columns to do the trick.

  • create a parent container that will hold your four images.
  • set a background color (desire color of the border).
  • set the padding to 0
  • then do the trick arrange the images by grid-template-column: auto
    auto;
  • then add gap to them grid-gap: 10px; (to show the background color of the container as grid).

please see code below for reference

Image here
Image Here
Image here
Image here

to help you visualize i create a sample code

You can reduce number of nth-child selector here from this answer.

body < margin: 0; >:root < --border: 2px dashed #393939; --symbol-color: #FF7F5B; >.grid < height: 100vh; display: grid; grid-template-columns: repeat(3, calc(100%/3)); grid-template-rows: repeat(3, calc(100%/3)); >.child < display: flex; align-items: center; justify-content: center; color: var(--symbol-color); font-size: 2.5rem; >.child:not(:nth-child(3n)) < border-right: var(--border); >.child:not(:nth-last-child(-n + 3))

CSS border property, Definition and Usage. The border property is a shorthand property for: border-width. border-style (required) border-color. If border-color is omitted, the color applied will be the color of the text. Show demo . Default value: medium none color.

How to add border radius to css grid element

https://codepen.io/siddagra/pen/vYBoJyM I want to add border-radius to each of the five purple+grey boxes, Ideally not adding any html styles as the arrangement of div elements within the boxes itself may be subject to change. Please help, I tried: first-child and second-child selectors but that did not work.

.ExpandedSetPiece:first-child < border-top-left-radius: 5px; border-top-right-radius: 5px; >.ExpandedSetPiece:last-child
.ExpandedSetPiece div:first-child < border-top-left-radius: 5px; border-top-right-radius: 5px; >.ExpandedSetPiece div:last-child

The term «child» is maybe a bit misleading. If you use :first-child on .ExpandedSetPiece it would mean the first of those groups.

First add the border to each Div container and then add the border-radius

Hope this is what you need.

Simply add the code below where —border-radius is your wanted radius.

:root < --border-radius: 10px; >.ExpandedArmor < border-radius: var(--border-radius) var(--border-radius) 0 0; >.ExpandedCells:nth-child(3) < border-radius: 0 0 var(--border-radius) var(--border-radius);

CSS Borders, CSS Border Style The border-style property specifies what kind of border to display. The following values are allowed: dotted - Defines a dotted border dashed - Defines a dashed border solid - Defines a solid border double - Defines a double border groove - Defines a 3D grooved border. The effect depends on the …

Horizontal border across entire row of CSS GRID

I need to use a grid layout but also need a horizontal line separating each row.

The only thing I've been able to find is applying a border to each cell, but this only works if there are enough cells to fill each row.

 Is there a way to fix the above so that the entire row has a border?

Add a grid-gap equal to the width of your border then consider gradient to achieve this:

 Another idea is to consider a pseudo-element that you add to the 1st,4th,7th .. (3n + 1) th element:
.wrapper < display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 100px); overflow:hidden; >.box < position:relative; padding: 1em; >.box:nth-child(3n + 1)::after

Imagine your table as a collection of cells (much like an excel spreadsheet). You can create a simple cell class that you append to each of your grid items to manipulate the cells without affecting the table data itself. Consider:

 
-- blank row --
First Column Only

Note that I only used a grid-row-gap. If you introduce a grid-gap, or a grid-column-gap, your lines will be broken at the column gaps (this may be the desired effect in some cases).

It is true that this is a more involved method of controlling the horizontal lines separating the grid and less "programmatic" and more micro-management-esque but, it does provide great control over introducing the lines into your grid.

The other answers were great options too! I just wanted to provide my two cents.

This can be achieved with a pseudo element, absolutely positioned. It will override the grid-gap. You will have to set it to a wide width, which is only a little hacky, then set the overflow on the container to hidden.

body < margin:0;padding:0; >.products < display:grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); grid-gap:20px; overflow:hidden; border-bottom:1px solid black; >.card-product < position:relative; text-align:center; >.card-product:after

How to add column borders in CSS Grid?, How do I add border-left or border-right in between each column? I tried to add a border left or right but it doesn't seem to help in this case, as the number of the column depends on the size of the screen, the bigger the screen, the more column it has. Here is the working jsfiddle. Below is the output that I get in …

Источник

Border inside Grid Layout

One thing you can use the nth-child selector in a better way like below instead of targeting one by one.

.child:nth-child(-n+3) < border-bottom: var(--border); >.child:nth-child(3n+1) < border-right: var(--border); >.child:nth-child(3n) < border-left: var(--border); >.child:nth-child(n+7)
:root < --border: 2px dashed #393939; --symbol-color: #FF7F5B; >.grid < height: 100%; display: grid; grid-template-columns: repeat(3, calc(100%/3)); grid-template-rows: repeat(3, calc(100%/3)); >.child < display: flex; align-items: center; align-content: center; color: var(--symbol-color); font-size: 2.5rem; >.child:nth-child(-n+3) < border-bottom: var(--border); >.child:nth-child(3n+1) < border-right: var(--border); >.child:nth-child(3n) < border-left: var(--border); >.child:nth-child(n+7)

Since you want a stylized border (dashed, in this case), then your approach and the approach taken in the other answers appears to be useful.

However, if you decide to use a simple, solid line border, then the approach can be simplified. Just use the background color of the grid for border color, and the grid-gap property for border width.

You may consider this workaround.

You may use grid-template-columns to do the trick.

  • create a parent container that will hold your four images.
  • set a background color (desire color of the border).
  • set the padding to 0
  • then do the trick arrange the images by grid-template-column: auto
    auto;
  • then add gap to them grid-gap: 10px; (to show the background color of the container as grid).

please see code below for reference

 
Image here
Image Here
Image here
Image here

to help you visualize i create a sample code

Источник

CSS grid как задать рамку вокруг ячеек

При использовании простой таблицы (table) рамка вокруг таблицы задается просто:

В случае css grid у меня возникли затруднения с рамкой вокруг ячеек. Я хочу чтобы рамка вокруг ячеек проходила ровно посередине grid-gap (примерно как зеленые линии)

Если задавать у элемента grid border, то чтобы он проходил посередине нужно задавать отрицательный margin (и не забывать убирать его по краям).

При этом почему то между border остается промежуток.

Как задать border в 1 пиксель посередине между элементами grid ?

body < box-sizing: border-box; --gap_size : 40px; --gap_size2 : -20px; >.wrapper < display: grid; grid-template-columns: repeat(3,1fr); grid-auto-rows: 200px; grid-gap: var(--gap_size); position: relative; align-items : center; margin : 50px; >.box2 < align-self : stretch; background:lightgrey; position : relative; >.flex < display: flex; justify-content: center; /*Центрирование по горизонтали*/ align-items: center; /*Центрирование по вертикали */ height : 100%; text-align : center; border : 1px solid green; margin : 0 var(--gap_size2 ); >div
 
One
Two является центром еще предложение
Этот блок

Ответы (2 шт):

Как делается: все grid item должны занимать полное пространство своего блока.

Контейнеру grid задаются свойства: background и grid-gap

В результате background контейнера выглядывает через grid-gap и получаются рамки между ячейками.

введите сюда описание изображения

#grid < display: grid; box-sizing: border-box; width: 100vw; height: 100vh; grid-template-columns: [start] minmax(auto, min-content) [center] auto [end] minmax(auto, min-content); grid-template-rows: [header] 2em [middle] auto [footer] 2em; padding: 0; grid-gap: 10px; /* grid-row-gap: 1px; */ background-color: red; >.start < grid-column-start: start; >.center < grid-column-start: center; >.end < grid-column-start: end; >.header < grid-row-start: header; >.middle < grid-row-start: middle; >.footer < grid-row-start: footer; >/* extra styles */ .scroll < overflow: auto; >.spacer < flex: 1; >.panel, .toolbar < background-color: #ccc; >.toolbar < display:flex; flex-direction: row; align-items: stretch; >.toolbar button < background-color: #ccc; border: 0px solid black; >.toolbar button:hover < background-color: #888; >.content < background-color: white; >html, body < font-family: sans-serif; padding:0; margin:0; >.start, .end < min-width: 150px; >.closed
   -->  
cool editor
This left side panel hasreallywidetextthattakesupalotofspace and a min width of 150px. Closing this panel will set display to none;
this is the content that we are editing
selected item
the right side panel
more actions

Еще один вариант добавить box-shadow:

box-shadow: 0px 0px 0px 4px blue; 

Если четвертый параметр задать как половину gap - то будут сплошные рамки.

Источник

Оцените статью