Positioning and Display
CSS positioning allows you to set the position of an HTML element relative to itself, the HTML document, or the screen. There are four basic types of positions in CSS - static, absolute, relative, and fixed.
Lets Go!

Positioning and Display
Lesson 16
Understand the different CSS positioning values and how they affect the layout and flow of elements.
Get Started 🍁Introduction to CSS Positioning
Welcome to our course on CSS positioning! My name is Mike, and I'm excited to guide you through the fundamental concepts of positioning in CSS.
CSS positions allow you to control the placement of HTML elements in relation to themselves, the HTML document, or the screen where the HTML page is being viewed. This course will explore the four basic types of positions: static, absolute, relative, and fixed. These positions are essential for creating unique layouts and positioning elements within your HTML files.
Have you ever wondered how to make elements stick at the top or the bottom of a webpage while you scroll? Or how to position elements relative to the viewport or the document flow? In this course, we will delve into these questions, exploring the nuances of each positioning type and how they can help you achieve your desired layout.
By the end of this course, you will have a solid understanding of CSS positioning and be able to apply different positioning styles to enhance your HTML projects. Join me on this journey as we unlock the power of positioning in CSS! Are you ready to dive in and master CSS positioning techniques? Let's get started!
Main Concepts of CSS Positioning
-
Static Positioning: By default, all HTML elements have a static position, which means they are laid out according to the structure in the HTML document. The elements will appear in the order they are written in the document.
-
Absolute Positioning: Allows you to position an element relative to its containing element. It is positioned according to the parent element it is nested in. This is useful for creating elements that are independent of the document flow.
-
Relative Positioning: Positions an element relative to its normal position in the document flow. This can be useful when you want to move an element from its original position without affecting the layout of the surrounding elements.
-
Fixed Positioning: Positions an element relative to the viewport, which means it will always stay in the same position on the screen even when scrolling. This is commonly used for elements like sticky headers or footers.
Understanding these four types of positioning in CSS can help you create more dynamic and responsive layouts for your HTML elements. Each type of positioning offers unique ways to control the placement of elements on a web page, giving you more flexibility in designing your website layout. By incorporating these concepts into your CSS coding, you can enhance the visual appeal and functionality of your web pages.
Practical Applications of CSS Positioning
Step 1: Understanding Different Positioning Types
- Static Positioning: Default layout based on HTML document structure.
- Absolute Positioning: Element positioned relative to the nearest positioned ancestor.
- Relative Positioning: Element positioned relative to its normal position.
- Fixed Positioning: Element positioned relative to the viewport.
Step 2: Applying Different Positioning Styles
- Position an element at the bottom: Use
bottom: 0
orbottom: 100px
for fixed or absolute positioning. - Make an element sticky: Set
position: fixed
and adjusttop
orbottom
property. - Customize element position: Experiment with pixel values to see the effect on the layout.
Step 3: Practical Example
- Open an HTML file with two div elements - one blue and one red.
- Experiment with different positioning styles on these divs.
- Try setting
position: fixed
for stickiness orposition: absolute
for specific positioning relative to the document flow.
Step 4: Testing Responsive Behavior
- Change viewport size to see how fixed and absolute positioning behave.
- Notice how elements stay fixed at the bottom or adjust relative to viewport changes.
Step 5: Further Exploration
- Apply different positioning styles to other elements in your HTML file.
- Use developer tools to inspect element positioning and understand the impact of CSS positions.
Step 6: Share Your Experience
- If you found this guide helpful, share your experience in the comments below.
- Subscribe to Draft Academy for more tutorials on CSS and web development.
Don't hesitate to get hands-on with CSS positioning and see how it can enhance your website layout. Have fun experimenting with different styles and share your creations with us!
Test your Knowledge
What is the default position value of HTML elements?
What is the default position value of HTML elements?
Advanced Insights into CSS Positioning
When it comes to CSS positions, understanding the different types - static, absolute, relative, and fixed - is crucial for creating dynamic layouts in your HTML files. Let's delve into some advanced insights to enhance your knowledge:
Absolute Positioning
-
Deeper Understanding: Absolute positioning allows you to position an element relative to its closest positioned ancestor, rather than the document flow.
-
Tip: Utilize absolute positioning for elements that need to be precisely placed within a specific container, giving you more control over their placement.
-
Curiosity Question: How can you ensure accurate positioning of absolutely positioned elements across various screen sizes and resolutions?
Relative Positioning
-
Advanced Aspect: Relative positioning moves an element in relation to its normal position in the document flow, without affecting the layout of surrounding elements.
-
Recommendation: Experiment with relative positioning to create subtle shifts in element placement without disrupting the overall layout structure.
-
Curiosity Question: Can you think of scenarios where relative positioning would be more advantageous than absolute positioning?
Fixed Positioning
-
Expert Advice: Fixed positioning positions an element relative to the viewport, ensuring it remains fixed in its location even when the page is scrolled.
-
Insight: Fixed positioning is ideal for creating elements like sticky headers or footers that remain visible as the user navigates through the page.
-
Curiosity Question: How can you ensure that fixed-positioned elements remain responsive and adaptable to different viewport sizes?
By mastering these advanced insights into CSS positioning, you'll have the tools to create visually appealing and functionally efficient layouts for your HTML projects. Keep experimenting and applying these techniques to enhance your web development skills!
Additional Resources for Understanding CSS Positioning
-
Article: [A Complete Guide to CSS Positioning](https://css-tricks.com/how does-css-positioning-work/): Dive deeper into the complexities of CSS positioning with this comprehensive guide from CSS-Tricks.
-
Video Tutorial: Mastering CSS Positioning: Watch this in-depth tutorial to fully grasp the concepts of static, absolute, relative, and fixed positioning in CSS.
-
Interactive Example: CSS Positioning Playground: Experiment with different CSS positioning properties in real-time on this interactive CodePen playground.
-
Online Course: Advanced CSS Layouts: Take your CSS skills to the next level with this course on creating complex layouts using CSS positioning techniques.
Explore these resources to enhance your understanding of CSS positioning and take your web development skills to new heights! If you have any questions or feedback, feel free to leave a comment below.
Practice
Task: Create an HTML page demonstrating each positioning type (static, relative, absolute, and fixed) with notes beside each element describing its behavior.