Воскресные сниппеты №4
12.08
Сегодня подборка CSS-снипеетов.
Ничего особенного, но многое полезно и одновременно забывается, так что сделал эту подборку больше для себя.
Подборка полезных css сиппетов
- Установка font-size для body (поможет легко использовать em вместо px):
body { font-size: 62.5%; /* font-size 1em = 10px */ } p { font-size: 1.2em; /* 1.2em = 12px */ }
- Вертикальное выравнивание картинки в блоке (полезно когда картинки разного размера):
div { width: 100px; height: 100px; line-height: 100px; overflow: hidden; } div img { max-width: 100px; vertical-align: middle; display: block; }
- Изменение цвета выделения:
::-moz-selection { background: #b3d4fc; text-shadow: none; } ::selection { background: #b3d4fc; text-shadow: none; }
- Предотвращение схлопывания родительского элемента если внутри есть плавающие блоки (clearfix):
.clearfix:before, .clearfix:after { content: ""; display: table; } .clearfix:after { clear: both; } .clearfix { *zoom: 1; }
- Можно использовать и вот такую конструкцию:
.clr {clear: both; height: 0; overflow: hidden;} а перед закрывающим тегом родительского блока вставить: <div class="clr"></div>
- Запрет выделения текста:
div { -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; }
- "Подсветка" целевого элемента на странице:
например имеем код:<h3 id="123">текст</h3>
при переходе по ссылке site.ru/index.html#123 внешний вид нашего заголовка поменяется.
На основе этого псевдокласса можно сделать даже галерею:h3 { font: normal 1.3em/1.8em Arial, Helvetica, sans-serif; color: #ccc; } h3:target { font-size: 1.6em; text-decoration: underline; color: #68c; }
- "Подсветка внешних ссылок"
a { color: #c06; } a[target*="blank"] { color: #f50; } a[target*="blank"]:after { content: url(icon.png); /*маленькая иконка для большей наглядности*/ }
- Размытый текст:
h1 { /*ie9 и старше, как всегда отсасывает... юзаем костыли*/ color:transparent; text-shadow: 0 0 10px #ccc; }​
- Треугольник на css:
.someclass { width: 0; height: 0; border-left:solid 5px transparent; border-right:solid 5px transparent; border-bottom:solid 5px black; }
- Тоже самое применимо к спискам:ul { margin: 0.75em 0; padding: 0 1em; list-style: none; } li:before { content: ""; border-color: transparent #111; border-style: solid; border-width: 0.35em 0 0.35em 0.45em; display: block; height: 0; width: 0; left: -1em; top: 0.9em; position: relative; }
- Кроссбраузерный inline-block для блочных элементов:
li { width: 200px; min-height: 250px; display: -moz-inline-stack; display: inline-block; vertical-align: top; margin: 5px; zoom: 1; *display: inline; }
- Замена текста картинкой (новый вариант без text-indent):
.ir { border: 0; font: 0/0 a; text-shadow: none; color: transparent; background-color: transparent; }
- Видимый контур у всех элементов (полезно для отладки):
* { outline: 2px dotted red } * * { outline: 2px dotted green } * * * { outline: 2px dotted orange } * * * * { outline: 2px dotted blue } * * * * * { outline: 1px solid red } * * * * * * { outline: 1px solid green } * * * * * * * { outline: 1px solid orange } * * * * * * * * { outline: 1px solid blue }
- Стили для печати страницы (от htbp.com):
@media print { * { background: transparent !important; color: black !important; box-shadow:none !important; text-shadow: none !important; } /* Black prints faster: h5bp.com/s */ a, a:visited { text-decoration: underline; } a[href]:after { content: " (" attr(href) ")"; } abbr[title]:after { content: " (" attr(title) ")"; } .ir a:after, a[href^="javascript:"]:after, a[href^="#"]:after { content: ""; } /* Don't show links for images, or javascript/internal links */ pre, blockquote { border: 1px solid #999; page-break-inside: avoid; } thead { display: table-header-group; } /* h5bp.com/t */ tr, img { page-break-inside: avoid; } img { max-width: 100% !important; } @page { margin: 0.5cm; } p, h2, h3 { orphans: 3; widows: 3; } h2, h3 { page-break-after: avoid; } }
- Стили для таблиц (от bootstrap):
/*TABLES*/ table.bordered { max-width: 100%; background-color: transparent; border-collapse: collapse; border-spacing: 0; width: 100%; margin-bottom: 18px; } table.bordered { border: 1px solid #dddddd; border-collapse: separate; *border-collapse: collapsed; border-left: 0; -webkit-border-radius: 4px; -moz-border-radius: 4px; border-radius: 4px; } table.bordered caption + thead tr:first-child th, table.bordered caption + tbody tr:first-child th, table.bordered caption + tbody tr:first-child td, table.bordered colgroup + thead tr:first-child th, table.bordered colgroup + tbody tr:first-child th, table.bordered colgroup + tbody tr:first-child td, table.bordered thead:first-child tr:first-child th, table.bordered tbody:first-child tr:first-child th, table.bordered tbody:first-child tr:first-child td { border-top: 0; } table.bordered caption + thead tr:first-child th, table.bordered caption + thead tr:first-child td, table.bordered colgroup + thead tr:first-child th, table.bordered colgroup + thead tr:first-child td, table.bordered thead:first-child tr:first-child th, table.bordered thead:first-child tr:first-child td { border-top: 0; } table.bordered tbody + tbody { border-top: 2px solid #dddddd; } table.bordered tbody tr:hover td, table.bordered tbody tr:hover th { background-color: #f5f5f5; } table.bordered tbody tr:nth-child(odd) td, table.bordered tbody tr:nth-child(odd) th { background-color: #f9f9f9; } table.bordered th { font-weight: bold; } table.bordered th, table.bordered td { padding: 8px; line-height: 18px; text-align: left; vertical-align: top; border-top: 1px solid #dddddd; } table.bordered th, table.bordered td { border-left: 1px solid #dddddd; } table.bordered thead th { vertical-align: bottom; } table.bordered thead:first-child tr:first-child th:first-child, table.bordered tbody:first-child tr:first-child td:first-child { -webkit-border-top-left-radius: 4px; border-top-left-radius: 4px; -moz-border-radius-topleft: 4px; } table.bordered thead:first-child tr:first-child th:last-child, table.bordered tbody:first-child tr:first-child td:last-child { -webkit-border-top-right-radius: 4px; border-top-right-radius: 4px; -moz-border-radius-topright: 4px; } table.bordered thead:last-child tr:last-child th:first-child, table.bordered tbody:last-child tr:last-child td:first-child { -webkit-border-radius: 0 0 0 4px; -moz-border-radius: 0 0 0 4px; border-radius: 0 0 0 4px; -webkit-border-bottom-left-radius: 4px; border-bottom-left-radius: 4px; -moz-border-radius-bottomleft: 4px; } table.bordered thead:last-child tr:last-child th:last-child, table.bordered tbody:last-child tr:last-child td:last-child { -webkit-border-bottom-right-radius: 4px; border-bottom-right-radius: 4px; -moz-border-radius-bottomright: 4px; } table.bordered th, table.bordered td { padding: 4px 5px; } table.bordered.character { border: none !important; padding: 0 !important; background: #fff !important; }
На сегодня всё.
P.S. На неделе будет плагин, о котором говорил ранее, пока расписывать всё по полкам вермени не хватает.
Комментарии
Добавить комментарий
Комментировать могут только зарегистрированные пользователи