How can I set margin top/bottom to a li?

前端 未结 4 1310
轮回少年
轮回少年 2020-12-20 06:16

Here is my code:



        
4条回答
  •  情话喂你
    2020-12-20 06:47

    Your problem is due to collapsing margins - from https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Box_Model/Mastering_margin_collapsing:

    Top and bottom margins of blocks are sometimes combined (collapsed) into a single margin whose size is the largest of the margins combined into it, a behavior known as margin collapsing.

    To get around this, I would wrap the content of the li in a span or div and put your original li padding on that, and then put the 3px margin as padding on your li instead:

    li {
      list-style: none;
      padding: 3px 0; /* this is in place of your margin */
    }
    li + li {
      border-top: 1px solid #eff0f1;
    }
    li > span {
      display: block;
      padding: 5px 4px 6px 7px; /* this is you original li padding */
    }
    li:hover > span {
      background-color: #f7f8f8;
    }
    • something
    • something else
    • something else again

    UPDATE

    If you cannot change your html then you will need something like this:

    li {
      list-style: none;
      padding: 5px 4px 6px 7px;
      margin-bottom:7px; /* 3px margin plus 1px for the border */
      position:relative;
    }
    li + li:before {        /* this is a pseudo element to create the border */
      content:'';
      display:block; 
      height:1px;
      background: #eff0f1;
      position:absolute;
      top:-4px;
      left:0;
      right:0;
    }
    li:hover {
      background-color: #f7f8f8;
    }
    • something
    • something else
    • something else again

提交回复
热议问题