如何相对于另一个块水平居中块

时间:2017-05-27 19:10:53

标签: html css alignment block centering

家伙!请问,有人可以写一下如何将.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;
}

2 个答案:

答案 0 :(得分:2)

更改你的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;
  }

答案 1 :(得分:0)

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

&#13;
&#13;
.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;
}
&#13;
<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>
&#13;
&#13;
&#13;