search
HomeWeb Front-endCSS TutorialImplementation of Flexbox dice layout in CSS3 and explanation of problems

This article is just to familiarize yourself with the usage of basic attributes and complete the production of each side of a set of dice. In the following content, I will not cover some of the more difficult issues with flexbox, such as old version syntax, vendor prefixes, browser quirks, etc.:

1. First Face
We know that the dice has six sides , the number of points in each face represents the value of the face, and the first face consists of a horizontally and vertically centered point. Let’s look at the specific implementation:

<section>   
  <span></span>   
 </section>   
face-01 {   
display: flex;   
justify-content: center;   
align-items: center;

For the usage of justify-content and align-items, please refer to here justify-content, align-items. Using flexbox, you can do it by vertically centering two rows of attributes. It’s very easy!

2. Second Face

.face-02 {   
 display: flex;   
 justify-content: space-between;   
}   
.face-02 .dot:nth-of-type(2) {   
 align-self: flex-end;   
}   
  <section>   
   <span></span>   
   <span></span>   
  </section>

We can’t use the align-items attribute here. Using it will affect both points. Flexbox provides an align-self attribute, which allows us to more conveniently control the flex items to set different layouts along the cross axias direction. For the usage of align-self, please refer to align-self here.

3. Third Face

.face-03 {   
 display: flex;   
 justify-content: space-between;   
}   
.face-03 .dot:nth-of-type(2) {   
 align-self: center;   
}   
.face-03 .dot:nth-of-type(3) {   
 align-self: flex-end;   
}   
<section>   
 <span></span>   
 <span></span>   
 <span></span>   
</section>

This face uses the same attributes as the second face and will not be explained again.

4. Fourth Face

.face-04 {   
 display: flex;   
 justify-content: space-between;   
 flex-direction: column;   
}   
.face-04 .column {   
 display: flex;   
 justify-content: space-between;   
}   
<section>   
  <div>   
    <span></span>   
    <span></span>   
  </div>   
  <div>   
    <span></span>   
    <span></span>   
  </div>   
</section>

In this example, flex-direction is used. From the literal meaning, it can be seen that it is used to control the direction of flex, that is, by column Or layout by row. For more detailed usage of this attribute, please refer to the Fifth Face and Sixth Face behind flex-direction

. According to the previous layout ideas, it is very easy and I won’t go into details!

Writing this, it should be easy to write a small game of playing dice with JS.

5. Implement 1,2,3,4,6,12 equal parts

.row {   
  display: flex;   
  box-sizing: border-box;   
}   
  
.column {   
  margin: 10px;   
  flex-grow: 1;   
  flex-shrink: 1;   
  flex-basis: 0;   
  box-sizing: border-box;   
}   
<section>   
  <div>One</div>   
</section>   
<section>   
  <div>One Half</div>   
  <div>One Half</div>   
</section>   
<section>   
  <div>One Third</div>   
  <div>One Third</div>   
  <div>One Third</div>   
</section>   
<section>   
  <div>One Fourth</div>   
  <div>One Fourth</div>   
  <div>One Fourth</div>   
  <div>One Fourth</div>   
</section>   
<section>   
  <div>One Sixth</div>   
  <div>One Sixth</div>   
  <div>One Sixth</div>   
  <div>One Sixth</div>   
  <div>One Sixth</div>   
  <div>One Sixth</div>   
</section>   
<section>   
  <div>One Twelve</div>   
  <div>One Twelve</div>   
  <div>One Twelve</div>   
  <div>One Twelve</div>   
  <div>One Twelve</div>   
  <div>One Twelve</div>   
  <div>One Twelve</div>   
  <div>One Twelve</div>   
  <div>One Twelve</div>   
  <div>One Twelve</div>   
  <div>One Twelve</div>   
  <div>One Twelve</div>   
</section>

In this example, flex-grow, flex-shrink, flex are used -basis three attributes.
1. flex-grow: Used to define the expansion capabilities of flex items as needed. It accepts a value without units as a ratio. It is mainly used to determine how much space should be expanded proportionally to the remaining space of the scalable container.
If the "flex-grow" of all flex items is set to "1", then each flex item will be set to an equal-sized remaining space. If you set the "flex-grow" value to "2" for one of the flex items, then the remaining space occupied by this flex item is twice the remaining space occupied by the other flex items. Negative values ​​are invalid.
2. flex-shrink: Used to define the ability to shrink flex items as needed. Negative values ​​are also invalid.
3. flex-basis: Used to set the scaling baseline value. The remaining space is scaled according to the ratio. Negative values ​​are not supported. If set to 0, additional space around the content is not taken into account. If set to auto, additional space is allocated based on the flex-grow value.

6. Implement 2-3-7 layout

.row237 .column:first-of-type {   
  flex-grow: 2;   
  flex-basis: 5px;   
}   
.row237 .column:nth-of-type(2) {   
  flex-grow: 3;   
  flex-basis: 18px;   
}   
.row237 .column:nth-of-type(3) {   
  flex-grow: 7;   
  flex-basis: 70.5px;   
}   
  
<section>   
  <div>One Half</div>   
  <div>One Third</div>   
  <div>One Seventh</div>   
</section>

There should be a formula (to be solved) for calculating the values ​​of various flex-basis here. If there is such a formula, cooperate with sass , less and other pre-processing languages ​​​​will be very convenient to implement multi-column adaptive layout.

For more related articles on the implementation and problem explanation of CSS3 Flexbox dice layout, please pay attention to the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Lost CSS Tricks of Cohost.orgThe Lost CSS Tricks of Cohost.orgApr 25, 2025 am 09:51 AM

In this post, Blackle Mori shows you a few of the hacks found while trying to push the limits of Cohost’s HTML support. Use these if you dare, lest you too get labelled a CSS criminal.

Next Level CSS Styling for CursorsNext Level CSS Styling for CursorsApr 23, 2025 am 11:04 AM

Custom cursors with CSS are great, but we can take things to the next level with JavaScript. Using JavaScript, we can transition between cursor states, place dynamic text within the cursor, apply complex animations, and apply filters.

Worlds Collide: Keyframe Collision Detection Using Style QueriesWorlds Collide: Keyframe Collision Detection Using Style QueriesApr 23, 2025 am 10:42 AM

Interactive CSS animations with elements ricocheting off each other seem more plausible in 2025. While it’s unnecessary to implement Pong in CSS, the increasing flexibility and power of CSS reinforce Lee's suspicion that one day it will be a

Using CSS backdrop-filter for UI EffectsUsing CSS backdrop-filter for UI EffectsApr 23, 2025 am 10:20 AM

Tips and tricks on utilizing the CSS backdrop-filter property to style user interfaces. You’ll learn how to layer backdrop filters among multiple elements, and integrate them with other CSS graphical effects to create elaborate designs.

SMIL on?SMIL on?Apr 23, 2025 am 09:57 AM

Well, it turns out that SVG's built-in animation features were never deprecated as planned. Sure, CSS and JavaScript are more than capable of carrying the load, but it's good to know that SMIL is not dead in the water as previously

'Pretty' is in the eye of the beholder'Pretty' is in the eye of the beholderApr 23, 2025 am 09:40 AM

Yay, let's jump for text-wrap: pretty landing in Safari Technology Preview! But beware that it's different from how it works in Chromium browsers.

CSS-Tricks Chronicles XLIIICSS-Tricks Chronicles XLIIIApr 23, 2025 am 09:35 AM

This CSS-Tricks update highlights significant progress in the Almanac, recent podcast appearances, a new CSS counters guide, and the addition of several new authors contributing valuable content.

Tailwind's @apply Feature is Better Than it SoundsTailwind's @apply Feature is Better Than it SoundsApr 23, 2025 am 09:23 AM

Most of the time, people showcase Tailwind's @apply feature with one of Tailwind's single-property utilities (which changes a single CSS declaration). When showcased this way, @apply doesn't sound promising at all. So obvio

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools