Reduce space between rows in CSS Grid

拟墨画扇 提交于 2019-12-02 16:35:08

问题


I'm wondering how to reduce the spacing between the rows? I've tried setting margins and paddings to 0, but nothing seems to be biting.

Destktop view on the left and mobile view on the right.

.content{
    margin: 0;
    padding: 0;  
    width: 100%;
    display: grid;
    grid-gap: 5px;
    grid-template-columns: repeat(36, 1fr); 
    justify-items: stretch;
    align-content: start;
    }

.one{
    grid-column: 1/37;
    width: 100%;
}

.two{
    grid-column: 1/11;

}
.three{
    grid-column: 12/24;
    justify-self: center;
    align-self: start;
    position: relative;
    bottom: 5px;
}
.four{
    grid-column: 25/37;
}

Here's a link to a test site: http://www.acm.no/test/valhall/


回答1:


The issue is coming from .ctrl. It's taking the space you need to decrease. It's position is relative and :

An element with position: relative; is positioned relative to its normal position.

Setting the top, right, bottom, and left properties of a relatively-positioned element will cause it to be adjusted away from its normal position. Other content will not be adjusted to fit into any gap left by the element.

You can add negative margin to fix this :

.ctrl {
    max-width: 30px;
    position: relative;
    bottom: 40px;
    color: black;
    text-align: left;
    padding-left: 1%;
    margin-bottom: -40px; /*added this line*/
}

Or you can use absolute position and adjust the code like this :

.one,.two,.three {
   position:relative;
}
.ctrl {
    max-width: 30px;
    position: absolute;
    bottom: 10px;
    color: black;
    text-align: left;
    padding-left: 1%;
}

You can read more about CSS positioning




回答2:


That gap doesn't exist between rows. The rows are close together.

The gap is the result of the content inside the grid items not filling 100% height.

Then, you have a second element which takes up space at the bottom.

<div class="ctrl">

You need to decide what to do with that element. Consider absolute positioning to remove it from the document flow. Then it won't occupy any space in the layout.

In any case, without that element (and with the optional addition of vertical-align), the video fills up the grid item.

(Also look into the object-fit property for getting videos / images to fill up their containers.)



来源:https://stackoverflow.com/questions/47285595/reduce-space-between-rows-in-css-grid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!