Hover (псевдоэлемент hover)

В HTML-элементу можно назначить несколько классов, они перечисляются в атрибуте class через пробел. Это позволяет применить к элементу несколько стилей, описанных в CSS.

Использование нескольких классов — это типовой приём (multiple CSS classes). Обычно его используют так: в один класс выносят общее оформление, а в дополнительных классах описывают его модификации.

Псевдокласс :hover Определяет стиль элемента при наведении на него курсора мыши, но при этом элемент еще не активирован, иными словами кнопка мыши не нажата.

test 1 - При наведении увеличиваем шрифт

пример

Наведи на меня

css

.test1 { display: inline-block; font-size: 20px; line-height: 50px; background-color : lightgrey; } .test1:hover { font-size: 45px;}

html

<div class = "test1"> Наведи на меня </div>

У каждого из элементов примера test 2 два класса. css класс test2 аналогичен test1, у второго класса свое собственное поведение. При наведении курсора на элемент срабатывают правила hover обоих классов.

test 2

пример

Гранат
Апельсин
Лимон
Слива

css

.test2 { font-size: 20px; display: inline-block; background-color : lightgrey; line-height: 50px; vertical-align:top; } .test2:hover { font-size: 45px;} .red:hover { background-color : red; } .orange:hover { background-color : orange; } .yellow:hover { background-color : yellow; } .purple:hover { background-color : purple; }

html

<div class = "test2 red">Гранат</div> <div class = "test2 orange">Апельсин</div> <div class = "test2 yellow">Лимон</div> <div class = "test2 purple">Слива</div>

Пример изменения стиля вложенных(дочерних) блоков при наведении на родителя

test 3

пример

Гранат
Апельсин
Лимон
Слива

css

.testbox3 { padding: 10px; background-color : lightgrey; } .testbox3:hover .test3 {font-size: 45px; } .test3 { display: inline-block; font-size: 20px; background-color : DarkGray; line-height: 50px; } .red:hover { background-color : red; } .orange:hover { background-color : orange; } .yellow:hover { background-color : yellow; } .purple:hover { background-color : purple; }

html

<div class = "testbox3"> <div class = "test3 red">Гранат</div> <div class = "test3 orange">Апельсин</div> <div class = "test3 yellow">Лимон</div> <div class = "test3 purple">Слива</div> </div>
test 4

пример

Наведи курсор
Убери курсор

css

.testbox4 { display: inline-block; font-size: 20px; background-color : lightgrey; line-height: 50px; } .testbox4 .block1 { display: block; } .testbox4 .block2 { display: none; } .testbox4:hover .block1 { display: none; } .testbox4:hover .block2 { display: block; }

html

<div class = "testbox4"> <div class = "block1">Наведи курсор</div> <div class = "block2"> Убери курсор</div> </div>
test 5

пример четыре вложенных друг в друга div

css

.test5 { display: inline-block; min-width : 30px; min-height : 30px; margin: 10px; border: solid 1px black; } .test5:hover .test5{ margin-top: 2px; margin-bottom: 18px; margin-left:2px; margin-right: 18px; }

html

<div class = "test5"> <div class = "test5"> <div class = "test5"> <div class = "test5"></div></div> </div> </div> </div>
test 10

пример


css

.test10, .test20, .test30, .test40 { /* тот же набор правил, что и для .test5 */ display: inline-block; min-width : 30px; min-height : 30px; margin: 10px; border: solid 1px black; } .box10 { border: dashed 3px black; } .box10:hover .test10 { margin-top: 18px; margin-bottom: 2px; margin-left: 18px; margin-right: 2px;} .box10:hover .test20 { margin-top: 18px; margin-bottom: 2px; margin-left: 2px; margin-right: 18px;} .box10:hover .test30 { margin-top: 2px; margin-bottom: 18px; margin-left: 18px; margin-right: 2px;} .box10:hover .test40 { margin-top: 2px; margin-bottom: 18px; margin-left: 2px; margin-right: 18px;}

html

<div class = "box10"> <div class = "test10"><div class = "test10"><div class = "test10"><div class = "test10"></div></div></div></div> <div class = "test20"><div class = "test20"><div class = "test20"><div class = "test20"></div></div></div></div><br> <div class = "test30"><div class = "test30"><div class = "test30"><div class = "test30"></div></div></div></div> <div class = "test40"><div class = "test40"><div class = "test40"><div class = "test40"></div></div></div></div> </div>
Выпадающее меню

пример

html + css

Примеры

добавляем цвет к тексту

Раскрась меня

Меняем межбуквенные интервалы с помощью letter-spacing: 10px;

Расширь меня

Меняем отступ вложенного элемента

Меняем размер фонового рисунка с помощью Background-size:

,

шахматная доска 2*2, меняет цвет клеток