• 회원가입
  • 로그인
  • 구글아이디로 로그인

[DOM_Style] JS - width 속성 - 요소너비 설정/반환 (= 요소가로길이 = width속성 = 위드스 속성)

목차
  1. width 예제 - <button> 요소의 너비 설정
  2. width 정의
  3. width 구문
  4. width 예제 - <div> 요소의 너비 변경
  5. width 예제 - <img> 요소의 너비/높이 변경
  6. width 예제 - <img> 요소의 너비 반환
  7. width 예제 - 클릭 시 이미지 크기 2배씩 증가 /  더블 클릭 시 초기화

 

width 예제 - <button> 요소의 너비 설정 

 

<button type="button" id="hz" onclick="homzzang()">클릭</button>


<script>

function homzzang() {

  document.getElementById("hz").style.width = "200px";

}

</script>

 

결과보기

 

width 정의

 

요소 너비 설정/반환.

 


 

1.

(블럭요소, 인라인블럭요소, position:absolute인 요소, position:fixed인 요소)에만 적용.

 

2.

요소의 width 범위 넘는 내용은 overflow 속성으로 제어.

 

3.

height 속성: 요소 높이 설정/반환.

 

4.

  • 기본값: auto
  • 반환값: 요소 너비 표시 문자열
  • CSS속성: width
  • CSS버전: CSS1


5.

모든 브라우저 지원. 

 

 

width 구문

 

요소 너비 반환 (★ 주의: 내부스타일로 지정된 경우에만 반환)

object.style.width 

 

요소 너비 설정

object.style.width = "속성값"

 


[속성값]

 

auto

브라우저가 너비를 설정. (기본값)

 

length

너비를 길이 단위로 정의. (px단위, cm단위 등)

 

%

부모 요소 너비의 % 

 

initial

이 속성의 기본값으로 설정

 

inherit

부모 요소의 속성값 상속

 

 

width 예제 - <div> 요소의 너비 변경

 

<style> 

#hz {

  width: 100px;

  height: 100px;

  background-color: tomato;

  color: white;

}

</style>


<button onclick="homzzang()">클릭</button>


<div id="hz">

  <h1>홈짱닷컴 Homzzang.com</h1>

</div>


<script>

function homzzang() {

  document.getElementById("hz").style.width = "500px";

}

</script>

 

결과보기

 

width 예제 - <img> 요소의 너비/높이 변경

 

<img id="hz" src="https://i.imgur.com/WfW5mBC.png" width="185" height="39">

<br>


<button type="button" onclick="homzzang()">클릭</button>


<script>

function homzzang() {

  document.getElementById("hz").style.height = "300px";

  document.getElementById("hz").style.width = "300px";

}

</script>

 

결과보기  (※ 비율계산기)

 

width 예제 - <img> 요소의 너비 반환

 

<img id="hz" src="https://i.imgur.com/WfW5mBC.png" style="width:185px;height:39px;">

<br>


<button type="button" onclick="homzzang()">클릭</button>


<script>

function homzzang() {

  alert(document.getElementById("hz").style.width);

}

</script>

 

결과보기 (※ 결과값: 185px)

PS. 주의: 태그 속성값 형태이거나 내부/외부 스타일로 지정된 경우엔 반환 X

 

 

width 예제 - 클릭 시 이미지 크기 2배씩 증가 /  더블 클릭 시 초기화

 

<img id="hz" src="https://i.imgur.com/PQNhCln.gif" height="50" width="50" onClick="imgSizeX2()" ondblclick="imgSizeX1()" title='클릭하면 2배 사이즈, 더블클릭하면 초기화'>


<script>

// 이미지 크기 초기화

function imgSizeX1() {

  var img = document.getElementById('hz');

  

  w = img.width;

  h = img.height;

 

  img.width = 50

  img.height = 50;

}

 

// 이미지 크기 2배 증가 

function imgSizeX2() {

  var img = document.getElementById('hz');

  

  w = img.width;

  h = img.height;

 

  img.width = w * 2;

  img.height = h * 2;

}

</script>

 

결과보기



분류 제목
Math JS - Math.log1p() 메서드 -
Math JS - Math.expm1() 메서드 -
Math JS - Math.clz32() 메서드 - 32비트 이진수에서 앞에 오는 0 개수 반환 (= Math.clz…
Math JS - Math.fround() 메서드 - 최근접 32비트 단정밀도 부동소수 반환 (= Math.froun…
Math JS - Math.sign() 메서드 - 숫자부호판별 (= Math.sign메서드 = 매스사인메서드) ※ 숫…
js JS - 모든 링크 주소를 특정 URL주소로 일괄 변경
js JS - 화면 리사이즈 시, 스크롤바 가운데로 이동 (= 화면크기 변경 시, 스크롤바 중앙에 위치시키기)
API_Fetch JS - Fetch API (= JavaScript로 파일 내용 가져오기 = fetch메서드=페치메서드)
API_MediaQueryList JS - window.MediaQueryList 객체 - 미디어쿼리 정보 저장 (= window.MediaQ…
Basic JS - 반복문 종류 ( for / for...of / for...in / while / do...while…
DOM_Element JS - remove() 메서드 - 요소 제거 (= remove메서드 = 리무브메서드) ※ 요소제거
Window_Object JS - window.jQuery 속성 - 제이쿼리 라이브러리 로드 여부 확인 (= 윈도우제이쿼리속성)
js JS - 특정 문자열 글자 포함 요소의 글자색 스타일 변경
67/67
목록
찾아주셔서 감사합니다. Since 2012