HTML и CSS интеграция
Интеграция HTML и CSS
Способы подключения и использования CSS в HTML документах.
1. Способы подключения CSS
<!-- Внешняя таблица стилей (рекомендуется) -->
<link rel="stylesheet" href="styles.css">
<!-- Внутренние стили -->
<style>
body { font-family: Arial, sans-serif; }
.container { max-width: 1200px; margin: 0 auto; }
</style>
<!-- Inline стили (избегать) -->
<div style="color: red; font-size: 16px;">Текст</div>
2. CSS классы и идентификаторы
<div class="card featured-card" id="main-card">
<h2 class="card-title">Заголовок</h2>
<p class="card-content">Содержимое карточки</p>
</div>
<style>
/* По классу */
.card { border: 1px solid #ddd; padding: 20px; }
/* По нескольким классам */
.card.featured-card { background: #f0f8ff; }
/* По ID */
#main-card { margin-bottom: 30px; }
/* Вложенность */
.card .card-title { color: #333; margin-top: 0; }
</style>
3. CSS переменные
<div class="theme-light">
<div class="component">Компонент 1</div>
<div class="component">Компонент 2</div>
</div>
<style>
.theme-light {
--primary-color: #3498db;
--secondary-color: #2ecc71;
--text-color: #2c3e50;
--background-color: #ecf0f1;
}
.component {
background-color: var(--background-color);
color: var(--text-color);
border: 2px solid var(--primary-color);
padding: 15px;
margin: 10px 0;
}
.component:hover {
background-color: var(--primary-color);
color: white;
}
</style>
4. Атрибутные селекторы
<input type="text" placeholder="Введите имя" required>
<input type="email" placeholder="Введите email" required>
<input type="password" placeholder="Введите пароль">
<a href="https://example.com" target="_blank">Внешняя ссылка</a>
<a href="/local-page">Локальная ссылка</a>
<style>
/* Селекторы по атрибуту */
input[type="text"], input[type="email"] {
border: 1px solid #bdc3c7;
padding: 8px 12px;
width: 200px;
}
input[required] {
border-left: 3px solid #e74c3c;
}
input[type="password"] {
border: 1px solid #34495e;
}
a[target="_blank"]::after {
content: " ↗";
font-size: 0.8em;
}
a[href^="https://"] {
color: #27ae60;
}
a[href$=".pdf"]::before {
content: "? ";
}
</style>
5. Псевдоклассы и псевдоэлементы
<button class="btn">Обычная кнопка</button>
<button class="btn primary">Основная кнопка</button>
<button class="btn" disabled>Неактивная кнопка</button>
<ul class="feature-list">
<li>Первая функция</li>
<li>Вторая функция</li>
<li>Третья функция</li>
</ul>
<style>
.btn {
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
transition: all 0.3s ease;
}
.btn:hover {
transform: translateY(-2px);
box-shadow: 0 4px 8px rgba(0,0,0,0.2);
}
.btn:active {
transform: translateY(0);
}
.btn:disabled {
opacity: 0.6;
cursor: not-allowed;
transform: none;
}
.btn.primary {
background: #3498db;
color: white;
}
.feature-list li {
padding: 8px 0;
border-bottom: 1px solid #ecf0f1;
}
.feature-list li:first-child {
padding-top: 0;
}
.feature-list li:last-child {
border-bottom: none;
padding-bottom: 0;
}
.feature-list li:nth-child(odd) {
background: #f8f9fa;
}
.feature-list li::before {
content: "✓ ";
color: #27ae60;
font-weight: bold;
}
</style>