Cara Menghubungkan CSS ke HTML (Inline, Internal, dan External CSS)

Created at by Aris Munandar

Dalam pembuatan website, salah satu langkah penting adalah menghubungkan CSS ke HTML agar tampilan halaman menjadi menarik dan profesional. CSS (Cascading Style Sheets) berfungsi untuk mengatur tampilan elemen-elemen HTML seperti warna, ukuran font, layout, hingga jarak antar elemen.
Artikel ini akan membahas secara lengkap cara menghubungkan CSS ke HTML melalui tiga metode utama: inline CSS, internal CSS, dan external CSS. Kita juga akan melihat contoh penerapan CSS pada HTML serta memahami perbedaan antara ketiganya.

Baca juga: Belajar CSS dari Nol: Pengertian, Fungsi, dan Cara Kerja CSS

1. Cara Menghubungkan CSS Inline ke HTML

CSS Inline adalah cara paling sederhana untuk menambahkan style langsung pada elemen HTML menggunakan atribut style.
Metode ini cocok untuk pengujian cepat atau mengubah tampilan satu elemen saja.

Contoh kode CSS inline:

<!DOCTYPE html>
<html>
<head>
    <title>Contoh CSS Inline</title>
</head>
<body>
    <h1 style="color: blue; text-align: center;">Halo, ini contoh CSS Inline</h1>
    <p style="font-size: 18px; color: gray;">Teks ini menggunakan CSS inline.</p>
</body>
</html>Code language: HTML, XML (xml)

Pada contoh di atas, atribut style ditulis langsung di dalam tag HTML.
Setiap properti CSS dipisahkan oleh titik koma (;).

Kelebihan CSS Inline:

  • Mudah digunakan untuk elemen tunggal.
  • Tidak membutuhkan file tambahan.

Kekurangan CSS Inline:

  • Sulit dikelola jika banyak elemen yang perlu diubah.
  • Tidak efisien untuk proyek besar.

3. Cara Menghubungkan CSS External ke HTML

Metode paling efisien dan profesional adalah menggunakan CSS External.
CSS ditulis dalam file terpisah dengan ekstensi .css, lalu dihubungkan ke file HTML menggunakan tag <link> di dalam bagian <head>.

Langkah-langkah menghubungkan CSS eksternal ke HTML:

  1. Buat file HTML (misalnya index.html).
  2. Buat file CSS (misalnya style.css).
  3. Hubungkan file CSS ke HTML dengan tag <link>.

Contoh kode:

File style.css:

body {
    background-color: #ffffff;
    font-family: 'Roboto', sans-serif;
}
h1 {
    color: #e74c3c;
    text-align: center;
}
p {
    color: #333;
    font-size: 16px;
}Code language: CSS (css)

File index.html:

<!DOCTYPE html>
<html>
<head>
    <title>Contoh CSS Eksternal</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <h1>Belajar CSS Eksternal</h1>
    <p>Contoh menghubungkan CSS eksternal ke HTML.</p>
</body>
</html>Code language: HTML, XML (xml)

Kelebihan CSS Eksternal:

  • File CSS dapat digunakan di banyak halaman.
  • Struktur HTML menjadi lebih bersih.
  • Lebih mudah dikelola dan dioptimalkan.

Kekurangan CSS Eksternal:

  • Memerlukan file tambahan.
  • Membutuhkan koneksi untuk memuat file eksternal saat halaman dibuka.

5. Contoh Penerapan CSS pada HTML

Berikut contoh sederhana penerapan struktur HTML dan CSS eksternal:

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Penerapan CSS pada HTML</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <header>
        <h1>Belajar HTML dan CSS untuk Pemula</h1>
    </header>
    <main>
        <p>Dengan memahami cara menghubungkan CSS ke HTML, kamu bisa membuat tampilan website menjadi lebih menarik.</p>
    </main>
</body>
</html>Code language: HTML, XML (xml)

6. Kesimpulan

Menghubungkan CSS ke HTML dapat dilakukan dengan tiga cara utama: Inline, Internal, dan External CSS.
Untuk proyek kecil, inline CSS mungkin cukup. Namun untuk website profesional, CSS eksternal lebih disarankan karena membuat kode lebih rapi, efisien, dan mudah dirawat.

Dengan memahami cara menambahkan CSS ke HTML dan perbedaan antar metode, kamu sudah berada di jalur yang tepat untuk menguasai tutorial CSS HTML untuk pemula.
Langkah selanjutnya adalah terus berlatih membuat layout dan eksperimen dengan berbagai properti CSS.

2 CSS Menengah

3 CSS Lanjutan

4 CSS Mahir

5 CSS Ahli

Comments

Congrats, you have the opportunity to be the first commenter on this article. Have questions or suggestions? Please leave a comment to start discussion.

Leave comment

Alamat email Anda tidak akan dipublikasikan. Required fields are marked *

news-1701

sabung ayam online

yakinjp

yakinjp

rtp yakinjp

slot thailand

yakinjp

yakinjp

yakin jp

yakinjp id

maujp

maujp

maujp

maujp

sabung ayam online

sabung ayam online

judi bola online

sabung ayam online

judi bola online

slot mahjong ways

slot mahjong

sabung ayam online

judi bola

live casino

sabung ayam online

judi bola

live casino

SGP Pools

slot mahjong

sabung ayam online

slot mahjong

SLOT THAILAND

article 138000526

article 138000527

article 138000528

article 138000529

article 138000530

article 138000531

article 138000532

article 138000533

article 138000534

article 138000535

article 138000536

article 138000537

article 138000538

article 138000539

article 138000540

article 138000541

article 138000542

article 138000543

article 138000544

article 138000545

article 138000546

article 138000547

article 138000548

article 138000549

article 138000550

article 138000551

article 138000552

article 138000553

article 138000554

article 138000555

article 138000556

article 138000557

article 138000558

article 138000559

article 138000560

article 138000561

article 138000562

article 138000563

article 138000564

article 138000565

article 138000566

article 138000567

article 138000568

article 138000569

article 138000570

article 138000571

article 138000572

article 138000573

article 138000574

article 138000575

article 138000576

article 138000577

article 138000578

article 138000579

article 138000580

article 138000581

article 138000582

article 138000583

article 138000584

article 138000585

article 158000416

article 158000417

article 158000418

article 158000419

article 158000420

article 158000421

article 158000422

article 158000423

article 158000424

article 158000425

article 158000426

article 158000427

article 158000428

article 158000429

article 158000430

article 158000431

article 158000432

article 158000433

article 158000434

article 158000435

article 158000436

article 158000437

article 158000438

article 158000439

article 158000440

article 208000436

article 208000437

article 208000438

article 208000439

article 208000440

article 208000441

article 208000442

article 208000443

article 208000444

article 208000445

article 208000446

article 208000447

article 208000448

article 208000449

article 208000450

article 208000451

article 208000452

article 208000453

article 208000454

article 208000455

article 208000456

article 208000457

article 208000458

article 208000459

article 208000460

article 208000461

article 208000462

article 208000463

article 208000464

article 208000465

article 208000466

article 208000467

article 208000468

article 208000469

article 208000470

208000436

208000437

208000438

208000439

208000440

208000441

208000442

208000443

208000444

208000445

208000446

208000447

208000448

208000449

208000450

208000451

208000452

208000453

208000454

208000455

article 228000286

article 228000287

article 228000288

article 228000289

article 228000290

article 228000291

article 228000292

article 228000293

article 228000294

article 228000295

article 228000296

article 228000297

article 228000298

article 228000299

article 228000300

article 228000301

article 228000302

article 228000303

article 228000304

article 228000305

article 228000306

article 228000307

article 228000308

article 228000309

article 228000310

article 228000311

article 228000312

article 228000313

article 228000314

article 228000315

article 238000281

article 238000282

article 238000283

article 238000284

article 238000285

article 238000286

article 238000287

article 238000288

article 238000289

article 238000290

article 238000291

article 238000292

article 238000293

article 238000294

article 238000295

article 238000296

article 238000297

article 238000298

article 238000299

article 238000300

sumbar-238000256

sumbar-238000257

sumbar-238000258

sumbar-238000259

sumbar-238000260

sumbar-238000261

sumbar-238000262

sumbar-238000263

sumbar-238000264

sumbar-238000265

sumbar-238000266

sumbar-238000267

sumbar-238000268

sumbar-238000269

sumbar-238000270

sumbar-238000271

sumbar-238000272

sumbar-238000273

sumbar-238000274

sumbar-238000275

sumbar-238000276

sumbar-238000277

sumbar-238000278

sumbar-238000279

sumbar-238000280

sumbar-238000281

sumbar-238000282

sumbar-238000283

sumbar-238000284

sumbar-238000285

sumbar-238000286

sumbar-238000287

sumbar-238000288

sumbar-238000289

sumbar-238000290

news-1701