d-_-b
d-_-b

Reputation: 23211

CSS clear left every 3rd element

I feel like peter griffin in that CSS meme, messing around with CSS. Can someone please explain how I can get rows of 3 and why the code below isn't working?

Here is a jsfiddle example to give more context

  <div id="container" class="mdl-grid mdl-cell mdl-cell--12-col mdl-color--white">
      <a class="mdl-button mdl-js-button mdl-button--fab mdl-js-ripple-effect mdl-color-text--white mdl-color--blue-A400"
      ng-repeat="i in [1,2,3,4,5,6,7,8,9] track by $index" 
      ng-bind="i">

      </a>
  </div>

#container a {
    line-height: 6rem;
    font-size: 2.5rem;
    width: 80px;
    height: 80px;
    margin-bottom: 10px;
    margin-top:10px;
    float:left;
    display:inline-block;
}
#container a:nth-child(3n) {
     color:red !important;
     clear:left;
}

Upvotes: 0

Views: 855

Answers (2)

Johannes
Johannes

Reputation: 67798

There is a class .mdl-grid in #container that defines #container as a flexbox-container. That makes its children flex-items (i.e. float is deactivated by that)

Overwrite that with

#container.mdl-grid {
  display: block;
}

and change the last selector to

#container a:nth-child(3n + 1) { ... }

Here's the changed fiddle (I know, the white background doesn't fit, but you'll have to fix that by yourself...)

https://jsfiddle.net/eqy1f8k4/2/

Upvotes: 1

Jon P
Jon P

Reputation: 19797

You're floating inline blocks, which are , well , inline, so clearing the float will do nothing! Change display:inline-block to display:block. Also there is display:flex on your container, remove that and you're good: https://jsfiddle.net/6w8a8qmt/1/

Upvotes: 1

Related Questions