【问题标题】:How to center the block horizontally relative to another block如何使块相对于另一个块水平居中
【发布时间】:2017-10-28 11:25:06
【问题描述】:

伙计们!请问,有人能写出如何将 .fl 和 .fr 相对于 .footer 水平居中吗?我使用像素来做类似于居中的事情,但我需要编辑以使元素居中。

这里是 HTML:

<!DOCTYPE html>
<head>
    <title>Google</title>
    <meta charset="utf-8">
    <link href="./style.css" rel="stylesheet">
</head>
<body>
<div class="footer">
    <div class="fl">
        <a href="#" class="fle al">Advertising</a>
        <a href="#" class="fle al">Business</a>
        <a href="#" class="fle al">About</a>
    </div>
    <div class="fr">
        <a href="#" class="fre al">Privacy</a>
        <a href="#" class="fre al">Terms</a>
        <a href="#" class="fre al">Settings</a>
        <a href="#" class="fre al">Use Google.com</a>
    </div>
</div>
</body>
</html>

这里是 CSS:

.footer {
    color: #666;
    background: #f2f2f2;
    border-top: 1px solid #e4e4e4;
    position: absolute;
    bottom: 0;
    font-size: 13px;
    height: 37px;
    width:100%;
}

.fl {
    white-space: nowrap;
    position:absolute;
    top:12px;
}

.fr {
    white-space: nowrap;
    position:absolute;
    top:12px;
    right:0;
}

.fle {
    margin-left: 30px;
}

.fre {
    margin-right: 30px;
}

.al:hover {
    text-decoration:underline;
    cursor:pointer;
}

a {
    color:inherit;
    text-decoration:inherit;
}

【问题讨论】:

    标签: html css alignment block centering


    【解决方案1】:

    如下更改你的 fl 和 fr 类:

      .fl {
      position: relative;
      top: 50%;
      -webkit-transform: translateY(-50%);
      -ms-transform: translateY(-50%);
      transform: translateY(-50%);
      float: left;
      }
      .fr {
      position: relative;
      top: 50%;
      -webkit-transform: translateY(-50%);
      -ms-transform: translateY(-50%);
      transform: translateY(-50%);
      float: right;
      }
    

    【讨论】:

      【解决方案2】:

      检查下面的代码段。我用position: relative;display: inline; 表示.fl.frtext-align:center 表示.footer

      .footer {
        color: #666;
        background: #f2f2f2;
        border-top: 1px solid #e4e4e4;
        position: absolute;
        bottom: 0;
        font-size: 13px;
        height: 37px;
        width: 100%;
        text-align: center;
      }
      
      .fl {
        white-space: nowrap;
        position: relative;
        top: 12px;
        display: inline;
      }
      
      .fr {
        white-space: nowrap;
        position: relative;
        top: 12px;
        display: inline;
        right: 0;
      }
      
      .fle {
        margin-left: 30px;
      }
      
      .fre {
        margin-right: 30px;
      }
      
      .al:hover {
        text-decoration: underline;
        cursor: pointer;
      }
      
      a {
        color: inherit;
        text-decoration: inherit;
      }
      <div class="footer">
        <div class="fl">
          <a href="#" class="fle al">Advertising</a>
          <a href="#" class="fle al">Business</a>
          <a href="#" class="fle al">About</a>
        </div>
        <div class="fr">
          <a href="#" class="fre al">Privacy</a>
          <a href="#" class="fre al">Terms</a>
          <a href="#" class="fre al">Settings</a>
          <a href="#" class="fre al">Use Google.com</a>
        </div>
      </div>

      【讨论】:

      • 哎呀,这是真的,但我刚刚注意到我打错了。不是水平的,而是垂直的
      • 无论如何,我已经看到了一种解决问题的方法。谢谢你的回答=)
      猜你喜欢
      • 2021-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-28
      • 2020-09-16
      • 2020-06-16
      • 1970-01-01
      • 2019-12-11
      相关资源
      最近更新 更多