input 박스 안 오른쪽에 돋보기 이모지를 넣는 방법에 대해 알아보도록 하겠습니다. 핵심은 css의 position: relative와 absolute를 사용하는 것입니다.
<div class="search-container"> <input type="text" placeholder="Search..."> <span class="icon">🔍</span> </div>
input 박스와 icon을 담을 span 요소를 모두 같은 부모 요소인 .search-container에 넣어줍니다.
/* 검색창 컨테이너 */ .search-container { position: relative; /* 아이콘 배치를 위해 상대 위치 설정 */ width: 300px; /* 검색창 너비 */ } /* 입력 박스 스타일 */ .search-container input { width: 100%; /* 컨테이너의 전체 너비 사용 */ padding: 10px 40px 10px 10px; /* 아이콘 공간 확보 */ border: 1px solid #ccc; border-radius: 4px; font-size: 16px; outline: none; box-sizing: border-box; /* 패딩 포함한 크기 계산 */ } /* 돋보기 아이콘 스타일 */ .search-container .icon { position: absolute; top: 50%; /* 입력 박스의 세로 중앙 정렬 */ right: 10px; /* 입력 박스 오른쪽에서 10px 떨어짐 */ transform: translateY(-50%); /* 세로 중앙 정렬 보정 */ font-size: 18px; color: #555; pointer-events: none; /* 아이콘 클릭 방지 */ }
css에서 핵심적인 부분만 보충 설명을 드리겠습니다.
조금 더 명확한 이해를 위해 핵심 부분만 담은 예제를 하나 만들었습니다.
<!DOCTYPE html> <html> <head> <title>Page Title</title> <style> .parent { position: relative; border: 5px solid black; } .child1 { height: 200px; background-color: yellow; } .child2 { position: absolute; right: 10px; top: 50%; transform: translateY(-50%); width: 100px; height: 100px; background-color: lightblue; } </style> </head> <body> <div class="parent"> <div class="child1"> child1 요소 </div> <div class="child2"> child2 요소 </div> </div> </body> </html>