Computer >> 컴퓨터 >  >> 프로그램 작성 >> CSS

CSS로 요소의 종횡비를 유지하는 방법은 무엇입니까?


CSS로 요소의 종횡비를 유지하기 위해 코드는 다음과 같습니다 -

예시

<!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;
   }
   .aspectContainer {
      background-color: rgb(191, 66, 216);
      position: relative;
      width: 100%;
      padding-top: 75%;
   }
   .aspectText {
      position: absolute;
      text-align: center;
      top: 0;
      left: 0;
      bottom: 0;
      right: 0;
      Text-align: center;
      font-size: 20px;
      color: white;
   }
</style>
</head>
<body>
<h1>Aspect ratio example</h1>
<div class="aspectContainer">
<div class="aspectText">4:3 Aspect ratio</div>
</div>
<h2>Resize the window to see 4:3 aspect ratio in action</h2>
</body>
</html>

출력

위의 코드는 다음과 같은 출력을 생성합니다 -

CSS로 요소의 종횡비를 유지하는 방법은 무엇입니까?

창 크기를 조정하면 요소가 4:3 -

비율로 축소됩니다.

CSS로 요소의 종횡비를 유지하는 방법은 무엇입니까?