建立精美的框
CSS 盒子是任何使用 CSS 樣式化的網頁的基本構建塊。讓它們看起來很漂亮既有趣又具有挑戰性。有趣是因為它涉及到將設計理念轉化為可工作的程式碼;具有挑戰性則是因為 CSS 的限制。讓我們來建立一些炫酷的盒子。
在開始實際操作之前,請確保你熟悉 CSS 盒子模型。熟悉一些 CSS 佈局基礎 也是有益的,但並非強制要求。
從技術層面來說,建立炫酷的盒子主要在於掌握 CSS 的 `border`(邊框)和 `background`(背景)屬性,以及如何將它們應用於特定的盒子。但除了技術本身,更在於釋放你的創造力。這不會一天就能完成,有些 Web 開發者會用一生去享受其中樂趣。
我們將看到許多示例,但我們始終將在儘可能簡單的 HTML 片段上進行操作,即一個簡單的元素
<div class="fancy">Hi! I want to be fancy.</div>
好的,這只是一個很小的 HTML 片段,我們可以對這個元素做些什麼修改呢?所有以下這些
- 它的盒子模型屬性:
width、height、padding、border等。 - 它的背景屬性:
background、background-color、background-image、background-position、background-size等。 - 它的偽元素:
::before和::after - 以及一些輔助屬性,如:
box-shadow、rotate、outline等。
所以我們擁有一個非常廣闊的遊樂場。開始享受樂趣吧。
盒子模型調整
僅靠盒子模型,我們就可以做一些基本的事情,比如新增簡單的邊框,製作正方形等。當您將屬性推向極限時,例如設定負數的 `padding` 和/或 `margin`,或者設定比盒子實際尺寸更大的 `border-radius` 時,事情才變得有趣起來。
製作圓形
這是既簡單又非常有趣的事情。`border-radius` 屬性用於建立應用於盒子的圓角,但如果半徑大小等於或大於盒子的實際寬度,會發生什麼?
.fancy {
/* Within a circle, centered text looks prettier. */
text-align: center;
/* Let's avoid our text touching the border. As
our text will still flow in a square, it looks
nicer that way, giving the feeling that it's a "real"
circle. */
padding: 1em;
/* The border will make the circle visible.
You could also use a background, as
backgrounds are clipped by border radius */
border: 0.5em solid black;
/* Let's make sure we have a square.
If it's not a square, we'll get an
ellipsis rather than a circle */
width: 4em;
height: 4em;
/* and let's turn the square into a circle */
border-radius: 100%;
}
是的,我們會得到一個圓
背景
當我們談論炫酷的盒子時,處理它的核心屬性是 `background-*` 屬性。當你開始擺弄背景時,就像你的 CSS 盒子變成了一個你可以填充的空白畫布。
在我們開始實際示例之前,讓我們稍微回顧一下,因為有兩個關於背景你應該知道的事情。
- 可以在一個盒子上設定 多個背景。它們像圖層一樣堆疊在一起。
- 背景可以是純色或影像:純色總是填充整個表面,但影像可以被縮放和定位。
好的,讓我們來玩轉背景
.fancy {
padding: 1em;
width: 100%;
height: 200px;
box-sizing: border-box;
/* At the bottom of our background stack,
let's have a misty grey solid color */
background-color: #e4e4d9;
/* We stack linear gradients on top of each
other to create our color strip effect.
As you will notice, color gradients are
considered to be images and can be
manipulated as such */
background-image:
linear-gradient(175deg, transparent 95%, #8da389 95%),
linear-gradient(85deg, transparent 95%, #8da389 95%),
linear-gradient(175deg, transparent 90%, #b4b07f 90%),
linear-gradient(85deg, transparent 92%, #b4b07f 92%),
linear-gradient(175deg, transparent 85%, #c5a68e 85%),
linear-gradient(85deg, transparent 89%, #c5a68e 89%),
linear-gradient(175deg, transparent 80%, #ba9499 80%),
linear-gradient(85deg, transparent 86%, #ba9499 86%),
linear-gradient(175deg, transparent 75%, #9f8fa4 75%),
linear-gradient(85deg, transparent 83%, #9f8fa4 83%),
linear-gradient(175deg, transparent 70%, #74a6ae 70%),
linear-gradient(85deg, transparent 80%, #74a6ae 80%);
}
注意:漸變可以以非常創造性的方式使用。如果您想看一些創意示例,請檢視 Lea Verou 的 CSS 模式。如果您想了解更多關於漸變的資訊,請隨時閱讀 我們的專門文章。
偽元素
在樣式化單個盒子時,您可能會發現自己受到限制,並希望擁有更多盒子來建立更令人驚歎的樣式。大多數時候,這會導致透過新增用於樣式化的額外 HTML 元素來汙染 DOM。即使有必要,這也有些被認為是糟糕的做法。避免此類陷阱的一種方法是使用 CSS 偽元素。
一片雲
讓我們透過將我們的盒子變成雲來舉個例子
.fancy {
text-align: center;
/* Same trick as previously used to make circles */
box-sizing: border-box;
width: 150px;
height: 150px;
padding: 80px 1em 0 1em;
/* We make room for the "ears" of our cloud */
margin: 0 100px;
position: relative;
background-color: #a4c9cf;
/* Well, actually we are not making a full circle
as we want the bottom of our cloud to be flat.
Feel free to tweak this example to make a cloud
that isn't flat at the bottom ;) */
border-radius: 100% 100% 0 0;
}
/* Those are common style that apply to both our ::before
and ::after pseudo elements. */
.fancy::before,
.fancy::after {
/* This is required to be allowed to display the
pseudo-elements, event if the value is an empty
string */
content: "";
/* We position our pseudo-elements on the left and
right sides of the box, but always at the bottom */
position: absolute;
bottom: 0;
/* This makes sure our pseudo-elements will be below
the box content whatever happens. */
z-index: -1;
background-color: #a4c9cf;
border-radius: 100%;
}
.fancy::before {
/* This is the size of the clouds left ear */
width: 125px;
height: 125px;
/* We slightly move it to the left */
left: -80px;
/* To make sure that the bottom of the cloud
remains flat, we must make the bottom right
corner of the left ear square. */
border-bottom-right-radius: 0;
}
.fancy::after {
/* This is the size of the clouds left ear */
width: 100px;
height: 100px;
/* We slightly move it to the right */
right: -60px;
/* To make sure that the bottom of the cloud
remains flat, we must make the bottom left
corner of the right ear square. */
border-bottom-left-radius: 0;
}
引用塊
使用偽元素的一個更實際的例子是為 HTML <blockquote> 元素構建漂亮的格式。所以讓我們看一個稍微不同的 HTML 片段的示例(這為我們提供了機會,也可以瞭解如何處理設計本地化)。
<blockquote>
People who think they know everything are a great annoyance to those of us who
do. <i>Isaac Asimov</i>
</blockquote>
<blockquote lang="fr">
L'intelligence, c'est comme les parachutes, quand on n'en a pas, on s'écrase.
<i>Pierre Desproges</i>
</blockquote>
那麼,這是我們的樣式
blockquote {
min-height: 5em;
padding: 1em 4em;
font: 1em/150% sans-serif;
position: relative;
background-color: lightgoldenrodyellow;
}
blockquote::before,
blockquote::after {
position: absolute;
height: 3rem;
font:
6rem/100% "Georgia",
serif;
}
blockquote::before {
content: "“";
top: 0.3rem;
left: 0.9rem;
}
blockquote::after {
content: "”";
bottom: 0.3rem;
right: 0.8rem;
}
blockquote:lang(fr)::before {
content: "«";
top: -1.5rem;
left: 0.5rem;
}
blockquote:lang(fr)::after {
content: "»";
bottom: 2.6rem;
right: 0.5rem;
}
blockquote i {
display: block;
font-size: 0.8em;
margin-top: 1rem;
text-align: right;
}
全部結合以及更多
因此,將所有這些混合在一起可以創造出絕妙的效果。在某個時候,要完成這種盒子的美化,就是發揮創造力,既要考慮設計,也要考慮 CSS 屬性的技術運用。透過這樣做,可以創造出視覺錯覺,讓您的盒子栩栩如生,就像在這個例子中一樣。
讓我們建立一些部分陰影效果。`box-shadow` 屬性允許我們建立內部光和平面陰影效果,但透過一些額外的工作,可以使用偽元素和 `rotate` 屬性(三個獨立的 `transform` 屬性之一)來建立更自然的幾何形狀。
.fancy {
position: relative;
background-color: #ffffcc;
padding: 2rem;
text-align: center;
max-width: 200px;
}
.fancy::before {
content: "";
position: absolute;
z-index: -1;
bottom: 15px;
right: 5px;
width: 50%;
top: 80%;
max-width: 200px;
box-shadow: 0px 13px 10px black;
rotate: 4deg;
}