How do I center floated elements?

前端 未结 12 2580
谎友^
谎友^ 2020-11-22 00:16

I\'m implementing pagination, and it needs to be centered. The problem is that the links need to be displayed as block, so they need to be floated. But then, text-alig

12条回答
  •  北荒
    北荒 (楼主)
    2020-11-22 00:54

    You can also do this by changing .pagination by replacing "text-align: center" with two to three lines of css for left, transform and, depending on circumstances, position.

    .pagination {
      left: 50%; /* left-align your element to center */
      transform: translateX(-50%); /* offset left by half the width of your element */
      position: absolute; /* use or dont' use depending on element parent */
    }
    .pagination a {
      display: block;
      width: 30px;
      height: 30px;
      float: left;
      margin-left: 3px;
      background: url(/images/structure/pagination-button.png);
    }
    .pagination a.last {
      width: 90px;
      background: url(/images/structure/pagination-button-last.png);
    }
    .pagination a.first {
      width: 60px;
      background: url(/images/structure/pagination-button-first.png);
    }
    
    

提交回复
热议问题