HTML新手求助:如何增大项目中卡片的间距?
增大卡片间距的两种实用方法
你的卡片采用Flex容器(.gameshell)布局,当前间距过小是因为卡片的margin值设置偏小,或是没利用Flex布局自带的间距控制属性,下面是具体解决思路:
方法一:调整卡片的margin值
直接修改.card类的margin属性,把默认的5px改成更大数值,就能快速增大卡片间距:
.card { display: inline-block; width: 200px; height: 160px; border: 1px solid #EF9A9A; border-radius: 4px; margin: 15px; /* 增大上下左右的间距 */ text-decoration: none; }
如果需要单独控制水平/垂直间距,可以拆分写:
margin: 10px 20px; /* 上下间距10px,左右间距20px */
方法二:使用Flex容器的gap属性(推荐)
因为父容器.gameshell是Flex布局,直接给它添加gap属性,能精准控制卡片间的间距,还不会出现外边距折叠的问题:
.gameshell{ height:65vh; justify-content: center; display: flex; flex-direction: row; flex-basis: auto; margin: 5px 20px; align-items: center; flex-wrap:wrap; gap: 20px; /* 新增这一行,控制卡片之间的间距 */ }
这种方法更简洁可靠,是Flex布局专门用于子元素间距控制的属性。
修改后的完整CSS示例(采用gap方法)
* { box-sizing: border-box;} body { margin: 0; font-family: Arial, Helvetica, sans-serif;} .shell { height:65vh; justify-content: center; display: flex; flex-direction: row; flex-basis: auto; margin: 5px 20px; align-items: center; flex-wrap:wrap; } .gameshell{ height:65vh; justify-content: center; display: flex; flex-direction: row; flex-basis: auto; margin: 5px 20px; align-items: center; flex-wrap:wrap; gap: 20px; /* 新增gap属性 */ } .card { display: inline-block; width: 200px; height: 160px; border: 1px solid #EF9A9A; border-radius: 4px; margin: 5px; /* 保留作为卡片与容器的间距,也可根据需求调整 */ text-decoration: none; } .card-header { color: #D32F2F; text-align: center; font-size: 24px; font-weight: 600; border-bottom: 1px solid #EF9A9A; background-color: #FFEBEE; padding: 5px 10px; } .card-main { display: flex; flex-direction: column; justify-content: center; align-items: center; padding: 15px 0; } .material-icons { font-size: 36px!Important; color: #D32F2F; margin-bottom: 5px; } .main-description { color: #D32F2F; font-size: 24px; text-align: center; } .header { overflow: hidden; background-color: #FFEBEE; padding: 20px 10px; border-bottom: 1px solid #EF9A9A; border-radius: 4px; } .header a { float: left; color: #D32F2F; text-align: center; padding: 12px; text-decoration: none; font-size: 18px; line-height: 25px; border-radius: 4px; } .header a.logo { font-size: 25px; font-weight: bold; } .header a:hover { background-color: #dfd5d7; color: #942626; } .header a.active { background-color: #D32F2F; color: #FFEBEE; } .header-right { float: right;} @media screen and (max-width: 500px) { .header a { float: none; display: block; text-align: left; } .header-right { float: none; } }
内容的提问来源于stack exchange,提问作者user20151407
相关产品推荐
相关产品推荐

