Flex GridFlex Grid CSS is a lightweight flex based responsive layout utility optimized for mobile phones, tablets and desktops. Flex Grid CSS is not included in PrimeNG as it is provided by PrimeFlex, a shared grid library between PrimeFaces, PrimeNG and PrimeReact projects.
Documentation” class=”ng-tns-c2-1
Getting Started
PrimeFlex is available at npm, if you have an existing application run the following command to download it to your project.
npm install primeflex --save
Then add the primeflex.css to your application, example below uses styles configuration of an Angular CLI project.
"styles": [
"node_modules/primeflex/primeflex.css"
],
A basic grid is defined by giving a container "p-grid" class and children the ".p-col" class. Children of the grid will have the same width and scale according to the width of the parent.
<div class="p-grid">
<div class="p-col">1</div>
<div class="p-col">2</div>
<div class="p-col">3</div>
</div>
1
2
3
Direction
Default direction is "row" and p-dir-* class at the container defines the other possible directions which can be row reverse, column and column reverse.
<!-- Row Reverse -->
<div class="p-grid p-dir-rev">
<div class="p-col">1</div>
<div class="p-col">2</div>
<div class="p-col">3</div>
</div>
<!-- Column -->
<div class="p-grid p-dir-col">
<div class="p-col">1</div>
<div class="p-col">2</div>
<div class="p-col">3</div>
</div>
<!-- Column Reverse -->
<div class="p-grid p-dir-col-rev">
<div class="p-col">1</div>
<div class="p-col">2</div>
<div class="p-col">3</div>
</div>
1
2
3
1
2
3
1
2
3
12 Column Grid
FlexGrid includes a 12 column based layout utility where width of a column is defined with the p-col-{number} style class. Columns with prefined widths can be used with columns with auto width (p-col) as well.
In the first example below, first column covers the 4 units out of 12 and the rest of the columns share the remaining space whereas in the second example, all three columns have explicit units.
<div class="p-grid">
<div class="p-col-4">4</div>
<div class="p-col">1 </div>
<div class="p-col">1 </div>
<div class="p-col">1 </div>
<div class="p-col">1 </div>
<div class="p-col">1 </div>
<div class="p-col">1 </div>
<div class="p-col">1 </div>
<div class="p-col">1 </div>
</div>
<div class="p-grid">
<div class="p-col-2">2</div>
<div class="p-col-6">6</div>
<div class="p-col-4">4</div>
</div>
4
1
1
1
1
1
1
1
1
2
6
4
Multi Line
When the number of columns exceed 12, columns wrap to a new line.
<div class="p-grid">
<div class="p-col-6">6</div>
<div class="p-col-6">6</div>
<div class="p-col-6">6</div>
<div class="p-col-6">6</div>
</div>
6
6
6
6
Fixed Width Column
A column can have a fixed width while siblings having auto width. Apply "p-col-fixed" class to fix a column width.
<div class="p-grid">
<div class="p-col-fixed" style="width:100px">Fixed</div>
<div class="p-col">Auto</div>
</div>
100px
auto
Responsive
Responsive layout is achieved by applying breakpoint specific classes to the columns whereas p-col-* define the default behavior for mobile devices with small screens. Four screen sizes are supported with different breakpoints.
Prefix | Devices | Media Query | Example |
---|---|---|---|
p-sm- | Small devices | min-width: 576px | p-sm-6, p-sm-4 |
p-md- | Medium sized devices such as tablets | min-width: 768px | p-md-2, p-md-8 |
p-lg- | Devices with large screen like desktops | min-width: 992px | p-lg-6, p-lg-12 |
p-xl- | Big screen monitors | min-width: 1200px | p-xl-2, p-xl-10 |
In example below, large screens display 4 columns, medium screens display 2 columns in 2 rows and finally on small devices, columns are stacked.
<div class="p-grid">
<div class="p-col-12 p-md-6 p-lg-3">A</div>
<div class="p-col-12 p-md-6 p-lg-3">B</div>
<div class="p-col-12 p-md-6 p-lg-3">C</div>
<div class="p-col-12 p-md-6 p-lg-3">D</div>
</div>
p-col-12 p-md-6 p-lg-3
p-col-12 p-md-6 p-lg-3
p-col-12 p-md-6 p-lg-3
p-col-12 p-md-6 p-lg-3
Horizontal Alignment
p-justify-* classes are used on the container element to define the alignment along the main axis.
Class | Description |
---|---|
p-justify-start (default) | Items are packed toward the start line |
p-justify-end | Items are packed toward to end line |
p-justify-center | Items are centered along the line |
p-justify-between | Items are evenly distributed in the line; first item is on the start line, last item on the end line |
p-justify-around | Items are evenly distributed in the line with equal space around them. |
p-justify-even | Items are distributed so that the spacing between any two items (and the space to the edges) is equal. |
<div class="p-grid p-justify-between">
<div class="p-col-2">2</div>
<div class="p-col-1">1</div>
<div class="p-col-4">4</div>
</div>
2
1
4
Vertical Alignment
p-align-* classes are used on the container element to define the alignment along the cross axis.
Class | Description |
---|---|
p-align-stretch (default) | stretch to fill the container. |
p-align-start | Cross-start margin edge of the items is placed on the cross-start line |
p-align-end | Cross-end margin edge of the items is placed on the cross-end line |
p-align-center | Items are centered in the cross-axis |
p-align-baseline | Items are aligned such as their baselines align |
<div class="p-grid p-align-center">
<div class="p-col">4</div>
<div class="p-col">4</div>
<div class="p-col">4</div>
</div>
4
4
4
Vertical alignment can also be defined at column level with the p-col-align-* classes.
Class | Description |
---|---|
p-col-align-stretch | Stretch to fill the container. |
p-col-align-start | Cross-start margin edge of the items is placed on the cross-start line |
p-col-align-end | Cross-end margin edge of the items is placed on the cross-end line |
p-col-align-center | Items are centered in the cross-axis |
p-col-align-baseline | Items are aligned such as their baselines align |
<div class="p-grid">
<div class="p-col p-col-align-start">4</div>
<div class="p-col p-col-align-center">4</div>
<div class="p-col p-col-align-end">4</div>
</div>
4
4
4
Offset
Offset classes allow defining a left margin on a column to avoid adding empty columns for spacing.
<div class="p-grid">
<div class="p-col-6 p-offset-3">6</div>
</div>
<div class="p-grid">
<div class="p-col-4">4 </div>
<div class="p-col-4 p-offset-4">4</div>
</div>
6
4
4
The list of offset classes varying within a range of 1 to 12.
Prefix | Devices | Media Query | Example |
---|---|---|---|
p-col-offset- | All devices | All screens | p-col-offset-6, p-col-offset-4 |
p-sm-offset- | Small devices | min-width: 576px | p-sm-offset-6, p-sm-offset-4 |
p-md-offset- | Medium sized devices such as tablets | min-width: 768px | p-md-offset-6, p-md-offset-4 |
p-lg-offset- | Devices with large screen like desktops | min-width: 992px | p-lg-offset-6, p-lg-offset-4 |
p-xl-offset-* | Big screen monitors | min-width: 1200px | p-xl-offset-6, p-xl-offset-4 |
Nested
Columns can be nested to create more complex layouts.
<div class="p-grid">
<div class="p-col-8">
<div class="p-grid">
<div class="p-col-6">
6
</div>
<div class="p-col-6">
6
</div>
<div class="p-col-12">
12
</div>
</div>
</div>
<div class="p-col-4">
4
</div>
</div>
6
6
12
4
Gutter
A .5 em padding is applied to each column along with negative margins on the container element, in case you'd like to remove these gutters, apply .p-nogutter class to the container. Gutters can also be removed on an ndividual columns with the same class name.
<div class="p-grid p-nogutter">
<div class="p-col">1</div>
<div class="p-col p-nogutter">2</div>
<div class="p-col">3</div>
</div>
Customization
PrimeFlex allows customization of breakpoints and gutters via SASS variables, visit the PrimeFlex repository to get access to the primeflex.scss file to build your own customized Grid.
Source” class=”ng-tns-c2-1
<h3 class="first">Basic</h3>
<div class="p-grid">
<div class="p-col">
<div class="box">1</div>
</div>
<div class="p-col">
<div class="box">2</div>
</div>
<div class="p-col">
<div class="box">3</div>
</div>
</div>
<h3>Dynamic</h3>
<button pButton type="button" icon="pi pi-plus" title="Add Column" (click)="addColumn()" [disabled]="columns.length === 20" style="margin-right: .5em"></button>
<button pButton type="button" icon="pi pi-minus" title="Remove Column" (click)="removeColumn()"[disabled]="columns.length === 1"></button>
<div class="p-grid" style="margin-top: .5em">
<div class="p-col" *ngFor="let col of columns; let i=index" [@animation]="'visible'">
<div class="box">{{i + 1}}</div>
</div>
</div>
<h3>Reverse Direction</h3>
<div class="p-grid p-dir-rev">
<div class="p-col">
<div class="box">1</div>
</div>
<div class="p-col">
<div class="box">2</div>
</div>
<div class="p-col">
<div class="box">3</div>
</div>
</div>
<h3>Column Direction</h3>
<div class="p-grid p-dir-col">
<div class="p-col">
<div class="box">1</div>
</div>
<div class="p-col">
<div class="box">2</div>
</div>
<div class="p-col">
<div class="box">3</div>
</div>
</div>
<h3>Reverse Column Direction</h3>
<div class="p-grid p-dir-col-rev">
<div class="p-col">
<div class="box">1</div>
</div>
<div class="p-col">
<div class="box">2</div>
</div>
<div class="p-col">
<div class="box">3</div>
</div>
</div>
<h3>12 Column Grid</h3>
<div class="p-grid">
<div class="p-col-4">
<div class="box">4</div>
</div>
<div class="p-col">
<div class="box">1</div>
</div>
<div class="p-col">
<div class="box">1</div>
</div>
<div class="p-col">
<div class="box">1</div>
</div>
<div class="p-col">
<div class="box">1</div>
</div>
<div class="p-col">
<div class="box">1</div>
</div>
<div class="p-col">
<div class="box">1</div>
</div>
<div class="p-col">
<div class="box">1</div>
</div>
<div class="p-col">
<div class="box">1</div>
</div>
</div>
<div class="p-grid">
<div class="p-col-2">
<div class="box">2</div>
</div>
<div class="p-col-6">
<div class="box">6</div>
</div>
<div class="p-col-4">
<div class="box">4</div>
</div>
</div>
<div class="p-grid">
<div class="p-col-8">
<div class="box">8</div>
</div>
<div class="p-col-2">
<div class="box">2</div>
</div>
<div class="p-col-2">
<div class="box">2</div>
</div>
</div>
<h3>MultiLine</h3>
<div class="p-grid">
<div class="p-col-6">
<div class="box">6</div>
</div>
<div class="p-col-6">
<div class="box">6</div>
</div>
<div class="p-col-6">
<div class="box">6</div>
</div>
<div class="p-col-6">
<div class="box">6</div>
</div>
</div>
<h3>Fixed Width Column</h3>
<div class="p-grid">
<div class="p-col-fixed" style="width:100px">
<div class="box">100px</div>
</div>
<div class="p-col">
<div class="box">auto</div>
</div>
</div>
<h3>Responsive</h3>
<div class="p-grid">
<div class="p-col-12 p-md-6 p-lg-3">
<div class="box">p-col-12 p-md-6 p-lg-3</div>
</div>
<div class="p-col-12 p-md-6 p-lg-3">
<div class="box">p-col-12 p-md-6 p-lg-3</div>
</div>
<div class="p-col-12 p-md-6 p-lg-3">
<div class="box">p-col-12 p-md-6 p-lg-3</div>
</div>
<div class="p-col-12 p-md-6 p-lg-3">
<div class="box">p-col-12 p-md-6 p-lg-3</div>
</div>
</div>
<h3>Horizontal Alignment - Start</h3>
<div class="p-grid p-justify-start">
<div class="p-col-2">
<div class="box">2</div>
</div>
<div class="p-col-1">
<div class="box">1</div>
</div>
<div class="p-col-4">
<div class="box">4</div>
</div>
</div>
<h3>Horizontal Alignment - End</h3>
<div class="p-grid p-justify-end">
<div class="p-col-2">
<div class="box">2</div>
</div>
<div class="p-col-1">
<div class="box">1</div>
</div>
<div class="p-col-4">
<div class="box">4</div>
</div>
</div>
<h3>Horizontal Alignment - Center</h3>
<div class="p-grid p-justify-center">
<div class="p-col-2">
<div class="box">2</div>
</div>
<div class="p-col-1">
<div class="box">1</div>
</div>
<div class="p-col-4">
<div class="box">4</div>
</div>
</div>
<h3>Horizontal Alignment - Between</h3>
<div class="p-grid p-justify-between">
<div class="p-col-2">
<div class="box">2</div>
</div>
<div class="p-col-1">
<div class="box">1</div>
</div>
<div class="p-col-4">
<div class="box">4</div>
</div>
</div>
<h3>Horizontal Alignment - Around</h3>
<div class="p-grid p-justify-around">
<div class="p-col-2">
<div class="box">2</div>
</div>
<div class="p-col-1">
<div class="box">1</div>
</div>
<div class="p-col-4">
<div class="box">4</div>
</div>
</div>
<h3>Horizontal Alignment - Even</h3>
<div class="p-grid p-justify-even">
<div class="p-col-2">
<div class="box">2</div>
</div>
<div class="p-col-1">
<div class="box">1</div>
</div>
<div class="p-col-4">
<div class="box">4</div>
</div>
</div>
<h3>Vertical Alignment - Start</h3>
<div class="p-grid p-align-start vertical-container">
<div class="p-col">
<div class="box">4</div>
</div>
<div class="p-col">
<div class="box">4</div>
</div>
<div class="p-col">
<div class="box">4</div>
</div>
</div>
<h3>Vertical Alignment - End</h3>
<div class="p-grid p-align-end vertical-container">
<div class="p-col">
<div class="box">4</div>
</div>
<div class="p-col">
<div class="box">4</div>
</div>
<div class="p-col">
<div class="box">4</div>
</div>
</div>
<h3>Vertical Alignment - Center</h3>
<div class="p-grid p-align-center vertical-container">
<div class="p-col">
<div class="box">4</div>
</div>
<div class="p-col">
<div class="box">4</div>
</div>
<div class="p-col">
<div class="box">4</div>
</div>
</div>
<h3>Vertical Alignment - Stretch</h3>
<div class="p-grid p-align-stretch vertical-container">
<div class="p-col">
<div class="box box box-stretched">4</div>
</div>
<div class="p-col">
<div class="box box box-stretched">4</div>
</div>
<div class="p-col">
<div class="box box box-stretched">4</div>
</div>
</div>
<h3>Vertical Alignment - Per Column</h3>
<div class="p-grid vertical-container">
<div class="p-col p-col-align-start">
<div class="box">4</div>
</div>
<div class="p-col p-col-align-center">
<div class="box">4</div>
</div>
<div class="p-col p-col-align-end">
<div class="box">4</div>
</div>
</div>
<h3>Offset</h3>
<div class="p-grid">
<div class="p-col-6 p-offset-3">
<div class="box">6</div>
</div>
</div>
<div class="p-grid">
<div class="p-col-4">
<div class="box">4</div>
</div>
<div class="p-col-4 p-offset-4">
<div class="box">4</div>
</div>
</div>
<h3>Nested</h3>
<div class="p-grid nested-grid">
<div class="p-col-8">
<div class="p-grid">
<div class="p-col-6">
<div class="box">6</div>
</div>
<div class="p-col-6">
<div class="box">6</div>
</div>
<div class="p-col-12">
<div class="box">12</div>
</div>
</div>
</div>
<div class="p-col-4">
<div class="box box-stretched">4</div>
</div>
</div>
<h3>Panel Integration</h3>
<div class="p-grid">
<div class="p-col">
<p-panel header="Godfather">
The story begins as Don Vito Corleone, the head of a New York Mafia family, oversees his daughter's wedding.
His beloved son Michael has just come home from the war, but does not intend to become part of his father's business.
Through Michael's life the nature of the family business becomes clear. The business of the family is just like the head of the family,
kind and benevolent to those who give respect, but given to ruthless violence whenever anything stands against the good of the family.
</p-panel>
</div>
<div class="p-col">
<p-panel header="Godfather">
The story begins as Don Vito Corleone, the head of a New York Mafia family, oversees his daughter's wedding.
His beloved son Michael has just come home from the war, but does not intend to become part of his father's business.
Through Michael's life the nature of the family business becomes clear. The business of the family is just like the head of the family,
kind and benevolent to those who give respect, but given to ruthless violence whenever anything stands against the good of the family.
</p-panel>
</div>
<div class="p-col">
<p-panel header="Godfather">
The story begins as Don Vito Corleone, the head of a New York Mafia family, oversees his daughter's wedding.
His beloved son Michael has just come home from the war, but does not intend to become part of his father's business.
Through Michael's life the nature of the family business becomes clear. The business of the family is just like the head of the family,
kind and benevolent to those who give respect, but given to ruthless violence whenever anything stands against the good of the family.
</p-panel>
</div>
</div>
<h3>Sample Layout</h3>
<div class="p-grid sample-layout">
<div class="p-col-12 p-md-2">
Menu
</div>
<div class="p-col-12 p-md-10 p-col-nogutter">
<div class="p-col-12 p-col-nogutter">
Top Bar
</div>
<div class="p-col-12">
<div class="p-grid">
<div class="p-col-12 p-md-4">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed imperdiet, orci nec
dictum convallis, ligula mauris vestibulum turpis, nec varius tortor quam at diam. Nullam a viverra nibh.
In tincidunt tempor lectus quis vulputate. Pellentesque nec dui aliquam, lobortis est in, lobortis ante</div>
<div class="p-col-12 p-md-4">Maecenas vel nisi aliquet, vulputate tortor id, laoreet massa. Maecenas mattis
tristique bibendum. Suspendisse vel mi dictum, vestibulum lacus quis, pulvinar quam. Proin vulputate, nibh
at finibus varius, leo eros lacinia elit, nec blandit odio tellus a justo. Donec nec ex auctor, tristique
nulla nec, rutrum sapien.</div>
<div class="p-col-12 p-md-4">Proin efficitur in leo eget ornare. Nam vestibulum neque sed velit sagittis
sodales. Sed scelerisque hendrerit magna a hendrerit. Cras tempor sem at justo pharetra convallis.
Curabitur vel sodales purus. Vestibulum interdum facilisis nulla imperdiet suscipit. Quisque lectus felis,
condimentum eget hendrerit sit amet.</div>
<div class="p-col-6 p-md-3"><img src="assets/showcase/images/demo/galleria/galleria1.jpg" style="width:100%"></div>
<div class="p-col-6 p-md-3"><img src="assets/showcase/images/demo/galleria/galleria2.jpg" style="width:100%"></div>
<div class="p-col-6 p-md-3"><img src="assets/showcase/images/demo/galleria/galleria3.jpg" style="width:100%"></div>
<div class="p-col-6 p-md-3"><img src="assets/showcase/images/demo/galleria/galleria4.jpg" style="width:100%"></div>
<div class="p-col-12 p-md-6">Phasellus faucibus purus volutpat mauris lacinia sodales. Ut sit amet sapien
facilisis, commodo dui non, fringilla tellus. Quisque tempus facilisis nisi sodales finibus. Pellentesque
neque orci, ullamcorper vitae ligula quis, dignissim euismod augue.</div>
<div class="p-col-12 p-md-6">Fusce ullamcorper congue massa, eget ullamcorper nunc lobortis egestas. Lorem
ipsum dolor sit amet, consectetur adipiscing elit. Quisque ultrices dui eget dolor feugiat dapibus. Aliquam
pretium leo et egestas luctus. Nunc facilisis gravida tellus.</div>
</div>
</div>
</div>
<div class="p-col-12">
Footer
</div>
</div>
import {Component,ViewEncapsulation} from '@angular/core';
import {trigger,state,style,transition,animate,AnimationEvent} from '@angular/animations';
@Component ({
styles:[`
.box,
.sample-layout > div {
background-color: #cce4f7;
text-align: center;
padding-top: 1em;
padding-bottom: 1em;
border-radius: 4px;
}
.box-stretched {
height: 100%;
}
.sample-layout {
margin: 0;
}
.sample-layout > div {
border: 1px solid #ffffff;
}
.vertical-container {
margin: 0;
height: 200px;
background: #efefef;
border-radius: 4px;
}
.nested-grid .p-col-4 {
padding-bottom: 1em;
}
`],
templateUrl: './flexgriddemo.html',
animations: [
trigger('animation', [
state('visible', style({
transform: 'translateX(0)',
opacity: 1
})),
transition('void => *', [
style({transform: 'translateX(50%)', opacity: 0}),
animate('300ms ease-out')
]),
transition('* => void', [
animate(('250ms ease-in'), style({
height: 0,
opacity: 0,
transform: 'translateX(50%)'
}))
])
])
],
encapsulation: ViewEncapsulation.None
})
export class FlexGridDemo {
columns: number[];
ngOnInit() {
this.columns = [0, 1, 2, 3, 4, 5];
}
addColumn() {
this.columns.push(this.columns.length);
}
removeColumn() {
this.columns.splice(-1, 1);
}
}