加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 资源网站 > 资源 > 正文

html – grid-column-gap导致溢出

发布时间:2020-12-14 18:41:08 所属栏目:资源 来源:网络整理
导读:我刚测试了CSS显示:grid.它运作良好,但网格列间隙:10px;打破父容器.我下面代码中的绿色区域小于网格区域. box-sizing:border-box;显然没有效果. 如何使网格区域和父容器具有相等的宽度? /* ------------------------------- Resets --------------------
我刚测试了CSS显示:grid.它运作良好,但网格列间隙:10px;打破父容器.我下面代码中的绿色区域小于网格区域.

box-sizing:border-box;显然没有效果.

如何使网格区域和父容器具有相等的宽度?

/* ------------------------------- Resets --------------------------- */

html,body,div,span,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,address,cite,code,del,dfn,em,img,ins,kbd,q,samp,small,strong,sub,sup,var,b,i,hr,dl,dt,dd,ol,ul,li,fieldset,form,label,legend,table,caption,tbody,tfoot,thead,tr,th,td,article,aside,canvas,details,figure,figcaption,hgroup,menu,footer,header,nav,section,summary,time,mark,audio,video {
	margin: 0;
	padding: 0;
	border: 0;
}

article,figure img,video {
	display: block;
}

a img {border: 0;}


/* Selection colours (easy to forget) */

::selection 	 	{background: rgba(11,161,226,0.5);}
::-moz-selection 	{background: rgba(11,0.5);}
img::selection 		{background: transparent;}
img::-moz-selection	{background: transparent;}
body {-webkit-tap-highlight-color: rgba(11,0.5);}


html,body {
	height:100%;
}
#content {
	width:600px;
	height:100vh;
	margin:0 auto;
	box-sizing: border-box;
}
.gritts {
	height:100vh;
  	display: grid;
  	grid-template-rows:1fr 2fr 1fr;
  	grid-template-columns:15% 25% 35% 25%;
  	grid-auto-flow: dense;
  	grid-row-gap: 10px;
  	grid-column-gap: 10px;
  	background-color: rgb(0,230,0);
  	box-sizing: border-box;
  	align-items:stretch;
}

.box {
	box-sizing: border-box;
	color:white;
	font-size:30px;
	position:relative;
	overflow:auto;
}

.box-1 {
	background-color: rgba(0,50,240,0.7);
	grid-column:4/5;
  	grid-row:2/3;
  	z-index: 11;
}
.box-1 h1 {
	position:absolute;
	top:100px;
	right:100px;
}
.box-2 {
	background-color: rgba(0,120,0.7);
}

.box-3 {
	background-color: rgba(200,0.7);
}
.box-4 {
	background-color: rgba(80,80,0.7);
}
.box-5 {
	background-color: rgba(150,150,0.7);
}
.box-6 {
	background-color: rgba(100,160,0.7);
}
.box-7 {
	background-color: rgba(0,0.7);
}
.box-8 {
	background-color: rgba(200,0.7);
}
.box-9 {
	background-color: rgba(180,0.7);
	grid-column:2 / span 2;
  	grid-row:2/ span 2;
  	z-index: 10;
}
.box-10 {
	background-color: rgba(100,40,0.7);
}
.box-11 {
	background-color: rgba(0,250,0.7);
}
.box-12 {
	background-color: rgba(100,0.7);
}
<body id="pageUid-1" class="pagePid-0">

<header id="header"></header>

<main id="content">

	<section class="gritts">
		<div class="box box-1">
			<h1>1</h1>
		</div>
		<div class="box box-2"><h1>2</h1></div>
		<div class="box box-3"><h1>3</h1></div>
		<div class="box box-4"><h1>4</h1></div>
		<div class="box box-5"><h1>5</h1></div>
		<div class="box box-6"><h1>6</h1></div>
		<div class="box box-7"><h1>7</h1></div>
		<div class="box box-8"><h1>8</h1></div>
		<div class="box box-9">
			<h1>9</h1>
			<p>Lorem ipsum dolor sit amet,consectetur adipiscing elit. Phasellus quis lectus metus,at posuere neque. Sed pharetra nibh eget orci convallis at posuere leo convallis. Sed blandit augue vitae augue scelerisque bibendum. Vivamus sit amet libero turpis,non venenatis urna. In blandit,odio convallis suscipit venenatis,ante ipsum cursus augue.</p>

			<p>Et mollis nunc diam eget sapien. Nulla facilisi. Etiam feugiat imperdiet rhoncus. Sed suscipit bibendum enim,sed volutpat tortor malesuada non. Morbi fringilla dui non purus porttitor mattis. Suspendisse quis vulputate risus. Phasellus erat velit,sagittis sed varius volutpat,placerat nec urna. Nam eu metus vitae dolor fringilla feugiat. Nulla.</p>

			<p>Facilisi. Etiam enim metus,luctus in adipiscing at,consectetur quis sapien. Duis imperdiet egestas ligula,quis hendrerit ipsum ullamcorper et. Phasellus id tristique orci. Proin consequat mi at felis scelerisque ullamcorper. Etiam tempus,felis vel eleifend porta,velit nunc mattis urna,at ullamcorper erat diam dignissim ante. Pellentesque justo risus.</p>
			
			<p>Lorem ipsum dolor sit amet,at ullamcorper erat diam dignissim ante. Pellentesque justo risus.</p>

		</div>
	</section>

</main>

<footer id="footer"></footer>


</body>

解决方法

您的列设置为占据容器的100%宽度:
.gritts {
    display: grid;
    grid-template-columns: 15% 25% 35% 25%;
    grid-column-gap: 10px;
}

这为您提供了四列,三列排水沟.

每个排水沟得到10px,你得到100%30px,这会导致溢出.

box-sizing:border-box无效,因为它仅适用于width,height,min-width / height / max-width / height和flex-basis(source).

相反,尝试这样的事情:

grid-template-columns: 15% 25% calc(35% - 30px) 25%;

revised demo

或这个:

grid-template-columns: 15% 25% 1fr 25%;

revised demo

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读