Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make container of elements with margin in-between elements but not the container?

Tags:

css

css-float

  • Container #666 has margin: 20px; overflow: hidden;.
  • Nodes #333 have margin: 20px 0 0 20px; float: left;.

Example, http://jsbin.com/owejal/3/edit or picture:

double margin

However, the intended result is:

  • container with 20px margin,
  • children with 20px margin in-between, but not with the container.

enter image description here

This could be achieved using negative padding (i.e. if container had padding: -20px 0 0 -20px), though such thing does not exist.

The desired result can be achieved using additional element (http://jsbin.com/owejal/4/), though I am keen to learn whether there is CSS only solution.

like image 921
Gajus Avatar asked Aug 06 '13 19:08

Gajus


People also ask

Is the margin is the space between elements?

Margin is the space between the border and the next element of your design. Think of the space outside the border and between it and the other elements. This is all the margin. Margin goes around all four sides of the content and you can target and change the margin for each side.

How do I add a space between elements so they fill their container div?

You can do this with Flexbox and justify-content: space-between .

Can we set the margins for an element?

The CSS margin properties are used to create space around elements, outside of any defined borders. With CSS, you have full control over the margins. There are properties for setting the margin for each side of an element (top, right, bottom, and left).

How do you prevent margin collapse?

How to Avoid Margin Collapse. First, remember that margins should preferably be used to increase the distance between sibling elements, not to create spacing between a child and a parent. If you need to increase space within the Flow layout, reach first for padding if possible.


2 Answers

If you only care about the spacing between the elements, you can discard the pseudo element. It's only there for the background.

http://codepen.io/cimmanon/pen/mucDv

<div class="foo"></div>
<div class="group">
  <div class="node"></div>
  <div class="node"></div>
  <div class="node"></div>
  <div class="node"></div>
  <div class="node"></div>
  <div class="node"></div>
  <div class="node"></div>
</div>
<div class="foo"></div>

The CSS:

.group {
  overflow: hidden;
  margin: -10px 0 -10px 10px;
  padding-right: 10px;
  position: relative;
}

.group:before {
  display: block;
  content: '';
  position: absolute;
  z-index: -1;
  top: 10px;
  right: 20px; /* 20px instead of 10px due to padding */
  bottom: 10px;
  left: 10px;
  background: #666;
}

.node {
  width: 100px;
  height: 100px;
  float: left;
  background: #333;
  margin: 10px;
}

.foo {
  height: 20px;
  background: #00f;
  margin: 20px;
}
like image 54
cimmanon Avatar answered Nov 15 '22 10:11

cimmanon


This is a little hacky, but how about just hiding the top and left margin areas with some strategically placed pseudo-elements? http://jsfiddle.net/SUJtd/

.foo {height:20px; background:#00f; margin:20px 20px 0;}

.group {overflow:hidden; margin:0 20px 20px 0; background:#666; position:relative;}
.group:before{content:""; position:absolute; top:0; left:0; right:0; height:20px; background:#fff;}
.group:after{content:""; position:absolute; top:0; bottom:0; left:0; width:20px; background:#fff;}

.node {width:100px; height:100px; float:left; background:#333; margin:20px 0 0 20px;}
like image 30
andi Avatar answered Nov 15 '22 12:11

andi