CSS - Flex elements with margin right

flex; align right
flex margin
flexbox padding
add margin between flex elements
margin auto not working with display flex
flex-wrap remove margin
flex-end not working
flex-wrap; margin

I want to create a grid of blocks using css flex.

The blocks need to be in three columns and they should be a 3rd of the width of the parent container.

My problem is I need a right margin on the blocks.

The blocks need to be a percentage of the container so I cant use space between.

.block {
  border: 1px solid lightgrey;
  display: flex;
  flex-wrap: wrap;
  padding: 5px;
  max-width: 900px;
}

.block__item {
  background: grey;
  height: 20px;
  margin-right: 2px;
  //margin-bottom: 2px;
  width: 33.33%;
}

.block__item:nth-child(3n){
  margin-right: 0;
}
<div class="block">
  <div class="block__item"></div>
  <div class="block__item"></div>
  <div class="block__item"></div>
  <div class="block__item"></div>
  <div class="block__item"></div>
</div>

Judging by your question, I believe this is what you are trying to accomplish:

  .block {
  border: 1px solid lightgrey;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  -webkit-flex-wrap: wrap;
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
  padding: 5px;
  max-width: 900px;
  -webkit-justify-content: space-between;
  -ms-flex-pack: justify;
   justify-content: space-between;
}

.block__item {
  background: grey;
  height: 20px;
  margin-right: 2px;
  margin-bottom: 2px;
  width: 33%;
}

<div class="block">
    <div class="block__item"></div>
      <div class="block__item"></div>
      <div class="block__item"></div>
      <div class="block__item"></div>
      <div class="block__item"></div>
      <div class="block__item"></div>
      <div class="block__item"></div>
      <div class="block__item"></div>
      <div class="block__item"></div>
    </div>

I added the vendor prefixes so that it displays consistently across all browsers. You can adjust the spacing between boxes by adjusting the percentage (i.e. 31% width). If you plan on using Flexbox often you should check out this site for quick vendor versions: http://the-echoplex.net/flexyboxes/

How Auto Margins Work in Flexbox, Now I want to push that “Menu” item to the far right. .parent { border-radius: 10px; padding: 0.5rem; display: flex; border: dashed 2px #78909C  Internal table-* elements don't have margins, use border-spacing instead: any, except flex, inline-flex, or table-* any fixed or absolute: 0, except if both margin-left and margin-right are set to auto. In this case, it is set to the value centering the border area inside the available width, if fixed. Absolutely positioned layout mode: flex, inline-flex any any

Maybe you can simply decrease the width of the blocks, and the rest of the witdh assign it to the margin.

Like this:

.block__item {
    background: grey;
    height: 20px;
    margin-right: 3%;
    width: 30%;
}

The peculiar magic of flexbox and auto margins, When we add margin-left: auto to the .child element like so: to auto , then we can let the flex item push itself into the bottom-right of the parent:. CSS Margins. Margins Margin The row-reverse value stacks the flex items horizontally (but from right to left):.flex-container The direct child elements of a

The problem is the padding on the container. But you can use width: calc((100% - 10px) / 3); to calculate the correct 33,33% of the container width excluding the padding.

.block {
  border: 1px solid lightgrey;
  display: flex;
  flex-wrap: wrap;
  padding: 5px;
  max-width: 900px;
}

.block__item {
  background: grey;
  height: 20px;
  margin-right: 2px;
  margin-bottom: 2px;
  width: calc((100% - 10px) / 3);
}

.block__item:nth-child(3n){
  margin-right: 0;
}
<div class="block">
  <div class="block__item"></div>
  <div class="block__item"></div>
  <div class="block__item"></div>
  <div class="block__item"></div>
  <div class="block__item"></div>
</div>

Everything You Need To Know About Alignment In Flexbox , I explained what happens when you declare display: flex on an element. For the entire history of CSS Layout, being able to properly align things If you have a flex-direction of row and are in a left to right language such as wrapper for your main page content by setting a margin left and right of auto )  Method #2: Flex Auto Margins & Invisible Flex Item (DOM element) With a combination of auto margins and a new, invisible flex item the layout can be achieved. The new flex item is identical to item D and is placed at the opposite end (the left edge).

Mastering CSS Layout with Flexbox, The margin-right CSS property sets the margin area on the right side In this case, it is set to the value centering the element inside its parent. flex , inline-​flex, any, any, 0 , except if there is any positive horizontal free space. When Sam says, “that item will automatically extend its specified margin to occupy the extra space in the flex container,” the way my empty filing cabinet brain interprets that is like so: Setting the margin property on a flex child will push the child away from that direction. Set margin-left to auto, the child will push right. Set margin-top to auto and the child will push to the bottom.

margin-right, There is however the ability to wrap flex items onto new lines, creating We start on the right then go onto the second line and again start from the right. that the negative margin can pull the items up to that wrapper element. The element will then take up the specified width, and the remaining space will be split equally between the two margins: This div element is centered. CSS Layout - Horizontal & Vertical Align, There are a few ways to align elements in CSS. for example, if you have an icon displayed alongside text, or text of different sizes.

Mastering Wrapping of Flex Items, Hello I want the last item in a flexbox to be justify-content: flex-end; how do I do solid' to the item so it will be easier to see where the element line up. ul { flex-​direction: row; } li { margin-right: 1rem; } .right { margin-top: 0;  In this next example I have items laid out with flex-direction: row-reverse and justify-content: flex-end. In a left to right language the items all line up on the left. Try changing flex-direction: row-reverse to flex-direction: row. You will see that the items now move to the right hand side.

Comments
  • "so I cant use space between" - space-between in combination with no margin and width: calc(33.33% - 2px) …?