Lab 07 Display Flex & Media Queries
Goal
- learn CSS flexbox layout
- learn what media queries are and how they create responsive web pages
- learn what a scaleable vector graphic is and why it is important with web design
Learn
CSS Flexbox
Flexbox is a dynamic alternative to display inline-block, that allows flex-elements within a flex-container to be arranged in either rows or columns based on flex properties.
Flex-Container Properties
.flex-container{
display:flex;
}
display:flex;
This defines a flex container. It enables a flex context for all its direct children(flex-items).
.flex-container{
display:flex;
flex-direction:row;
}
flex-direction
This defines the main-axis of the flex container in which the flex-items will arrange themselves.
Values for flex-direction:
- row - flex-items will arrange from left to right
- row-reverse - flex-items will arrange from right to left
- column - flex-items will arrange from top to bottom
- column-reverse - flex-items will arrange from bottom to top
.flex-container{
display:flex;
flex-wrap:wrap;
}
flex-wrap
This defines whether the flex-items will be contained on one line or be allowed to wrap on to new lines.
Values for flex-wrap:
- no-wrap - all flex items will be on one line
- wrap - flex items will wrap onto multiple lines, from top to bottom
- wrap-reverse - flex items will wrap onto multiple lines from bottom to top
.flex-container{
display:flex;
flex-flow:column wrap;
}
flex-flow
This is a shorthand property for both flex-direction and flex-wrap.
.flex-container{
display:flex;
justify-content:center;
}
justify-content
This defines the alignment of the flex-items along the main axis.
Values for justify-content:
- flex-start - flex-items begin at the beginning of the flex-container
- flex-end - flex-items begin at the end of the flex-container
- center - flex-items will center themselves in the middle of the main axis
- space-evenly- flex-items will space themselves out across the main axis with equal space in between each item and the edge of the container.
- space-around - flex-items will space themselves out across the main axis with equal space around each item.
- space-between - the first flex-item will be positioned at the start of the flex-container, the last flex-item will be at the end of the flex-container, and the rest of the items will space out with equal space in between.
.flex-container{
display:flex;
align-items:flex-start;
}
align-items
This defines how flex-items are aligned on the cross axis of the flex-container on the current line.
Values for align-items:
- flex-start - the flex-items will begin at the beginning of the flex-container on the cross axis
- flex-end - the flex-items will begin at the end of the flex-container on the cross axis
- center - the flex-items will align in the middle of the flex-container on the cross axis
- stretch - the flex-items will stretch to fill the flex-container on the cross axis
- baseline - - the flex-items will align with each other based on the baseline of their text content
align-content
This defines how flex-items are aligned on the cross axis of the flex-container when there is extra space.
*Note this property will only work if flex-wrap:wrap;
or flex-wrap:wrap-reverse;
is used.
Values for align-content:
- flex-start - the flex-items will begin at the beginning of the flex-container on the cross axis
- flex-end - the flex-items will begin at the end of the flex-container on the cross axis
- center - the flex-items will align in the middle of the flex-container on the cross axis
- stretch - the flex-items will stretch to fill the flex-container on the cross axis
- space-between - the first flex-item will be positioned at the start of the flex-container, the last flex-item will be at the end of the flex-container, and the rest of the items will space out with equal space in between on the cross axis.
- space-around - flex-items will space themselves out across the cross axis with equal space around each item.
Media Queries
Media queries are used to apply conditional CSS styles. We will be using them to specify what happens when the devices screen size exceeds certain sizes.
main{
width:50%;
}
h1{
margin:2rem;
}
@media (min-width: 1200px){
main{
width: 70%;
}
h1{
margin:1rem;
}
}
In the example:
The main has a width of 50% and the h1 has a margin of 2 rem. Except when the screen size is bigger that 1200 pixels, then the main has a width of 70% and h1 has a margin of 1 rem.
Notice how the main and h1 are inside the @media ruleset. When a ruleset is inside the media query ruleset then it is conditionally applied when the condition is met.
Mobile First
For the rest of the semester we will be practicing mobile first CSS. This means when you are writing your CSS it will be for narrow screens. Then you will use a media query to adjust elements for wider screens.
SVG
Scalable Vector Graphics is an XML-based markup language for describing two dimensional based vector graphics. SVG is essentially to graphics what HTML is to text.
What makes SVG powerful in terms of web design is the fact the image is made of vectors instead of pixels, therefore it can be scaled to any size with out losing quality. Also because SVG are lines of code, they are tiny files that require little load time. SVGs are ideal for logos and icons on websites.
Vector graphics are made of points connected by lines to form polygons and other shapes. Adobe Illustrator is a graphic software designed for creating vector graphics.
Example of SVG code:
HTML
Preview
Practice
Experiment Try adding flex-direction to the .flex-container using the four different flex-direction values to see how the items will arrange in the flex-container. Possible values: row(default), row-reverse, column, column-reverse
HTML
CSS
Preview
Experiment In the demo below add flex-wrap to allow the element to flow on to a new line. Now try different justify-content values. Then add a height 20rem to the flex-container and try different align-content values to see how they work.
HTML
CSS
Preview
Experiment Add a red background color to the paragraph in the media query. Then resize your browser window until you can see the changes happening.
HTML
CSS
Preview
Successfully complete Flexbox Froggy. Take a screenshot of the completion page and save in lab 7 folder.
- Windows. Search for Snipping Tool to take a screenshot or Windows + Shift + S
- Mac. Shift + Command + 4 to take a screenshot
Open GitHub Desktop (you may need to download the program again if the computers get wiped.)
Couple of things to check in GitHub Desktop.
- Check to see if it says Pull Origin. If it does, click Pull Origin. Why? A pull occurs if you or a collaborator made a change to the repository and pushed the changes. ALWAYS MAKE SURE TO PULL BEFORE CODING!
- If the computers were wiped you made need to reclone your repository to the computer.
With your cs-318 repository selected, Click open in Visual Studio code.
Create a new folder called lab7
Create a new file called index.html in the lab7 folder
Setup your HTML files
Create your stylesheet
Link your stylesheet to the HTML
Change Document in the <title>
element to "Tree Photography"
In the <body>
add a <header>
and <main>
In the <main>
add two <section>
In the <header>
add a <h1>
with the content "Tree Photography"
Using the image below, recreate the logo as close as possible in Adobe Illustrator. Tutorial to create the logo in Adobe Illustrator.
The logo was creating using the rounded rectangle tool, circle tool, text tool, and shapes mode. You could also recreate it using just the pen tool. Note the logo is made up of black shapes, there are NO WHITE SHAPES in the logo. Font used was Helvetica bold, but any san-serif font should work. Make sure to save your logo as an Illustrator file (ai), in case you need to go back and make edits.
How to save the logo as an SVG
- Go to file>export.
- Select svg.
- Type in treelogo for file name
- Click save
- Use the setting in the image below to save as an svg.
Create an images folder
Add the svg to the images folder
After the <h1>
add the svg with an <img>
Add a <nav>
in the <header>
Add two <a>
with the following content but leave the href blank for now.
- About Me
- Gallery
In the first <section>
add a <h2>
with the content "About Me"
Add an ID attribute to the <h2>
with the value "aboutme"
Add a <div>
in the first <section>
after the <h2>
Add two <p>
in the <div>
with the following content
- Hi! I'm Danielle. My whole life, I've loved photography. What fascinates me the most, is the ability to capture a moment and freeze it in time. I decided to pursue my passion for photography in college. The knowledge I gained about the technique and art of photography truly propelled my interest to new heights. Photography has greatly impacted and changed our world. I look forward to adding my work to this revolutionary collection.
- My love behind the lens comes forth in my photography. To me, "Photography is life's elegance preserved in the most beautiful format."
Add a class attribute to the first <section>
with the value "about-me"
In the second <section>
add a <h2>
with the content "Gallery"
Add an ID attribute to the <h2>
with the value "gallery"
In the <a>
in the navigation add the following values to the href
#aboutme
#gallery
Add the following images into your images folder
- lab7-image1
- lab7-image2
- lab7-image3
- lab7-image4
- lab7-image5
- lab7-image6
- lab7-image7
- lab7-image8
- lab7-image9
- lab7-image10
In the second <section>
add all of the images with <img>
.
Don't forget the alt=""
with a description of the image
Add a class
to the second <section>
with the value "gallery"
CSS
Make your screen narrow, about 500px wide.
body
margin 0
font-family Verdana, Geneva, Tahoma, sans-serif
color #fff
background-color rgb(64, 73, 61)
Download the following image into the images folder
header
Add the new image as a background-image
background-size cover
background-repeat no-repeat
background-position center
h1
display none.
This doesn't need to display because we have the logo, but we want the browser to be able to read the <h1>
for SEO reasons
header>img
width 23rem
max-width 100%
header
padding 1rem
nav>a
background color rgb(245, 245, 245)
color rgb(15, 15, 45)
text-decoration none
padding 1rem 2rem.
Hint: Two values with padding is: top&bottom | left&right
display inline-block.
Hint: <a>
are inline by default which ignores margin on the top and bottom which is the reason the links are overlapping. We change it to inline-block to fix this.
margin 1rem
nav>a:hover
background-color rgb(200, 200, 205)
header
display flex
flex-wrap wrap
Use justify-content to get the logo and nav in the positions shown in the image below.
Hint: You might need to add the next ruleset to get justify-content to work, because of your browser width.
nav
flex-basis 100%
text-align center
main
padding 1rem
main img
max-width 100%
margin .5rem 0
Download the following image
.about-me h2
Add the image above using background-image
background-size cover
padding 10rem 0 10rem 4rem.
Hint: When using four values with padding it is the following: top | right | bottom | left
text-shadow 2px 2px 2px #0000008f
.
Hint: Text-shadow is a property used to define a drop shadow on text. The values are the following: offset-x | offset-y | blur-radius | color
.gallery h2
background-color rgb(245, 245, 245)
color rgb(15, 15, 46)
padding 2rem 4rem
Mobile styles are complete
Desktop CSS
When testing different screen sizes, I found that the web page started having some issues at about 1100px.
Add @media (min-width:1100px){}
to the end of your CSS document
From here on you will add the CSS inside the curly brackets of the media query
nav
flex-basis unset
header
flex-wrap nowrap
Use justify-content and align-items to achieve the positions in the picture below.
.about-me
display flex
.about-me h2
flex-basis 40%
background-position center
.about-me div
flex-basis 50%
margin-left 2rem
h2
padding-left 4rem
.gallery
display flex
flex-flow column wrap
max-height 78rem.
.gallery h2
margin .5rem
.gallery img
width 32%
margin .5rem
Now when I tested my gallery on different browser sizes I noticed that I had four columns at 1500px so let's add another media query.
Add @media (min-width:1500px){}
at the end of the CSS document
.gallery
max-height 120rem
Again when I tested my gallery on different browser sizes I noticed that I had four columns now at 2300px so let's one more media query.
Add @media (min-width:2300px){}
at the end of the CSS document
.gallery
max-height 150rem
Final Steps
Validate your HTML.
- Navigate to W3C Validator
- Select the validate by file upload tab at the top of the page
Lint your CSS at Jigsaw.w3.org
Now look at GitHub Desktop. You should see all of your files listed out in the left column.
Now you are going to commit your changes.
With all of the files checked. Fill out the Summary field in the bottom left and then click Commit to main.
Committing is staging the files, so they can be pushed to the repository hosted at GitHub.
Now you want to push your changes to the Repository. Now if you access the repository from another computer Lab7 will be there. After you push in the top right of the nav it should say "Fetch Origin" instead of "Push Origin"
Your are Done with Lab7
Are you someone who needs to double check that it worked? If you want to make sure the push was successful click on the "View on GitHub" button from GitHub Desktop. It will open up your repository in the browser. From there you should be able to see all of your files.
...What if I need to make a change after pushing?
So let's say you have already pushed but noticed an error. No worries, this is easy to fix.
In visual studio code make any changes you need to. Once you are done making corrections. Save.
Go back to GitHub Desktop. You should see your changes to the documents listed there.
Fill out the Summary field again. I suggest saying what you fixed. For example: "Fixed paragraph error on Sea-Jellies"
Click "Commit to main"
Click "Push Origin" and you are done.