Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static Padding Between CSS Flex Items

I am trying to create a flexible layout in CSS that will wrap according to the client's resolution.

For example, on an ipad in landscape (1024px wide), i would like to display the following:

enter image description here

But on a playbook in portrait (600px wide), i would like to display the following:

enter image description here

I have almost everything working except the 20px margins. I cannot seem to figure out how to specify the margins for wrapped elements.

Here is my code:

HTML:

<div class="box-row-fluid">
     <div class="col">Box 1</div>
     <div class="col">Box 2</div>
</div>

CSS:

.box-row-fluid {
    display: -webkit-flex;
    -webkit-flex-flow: row wrap;
}

.box-row-fluid > .col {
    -webkit-flex: 1 400px;
    background: #fff;
}

Code assumes that the browser is a valid webkit browser.

I would appreciate some help. Thank you for your time.

like image 803
Markus Avatar asked Oct 15 '12 16:10

Markus


People also ask

How do you put a space between two flex items in CSS?

To set space between the flexbox you can use the flexbox property justify-content you can also visit all the property in that link. We can use the justify-content property of a flex container to set space between the flexbox.

How do I reduce the gap between my Flex items?

You set the left and right margins of each item to x/2 so that the distance between the items will be x (margin + margin). Then you wrap all of the items in a container with a left and right margin of -x/2 . This will hide the margin on the items at the edges of each row. Save this answer.

Can I use gap with Flex?

The gap property is designed for use in grid, flex and multi-column layouts.


1 Answers

You can certainly do it like this My Fiddle (Edited background color for margin visibility)

CSS

.col:nth-child(1) {
   margin-bottom: 20px;
}

For adding a margin between 2 div horizontally you can use @media query for particular resolutions and add margin-right: 20px; for the other one

Explanation: Use :nth-child(1) because you are using class="col" for both <div>

like image 95
Mr. Alien Avatar answered Sep 20 '22 17:09

Mr. Alien