CSS Layout: Position
Overview
Elements are positioned on a document using the normal flow by default. Sometimes, the layout calls for placing items by absolute or fixed positions. Because this can lead to unintended outcomes, using absolute or fixed positions should be done sparingly and only for specific areas and not the entire document.
"The position CSS property sets how an element is positioned in a document. The top, right, bottom, and left properties determine the final location of positioned elements." - MDN
Here is an example of using the position property and having text, "New Here?" and "Learn What We Believe" overlaying an image in the same HTML container:
Prepare
- Read the
absolute
andfixed
sections under the Values section of the CSS position in the MDN documentation.
Images and position/sizing
Working with images can be challenging because they have a set width and height that must be otherwise the image will appear pixelated and/or distorted. Normally, a general rule of thumb is to set only the width or the height, not both in CSS in order to maintain the image's aspect ratio. If the image containers are set, then setting the width to 100% is a common practice to ensure the image fills the container.
It is recommended that you start with the following CSS declaration for all of your work in this class to help control the aspect ratio of images.
img {
width: 100%;
height: auto;
}
The CSS above sets all image widths in the document to 100% of the width
of their
containers and the height
to auto, thus maintaining the image's intrinsic aspect ratio.
img {
width: 100%;
height: 100%;
object-fit: cover;
}
Activity Instructions
File Setup
- Create a folder named "week03" in your wdd130 directory.
- Create a folder named "styles" within the week03 folder.
- Create a file named "overlay.html" in the week03 folder.
- Add a CSS file named "overlay.css"" in the week03/styles folder.
HTML
- Give the overlay.html document a basic and valid HTML structure.
- Be sure to
link
the CSS file in your HTML documenthead
. - Copy the HTML from this CodePen: CSS position: absolute - overlay and paste it into the overlay.html document's
body
.
CSS
- Copy the CSS from the same CodePen into overlay.css file.
- Move the
section class="stats"
display box from the upper right to the lower left of the image.Check Your Understanding
... bottom: 25px; left: 15px; ...
- Remove the
position: relative;
property from the containerdiv
(this div has a class ofherodiv
assigned to it).
What happens?Answer
The information
section
overlay moves to the bottom left of the document instead of the bottom left of thediv.herodiv
container. This happens because, with the position property, the absolute positioned item is positioned relative to its ancestors that have a declared, position property. Since none have that property now, it moves to the overall document position instead of the<div class="herodiv">
container. - Also note the footer using a
position: fixed
declaration.
Submission
- Test your page in your local browser using Live Server.
- Commit and push your work to your wdd130 GitHub Pages enabled repository.
https://githubusername.github.io/wdd130/week03/overlay.html
- Share and discuss your work with your peers on Microsoft Teams.
Optional Resources
- Normal Flow - MDN
- CSS Position - MDN