N-by-2 grid, and if the last row has only a single column, center it

前端 未结 2 1615
盖世英雄少女心
盖世英雄少女心 2021-01-22 07:54

I want to have an N-by-2 grid like this, in which some elements (columns) might be set to display:none based on run-time factors, making the number of rows and colu

2条回答
  •  一生所求
    2021-01-22 08:48

    In your case, you can use last-child property. But you need to find the odd last element, because even last element no need to come in center place. So you can use it in the following way.

    .col-xs-6
    {
      display:inline-block;
      text-align:center;
    }
    .col-xs-6:nth-last-child(1):nth-child(odd) { /* This will find last child with odd element */
      display:inline-block;
      text-align:center;
      width:100%;
    }
    

    DEMO

提交回复
热议问题