CSS를 활용하면 별도의 JavaScript 없이도 화면 크기에 따라 레이아웃이 자동으로 조정되는 반응형 고객 후기(테스티모니얼) 섹션을 만들 수 있습니다. 데스크톱에서는 프로필 사진이 왼쪽에 배치되고, 화면 너비가 좁아지면 미디어 쿼리에 의해 이미지가 중앙으로 이동하며 텍스트가 가운데 정렬됩니다.
아래는 완성된 전체 예제 코드입니다.
전체 예제 코드
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
body {
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}
.testimonialContainer {
border: 2px solid rgba(0, 0, 0, 0.363);
background-color: rgb(207, 235, 218);
border-radius: 5px;
padding: 16px;
margin: 16px 0;
}
.profilePic {
width: 100px;
height: 100px;
}
.testimonialContainer::after {
content: "";
clear: both;
display: table;
}
.testimonialContainer img {
float: left;
margin-right: 20px;
border-radius: 50%;
}
.testimonialContainer span {
font-size: 24px;
font-weight: 500;
margin-right: 15px;
color: purple;
}
.testimonialContainer p {
font-size: 18px;
font-style: oblique;
}
@media (max-width: 500px) {
.testimonialContainer {
text-align: center;
}
.testimonialContainer img {
margin: auto;
float: none;
display: block;
}
}
</style>
</head>
<body>
<h1>Responsive Testimonials Example</h1>
<div class="testimonialContainer">
<img class="profilePic" src="https://images.pexels.com/photos/614810/pexels-photo614810.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500"/>
<span>James Anderson</span>
<p>Shawn is a hard working and comitted individual</p>
</div>
<div class="testimonialContainer">
<img class="profilePic" src="https://images.pexels.com/photos/2128807/pexels-photo2128807.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500"/>
<span>Steve Boulder</span>
<p>Shawn is great at managing people</p>
</div>
</body>
</html>코드 핵심 설명
1. 카드 스타일링
.testimonialContainer 클래스는 각 후기 카드의 테두리, 배경색, 둥근 모서리(border-radius), 여백(padding·margin)을 담당합니다. 연한 초록색 배경과 은은한 검정 테두리를 조합해 부드러운 느낌을 줍니다.
2. 원형 프로필 이미지
border-radius: 50%를 적용하면 정사각형 이미지가 완벽한 원형으로 표시됩니다. 이미지에는 float: left를 지정해 이름과 후기 텍스트가 오른쪽에 자연스럽게 흐르도록 했습니다.
3. ::after 클리어픽스(Clearfix)
::after 가상 요소와 clear: both, display: table을 사용하면 float된 이미지 때문에 무너지는 부모 컨테이너의 높이 문제를 해결할 수 있습니다. float 레이아웃을 사용할 때 필수적인 기법입니다.
4. 미디어 쿼리로 반응형 구현
화면 너비가 500px 이하(주로 모바일 환경)로 줄어들면 다음과 같이 동작합니다.
- 카드 내부 텍스트가
text-align: center로 가운데 정렬됩니다. - 프로필 이미지의 float가 해제되고(
float: none)margin: auto와display: block덕분에 수평 중앙에 배치됩니다.
덕분에 작은 화면에서도 이미지와 텍스트가 세로로 깔끔하게 쌓여 가독성이 유지됩니다.
실행 결과
위 코드를 실행하면 두 개의 후기 카드가 나란히 세로로 배치되며, 브라우저 창 너비를 500px 아래로 줄이면 프로필 사진이 중앙으로 이동하고 텍스트도 함께 가운데 정렬되는 것을 확인할 수 있습니다.