พื้นฐาน ภาษา CSS

พื้นฐานภาษา CSS

CSS (Cascading Style Sheets) เป็นภาษาที่ใช้สำหรับกำหนดรูปแบบและลักษณะการแสดงผลของเอกสาร HTML

โครงสร้างพื้นฐานของ CSS

selector {
    property: value;
    property: value;
}

ตัวอย่างการใช้งาน CSS

1. การเชื่อม CSS กับ HTML

มี 3 วิธีหลัก:

  1. Inline CSS (ใช้แอตทริบิวต์ style ในแท็ก HTML)
   <p style="color: blue; font-size: 16px;">ข้อความนี้มีสไตล์</p>
  1. Internal CSS (ใช้แท็ก <style> ในส่วน <head>)
   <head>
       <style>
           p {
               color: blue;
               font-size: 16px;
           }
       </style>
   </head>
  1. External CSS (ใช้ไฟล์ .css แยก)
   <head>
       <link rel="stylesheet" href="styles.css">
   </head>

2. Selectors พื้นฐาน

  • Element Selector (เลือกตามชื่อแท็ก)
  p {
      color: red;
  }
  • Class Selector (เลือกตาม class)
  .highlight {
      background-color: yellow;
  }
  • ID Selector (เลือกตาม id)
  #header {
      font-size: 24px;
  }
  • Grouping Selector
  h1, h2, h3 {
      font-family: 'Arial', sans-serif;
  }

3. Properties พื้นฐานที่ควรรู้

การกำหนดสีและพื้นหลัง

body {
    color: #333333;           /* สีข้อความ */
    background-color: #f0f0f0; /* สีพื้นหลัง */
    background-image: url('bg.jpg');
}

การกำหนดตัวอักษร

p {
    font-family: 'Arial', sans-serif;
    font-size: 16px;
    font-weight: bold;
    line-height: 1.5;
    text-align: center;
}

การกำหนดกล่อง (Box Model)

div {
    width: 300px;
    height: 200px;
    padding: 20px;
    border: 1px solid #ccc;
    margin: 10px;
    box-sizing: border-box;
}

การกำหนดตำแหน่ง

.position-example {
    position: relative; /* static, relative, absolute, fixed, sticky */
    top: 10px;
    left: 20px;
}

4. Flexbox และ Grid

Flexbox

.container {
    display: flex;
    justify-content: center; /* จัดแนวนอน */
    align-items: center;    /* จัดแนวตั้ง */
    flex-direction: row;    /* row, column */
}

Grid

.grid-container {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    grid-gap: 10px;
}

5. Media Queries (Responsive Design)

@media screen and (max-width: 768px) {
    body {
        font-size: 14px;
    }
    .container {
        flex-direction: column;
    }
}

6. Animation และ Transition

Transition

.button {
    transition: all 0.3s ease;
}
.button:hover {
    background-color: blue;
}

Animation

@keyframes slide {
    from { transform: translateX(0); }
    to { transform: translateX(100px); }
}

.slide-element {
    animation: slide 2s infinite alternate;
}

ตัวอย่างไฟล์ CSS เต็มรูปแบบ

/* Reset บางส่วน */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    line-height: 1.6;
    color: #333;
    background: #f4f4f4;
}

.container {
    width: 80%;
    margin: auto;
    padding: 20px;
}

.header {
    background: #35424a;
    color: #ffffff;
    padding: 20px 0;
}

.navbar {
    display: flex;
    justify-content: space-between;
    padding: 10px 0;
}

.btn {
    display: inline-block;
    background: #e8491d;
    color: #fff;
    padding: 10px 20px;
    border: none;
    cursor: pointer;
    transition: background 0.3s ease;
}

.btn:hover {
    background: #333;
}

@media(max-width: 768px) {
    .container {
        width: 95%;
    }
    .navbar {
        flex-direction: column;
    }
}

CSS ช่วยให้คุณควบคุมการแสดงผลของเว็บไซต์ได้อย่างละเอียด และเมื่อใช้ร่วมกับ HTML และ JavaScript จะช่วยสร้างเว็บไซต์ที่สวยงามและมีประสิทธิภาพ

Leave a Comment

อีเมลของคุณจะไม่แสดงให้คนอื่นเห็น ช่องข้อมูลจำเป็นถูกทำเครื่องหมาย *