对齐中心不在中心位置

justify center not in center position

提问人:power-cut 提问时间:11/13/2023 最后编辑:power-cut 更新时间:11/14/2023 访问量:55

问:

我正在 Vue 中创建一个简单的步进器组件。我已经能够达到所需的功能,但即使在应用后,对齐也会向左移动justify-content: center

下面是我的代码 https://codesandbox.io/s/awesome-sky-pj9p5x

<template>
  <div class="flex justify-center full-width" style="border: 1px">
    <div class="flex no-wrap justify-center full-width">
      <div
        class="flex full-width items-center q-pb-xl"
        v-for="steps in data"
        :key="steps.key"
      >
        <div
          class="stepper-circle"
          :class="{
            'stepper-circle--filled': currentStep === steps.key,
            'stepper-circle--unfilled': currentStep < steps.key,
          }"
        >
          <q-icon
            v-if="currentStep > steps.key"
            name="check"
            color="white"
            size="xs"
          />
          <div
            v-else
            class="stepper-circle__dot"
            :class="{
              'stepper-circle__dot--active': currentStep === steps.key,
            }"
          />
          <div
            class="stepper-circle__label"
            :class="{ 'text-grey fw3': currentStep < steps.key }"
          >
            {{ steps.name }}
          </div>
        </div>
        <div
          :class="{
            'stepper-separator': steps.key < data.length,
            'stepper-separator--active': steps.key < currentStep,
          }"
        ></div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: "Stepper",

  data() {
    return {
      currentStep: 1,
      data: [
        {
          name: "Step 1",
          key: 1,
        },
        {
          name: "Step 2",
          key: 2,
        },
        {
          name: "Step 3",
          key: 3,
        },
      ],
    };
  },

  methods: {
    getSeparatorWidth(stepKey) {
      const totalSteps = this.data.length - 1;
      const widthPercentage = (100 / totalSteps) * stepKey;
      return widthPercentage;
    },
  },
};
</script>
<style lang="scss" scoped>
.items-center {
  align-items: center;
}
.full-width {
  width: 100%;
}
.justify-center {
  justify-content: center;
}
.flex {
  display: flex;
}
.stepper-separator {
  height: 2px;
  background: #d1d5db;
  transition: width 2s;
  flex-grow: 1;
}

.stepper-separator--active {
  background: #08104d;
}

.stepper-circle {
  position: relative;
  width: 32px;
  height: 32px;
  border: 2px solid #08104d;
  border-radius: 50%;
  background-color: #08104d;
  color: #000;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 10px;
  line-height: 15px;
}

.stepper-circle__dot {
  background: #d1d5db !important;
  border-radius: 50%;
  height: 10px;
  width: 10px;
}

.stepper-circle__dot--active {
  background: #08104d !important;
}

.stepper-circle__label {
  position: absolute;
  top: 45px;
  font-size: 10px;
  color: #08104d;
  font-weight: 600;
  display: flex;
  white-space: normal;
  width: 80px;
  justify-content: center;
  text-align: center;
}

.stepper-circle--filled {
  background: rgb(255, 255, 255) !important;
  border: 2px solid #08104d !important;
}

.stepper-circle--unfilled {
  background: white !important;
  border: 2px solid #d1d5db !important;
}
</style>
css vue.js sass vuejs2

评论

2赞 Quentin 11/13/2023
请阅读我的网站上的某些内容不起作用。我可以只粘贴一个链接吗?当外部资源消失或被修复时,依赖于外部资源才能理解的问题变得毫无用处。创建一个最小的可重现示例,并将其放在问题本身中。Stackoverflow 确实支持内联现场演示
0赞 Quentin 11/13/2023
(强调最小;如果你有布局问题,那么 JavaScript 和 Vue 模板是多余的。将其简化为简单的 HTML 和 CSS。这将是一个更有用的问题,更多的人将能够回答和学习)。

答:

1赞 iorgv 11/13/2023 #1

您必须最大化最后一个元素的宽度,并通过响应能力解决您的问题,如下所示:

.flex.full-width.items-center.q-pb-xl:nth-child(3) {max-width: 36px;}

否则它会将内容向左拉伸。

评论

0赞 power-cut 11/13/2023
不确定这是否适用于响应式布局。
0赞 iorgv 11/13/2023
正如我所看到的,它确实有效,你也会看到的。窗口大小可调整。
0赞 iorgv 11/13/2023
我已经更新了我的答案。
0赞 Keyboard Corporation 11/14/2023 #2

要使步进器居中,您可以将整个元件包装在另一个元件中并应用于该元件。divjustify-content: center;

例;

<template>
 <div class="flex justify-center full-width" style="border: 1px">
   <div class="flex justify-center">
     <div class="flex no-wrap justify-center full-width">
       <!-- Your existing code -->
     </div>
   </div>
 </div>
</template>

但是,如果还想使步进器组件垂直居中,则可以使用该属性沿容器中心的垂直线对齐柔性项。align-items: center;

例;

<template>
 <div class="flex justify-center items-center full-height" style="border: 1px">
   <div class="flex justify-center">
     <div class="flex no-wrap justify-center full-width">
       <!-- Your existing code -->
     </div>
   </div>
 </div>
</template>

请注意,要正常工作,flex 容器的父级的高度应大于 flex 容器的高度。因此,将高度设置为最大值;align-items: center;

.full-height {
 height: 100%;
}