Skip to content

Flex 弹性布局

设置元素之间的间距

代码演示

vue
<template>
  <a-flex gap="middle" vertical>
    <a-radio-group v-model:value="value">
      <a-radio value="horizontal">horizontal</a-radio>
      <a-radio value="vertical">vertical</a-radio>
    </a-radio-group>
    <a-flex :vertical="value === 'vertical'">
      <div
        v-for="(item, index) in new Array(4)"
        :key="item"
        :style="{ ...baseStyle, background: `${index % 2 ? '#1677ff' : '#1677ffbf'}` }"
      />
    </a-flex>
  </a-flex>
</template>
<script setup>
import { ref } from 'vue';
const value = ref('horizontal');
const baseStyle {
  width: '25%',
  height: '54px',
};
</script>

对齐方式

设置对齐方式。

Select justify :

Select align :

vue
<template>
  <a-flex gap="middle" align="start" vertical>
    <p>Select justify :</p>
    <a-segmented v-model:value="justify" :options="justifyOptions" />
    <p>Select align :</p>
    <a-segmented v-model:value="alignItems" :options="alignOptions" />
    <a-flex :style="{ ...boxStyle }" :justify="justify" :align="alignItems">
      <a-button type="primary">Primary</a-button>
      <a-button type="primary">Primary</a-button>
      <a-button type="primary">Primary</a-button>
      <a-button type="primary">Primary</a-button>
    </a-flex>
  </a-flex>
</template>
<script setup>
import { reactive, ref } from 'vue';
const justifyOptions = reactive([
  'flex-start',
  'center',
  'flex-end',
  'space-between',
  'space-around',
  'space-evenly',
]);
const alignOptions = reactive(['flex-start', 'center', 'flex-end']);
const justify = ref(justifyOptions[0]);
const alignItems = ref(alignOptions[0]);
const boxStyle = {
  width: '100%',
  height: '120px',
  borderRadius: '6px',
  border: '1px solid #40a9ff',
};
</script>

设置间隙

使用 gap 设置元素之间的间距,预设了 smallmiddlelarge 三种尺寸,也可以自定义间距。

vue
<template>
  <a-flex gap="middle" vertical>
    <a-radio-group v-model:value="gapSize">
      <a-radio value="small">small</a-radio>
      <a-radio value="middle">middle</a-radio>
      <a-radio value="large">large</a-radio>
      <a-radio value="customize">customize</a-radio>
    </a-radio-group>
    <template v-if="gapSize === 'customize'">
      <a-slider v-model:value="customGapSize" />
    </template>
    <a-flex :gap="gapSize !== 'customize' ? gapSize : customGapSize">
      <a-button type="primary">Primary</a-button>
      <a-button>Default</a-button>
      <a-button type="dashed">Dashed</a-button>
      <a-button type="link">Link</a-button>
    </a-flex>
  </a-flex>
</template>
<script setup>
import { ref } from 'vue';
const gapSize = ref('small');
const customGapSize = ref(0);
</script>

API

属性说明类型默认值
verticalflex 主轴的方向是否垂直,使用 flex-direction: columnbooleanfalse
wrap设置元素单行显示还是多行显示参考 flex-wrapnowrap
justify设置元素在主轴方向上的对齐方式参考 justify-contentnormal
align设置元素在交叉轴方向上的对齐方式参考 align-itemsnormal
flexflex CSS 简写属性参考 flexnormal
gap设置网格之间的间隙small | middle | large | string | number-
component自定义元素类型Componentdiv