建立花哨的盒子

CSS 盒子是任何使用 CSS 樣式化的網頁的構建塊。使它們看起來漂亮既有趣又具有挑戰性。它很有趣,因為這完全是關於將設計理念轉化為有效程式碼;它具有挑戰性,因為 CSS 存在限制。讓我們做一些花哨的盒子。

在我們開始實際操作之前,請確保您熟悉CSS 盒模型。此外,熟悉一些CSS 佈局基礎知識也是一個好主意,但不是先決條件。

從技術角度來看,建立花哨的盒子完全取決於掌握 CSS 邊框和背景屬性,以及如何將它們應用於給定的盒子。但除了技術之外,它也關乎釋放你的創造力。這並非一蹴而就,一些 Web 開發人員終其一生都在享受其中。

我們將看到很多示例,但我們始終會在儘可能簡單的 HTML 片段上進行操作,即一個簡單的元素。

html
<div class="fancy">Hi! I want to be fancy.</div>

好的,這是一個非常小的 HTML 片段,我們可以在這個元素上調整什麼?以下所有內容

所以我們有一個非常廣闊的遊樂場。讓我們開始玩吧。

盒子模型調整

僅盒模型就允許我們做一些基本的事情,例如新增簡單的邊框、建立正方形等。當您透過使用負padding和/或margin以及使border-radius大於盒子的實際大小來將屬性推向極限時,它就開始變得有趣了。

建立圓形

這是一件既簡單又有趣的事情。border-radius 屬性用於建立應用於盒子的圓角,但是如果半徑大小等於或大於盒子的實際寬度會發生什麼情況呢?

css
.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 盒子變成了一個您將填充的空白畫布。

在我們跳轉到一些實際示例之前,讓我們退一步,因為您應該瞭解有關背景的兩件事。

  • 可以在單個盒子上設定多個背景。它們像圖層一樣彼此疊加。
  • 背景可以是純色或影像:純色始終填充整個表面,但影像可以縮放和定位。

好的,讓我們來玩玩背景吧。

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, rgb(0 0 0 / 0%) 95%, #8da389 95%),
                    linear-gradient( 85deg, rgb(0 0 0 / 0%) 95%, #8da389 95%),
                    linear-gradient(175deg, rgb(0 0 0 / 0%) 90%, #b4b07f 90%),
                    linear-gradient( 85deg, rgb(0 0 0 / 0%) 92%, #b4b07f 92%),
                    linear-gradient(175deg, rgb(0 0 0 / 0%) 85%, #c5a68e 85%),
                    linear-gradient( 85deg, rgb(0 0 0 / 0%) 89%, #c5a68e 89%),
                    linear-gradient(175deg, rgb(0 0 0 / 0%) 80%, #ba9499 80%),
                    linear-gradient( 85deg, rgb(0 0 0 / 0%) 86%, #ba9499 86%),
                    linear-gradient(175deg, rgb(0 0 0 / 0%) 75%, #9f8fa4 75%),
                    linear-gradient( 85deg, rgb(0 0 0 / 0%) 83%, #9f8fa4 83%),
                    linear-gradient(175deg, rgb(0 0 0 / 0%) 70%, #74a6ae 70%),
                    linear-gradient( 85deg, rgb(0 0 0 / 0%) 80%, #74a6ae 80%);
}

注意:漸變可以以一些非常有創意的方式使用。如果您想檢視一些創意示例,請檢視Lea Verou 的 CSS 圖案。如果您想了解更多關於漸變的資訊,請隨時查閱我們專門的文章

偽元素

在為單個盒子設定樣式時,您可能會發現自己受到限制,並且可能希望擁有更多盒子來建立更令人驚歎的樣式。大多數情況下,這會導致透過新增額外的 HTML 元素來汙染 DOM,其唯一目的是樣式。即使有必要,這在某種程度上也被認為是不好的做法。避免此類陷阱的一種解決方案是使用CSS 偽元素

雲彩

讓我們舉一個例子,將我們的盒子變成雲彩。

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 程式碼片段的示例(這使我們有機會了解如何處理設計本地化)。

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>

所以這裡就是我們的樣式。

css
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,
    "Times New Roman",
    Times,
    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屬性之一),可以建立更自然的幾何形狀。

css
.fancy {
  position: relative;
  background-color: #ffc;
  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;
}