Working with Flexbox
Rus
Eng
Работа с Flexbox & Grid
I solve complex technical challenges for Bitrix and 1C—ranging from auditing existing code to website development and configuring complex integrations—with a guarantee of results.
Recommended

Website hosting

If you are choosing hosting for your project, I recommend Timeweb. Modern infrastructure, a wide range of plans from starter options to high-load VPS, and responsive tech support that actually helps instead of sending template replies. I host some of my own projects there and recommend it to my clients.

Go to site Affiliate link. The price remains the same for you.

All English-language materials have been translated fully automatically using the Google service

Center the responsive box horizontally and vertically

css

  .aOuter {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}  

html

<div class = "aOuter">
  <div class = "aInner"> </div>
</div> 

or

  .aOuter {
text-align: center;
}

.aOuter: before {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
}

.aInner {
display: inline-block;
vertical-align: middle;
}  

Press the footer to the bottom of the page \ container

css

  .aContainer {display: flex; flex-direction: column; height: 100%;}
.aHeader {flex: 0 0 auto; }
.aBody {flex: 1 0 auto; }
.aFooter {flex: 0 0 auto; }  

html

<div class = "aContainer">
  <div class = "aHeader"> </div>
  <div class = "aBody"> </div>
  <div class = "aFooter"> </div>
</div>

5 Column Flexbox Grid

Thanks for the example Paper Matthew

Stretch items to fit

  ul {
  display: flex;
}

ul li {
  flex-basis: auto;
  flex-grow: 1;
}
 

3 columns flexbox

  div {
  display: flex;
  flex-wrap: wrap;
}

div> div {
  flex-grow: 1;
  width: 33%;
  height: 100px;
}  

Change the order of the blocks

  .block1 {
  -webkit-box-ordinal-group: 2;
  -webkit-order: 1;
      -ms-flex-order: 1;
          order: 1;
}

.block2 {
  -webkit-box-ordinal-group: 3;
  -webkit-order: 2;
      -ms-flex-order: 2;
          order: 2;
}  

Automatically calculate the distance between flexbox columns

.container {
  display: flex;
  --gap: 100px;
  --columns: 4;
  gap: var(--gap);
  flex-wrap: wrap
}

.item {
  width: calc((100% / var(--columns)) - var(--gap) + (var(--gap) / var(--columns)));
}

Flexbox and object-fit for img

 img {
  object-fit: cover;
  object-position: center;
  width: 100%;
  height: 100%;
}  

Gap between two elements with display:flexbox

  .parent {
  display: flex;
}
.child + .child {
  margin-left: 12px;
}  

Columns of the same height

  .row-flex {
    display: flex;
    flex-flow: row wrap;
}  

Flexbox: in a vertical container we push one div down and center the other

<div class = "block">
    <div class = "block__middle"> middle </div>
    <div class = "block__bottom"> bottom </div>
</div>
  .block {
    height: 100vh;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    box-sizing: border-box;
}

.block: before {
    content: '';
    padding: 0;
    box-sizing: border-box;
}

.block__middle,
.block__bottom {
    padding: 0;
    width: 100%;
    box-sizing: border-box;
}
 

3 columns, the sides are pressed to the edges. Medium indent 20 pixels

html

<div class="flex">
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
</div>

css

.flex{
  display:flex;
  flex-wrap:wrap;
}
.item{
  width:calc((100% - 40px) / 3);
}
.item:nth-child(3n-1){
  margin:0 20px;
}

Automatic n-column splitting

with flex

-webkit-column-count: 3;
-moz-column-count: 3;
column-count: 3;

using grid

.grid {
	display: grid;
	grid-template-columns: repeat(7, 1fr);
	grid-gap: 0;
}
Aleksandr Tichiy
Author

Aleksandr Tichiy

Fullstack developer · 1C-Bitrix

I build and modernize online stores and corporate portals on 1C-Bitrix. Integrations with 1C, high-load performance, 152-FZ compliance. Writing about architecture and e-commerce.


Comments

There are no comments yet, you can be the first to leave it

Leave a comment

The site uses a comment pre-moderation system, so your message will be published only after approval by the moderator

You are replying to a user's comment

Send

FEEDBACK

Email me

Please message me to discuss the task. I work remotely and officially (as a self-employed professional); my hourly rate is 1,800 RUB, and I can dedicate 3–4 hours a day to the project. I respond quickly, and the consultation is free.

An error occurred while sending, please try again after a while
Message sent successfully

Phones

+7(993) 007-18-96

Email

info@tichiy.ru

Address

Россия, г. Москва

By submitting the form, you automatically confirm that you have read and accept the Privacy Policy site