Content move down when I apply float property

后端 未结 3 1679
北恋
北恋 2021-01-25 21:28

I have to float the class time to right but when I try to float it right, its content moves down. Without float contents look fine but I have to float it.

h

3条回答
  •  悲&欢浪女
    2021-01-25 22:12

    First of all, don't use margin-top: -3px to fight off that default padding browsers set to body, as this padding varies from browser to browser. To achieve a more consistent visual output, you can try normalising this properties with something like:

    html, body {
        margin: 0;
        padding: 0;
    }
    

    Now, coming to your alignment issue, try removing height and width from .time and vertical-align: middle to your li elements.

    jsFiddle: → here

    Code: (expand the snippet)

    html, body {
      margin: 0;
      padding: 0;
    }
    .main-nav, .main-nav ul li {
      display: inline-block;
    }
    .main-nav {
      color: #FFF;
      width: 100%;
      background-color: #5e2d91;
      line-height: 24px;
    }
    .main-nav ul li {
      padding: 0px 10px;
      vertical-align: middle;
    }
    .main-nav ul li a {
      color: #FFF;
      text-decoration: none;
      padding: 20px 14px;
    }
    .main-nav ul li a:hover {
      background-color: #0098aa;
    }
    .main-nav ul li a:active {
      background-color: #6A006A;
    }
    .time {
      float: right;
      border-color: #718b88;
      font-family: sans-serif;
      font-weight: bold;
      border-style: solid;
      margin-left: 10%;
    }

提交回复
热议问题