Chiều rộng thực tế trong CSS

Tóm lược. trong hướng dẫn này, bạn sẽ học cách lấy kích thước tính toán hiện tại của một phần tử, bao gồm chiều rộng và chiều cao

Hình ảnh sau đây hiển thị mô hình hộp CSS bao gồm phần tử khối có nội dung, phần đệm, đường viền và lề

Chiều rộng thực tế trong CSS
Chiều rộng thực tế trong CSS

Để lấy chiều rộng và chiều cao của phần tử bao gồm phần đệm và đường viền, bạn sử dụng thuộc tính

let box = document.querySelector('.box'); let width = box.offsetWidth; let height = box.offsetHeight;

Code language: JavaScript (javascript)
7 và

html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Getting the Width and Height of an Elementtitle> head> <body> <div class="box" style="width:100px;height:150px;border:solid 1px #000;padding:10px">div> <script> let box = document.querySelector('.box'); let width = box.offsetWidth; let height = box.offsetHeight; console.log({ width, height }); script> body> html>

Code language: HTML, XML (xml)
0 của phần tử

let box = document.querySelector('.box'); let width = box.offsetWidth; let height = box.offsetHeight;

Code language: JavaScript (javascript)

Hình ảnh sau đây minh họa

let box = document.querySelector('.box'); let width = box.offsetWidth; let height = box.offsetHeight;

Code language: JavaScript (javascript)
7 và

html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Getting the Width and Height of an Elementtitle> head> <body> <div class="box" style="width:100px;height:150px;border:solid 1px #000;padding:10px">div> <script> let box = document.querySelector('.box'); let width = box.offsetWidth; let height = box.offsetHeight; console.log({ width, height }); script> body> html>

Code language: HTML, XML (xml)
0 của một phần tử

Chiều rộng thực tế trong CSS
Chiều rộng thực tế trong CSS

Xem ví dụ sau

html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Getting the Width and Height of an Elementtitle> head> <body> <div class="box" style="width:100px;height:150px;border:solid 1px #000;padding:10px">div> <script> let box = document.querySelector('.box'); let width = box.offsetWidth; let height = box.offsetHeight; console.log({ width, height }); script> body> html>

Code language: HTML, XML (xml)

đầu ra

{width: 122, height: 172}

Code language: CSS (css)

trong ví dụ này

  • Chiều rộng là 100px
  • Đường viền là 1px ở mỗi bên, vì vậy 2px cho cả hai
  • Phần đệm 10px ở mỗi bên, vì vậy 20px cho cả hai

Do đó, tổng chiều rộng 12px. Tương tự, chiều cao là 172px

Để lấy chiều rộng và chiều cao của phần tử dưới dạng dấu phẩy động sau khi chuyển đổi CSS, bạn sử dụng phương thức

html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Getting the Width and Height of an Elementtitle> head> <body> <div class="box" style="width:100px;height:150px;border:solid 1px #000;padding:10px">div> <script> let box = document.querySelector('.box'); let width = box.offsetWidth; let height = box.offsetHeight; console.log({ width, height }); script> body> html>

Code language: HTML, XML (xml)
3 của phần tử DOM. Ví dụ

html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Getting the Width and Height of an Elementtitle> head> <body> <div class="box" style="width:100px;height:150px;border:solid 1px #000;padding:10px">div> <script> let box = document.querySelector('.box'); let width = box.offsetWidth; let height = box.offsetHeight; console.log({ width, height }); const domRect = box.getBoundingClientRect(); console.log(domRect); script> body> html>

Code language: HTML, XML (xml)

đầu ra

{width: 122, height: 172} DOMRect {x: 7.997685432434082, y: 7.997685432434082, width: 121.95602416992188, height: 171.95602416992188, top: 7.997685432434082, …}

Code language: CSS (css)

clientWidth & clientHeight

Để lấy chiều rộng và chiều cao của phần tử bao gồm phần đệm nhưng không có đường viền, bạn sử dụng thuộc tính

html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Getting the Width and Height of an Elementtitle> head> <body> <div class="box" style="width:100px;height:150px;border:solid 1px #000;padding:10px">div> <script> let box = document.querySelector('.box'); let width = box.offsetWidth; let height = box.offsetHeight; console.log({ width, height }); script> body> html>

Code language: HTML, XML (xml)
4 và

html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Getting the Width and Height of an Elementtitle> head> <body> <div class="box" style="width:100px;height:150px;border:solid 1px #000;padding:10px">div> <script> let box = document.querySelector('.box'); let width = box.offsetWidth; let height = box.offsetHeight; console.log({ width, height }); script> body> html>

Code language: HTML, XML (xml)
5

let box = document.querySelector('.box'); let width = box.offsetWidth; let height = box.offsetHeight;

Code language: JavaScript (javascript)
0

Hình ảnh sau đây minh họa

html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Getting the Width and Height of an Elementtitle> head> <body> <div class="box" style="width:100px;height:150px;border:solid 1px #000;padding:10px">div> <script> let box = document.querySelector('.box'); let width = box.offsetWidth; let height = box.offsetHeight; console.log({ width, height }); script> body> html>

Code language: HTML, XML (xml)
4 và

html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Getting the Width and Height of an Elementtitle> head> <body> <div class="box" style="width:100px;height:150px;border:solid 1px #000;padding:10px">div> <script> let box = document.querySelector('.box'); let width = box.offsetWidth; let height = box.offsetHeight; console.log({ width, height }); script> body> html>

Code language: HTML, XML (xml)
5 của một phần tử

Chiều rộng thực tế trong CSS
Chiều rộng thực tế trong CSS

Để lấy lề của một phần tử, bạn sử dụng phương thức

html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Getting the Width and Height of an Elementtitle> head> <body> <div class="box" style="width:100px;height:150px;border:solid 1px #000;padding:10px">div> <script> let box = document.querySelector('.box'); let width = box.offsetWidth; let height = box.offsetHeight; console.log({ width, height }); script> body> html>

Code language: HTML, XML (xml)
8

let box = document.querySelector('.box'); let width = box.offsetWidth; let height = box.offsetHeight;

Code language: JavaScript (javascript)
4

Để lấy độ rộng đường viền của một phần tử, bạn sử dụng thuộc tính của đối tượng

html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Getting the Width and Height of an Elementtitle> head> <body> <div class="box" style="width:100px;height:150px;border:solid 1px #000;padding:10px">div> <script> let box = document.querySelector('.box'); let width = box.offsetWidth; let height = box.offsetHeight; console.log({ width, height }); script> body> html>

Code language: HTML, XML (xml)
9 được trả về bởi phương thức

{width: 122, height: 172}

Code language: CSS (css)
0

Làm cách nào để tìm chiều rộng trong CSS?

Thuộc tính chiều rộng trong CSS chỉ định chiều rộng của vùng nội dung của phần tử . Vùng “nội dung” này là phần bên trong phần đệm, đường viền và lề của một phần tử (mô hình hộp). Trong ví dụ trên, các phần tử có tên lớp là. bọc sẽ rộng bằng 80% so với phần tử cha của chúng.

Chiều rộng của div có phải là 100% theo mặc định không?

auto tự động tính toán chiều rộng sao cho tổng chiều rộng của div phù hợp với cấp độ gốc, nhưng việc đặt 100% sẽ buộc riêng nội dung thành 100%, meaning the padding etc. will stick out of the div, making it larger than the parent. so setting the 'width' to 'auto' would be better? Yes, but that's the default anyway.

Chiều rộng 100% có nghĩa là gì trong CSS?

Khi bạn cho một phần tử có chiều rộng 100% trong CSS, về cơ bản, bạn đang nói “ Làm cho vùng nội dung của phần tử này chính xác bằng với chiều rộng rõ ràng của phần tử gốc — nhưng chỉ . ” Vì vậy, nếu bạn có vùng chứa chính rộng 400px, phần tử con có chiều rộng 100% cũng sẽ là 400px. .” So, if you have a parent container that's 400px wide, a child element given a width of 100% will also be 400px ...

Là chiều rộng 100 và chiều rộng 100%?

Câu trả lời là Không . vì 100 là pixel và 100% là phần trăm kích thước tổng thể của trang.