Skip to content

Layout Component

Learning Objectives

  • Master the 24-grid system usage
  • Understand responsive breakpoint design
  • Complete responsive layout implementation

Detailed Learning Content

1. Layout Grid System

1.1 Basic Concepts

Element Plus's grid system is based on 24 columns, using el-row and el-col components to create flexible layouts.

1.2 Basic Usage

vue
<template>
  <!-- Basic Grid -->
  <el-row>
    <el-col :span="24"><div class="grid-content bg-purple-dark"></div></el-col>
  </el-row>
  <el-row>
    <el-col :span="12"><div class="grid-content bg-purple"></div></el-col>
    <el-col :span="12"><div class="grid-content bg-purple-light"></div></el-col>
  </el-row>
  <el-row>
    <el-col :span="8"><div class="grid-content bg-purple"></div></el-col>
    <el-col :span="8"><div class="grid-content bg-purple-light"></div></el-col>
    <el-col :span="8"><div class="grid-content bg-purple"></div></el-col>
  </el-row>
  <el-row>
    <el-col :span="6"><div class="grid-content bg-purple"></div></el-col>
    <el-col :span="6"><div class="grid-content bg-purple-light"></div></el-col>
    <el-col :span="6"><div class="grid-content bg-purple"></div></el-col>
    <el-col :span="6"><div class="grid-content bg-purple-light"></div></el-col>
  </el-row>
</template>

<style>
.el-row {
  margin-bottom: 20px;
}
.el-col {
  border-radius: 4px;
}
.bg-purple-dark {
  background: #99a9bf;
}
.bg-purple {
  background: #d3dce6;
}
.bg-purple-light {
  background: #e5e9f2;
}
.grid-content {
  border-radius: 4px;
  min-height: 36px;
}
</style>

2. Column Spacing

Use the gutter attribute to specify spacing between columns.

vue
<template>
  <el-row :gutter="20">
    <el-col :span="6"><div class="grid-content bg-purple"></div></el-col>
    <el-col :span="6"><div class="grid-content bg-purple"></div></el-col>
    <el-col :span="6"><div class="grid-content bg-purple"></div></el-col>
    <el-col :span="6"><div class="grid-content bg-purple"></div></el-col>
  </el-row>
</template>

3. Column Offset

Use the offset attribute to specify the number of columns to offset.

vue
<template>
  <el-row :gutter="20">
    <el-col :span="6"><div class="grid-content bg-purple"></div></el-col>
    <el-col :span="6" :offset="6"><div class="grid-content bg-purple"></div></el-col>
  </el-row>
  <el-row :gutter="20">
    <el-col :span="6" :offset="6"><div class="grid-content bg-purple"></div></el-col>
    <el-col :span="6" :offset="6"><div class="grid-content bg-purple"></div></el-col>
  </el-row>
  <el-row :gutter="20">
    <el-col :span="12" :offset="6"><div class="grid-content bg-purple"></div></el-col>
  </el-row>
</template>

4. Alignment

Use the justify attribute to define how the columns are aligned.

vue
<template>
  <el-row class="row-bg">
    <el-col :span="6"><div class="grid-content bg-purple"></div></el-col>
    <el-col :span="6"><div class="grid-content bg-purple-light"></div></el-col>
    <el-col :span="6"><div class="grid-content bg-purple"></div></el-col>
  </el-row>
  <el-row class="row-bg" justify="center">
    <el-col :span="6"><div class="grid-content bg-purple"></div></el-col>
    <el-col :span="6"><div class="grid-content bg-purple-light"></div></el-col>
    <el-col :span="6"><div class="grid-content bg-purple"></div></el-col>
  </el-row>
  <el-row class="row-bg" justify="end">
    <el-col :span="6"><div class="grid-content bg-purple"></div></el-col>
    <el-col :span="6"><div class="grid-content bg-purple-light"></div></el-col>
    <el-col :span="6"><div class="grid-content bg-purple"></div></el-col>
  </el-row>
  <el-row class="row-bg" justify="space-between">
    <el-col :span="6"><div class="grid-content bg-purple"></div></el-col>
    <el-col :span="6"><div class="grid-content bg-purple-light"></div></el-col>
    <el-col :span="6"><div class="grid-content bg-purple"></div></el-col>
  </el-row>
  <el-row class="row-bg" justify="space-around">
    <el-col :span="6"><div class="grid-content bg-purple"></div></el-col>
    <el-col :span="6"><div class="grid-content bg-purple-light"></div></el-col>
    <el-col :span="6"><div class="grid-content bg-purple"></div></el-col>
  </el-row>
  <el-row class="row-bg" justify="space-evenly">
    <el-col :span="6"><div class="grid-content bg-purple"></div></el-col>
    <el-col :span="6"><div class="grid-content bg-purple-light"></div></el-col>
    <el-col :span="6"><div class="grid-content bg-purple"></div></el-col>
  </el-row>
</template>

5. Responsive Layout

Referring to the responsive design of Bootstrap, five breakpoints are preset: xs, sm, md, lg and xl.

vue
<template>
  <el-row :gutter="10">
    <el-col :xs="8" :sm="6" :md="4" :lg="3" :xl="1">
      <div class="grid-content bg-purple"></div>
    </el-col>
    <el-col :xs="4" :sm="6" :md="8" :lg="9" :xl="11">
      <div class="grid-content bg-purple-light"></div>
    </el-col>
    <el-col :xs="4" :sm="6" :md="8" :lg="9" :xl="11">
      <div class="grid-content bg-purple"></div>
    </el-col>
    <el-col :xs="8" :sm="6" :md="4" :lg="3" :xl="1">
      <div class="grid-content bg-purple-light"></div>
    </el-col>
  </el-row>
</template>

API Reference

Row Attributes

AttributeDescriptionTypeAccepted ValuesDefault
guttergrid spacingnumber0
justifyhorizontal alignment of flex layoutstringstart/end/center/space-around/space-between/space-evenlystart
alignvertical alignment of flex layoutstringtop/middle/bottomtop
tagcustom element tagstring*div

Col Attributes

AttributeDescriptionTypeAccepted ValuesDefault
spannumber of column the grid spansnumber24
offsetnumber of spacing on the left side of the gridnumber0
pushnumber of columns that grid moves to the rightnumber0
pullnumber of columns that grid moves to the leftnumber0
xs<768px Responsive columns or column props objectnumber/object (e.g. {span: 4, offset: 4})
sm≥768px Responsive columns or column props objectnumber/object (e.g. {span: 4, offset: 4})
md≥992px Responsive columns or column props objectnumber/object (e.g. {span: 4, offset: 4})
lg≥1200px Responsive columns or column props objectnumber/object (e.g. {span: 4, offset: 4})
xl≥1920px Responsive columns or column props objectnumber/object (e.g. {span: 4, offset: 4})
tagcustom element tagstring*div

Best Practices

  1. Use appropriate grid spans: Choose grid spans that make sense for your content
  2. Consider mobile-first: Design for mobile devices first, then enhance for larger screens
  3. Use consistent spacing: Maintain consistent gutter values throughout your application
  4. Test responsiveness: Always test your layouts on different screen sizes
  5. Semantic HTML: Use appropriate HTML tags for better accessibility

Common Use Cases

Dashboard Layout

vue
<template>
  <el-row :gutter="20">
    <el-col :span="6">
      <!-- Sidebar -->
    </el-col>
    <el-col :span="18">
      <!-- Main content -->
    </el-col>
  </el-row>
</template>

Card Grid

vue
<template>
  <el-row :gutter="20">
    <el-col :xs="24" :sm="12" :md="8" :lg="6" v-for="item in items" :key="item.id">
      <!-- Card component -->
    </el-col>
  </el-row>
</template>

Element Plus Study Guide