Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> CSS

วิธี Comment CSS Code

วิธีใช้ comments ใน CSS

เขียน comments ภายใน CSS stylesheets เพื่อ document (อธิบาย) code สั้นๆ ให้ตัวเองหรือทีม

Syntax/format เขียน comments ใน CSS:

/* This is a CSS comment */

Format /* */ เป็น 唯一 วิธีเขียน comments ใน CSS ใช้ทั้ง single และ multi-line comments

/* This is a multi-line CSS comment. Also known as a block comment. */

ตัวอย่างใช้ comment syntax comment out (deactivate) CSS code ใน rule-sets:

 .box { background-color: #000; padding: 1.5rem; /* border-radius: 4px; */ }
 .box { background-color: #000; padding: 1.5rem; /* border-radius: 4px; box-shadow: 5px 10px #888888; */ }

ในทางปฏิบัติ comments ใช้ทั้ง document code และ comment out style declarations ระหว่าง testing

别把 CSS comments กับ JavaScript comments สับสน ใน JavaScript /* */ (format เดียว CSS) ใช้ multi-lines single line comments ใช้ double forward slashes //

  • Single-line comments = inline comments
  • Multi-line comments = block comments

อาจเห็น projects ใช้ // สำหรับ single-line comments ใน CSS code ดูใกล้ๆ จะเห็นว่าใช้ CSS Preprocessor เช่น Sass/SCSS, Less ที่ allow single-line comments — เหมือน JavaScript ใน vanilla (plain) CSS มีแค่ /* */

ควรเขียน Comments ใน CSS Code ไหม?

เหมือน how to comment HTML tutorial developers ไม่เห็นพ้องกันเรื่องนี้ บางคนเชื่อ code ควร explicit self-explanatory

สำหรับผม ไม่ใช่ขาว-ดำ Not all comments written equal ไม่ควรใช้ CSS stylesheets เขียน novel แตะ small concise comments ทำความเข้าใจ code ให้ beginners/newcomers ในทีม เข้าใจง่ายขึ้นเยอะ

ผมเลือก explicit over implicit ทุกครั้ง

別擔心 comments ใช้พื้นที่มาก hurt page load Comments/text โดยทั่วไปคือสิ่งสุดท้ายที่ควรกังวล performance/page loading speeds นอกจากนี้มี tools ลบ comments ระหว่าง post-processing