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

JavaScript로 요소의 콘텐츠 수직 정렬을 설정하는 방법은 무엇입니까?


수직 정렬을 설정하려면 verticalAlign 속성을 사용하십시오. 특정 텍스트를 맨 위로 이동하려면 맨 위에 설정하세요.

예시

다음 코드를 실행하여 JavaScript를 사용하여 요소에 있는 콘텐츠의 수직 정렬을 반환할 수 있습니다.

<!DOCTYPE html>
<html>
   <head>
      <style>
         table {
            border: 2px solid blue;
            width: 200px;
            height: 200px;
         }
      </style>
   </head>
   <body>
      <table>
         <tr>
            <td id = "myTable">Demo Text1</td>
         </tr>
         <tr>
            <td>Demo Text2</td>
         </tr>
         <tr>
            <td>Demo Text3</td>
         </tr>
      </table>
      <br>
      <button type = "button" onclick = "display()"> Set top position </button>
      <script>
         function display() {
            document.getElementById("myTable").style.verticalAlign="top";
         }
      </script>
   </body>
</html>