垂直居中一直是讓人頭痛的老問題,幸好現在可以用 CSS3 的 transform 優雅解決:
.vc {
position: relative;
top: 50%;
-webkit-transform: translateY(-50%);
-o-transform: translateY(-50%);
transform: translateY(-50%);
}.box-shadow {
background-color: #AC92EC;
width: 160px; height: 90px;
margin-top: -45px; margin-left: -80px;
position: absolute; top: 50%; left: 50%;
}
.box-shadow:after {
content: "";
width: 150px; height: 1px;
margin-top: 88px; margin-left: -75px;
display: block; position: absolute; left: 50%; z-index: -1;
-webkit-box-shadow: 0px 0px 8px 2px #000000;
-moz-box-shadow: 0px 0px 8px 2px #000000;
box-shadow: 0px 0px 8px 2px #000000;
}自從 CSS3 問世,動畫變得相當有趣,但仍要避免濫用。以下技巧巧妙地移動背景位置,營造出動畫般的視覺:
button {
padding: 15px;
background-image: linear-gradient(#FC6E51, #FFF);
background-size: auto 200%;
background-position: 0 100%;
transition: background-position 0.5s;
}
button:hover {
background-position: 0 0;
}div {
-moz-column-count: 3; /* Firefox */
-webkit-column-count: 3; /* Safari and Chrome */
column-count: 3;
}td { white-space: nowrap; }p:first-child::first-letter {
font-family: "papyrus";
font-size: 28px;
font-weight: bold;
}跨瀏覽器相容性有時很棘手,這些針對特定瀏覽器的技巧或許能幫上忙:
/***** Selector Hacks ******/
/* IE6 and below */ * html #uno { color: red }
/* IE7 */ *:first-child+html #dos { color: red }
/* IE7, FF, Saf, Opera */ html>body #tres { color: red }
/* IE8, FF, Saf, Opera */ html>/**/body #cuatro { color: red }
/* Opera 9.27 and below, safari 2 */ html:first-child #cinco { color: red }
/* Safari 2-3 */ html[xmlns*=""] body:last-child #seis { color: red }
/* safari 3+, chrome 1+, opera9+, ff 3.5+ */ body:nth-of-type(1) #siete { color: red }
/* safari 3+, chrome 1+, opera9+, ff 3.5+ */ body:first-of-type #ocho { color: red }
/* saf3+, chrome1+ */ @media screen and (-webkit-min-device-pixel-ratio:0) { #diez { color: red } }
/* iPhone / mobile webkit */ @media screen and (max-device-width: 480px) { #veintiseis { color: red } }
/* Safari 2 - 3.1 */ html[xmlns*=""]:root #trece { color: red }
/* Safari 2 - 3.1, Opera 9.25 */ *|html[xmlns*=""] #catorce { color: red }
/* Everything but IE6-8 */ :root *> #quince { color: red }
/* IE7 */ *+html #dieciocho { color: red }
/* Firefox only. 1+ */ #veinticuatro, x:-moz-any-link { color: red }
/* Firefox 3.0+ */ #veinticinco, x:-moz-any-link, x:default { color: red }
/***** Attribute Hacks ******/
/* IE6 */ #once { _color: blue }
/* IE6, IE7 */ #doce { *color: blue; }
/* Everything but IE6 */ #diecisiete { color/**/: blue }
/* IE6, IE7, IE8 */ #diecinueve { color: blue\9; }
/* IE7, IE8 */ #veinte { color/*\**/: blue\9; }
/* IE6, IE7 -- acts as an !important */ #veintesiete { color: blue !ie; }
.blurry-text {
color: transparent;
text-shadow: 0 0 5px rgba(0,0,0,0.5);
}這段程式碼能在已知寬高的容器內,讓一張未知大小的圖片垂直水平居中,是一段 IE hack:
<figure class='logo'>
<span></span>
<img class='photo'/>
</figure>
.logo {
display: block; text-align: center;
vertical-align: middle;
border: 4px solid #dddddd; padding: 4px;
height: 74px; width: 74px;
}
.logo * { display: inline-block; height: 100%; vertical-align: middle; }
.logo .photo { height: auto; width: auto; max-width: 100%; max-height: 100%; }
// HTML
<input id="mycheck1" type="checkbox" />
<label for="mycheck1">Check box label here</label><br />
<input id="mycheck2" type="checkbox" checked/>
<label for="mycheck2">Check box label here</label><br />
<input id="mycheck3" type="checkbox" />
<label for="mycheck3">Check box label here</label>
// CSS
input:checked + label { background: yellow; }
selector {
filter: alpha(opacity=50); /* internet explorer */
-khtml-opacity: 0.5; /* khtml, old safari */
-moz-opacity: 0.5; /* mozilla, netscape */
opacity: 0.5; /* fx, safari, opera */
}// 外投影
.shadow {
-moz-box-shadow: 5px 5px 5px #ccc;
-webkit-box-shadow: 5px 5px 5px #ccc;
box-shadow: 5px 5px 5px #ccc;
}
// 內投影
.shadow {
-moz-box-shadow: inset 0 0 10px #000000;
-webkit-box-shadow: inset 0 0 10px #000000;
box-shadow: inset 0 0 10px #000000;
}
#div { min-height: 500px; height: auto !important; height: 500px; }#footer {
position: fixed; left: 0px; bottom: 0px;
height: 30px; width: 100%; background: #999;
}
/* IE 6 */
* html #footer {
position: absolute;
top: expression((0-(footer.offsetHeight)+(document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight)+(ignoreMe = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop))+'px');
}.clearfix:after {
visibility: hidden;
display: block;
font-size: 0;
content: " ";
clear: both;
height: 0;
}
.clearfix { display: inline-block; }
* html .clearfix { height: 1%; }
.clearfix { display: block; }a[href], input[type='submit'], input[type='image'], label[for], select, button, .pointer {
cursor: pointer;
}<!-- css -->
@media only screen and (max-device-width: 1024px) and (orientation:portrait) {
.landscape { display: none; }
}
@media only screen and (max-device-width: 1024px) and (orientation:landscape) {
.portrait { display: none; }
}
<!-- example markup -->
<h1 class="portrait">Your device orientation is "portrait"</h1>
<h1 class="landscape">Your device orientation is "landscape"</h1>
pre {
height: 120px; overflow: auto;
font-family: "Consolas", monospace;
font-size: 9pt; text-align: left;
background-color: #FCF7EC; overflow-x: auto;
white-space: -moz-pre-wrap !important;
word-wrap: break-word;
margin: 0px 0px 0px 0px; padding: 5px 5px 3px 5px;
white-space: normal;
}/* Smartphones (portrait and landscape) */
@media only screen and (min-device-width : 320px) and (max-device-width : 480px) { /* Styles */ }
/* Smartphones (landscape) */
@media only screen and (min-width : 321px) { /* Styles */ }
/* Smartphones (portrait) */
@media only screen and (max-width : 320px) { /* Styles */ }
/* iPads (portrait and landscape) */
@media only screen and (min-device-width : 768px) and (max-device-width : 1024px) { /* Styles */ }
/* iPads (landscape) */
@media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : landscape) { /* Styles */ }
/* iPads (portrait) */
@media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : portrait) { /* Styles */ }
/* Desktops and laptops */
@media only screen and (min-width : 1224px) { /* Styles */ }
/* Large screens */
@media only screen and (min-width : 1824px) { /* Styles */ }
/* iPhone 4 */
@media only screen and (-webkit-min-device-pixel-ratio : 1.5), only screen and (min-device-pixel-ratio : 1.5) { /* Styles */ }
html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, font, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td {
margin: 0; padding: 0; border: 0; outline: 0;
font-weight: inherit; font-style: inherit; font-size: 100%; font-family: inherit;
vertical-align: baseline;
}
:focus { outline: 0; }
body { line-height: 1; color: black; background: white; }
ol, ul { list-style: none; }
table { border-collapse: separate; border-spacing: 0; }
caption, th, td { text-align: left; font-weight: normal; }
blockquote:before, blockquote:after, q:before, q:after { content: ""; }
blockquote, q { quotes: "" ""; }元素必須是相對定位,並具備足夠的 padding 來顯示多餘的邊框:
#borders {
position: relative; z-index: 1; padding: 30px;
border: 5px solid #f00; background: #ff9600;
}
#borders:before {
content: ""; position: absolute; z-index: -1;
top: 5px; left: 5px; right: 5px; bottom: 5px;
border: 5px solid #ffea00; background: #4aa929;
}
#borders:after {
content: ""; position: absolute; z-index: -1;
top: 15px; left: 15px; right: 15px; bottom: 15px;
border: 5px solid #00b4ff; background: #fff;
}textarea { overflow: auto; }
blockquote {
background: #f9f9f9;
border-left: 10px solid #ccc;
margin: 1.5em 10px;
padding: .5em 10px;
quotes: "\201C" "\201D" "\2018" "\2019";
}
blockquote:before {
color: #ccc; content: open-quote;
font-size: 4em; line-height: .1em;
margin-right: .25em; vertical-align: -.4em;
}
blockquote p { display: inline; }<!doctype html>
<html>
<head>
<title>Placeholder demo</title>
<style type="text/css">
input:-moz-placeholder { color: green; }
</style>
</head>
<body>
<input id="test" placeholder="Placeholder text!">
</body>
</html>
* { margin: 0; padding: 0; }
html, body, #wrap { height: 100%; }
body > #wrap { height: auto; min-height: 100%; }
#main { padding-bottom: 150px; } /* 必須與 footer 同高 */
#footer {
position: relative;
margin-top: -150px; /* footer 高度的負值 */
height: 150px;
clear: both;
}
/* CLEAR FIX */
.clearfix:after { content: "."; display: block; height: 0; clear: both; visibility: hidden; }
.clearfix { display: inline-block; }
* html .clearfix { height: 1%; }
.clearfix { display: block; }
<div id="wrap">
<div id="main" class="clearfix"></div>
</div>
<div id="footer"></div>.rgba {
background-color: transparent;
background-color: rgba(200,200,200,0.8);
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#99dddddd,endColorstr=#99dddddd);
-ms-filter: "progid:DXImageTransform.Microsoft.gradient(startColorstr=#99dddddd,endColorstr=#99dddddd)";
}.content { margin: 0 auto 8px; display: table; }
.content div { display: table-cell; }
<!--[if IE]>
.content { display: block; text-align: center; }
.content div { display: inline; zoom: 1; }
<![endif]-->/* external links */
a[href^="https://"] { padding-right: 13px; background: url(external.gif) no-repeat center right; }
/* emails */
a[href^="mailto:"] { padding-right: 20px; background: url(email.png) no-repeat center right; }
/* pdfs */
a[href$=".pdf"] { padding-right: 18px; background: url(acrobat.png) no-repeat center right; }
ul li {
float: right;
margin-right: 10px;
*display: inline; /* Target IE7 and below */
_display: inline; /* Target IE6 and below */
}::selection { color: white; background-color: red; }
::-moz-selection { color: white; background-color: red; }p:first-letter {
display: block;
margin: 5px 0 0 5px;
float: left;
color: #FF3366;
font-size: 60px;
font-family: Georgia;
}