![]() |
The Chakra UI library is a popular React UI library that helps build advanced and customizable user interfaces. One essential aspect of building UI is positioning the elements at the right positions on the screen. Prerequisites: Chakra UI Position props
This article will explain various approaches to position elements using Chakra UI. Table of Content Approach 1: Absolute PositioningAbsolute positioning helps you position an element relative to its closest parent or the document itself. The element does not affect the positions of other elements. Syntax:<Box position="absolute" top="50%" left="50%">
{ /* Your content here */ }
</Box>
Approach 2: Fixed PositioningFixed positioning positions the element relative to the screen viewport. The elements remains fixed in its position even if the page is scrolled. Syntax:<Box position="fixed" top="0" left="0">
{ /* Your content will come here */ }
</Box>
Approach 3: Relative positioningRelative positioning is used as a reference to its absolute children elements. Relative positioning positions the elements at its normal position. Syntax:<Box position="relative">
{ /* Your element at absolute positioning */ }
</Box>
Approach 4: Sticky PositioningSticky positioning lies somewhere between relative and fixed positioning. The element behaves as relative until a certain point, after which it becomes fixed in position. Syntax:<Box position="sticky" top="0">
{ /* Your content will come here */ }
</Box>
Steps to Create a React App and Installing ModuleStep 1: Create a new react app and enter into it by running the following commands: npx create-react-app my-app
cd my-app
Step 2: Install the dependencies `@chakra-ui/react` by running the following command into the terminal: npm i @chakra-ui/react
Project Structure:![]() Project Structure The updated dependencies in package.json file will look like. "dependencies": {
"@chakra-ui/react": "^2.8.2",
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}
Example: Here is the implementation for the Chakra UI position. Javascript
Javascript
Javascript
Javascript
Javascript
Javascript
Output: |
Reffered: https://www.geeksforgeeks.org
ReactJS |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |