Coder Social home page Coder Social logo

negative margins about css-protips HOT 8 CLOSED

allthingssmitty avatar allthingssmitty commented on May 9, 2024 5
negative margins

from css-protips.

Comments (8)

albertorestifo avatar albertorestifo commented on May 9, 2024 1

Plenty of times I've used this trick when creating grids

from css-protips.

neworldwebsites avatar neworldwebsites commented on May 9, 2024

Congrat

from css-protips.

vikrantnegi avatar vikrantnegi commented on May 9, 2024

Nice.

from css-protips.

alexcarpenter avatar alexcarpenter commented on May 9, 2024

or you could simplify it to be:

.navigation {
  display: flex;
}

.navigation li:not(:first-of-type) {
  margin-right: 1rem;
}

from css-protips.

marcobiedermann avatar marcobiedermann commented on May 9, 2024

@alexcarpenter
This is not the same:

Your example might work items which are only placed in a row.
It does not work with a grid like layout (rows & cols).
With negative margins you can cut of the gutter on all edges (top, right, bottom, left).
Great for a multi line image gallery or products page

from css-protips.

manfromanotherland avatar manfromanotherland commented on May 9, 2024

I think we could also use the adjacent selector – that's what I end up doing in most cases:

.nav li      { display: inline-block; }
.nav li + li { margin-left: 1rem; }

Supported in every browser too!

from css-protips.

marcobiedermann avatar marcobiedermann commented on May 9, 2024

@EdmundoJr As mentioned this only would work in one direction. Horizontally or vertically.

Using negative margins will solve the problem with space between all items equally. No matter which direction (inline or stacked).

from css-protips.

marcobiedermann avatar marcobiedermann commented on May 9, 2024

Maybe this example will clear some things up: https://jsfiddle.net/marcobiedermann/yjL5qf0b/

<div class="outline">
  <ul class="items">
    <li class="item"></li>
    <li class="item"></li>
    <li class="item"></li>
    <li class="item"></li>
    <li class="item"></li>
    <li class="item"></li>
    <li class="item"></li>
    <li class="item"></li>
    <li class="item"></li>
  </ul>
</div>
.outline {
  border: 1px solid blue;
}

.items {
  display: flex;
  flex-wrap: wrap; 
  list-style: none;
  margin: -0.5rem;
  overflow: hidden;
  padding: 0;
}

.item {
  background-color: tomato;
  flex-basis: calc(33.33% - 1rem);
  margin: 0.5rem;
  min-height: calc(100vw / 3 - 1rem);
}

image

from css-protips.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.