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 on the document. This should be used sparingly and typically not for the entire document layout, only specific areas because of unintended outcomes.
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 maintained wherever it is used on the page or else 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 your images.
img {
width: 100%;
height: auto;
}
The CSS sets all image widths in the document to 100% the width
of their
containers and the height
to auto, thus maintaining the intrinsic aspect ratio of the image.
img { /* be careful as this rule applies to all images */
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 a basic, valid html structure.
- Be sure to
link
the CSS file in your HTML documenthead
. - Copy the HTML from this ⚙️ CodePen -
Position Absolute: Overlay Example into the
overlay.html
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 is because of the principles of using position, the absolute positioned item is feeding off of its ancestors that HAVE a position property. None have that property now so it moves to the default document position. Still absolute but based upon the document now. - 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 MS Teams.
Optional Resources
- Normal Flow - MDN
- CSS Position - MDN