我不明白为什么这两个元素没有并排排列?

时间:2017-06-05 00:24:57

标签: html css vertical-alignment

我想让这两个元素使用相对位置值并排排列。我很难理解为什么这两个元素不想对齐。

有人可以解释我做错了什么吗?如果可能的话找到解决方案?

https://jsfiddle.net/kerberonix/qcq68gfg/

HTML

<html>
  <body>
    <footer>
      <div class="test">
        <p>Footer Text</p>
      </div>
      <ul class="social-links">
        <li>Link 1</li>
        <li>Link 2</li>
      </ul>
    </footer>
  </body>

CSS

footer {
    position: absolute;
    bottom: 0;
    width: 100%;
    height: 70px;
    padding: 0 20px;
    background-color: #262626;
}

.test {
    position: relative;
    display: inline-block;
    top: 50%;
    width: 50%;
    transform: translateY(-50%);
}

footer p {
    font-size: 100%;
    color: #888;
}

.social-links {
    position: relative;
  display: inline-block;
    top: 50%;
    width: 50%;
    transform: translateY(-50%);
}

.social-links li {
    display: inline-block;
    margin-right: 35px;
    text-decoration: none;
}

.social-links li:last-child { 
  margin: 0; 
}

3 个答案:

答案 0 :(得分:0)

我会删除文本和社交元素上的所有定位,并在父级上使用display: flex; align-items: center;

footer {
  position: absolute;
  bottom: 0;
  height: 70px;
  padding: 0 20px;
  background-color: #262626;
  display: flex;
  align-items: center;
  box-sizing: border-box;
  left: 0; right: 0;
}

.test {
  position: relative;
}

footer p {
  font-size: 100%;
  color: #888;
}


/*--------------------------------------------------------------------------------------------
SOCIAL LINKS
--------------------------------------------------------------------------------------------*/

.social-links {
  position: relative;
  color: white;
  text-align: right;
  padding: 0;
}

.test, .social-links {
  flex: 1 0 0;
}

.social-links li {
  display: inline-block;
  margin-left: 35px;
  text-decoration: none;
}

.social-links li:last-child {
  margin: 0;
}
<html>

  <body>
    <footer>
      <div class="test">
        <p>Footer Text</p>
      </div>
      <ul class="social-links">
        <li>Link 1</li>
        <li>Link 2</li>
      </ul>
    </footer>
  </body>

</html>

答案 1 :(得分:0)

对这两个元素使用position: absolute,并考虑页脚填充引起的宽度和位置的额外20px。另外,请不要忘记重置ul的填充。有关详细信息,请参阅我的代码段中的代码:

&#13;
&#13;
html,
body {
  margin: 0;
}

footer {
  box-sizing: border-box;
  position: absolute;
  bottom: 0;
  width: 100%;
  height: 70px;
  padding: 0 20px;
  background-color: #262626;
}

.test {
  position: absolute;
  display: inline-block;
  top: 50%;
  width: calc(50% - 20px);
  transform: translateY(-50%);
}

footer p {
  font-size: 100%;
  color: #888;
}

.social-links {
  position: absolute;
  left: calc(50% - 20px);
  top: 50%;
  width: calc(50% - 20px);
  transform: translateY(-50%);
  text-align: right;
  margin: 0;
  padding: 0;
}

.social-links ul {}

.social-links li {
  display: inline-block;
  margin-right: 35px;
  text-decoration: none;
}

.social-links li:last-child {
  margin: 0;
}
&#13;
<footer>
  <div class="test">
    <p>Footer Text</p>
  </div>
  <ul class="social-links">
    <li>Link 1</li>
    <li>Link 2</li>
  </ul>
</footer>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

现在它好了我觉得你得到了你需要的东西只需替换s social links

中的代码
footer {
    position: absolute;
    bottom: 0;
    width: 100%;
    height: 70px;
    padding: 0 20px;
    background-color: #262626;
}

.test {
    position: relative;
  display: inline-block;
    top: 50%;
    width: 50%;
    transform: translateY(-50%);
}

footer p {
    font-size: 100%;
    color: #888;
}

/*--------------------------------------------------------------------------------------------
SOCIAL LINKS
--------------------------------------------------------------------------------------------*/

.social-links {
    position: relative;
  display: inline-block;
    top: 50%;
    width: 50%;
    transform: translateY(-50%);
  text-align: right;
}

.social-links li {
  top 150px;
  height:20px;
  width: 250px;
  position: relative;
  left: calc(50% – 125px);

}

.social-links li:last-child { 
  margin: 0; 
}