수용된 솔루션은 훌륭하게 작동하지만 IMO는 왜 작동하는지에 대한 설명이 부족합니다. 아래 예제는 기본 사항으로 요약되어 있으며 중요한 CSS를 관련없는 스타일링 CSS와 분리합니다. 보너스로 CSS 포지셔닝 작동 방식에 대한 자세한 설명도 포함했습니다.
TLDR; 코드 만 원하는 경우 아래로 스크롤하여 결과로 이동하십시오 .
문제
두 개의 개별 형제 요소가 있으며 목표는 두 번째 요소 ( idof infoi)를 배치하는 것이므로 이전 요소 ( classof navi)에 표시됩니다. HTML 구조는 변경할 수 없습니다.
제안 된 해결책
원하는 결과를 얻기 위해 두 번째 요소를 이동하거나 배치합니다. 두 번째 요소 #infoi는 첫 번째 요소 내에 표시됩니다 .navi. 구체적 #infoi으로의 오른쪽 상단 모서리에 배치 하려고 합니다 .navi.
CSS 위치 필수 지식
CSS에는 요소 배치를위한 여러 속성이 있습니다. 기본적으로 모든 요소는 position: static입니다. 즉, 요소는 HTML 구조에서 순서에 따라 배치되지만 거의 예외는 없습니다.
다른 position값은 relative, absolute, sticky,와 fixed. 요소 position를 다른 값 중 하나로 설정하면 다음 네 가지 속성 조합을 사용하여 요소를 배치 할 수 있습니다.
즉,를 설정 하면 페이지 상단에서 요소를 100 픽셀 떨어진 위치에 position: absolute추가 할 수 있습니다 top: 100px. 반대로, 우리가 설정 bottom: 100px하면 요소는 페이지 하단에서 100 픽셀에 위치합니다.
여기에 많은 CSS 초보자가 잃어버린 곳이 있습니다 -position: absolute참조 프레임이 있습니다. 위의 예에서 참조 프레임은body요소입니다. position: absolutewithtop: 100px는 요소가 요소 상단에서 100 픽셀 떨어진 곳에 있음을의미합니다body.
position부모 요소의 값을 이외의 값position: static 으로 설정하여 위치 참조 프레임 또는 위치 컨텍스트를 변경할 수 있습니다 . 즉, 부모 요소를 제공하여 새 위치 컨텍스트를 만들 수 있습니다.
position: relative;
position: absolute;
position: sticky;
position: fixed;
예를 들어, <div class="parent">요소가 제공 position: relative되면 모든 하위 요소 <div class="parent">가 위치 컨텍스트로 사용 됩니다. 자식 요소에 position: absolute및 이 지정된 top: 100px경우 요소 는 이제 위치 컨텍스트 <div class="parent">이므로 요소 의 상단에서 100 픽셀 떨어진 <div class="parent">위치에 배치됩니다.
알아야 할 다른 요소 는 스택 순서 또는 요소가 z 방향으로 쌓이는 방식입니다. 여기서 반드시 알아야 할 것은 요소의 스택 순서는 기본적으로 HTML 구조에서 순서의 역순으로 정의됩니다. 다음 예제를 고려하십시오.
<body>
<div>Bottom</div>
<div>Top</div>
</body>
이 예에서 두 <div>요소가 페이지의 동일한 위치에 있으면 요소가 해당 <div>Top</div>요소를 덮습니다 <div>Bottom</div>. 때문에 <div>Top</div>뒤에 오는 <div>Bottom</div>HTML 구조에 더 높은 스택 순서가 있습니다.
div {
position: absolute;
width: 50%;
height: 50%;
}
#bottom {
top: 0;
left: 0;
background-color: blue;
}
#top {
top: 25%;
left: 25%;
background-color: red;
}
<div id="bottom">Bottom</div>
<div id="top">Top</div>
CSS를 사용하여 z-index또는 order속성을 사용하여 스택 순서를 변경할 수 있습니다 .
요소의 자연적인 HTML 구조는 우리가 표시하고자하는 요소가 top다른 요소 뒤에 오는 것을 의미하므로이 문제에서 쌓인 순서를 무시할 수 있습니다 .
따라서 당면한 문제로 돌아가서-이 문제를 해결하기 위해 위치 컨텍스트를 사용합니다.
해결책
위에서 언급했듯이 우리의 목표는 #infoi요소가 .navi요소 내에 나타나도록 요소 를 배치하는 것 입니다. 이를 위해, .naviand #infoi요소를 새로운 요소로 감싸서 <div class="wrapper">새로운 위치 컨텍스트를 만들 수 있습니다.
<div class="wrapper">
<div class="navi"></div>
<div id="infoi"></div>
</div>
그런 다음 제공함으로써 새로운 위치 문맥 만들 .wrapper을 position: relative.
.wrapper {
position: relative;
}
이 새로운 위치 문맥, 우리는 위치를 지정할 수 있습니다 #infoi내 .wrapper. 먼저, 줄 #infoi을 position: absolute위치로 우리를 허용, #infoi절대적으로 .wrapper.
그런 다음 요소 를 추가 top: 0하여 오른쪽 상단 모서리에 배치하십시오. 요소가 위치 컨텍스트로 사용 되므로 요소의 오른쪽 상단에 있습니다 .right: 0#infoi#infoi.wrapper.wrapper
#infoi {
position: absolute;
top: 0;
right: 0;
}
있으므로 .wrapper단순히하는 컨테이너 .navi의 위치, #infoi오른쪽 상단의 .wrapper오른쪽 상단에 위치되는 효과를 제공한다 .navi.
그리고 우리는 그것을 가지고 있습니다. #infoi이제 오른쪽 상단 모서리에있는 것 같습니다 .navi.
결과
아래 예제는 기본 사항으로 요약되어 있으며 최소한의 스타일이 포함되어 있습니다.
/*
* position: relative gives a new position context
*/
.wrapper {
position: relative;
}
/*
* The .navi properties are for styling only
* These properties can be changed or removed
*/
.navi {
background-color: #eaeaea;
height: 40px;
}
/*
* Position the #infoi element in the top-right
* of the .wrapper element
*/
#infoi {
position: absolute;
top: 0;
right: 0;
/*
* Styling only, the below can be changed or removed
* depending on your use case
*/
height: 20px;
padding: 10px 10px;
}
<div class="wrapper">
<div class="navi"></div>
<div id="infoi">
<img src="http://via.placeholder.com/32x20/000000/ffffff?text=?" height="20" width="32"/>
</div>
</div>
대체 (격자) 솔루션
CSS Grid를 사용 하여 가장 오른쪽에있는 .navi요소와 요소 를 배치하는 대체 솔루션이 #infoi있습니다. 자세한 grid속성을 사용하여 최대한 명확하게 만들었습니다.
:root {
--columns: 12;
}
/*
* Setup the wrapper as a Grid element, with 12 columns, 1 row
*/
.wrapper {
display: grid;
grid-template-columns: repeat(var(--columns), 1fr);
grid-template-rows: 40px;
}
/*
* Position the .navi element to span all columns
*/
.navi {
grid-column-start: 1;
grid-column-end: span var(--columns);
grid-row-start: 1;
grid-row-end: 2;
/*
* Styling only, the below can be changed or removed
* depending on your use case
*/
background-color: #eaeaea;
}
/*
* Position the #infoi element in the last column, and center it
*/
#infoi {
grid-column-start: var(--columns);
grid-column-end: span 1;
grid-row-start: 1;
place-self: center;
}
<div class="wrapper">
<div class="navi"></div>
<div id="infoi">
<img src="http://via.placeholder.com/32x20/000000/ffffff?text=?" height="20" width="32"/>
</div>
</div>
대체 (래퍼 없음) 솔루션
HTML을 편집 할 수없는 경우 래퍼 요소를 추가 할 수 없으므로 원하는 효과를 얻을 수 있습니다.
요소 를 사용 position: absolute하는 대신을 사용 #infoi합니다 position: relative. 이를 통해 #infoi요소 아래의 기본 위치에서 요소 를 재배치 할 수 있습니다 .navi. 함께 position: relative우리는 부정적인를 사용할 수있는 top기본 위치, 그리고에서 위로 이동하려면 값을 left값 100%사용, 마이너스 몇 픽셀 left: calc(100% - 52px), 오른쪽 근처에 위치합니다.
/*
* The .navi properties are for styling only
* These properties can be changed or removed
*/
.navi {
background-color: #eaeaea;
height: 40px;
width: 100%;
}
/*
* Position the #infoi element in the top-right
* of the .wrapper element
*/
#infoi {
position: relative;
display: inline-block;
top: -40px;
left: calc(100% - 52px);
/*
* Styling only, the below can be changed or removed
* depending on your use case
*/
height: 20px;
padding: 10px 10px;
}
<div class="navi"></div>
<div id="infoi">
<img src="http://via.placeholder.com/32x20/000000/ffffff?text=?" height="20" width="32"/>
</div>
overflow: hidden, 사용overflow: visible대신.