flexbox对齐中心垂直和水平不正常

时间:2018-04-21 16:41:49

标签: javascript html css vue.js flexbox

我知道将Flex容器居中的基本概念,使用对齐内容:center和align-items center,就是盒子没有正确对齐:S

这是我到目前为止所尝试的内容:

<template>
  <div>
    <main class="container">
        <div class="container__left">
          <div>I'm Jonathan Doe</div>
          <div>Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum v Lorem ipsumv Lorem ipsumLorem ipsumLorem ipsum v v</div>
          <ul>
            <li>one bla bla bla</li>
            <li>two bla bla bla</li>
            <li>three bla bla bla</li>
          </ul>
          <div>
            <button>first</button>
            <button>second</button>
          </div>
        </div>
        <div class="container__right">

        </div>
    </main>
  </div>
</template>

<script>
export default {
  name: "app",
  data() {
    return {
      msg: "Welcome to Your Vue.js App"
    };
  }
};
</script>

<style lang="scss">
@import url("https://fonts.googleapis.com/css?family=Exo");

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}

body {
  padding: 1rem;
  font-family: "Exo", sans-serif;
}

.main {
  display: flex;
  justify-content: center;
  align-items: center;
}

.container {
  height: 80vh;
  width: 80%;
  border: 8px solid #3f3f3f;
}

.container__left {
  height: 100%;
  width: 100%;
  clip-path: polygon(0 0, 50% 0, 48% 100%, 0 100%);
  background-color: #1c1f24;
}
</style>

宽度和高度可能有问题,但我无法弄清楚这有什么问题,有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

首先,你在ur css中使用 .main 而不是 main 。 第二件事是,你不需要flexbox来集中你的盒子,

margin:auto; 
容器上的

就足够了

检查下面的代码段

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  padding: 1rem;
  font-family: "Exo", sans-serif;
}

.main-parent {
  height: 100%;
  display: flex;
}

main {
  margin: auto;
}

.container {
  height: 80vh;
  width: 80%;
  border: 8px solid #3f3f3f;
}

.container__left {
  height: 100%;
  width: 100%;
  clip-path: polygon(0 0, 50% 0, 48% 100%, 0 100%);
  background-color: #1c1f24;
}
<div class="main-parent">
  <main class="container">
    <div class="container__left">
      <div>I'm Jonathan Doe</div>
      <div>Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum v Lorem ipsumv Lorem ipsumLorem ipsumLorem ipsum v v</div>
      <ul>
        <li>one bla bla bla</li>
        <li>two bla bla bla</li>
        <li>three bla bla bla</li>
      </ul>
      <div>
        <button>first</button>
        <button>second</button>
      </div>
    </div>
    <div class="container__right">

    </div>
  </main>
</div>