How to get tiles centered and left-justified at the same time

前端 未结 9 1921
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-30 10:33

I have a container of tiles (or divs) and I want the container to be centered, while the tiles are left justified in the container.

so if the window is small:

<
9条回答
  •  再見小時候
    2020-11-30 11:17

    No idea how to do it with CSS, if it's even possible. But with jQuery it's rather simple:

    $('button').click(function(){
    
      $('nav ul').each(function(){
        
        $parent = $(this).parent();
        
        $parent.width( $(this).width() );
        
      });
    });
    nav {
      display: inline-block;
      text-align: left; /* doesn't do anything, unlike some might guess */
    }
    ul {
      display: inline;
    }
    
    /* needed style */
    ul {
      padding: 0;
    }
    body {
      width: 420px;
    }
    
    /* just style */
    body {
      background: #ffffd;
      margin: 1em auto;
    }
    button {
      display: block;
    }
    nav {
      background: #bbb;
      margin: 1rem auto;
      padding: 0.5rem;
    }
    li {
      display: inline-block;
      width: 40px;
      height: 20px;
      border: solid thin #777;
      margin: 4px;
      background: #999;
      text-align: center;
    }
    
    
    
    
    
    
    

提交回复
热议问题