각 버튼이 고유 한 너비 (현재 텍스트 너비 기준)를 유지하고 버튼이 수평으로 더 이상 공간이 없을 때 다음 행에 자동으로 배치되기를 원합니다.
을 사용 grid-auto-flow: column
하면 버튼이 너비를 유지할 수 있지만 감싸지는 않습니다. 사용 grid-template-columns: repeat(auto-fill, minmax(1rem, 15rem))
하면 포장 할 수 있지만 개별 너비가 손실됩니다.
코드 샌드 박스 : https://codesandbox.io/s/jovial-shannon-btp3x?file=/src/styles.css
.grid {
margin-top: 1rem;
margin-left: 0.1rem;
display: grid;
grid-template-columns: repeat(auto-fill, minmax(1rem, 11.3rem));
max-width: 100%;
grid-row-gap: 2.5rem;
grid-column-gap: 2.5rem;
}
.button {
padding: 1rem;
outline: 1px solid rgba(110, 110, 110, 0.34);
color: white;
background-color: #1d1f1f;
font-family: Calibri;
font-size: 1.4rem;
font-weight: bold;
}
<div class="grid">
<button class="button">
<span class="button-text">
ab
</span>
</button>
<button class="button">
<span class="button-text">
abcd
</span>
</button>
<button class="button">
<span class="button-text">
abcdefg
</span>
</button>
<button class="button">
<span class="button-text">
abcdefghij
</span>
</button>
<button class="button">
<span class="button-text">
abcdefghijklm
</span>
</button>
<button class="button">
<span class="button-text">
abcdefghijklmnop
</span>
</button>
</div>
각 버튼이 고유 한 너비 (현재 텍스트 너비 기준)를 유지하고 버튼이 수평으로 더 이상 공간이 없을 때 다음 행에 자동으로 배치되기를 원합니다.
사용
grid-template-columns: repeat(auto-fill, minmax(1rem, 15rem))
하면 포장 할 수 있지만 개별 너비가 손실됩니다.
글쎄, 버튼은 열에 포함되어 있습니다. 행과 같은 열은 그리드 컨테이너에서 고정 된 길이를 갖습니다.
실제로 요청하는 것은 셀의 다양한 너비에 맞게 조정되는 유연한 열 길이 인 것으로 보입니다. 이것은 그리드가 아니므로 그리드 레이아웃에서는 불가능합니다.
다음은 제가 말하는 내용의 예입니다.
그리드는 고정 너비 열과 고정 높이 행이 교차하는 행렬입니다. 트랙 내에서 버튼의 너비를 조정할 수 있지만이 경우 버튼 사이에보기 흉한 간격이 생길뿐입니다.
을 사용
grid-auto-flow: column
하면 버튼이 너비를 유지할 수 있지만 감싸지는 않습니다.
예, grid-auto-flow: column
정의 된 행이없고 모든 버튼이 단일 행에 존재하고 자체 열이 있기 때문입니다 .
두 번째 요점은 auto-fill
또는 없이 래핑 할 이유가 없으며 가능 auto-fit
하더라도 이전 섹션에서 설명한 것과 동일한 문제가 발생합니다.
열 및 행 트랙에 의해 제한되지 않는 flexbox를 고려하십시오. gap
플렉스 를 사용하는 속성은 현재 Firefox에서만 지원되므로 거터로가는 길을 해킹해야 합니다.
.grid {
display: flex;
flex-wrap: wrap;
row-gap: 2.5rem; /* FF only */
column-gap: 2.5rem; /* FF only */
}
.button {
max-width: 11.3rem;
min-width: 1rem;
padding: 1rem;
outline: 1px solid rgba(110, 110, 110, 0.34);
color: white;
background-color: #1d1f1f;
font-family: Calibri;
font-size: 1.4rem;
font-weight: bold;
}
<div class="grid">
<button class="button">
<span class="button-text">
ab
</span>
</button>
<button class="button">
<span class="button-text">
abcd
</span>
</button>
<button class="button">
<span class="button-text">
abcdefg
</span>
</button>
<button class="button">
<span class="button-text">
abcdefghij
</span>
</button>
<button class="button">
<span class="button-text">
abcdefghijklm
</span>
</button>
<button class="button">
<span class="button-text">
abcdefghijklmnop
</span>
</button>
</div>
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다