2017-10-18 24 views
0

아래의 예를 참조하십시오. 나는 CSS 격자를 배우고있다. 모든 목적을 단순하게 유지하고 불필요하게 하위 요소에 대한 고유 한 레이아웃 세부 정보를 지정할 필요가 없으므로 솔루션은이 패턴을 준수해야합니다.간단한 CSS 레이아웃 유지

을 감안할 때 다음

function toggle(t) { 
 
    document.querySelectorAll('div').forEach(el => 
 
    el.classList[0] === t.classList[0] ? 
 
    el.classList.toggle('selected') : 
 
    el.classList.remove('selected')) 
 
}
:root, 
 
html, 
 
body, 
 
main { 
 
    margin: 0; 
 
    padding: 0; 
 
    font-family: sans-serif; 
 
    height: 100%; 
 
} 
 

 
main { 
 
    border: solid 3pt white; 
 
    display: grid; 
 
    grid-template-columns: 1fr 1fr 1fr 1fr; 
 
    grid-template-rows: 1fr 1fr 1fr 1fr; 
 
} 
 

 
div { 
 
    grid-area: span 2/span 2; 
 
    display: flex; 
 
    justify-content: center; 
 
    align-items: center; 
 
    cursor: pointer; 
 
    font-size: 5em; 
 
    font-weight: bold; 
 
    color: white; 
 
    background: grey; 
 
} 
 

 
.one { 
 
    background: red 
 
} 
 

 
.two { 
 
    background: green 
 
} 
 

 
.three { 
 
    background: blue 
 
} 
 

 
.selected { 
 
    width: 150%; 
 
    height: 150%; 
 
    z-index: 2; 
 
}
<main> 
 
    <div class=one onclick="toggle(event.target)">one</div> 
 
    <div class=two onclick="toggle(event.target)">two</div> 
 
    <div class=three onclick="toggle(event.target)">three</div> 
 
    <div class=four onclick="toggle(event.target)">four</div> 
 
</main>

https://jsfiddle.net/robbie_at_harvard/a3ouq711/1/

지역 둘, 셋, 넷은 왼쪽 상단에서 항상 중앙으로 확장 대신하게하는 조정하십시오 모서리? 다시 말하지만, div.class 사양과 관련된 규칙보다는 일반적인 규칙을 사용하는 것이 좋습니다.

두 번째 질문 : 2x2 하위 요소의 4x4 레이아웃 대신 하나의 요소를 클릭하여 3x3으로 확장하고 다른 요소를 반대 구석의 1x2, 2x1 및 1x1로 축소하려는 경우 두 번째 질문을 생성하는 데 필요한 사항 해결책?

+0

사용 CSS 규모를해야합니다 모든 경우에 생각하고 신장? https://stackoverflow.com/questions/28726430/css-how-to-scale-an-image-from-the-center-instead-of-top-left – evolutionxbox

+0

거의 작동합니다 .. 또한 오프셋도 가능합니다. 완전한 해결책을 제공하기 위해, 오프셋은 선택된 클래스와 위치 클래스 모두에 의존해야합니다. 위치 클래스를 사용해야한다면 괜찮은 일이지만,이 간단한 예제가 적어도 필요하지는 않을 것이라고 기대하고있었습니다. –

답변

2

transform:scale(1.5)을 사용하고 transform-origin으로 지정할 수 있습니다.

그것은 2 × 2 사용

main div:nth-child(1){transform-origin: top left;} 
main div:nth-child(2){transform-origin: top right;} 
main div:nth-child(3){transform-origin: bottom left;} 
main div:nth-child(4){transform-origin: bottom right;} 

.selected { 
    transform:scale(1.5); 
    z-index: 2; 
} 

function toggle(t) { 
 
    document.querySelectorAll('div').forEach(el => 
 
    el.classList[0] === t.classList[0] ? 
 
    el.classList.toggle('selected') : 
 
    el.classList.remove('selected')) 
 
}
:root, 
 
html, 
 
body, 
 
main { 
 
    margin: 0; 
 
    padding: 0; 
 
    font-family: sans-serif; 
 
    height: 100%; 
 
} 
 

 
main { 
 
    border: solid 3pt white; 
 
    display: grid; 
 
    grid-template-columns: 1fr 1fr 1fr 1fr; 
 
    grid-template-rows: 1fr 1fr 1fr 1fr; 
 
} 
 

 
div { 
 
    grid-area: span 2/span 2; 
 
    display: flex; 
 
    justify-content: center; 
 
    align-items: center; 
 
    cursor: pointer; 
 
    font-size: 5em; 
 
    font-weight: bold; 
 
    color: white; 
 
    background: grey; 
 
} 
 

 
.one { 
 
    background: red 
 
} 
 

 
.two { 
 
    background: green 
 
} 
 

 
.three { 
 
    background: blue 
 
} 
 

 
main div:nth-child(1) { 
 
    transform-origin: top left; 
 
} 
 

 
main div:nth-child(2) { 
 
    transform-origin: top right; 
 
} 
 

 
main div:nth-child(3) { 
 
    transform-origin: bottom left; 
 
} 
 

 
main div:nth-child(4) { 
 
    transform-origin: bottom right; 
 
} 
 

 
.selected { 
 
    transform: scale(1.5); 
 
    z-index: 2; 
 
}
<main> 
 
    <div class=one onclick="toggle(event.target)">one</div> 
 
    <div class=two onclick="toggle(event.target)">two</div> 
 
    <div class=three onclick="toggle(event.target)">three</div> 
 
    <div class=four onclick="toggle(event.target)">four</div> 
 
</main>

2
를 들어

(전 각 요소을 대상으로 :nth-child를 사용 )를 그리드에 따라 특정 CSS 규칙을 필요로

4 블록이 다른 방법으로 이동해야하기 때문에 일반적인 솔루션을 만드는 것은 어렵다고 생각합니다. 그런데 여기에 몇 가지 CSS가 변경되는 솔루션이 있습니다.

난 당신이 일반적인 일 원하는지하지만 난 당신이 어떤 (색상 및 컨텐츠 등) 특이성 대신 폭의

function toggle(t) { 
 
    document.querySelectorAll('div').forEach(el => 
 
    el.classList[0] === t.classList[0] ? 
 
    el.classList.toggle('selected') : 
 
    el.classList.remove('selected')) 
 
}
:root, 
 
html, 
 
body, 
 
main { 
 
    margin: 0; 
 
    padding: 0; 
 
    font-family: sans-serif; 
 
    height: 100%; 
 
} 
 

 
main { 
 
    position:relative; 
 
    border: solid 3pt white; 
 
    display: grid; 
 
    grid-template-columns: 1fr 1fr 1fr 1fr; 
 
    grid-template-rows: 1fr 1fr 1fr 1fr; 
 
} 
 

 
div { 
 
    grid-area: span 2/span 2; 
 
    display: flex; 
 
    justify-content: center; 
 
    align-items: center; 
 
    cursor: pointer; 
 
    font-size: 5em; 
 
    font-weight: bold; 
 
    color: white; 
 
    background: grey; 
 
    transition:0.5s; 
 
} 
 

 
.one { 
 
    background: red; 
 
} 
 

 
.two { 
 
    background: green; 
 
    right:25%; 
 
} 
 

 
.three { 
 
    background: blue; 
 
    bottom:25%; 
 
} 
 
.four { 
 
    bottom:25%; 
 
    right:25%; 
 
} 
 
.selected { 
 
    position:relative; 
 
    width:150%; 
 
    height:150%; 
 
}
<main> 
 
    <div class=one onclick="toggle(event.target)">one</div> 
 
    <div class=two onclick="toggle(event.target)">two</div> 
 
    <div class=three onclick="toggle(event.target)">three</div> 
 
    <div class=four onclick="toggle(event.target)">four</div> 
 
</main>