
디자인 아이디어(scss 이하 여부와 관계없음)
1. 내부 요소의 수평/수직 중심 배치를 용이하게 하기 위해 전체적으로 플렉스 레이아웃을 사용합니다.
2. 사각형 자리 표시자를 사용합니다. -top:100은 %를 사용하므로 별도의 div를 사용하여 콘텐츠를 보관해야 하며 이름을 "item__content"로 지정했습니다.
3 콘텐츠 컨테이너 div가 상자를 채우도록 스타일을 설정했습니다. it: position:absolute;top:0; left:0;right:0;bottom:0;;
(추천 튜토리얼: CSS 입문 튜토리얼)
HTML code
<!-- a-grid是一个flex容器, 方便他的内容做"水平/垂直居中" -->
<div class="a-grid">
<!-- a-grid__item用来占位实现正方形 -->
<div class="a-grid__item">
<!-- item__content才是真正装内容的容器 -->
<div class="item__content">
内容...
</div>
</div>
</div>CSS code
중복되므로 공통 부분을 추출했습니다. 이름을 ".a-grid"로 지정합니다.
mixin은 $row(행 수), $column(열 수), $hasBorder(경계 여부)라는 4개의 매개 변수를 지원합니다. , $isSquare(각 블록이 정사각형인지 여부).
mixin은 내부적으로 :nth-child를 계산하고 결합하여 "전체적으로 외부 경계 없음" 효과를 달성합니다.
.a-grid {
display: flex;
flex-wrap: wrap;
width: 100%;
.a-grid__item {
text-align:center;
position:relative;
>.item__content {
display:flex
flex-flow: column;
align-items: center;
justify-content: center;
}
}
}
@mixin grid($row:3, $column:3, $hasBorder:false, $isSquare:true) {
@extend .a-grid;
.a-grid__item {
flex-basis: 100%/$column;
@if($isSquare) {
padding-bottom: 100%/$column;
height: 0;
}
>.item__content {
@if($isSquare) {
position:absolute;
top:0;left:0;right:0;bottom:0;
}
}
}
@for $index from 1 to (($row - 1) * $column + 1) {
.a-grid__item:nth-child(#{$index}) {
@if($hasBorder) {
border-bottom: 1px solid #eee;
}
}
}
@for $index from 1 to $column {
.a-grid__item:nth-child(#{$column}n + #{$index}) {
@if($hasBorder) {
border-right: 1px solid #eee;
}
}
}
}Use
// 生成一个 3行3列, 正方形格子的宫格
.a-grid-3-3 {
@include grid(3, 3, true);
}
// 生成一个 2行5列, 无边框宫格, 每个格子由内容决定高度
.a-grid-2-5 {
@include grid(2, 5, false, false);
}모두에게 알림: 원하는 경우 n x m 레이아웃을 만들려면 @include Grid(n, m)을 사용하세요. 마지막으로 n x m에 해당하는 dom 구조를 html에 추가하는 것을 잊지 마세요.
추천 관련 비디오 튜토리얼: css 비디오 튜토리얼
위 내용은 CSS에서 n-square 레이아웃을 구현하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!