Path: blob/main/files/en-us/learn/css/howto/create_fancy_boxes/index.md
6538 views
------{{LearnSidebar}}
CSS boxes are the building blocks of any web page styled with CSS. Making them nice looking is both fun and challenging. It's fun because it's all about turning a design idea into working code; it's challenging because of the constraints of CSS. Let's do some fancy boxes.
Before we start getting into the practical side of it, make sure you are familiar with the CSS box model. It's also a good idea, but not a prerequisite, to be familiar with some CSS layout basics.
On the technical side, Creating fancy boxes are all about mastering CSS border and background properties and how to apply them to a given box. But beyond the technics it's also all about unleashing your creativity. It will not be done in one day, and some web developers spend their whole life having fun with it.
We are about to see many examples, but we will always work on the most simple piece of HTML possible, a simple element:
Ok, that's a very small bit of HTML, what can we tweak on that element? All the following:
Its box model properties: {{cssxref("width")}}, {{cssxref("height")}}, {{cssxref("padding")}}, {{cssxref("border")}}, etc.
Its background properties: {{cssxref("background")}}, {{cssxref("background-color")}}, {{cssxref("background-image")}}, {{cssxref("background-position")}}, {{cssxref("background-size")}}, etc.
Its pseudo-element: {{cssxref("::before")}} and {{cssxref("::after")}}
and some aside properties like: {{cssxref("box-shadow")}}, {{cssxref("rotate")}}, {{cssxref("outline")}}, etc.
So we have a very large playground. Let the fun begin.
Box model tweak
The box model alone allows us to do some basic stuff, like adding simple borders, making squares, etc. It starts to get interesting when you push the properties to the limit by having negative padding and/or- margin by having border-radius larger than the actual size of the box.
Making circles
This is something that is both very simple and very fun. The {{cssxref("border-radius")}} property is made to create a rounded corner applied to boxes, but what happens if the radius size is equal or larger than the actual width of the box?
Yes, we get a circle:
{{ EmbedLiveSample('Making_circles', '100%', '120') }}
Backgrounds
When we talk about a fancy box, the core properties to handle that are background-* properties. When you start fiddling with backgrounds it's like your CSS box is turned into a blank canvas you'll fill.
Before we jump to some practical examples, let's step back a bit as there are two things you should know about backgrounds.
It's possible to set several backgrounds on a single box. They are stacked on top of each other like layers.
Backgrounds can be either solid colors or images: solid color always fills the whole surface but images can be scaled and positioned.
Okay, let's have fun with backgrounds:
{{ EmbedLiveSample('Backgrounds', '100%', '200') }}
Note: Gradients can be used in some very creative ways. If you want to see some creative examples, take a look at Lea Verou's CSS patterns. If you want to learn more about gradients, feel free to get into our dedicated article.
Pseudo-elements
When styling a single box, you could find yourself limited and could wish to have more boxes to create even more amazing styles. Most of the time, that leads to polluting the DOM by adding extra HTML element for the unique purpose of style. Even if it is necessary, it's somewhat considered bad practice. One solution to avoid such pitfalls is to use CSS pseudo-elements.
A cloud
Let's have an example by turning our box into a cloud:
{{ EmbedLiveSample('A_cloud', '100%', '160') }}
Blockquote
A more practical example of using pseudo-elements is to build a nice formatting for HTML {{HTMLElement('blockquote')}} elements. So let's see an example with a slightly different HTML snippet (which provide us an opportunity to see how to also handle design localization):
So here comes our style:
{{ EmbedLiveSample('Blockquote', '100%', '300') }}
All together and more
So it's possible to create a wonderful effect when we mix all of this together. At some point, to accomplish such box embellishment is a matter of creativity, both in design and technical use of CSS properties. By doing such it's possible to create optical illusions that can bring your boxes alive like in this example:
Let's create some partial drop-shadow effects. The {{cssxref("box-shadow")}} property allow us to create inner light and a flat drop shadow effect, but with some little extra work it becomes possible to create a more natural geometry by using a pseudo-element and the {{cssxref("rotate")}} property, one of the three individual {{cssxref("transform")}} properties.
{{ EmbedLiveSample('All_together_and_more', '100%', '120') }}