Skip to content

Col Layout Rules ​

TIP

All components support layout configuration rules. For detailed information, see

ElementUI Grid Layout Col props

Custom Form Layout

Configuration Method ​

Set component layout using the rule.col option. If col is not set, components default to { span: 24 }, taking up the full row.

js
const rule = {
    type: 'input',
    field: 'username',
    title: 'Username',
    col: {
        span: 12,  // Occupies 12 columns (half the width)
    }
}

Configuration Parameters ​

ParameterDescriptionTypeOptional ValuesDefault Value
spanNumber of columns occupied by the gridnumber—24
offsetNumber of spacing columns on the leftnumber—0
pushNumber of columns to move rightnumber—0
pullNumber of columns to move leftnumber—0
xsResponsive grid number or grid attribute object for <768pxnumber/object (e.g.: {span: 4, offset: 4})——
smResponsive grid number or grid attribute object for ≥768pxnumber/object (e.g.: {span: 4, offset: 4})——
mdResponsive grid number or grid attribute object for ≥992pxnumber/object (e.g.: {span: 4, offset: 4})——
lgResponsive grid number or grid attribute object for ≥1200pxnumber/object (e.g.: {span: 4, offset: 4})——
xlResponsive grid number or grid attribute object for ≥1920pxnumber/object (e.g.: {span: 4, offset: 4})——
tagCustom element tagstring*div

Layout Examples ​

Basic Layout ​

Single Column Layout (Default) ​

js
const rule = {
    type: 'input',
    title: 'Product Name',
    field: 'goods_name',
    // If col is not set, defaults to span: 24, occupying the full row
}

Two Column Layout ​

js
const rule = [
    {
        type: 'input',
        title: 'Product Name',
        field: 'goods_name',
        col: {
            span: 12,  // Occupies 12 columns (half the width)
        }
    },
    {
        type: 'input',
        title: 'Product Price',
        field: 'price',
        col: {
            span: 12,  // Occupies 12 columns (half the width)
        }
    }
]

Three Column Layout ​

js
const rule = [
    {
        type: 'input',
        title: 'Product Name',
        field: 'goods_name',
        col: {
            span: 8,  // Occupies 8 columns (1/3 width)
        }
    },
    {
        type: 'input',
        title: 'Product Price',
        field: 'price',
        col: {
            span: 8,  // Occupies 8 columns (1/3 width)
        }
    },
    {
        type: 'input',
        title: 'Product Stock',
        field: 'stock',
        col: {
            span: 8,  // Occupies 8 columns (1/3 width)
        }
    }
]

Four Column Layout ​

js
const rule = [
    {
        type: 'input',
        title: 'Field 1',
        field: 'field1',
        col: {
            span: 6,  // Occupies 6 columns (1/4 width)
        }
    },
    {
        type: 'input',
        title: 'Field 2',
        field: 'field2',
        col: {
            span: 6,
        }
    },
    {
        type: 'input',
        title: 'Field 3',
        field: 'field3',
        col: {
            span: 6,
        }
    },
    {
        type: 'input',
        title: 'Field 4',
        field: 'field4',
        col: {
            span: 6,
        }
    }
]

Offset Layout ​

Using offset to Set Left Offset ​

js
const rule = {
    type: 'input',
    title: 'Product Name',
    field: 'goods_name',
    col: {
        span: 12,
        offset: 6,  // Left offset 6 columns
    }
}

Using push to Move Right ​

js
const rule = {
    type: 'input',
    title: 'Product Name',
    field: 'goods_name',
    col: {
        span: 8,
        push: 8,  // Move right 8 columns
    }
}

Using pull to Move Left ​

js
const rule = {
    type: 'input',
    title: 'Product Name',
    field: 'goods_name',
    col: {
        span: 8,
        pull: 4,  // Move left 4 columns
    }
}

Responsive Layout ​

Basic Responsive ​

js
const rule = {
    type: 'input',
    title: 'Product Name',
    field: 'goods_name',
    col: {
        xs: 24,  // <768px: occupies full row
        sm: 12,  // ≥768px: occupies half
        md: 8,   // ≥992px: occupies 1/3
        lg: 6,   // ≥1200px: occupies 1/4
        xl: 4,   // ≥1920px: occupies 1/6
    }
}

Responsive Object Configuration ​

js
const rule = {
    type: 'input',
    title: 'Product Name',
    field: 'goods_name',
    col: {
        xs: { span: 24, offset: 0 },  // Mobile: occupies full row
        sm: { span: 12, offset: 0 },  // Tablet: occupies half
        md: { span: 8, offset: 0 },    // Small screen: occupies 1/3
        lg: { span: 6, offset: 0 },   // Large screen: occupies 1/4
    }
}

Mixed Layout ​

Components with Different Widths ​

js
const rule = [
    {
        type: 'input',
        title: 'Product Name',
        field: 'goods_name',
        col: {
            span: 16,  // Occupies 16 columns (2/3 width)
        }
    },
    {
        type: 'input',
        title: 'Product Price',
        field: 'price',
        col: {
            span: 8,  // Occupies 8 columns (1/3 width)
        }
    },
    {
        type: 'input',
        title: 'Product Description',
        field: 'description',
        props: {
            type: 'textarea',
        },
        col: {
            span: 24,  // Occupies full row
        }
    }
]

Layout with Offset ​

js
const rule = [
    {
        type: 'input',
        title: 'Product Name',
        field: 'goods_name',
        col: {
            span: 8,
        }
    },
    {
        type: 'input',
        title: 'Product Price',
        field: 'price',
        col: {
            span: 8,
            offset: 8,  // Left offset 8 columns
        }
    }
]

Practical Application Scenarios ​

Product Information Form ​

js
const rule = [
    {
        type: 'input',
        title: 'Product Name',
        field: 'goods_name',
        col: {
            span: 12,
        }
    },
    {
        type: 'select',
        title: 'Product Category',
        field: 'category',
        col: {
            span: 12,
        }
    },
    {
        type: 'input-number',
        title: 'Product Price',
        field: 'price',
        col: {
            span: 8,
        }
    },
    {
        type: 'input-number',
        title: 'Product Stock',
        field: 'stock',
        col: {
            span: 8,
        }
    },
    {
        type: 'switch',
        title: 'On Sale',
        field: 'is_show',
        col: {
            span: 8,
        }
    },
    {
        type: 'input',
        title: 'Product Description',
        field: 'description',
        props: {
            type: 'textarea',
        },
        col: {
            span: 24,  // Description occupies full row
        }
    }
]

Responsive Form Layout ​

js
const rule = [
    {
        type: 'input',
        title: 'Username',
        field: 'username',
        col: {
            xs: 24,  // Mobile: occupies full row
            sm: 12,  // Tablet: occupies half
            md: 12,  // Small screen: occupies half
            lg: 8,   // Large screen: occupies 1/3
        }
    },
    {
        type: 'input',
        title: 'Email',
        field: 'email',
        col: {
            xs: 24,
            sm: 12,
            md: 12,
            lg: 8,
        }
    },
    {
        type: 'input',
        title: 'Phone',
        field: 'phone',
        col: {
            xs: 24,
            sm: 12,
            md: 12,
            lg: 8,
        }
    }
]

Using Row and Col Components for Layout ​

You can also use row and col components to manually control layout instead of rule.col, providing more flexibility.

Basic Usage ​

Using row and col components together to implement layout:

js
const rule = [
    {
        type: 'row',
        native: true,  // Generate component as-is, without FormItem wrapper
        children: [
            {
                type: 'col',
                props: {
                    span: 12,  // Occupies 12 columns
                },
                native: true,
                children: [
                    {
                        type: 'input',
                        title: 'Product Name',
                        field: 'goods_name',
                    }
                ]
            },
            {
                type: 'col',
                props: {
                    span: 12,  // Occupies 12 columns
                },
                native: true,
                children: [
                    {
                        type: 'input',
                        title: 'Product Price',
                        field: 'price',
                    }
                ]
            }
        ]
    }
]

Multi-Row Layout ​

js
const rule = [
    {
        type: 'row',
        native: true,
        children: [
            {
                type: 'col',
                props: { span: 12 },
                native: true,
                children: [
                    {
                        type: 'input',
                        title: 'Product Name',
                        field: 'goods_name',
                    }
                ]
            },
            {
                type: 'col',
                props: { span: 12 },
                native: true,
                children: [
                    {
                        type: 'select',
                        title: 'Product Category',
                        field: 'category',
                    }
                ]
            }
        ]
    },
    {
        type: 'row',
        native: true,
        children: [
            {
                type: 'col',
                props: { span: 8 },
                native: true,
                children: [
                    {
                        type: 'input-number',
                        title: 'Product Price',
                        field: 'price',
                    }
                ]
            },
            {
                type: 'col',
                props: { span: 8 },
                native: true,
                children: [
                    {
                        type: 'input-number',
                        title: 'Product Stock',
                        field: 'stock',
                    }
                ]
            },
            {
                type: 'col',
                props: { span: 8 },
                native: true,
                children: [
                    {
                        type: 'switch',
                        title: 'On Sale',
                        field: 'is_show',
                    }
                ]
            }
        ]
    }
]

Row with Styles ​

js
const rule = [
    {
        type: 'row',
        style: { width: '100%' },  // Set row style
        native: true,
        children: [
            {
                type: 'col',
                props: { span: 12 },
                native: true,
                children: [
                    {
                        type: 'date-picker',
                        title: 'Activity Date',
                        field: 'section_day',
                        props: {
                            type: 'daterange',
                        }
                    }
                ]
            },
            {
                type: 'col',
                props: { span: 12 },
                native: true,
                children: [
                    {
                        type: 'input-number',
                        title: 'Sort',
                        field: 'sort',
                    }
                ]
            }
        ]
    }
]

Nested Layout ​

js
const rule = [
    {
        type: 'row',
        native: true,
        children: [
            {
                type: 'col',
                props: { span: 16 },
                native: true,
                children: [
                    {
                        type: 'row',
                        native: true,
                        children: [
                            {
                                type: 'col',
                                props: { span: 12 },
                                native: true,
                                children: [
                                    {
                                        type: 'input',
                                        title: 'Product Name',
                                        field: 'goods_name',
                                    }
                                ]
                            },
                            {
                                type: 'col',
                                props: { span: 12 },
                                native: true,
                                children: [
                                    {
                                        type: 'input',
                                        title: 'Product Code',
                                        field: 'goods_code',
                                    }
                                ]
                            }
                        ]
                    }
                ]
            },
            {
                type: 'col',
                props: { span: 8 },
                native: true,
                children: [
                    {
                        type: 'upload',
                        title: 'Product Image',
                        field: 'image',
                    }
                ]
            }
        ]
    }
]

Row Component Configuration ​

Row component supports the following configuration items:

ParameterDescriptionTypeOptional ValuesDefault Value
gutterGrid spacingnumber—0
typeLayout modestringflex—
justifyHorizontal alignment in flex layoutstringstart/end/center/space-around/space-betweenstart
alignVertical alignment in flex layoutstringtop/middle/bottomtop
tagCustom element tagstring*div

Setting Row Spacing ​

js
const rule = [
    {
        type: 'row',
        props: {
            gutter: 20,  // Set grid spacing to 20px
        },
        native: true,
        children: [
            {
                type: 'col',
                props: { span: 12 },
                native: true,
                children: [
                    {
                        type: 'input',
                        title: 'Product Name',
                        field: 'goods_name',
                    }
                ]
            },
            {
                type: 'col',
                props: { span: 12 },
                native: true,
                children: [
                    {
                        type: 'input',
                        title: 'Product Price',
                        field: 'price',
                    }
                ]
            }
        ]
    }
]

Using Flex Layout ​

js
const rule = [
    {
        type: 'row',
        props: {
            type: 'flex',
            justify: 'space-between',  // Space between
            align: 'middle',  // Vertical center
        },
        native: true,
        children: [
            {
                type: 'col',
                props: { span: 8 },
                native: true,
                children: [
                    {
                        type: 'input',
                        title: 'Product Name',
                        field: 'goods_name',
                    }
                ]
            },
            {
                type: 'col',
                props: { span: 8 },
                native: true,
                children: [
                    {
                        type: 'input',
                        title: 'Product Price',
                        field: 'price',
                    }
                ]
            }
        ]
    }
]

Practical Application Scenarios ​

Complex Form Layout ​

js
const rule = [
    {
        type: 'row',
        props: { gutter: 20 },
        native: true,
        children: [
            {
                type: 'col',
                props: { span: 12 },
                native: true,
                children: [
                    {
                        type: 'input',
                        title: 'Product Name',
                        field: 'goods_name',
                        validate: [
                            { required: true, message: 'Enter product name' }
                        ]
                    }
                ]
            },
            {
                type: 'col',
                props: { span: 12 },
                native: true,
                children: [
                    {
                        type: 'select',
                        title: 'Product Category',
                        field: 'category',
                        options: [
                            { value: '1', label: 'Electronics' },
                            { value: '2', label: 'Clothing & Accessories' },
                        ]
                    }
                ]
            }
        ]
    },
    {
        type: 'row',
        props: { gutter: 20 },
        native: true,
        children: [
            {
                type: 'col',
                props: { span: 8 },
                native: true,
                children: [
                    {
                        type: 'input-number',
                        title: 'Product Price',
                        field: 'price',
                    }
                ]
            },
            {
                type: 'col',
                props: { span: 8 },
                native: true,
                children: [
                    {
                        type: 'input-number',
                        title: 'Product Stock',
                        field: 'stock',
                    }
                ]
            },
            {
                type: 'col',
                props: { span: 8 },
                native: true,
                children: [
                    {
                        type: 'switch',
                        title: 'On Sale',
                        field: 'is_show',
                    }
                ]
            }
        ]
    },
    {
        type: 'row',
        native: true,
        children: [
            {
                type: 'col',
                props: { span: 24 },
                native: true,
                children: [
                    {
                        type: 'input',
                        title: 'Product Description',
                        field: 'description',
                        props: {
                            type: 'textarea',
                            rows: 4,
                        }
                    }
                ]
            }
        ]
    }
]

Comparison of Two Layout Methods ​

MethodAdvantagesApplicable Scenarios
rule.colSimple configuration, concise codeSimple single-row layout
row + col componentsMore flexible, supports nesting and complex layoutsPrecise layout control, nested layouts, complex forms

Tips

  • When using row and col components, you need to set native: true to generate components as-is
  • row component is used to create row containers, col component is used to create column containers
  • Form components need to be placed in the children of col
  • You can set properties like gutter, type, justify, align through row's props

FormCreate is an open-source project released under the MIT License. Free for personal and commercial use.