Responsive Web Dedign(十三)
freeCodeCamp —- Responsive Web Dedign
Learn CSS Grid by Building a Magazine
Begin with your standard HTML boilerplate. Add a
DOCTYPE
declaration, anhtml
element specifying this page is in English, ahead
element, and abody
element.Add a
<meta>
tag with the appropriatecharset
and a<meta>
tag for mobile responsiveness within thehead
element.Add a
title
element with the textMagazine
, alink
element for thehttps://fonts.googleapis.com/css?family=Anton%7CBaskervville%7CRaleway&display=swap
font stylesheet, alink
for thehttps://use.fontawesome.com/releases/v5.8.2/css/all.css
FontAwesome stylesheet, and alink
for your./styles.css
stylesheet.Your font stylesheet will load three separate fonts:
Anton
,Baskervville
, andRaleway
.Within your
body
, create amain
element. Then in that element, create asection
with aclass
set toheading
.Within your
.heading
element, create aheader
element with theclass
set tohero
.In that element, create an
img
element with thesrc
set tohttps://cdn.freecodecamp.org/platform/universal/fcc_meta_1920X1080-indigo.png
, thealt
set tofreecodecamp logo
, and theclass
set tohero-img
.The
loading
attribute on animg
element can be set tolazy
to tell the browser not to fetch the image resource until it is needed (as in, when the user scrolls the image into view). As an additional benefit, lazy loaded elements will not load until the non-lazy elements are loaded - this means users with slow internet connections can view the content of your page without having to wait for the images to load.Give your new
img
element aloading
attribute set tolazy
.After your
img
element, add anh1
element with theclass
set tohero-title
and the text set toOUR NEW CURRICULUM
, followed by ap
element with theclass
set tohero-subtitle
and the text set toOur efforts to restructure our curriculum with a more project-based focus
.Your image currently takes up a lot of space. To better see what you are working on, add a
width
attribute to theimg
element, with a value of400
.You will remove this later on when you have worked on the CSS.
After your
header
element, create adiv
with theclass
set toauthor
.Within that
div
, create ap
element with theclass
set toauthor-name
and give it the textBy freeCodeCamp
. Wrap thefreeCodeCamp
portion in ana
element with thehref
set tohttps://freecodecamp.org
, and thetarget
set to_blank
.Below that, add a second
p
element with the classpublish-date
and the textMarch 7, 2019
.The
Referer
HTTP header contains information about the address or URL of a page that a user might be visiting from. This information can be used in analytics to track how many users from your page visit freecodecamp.org, for example. Setting therel
attribute tonoreferrer
omits this information from the HTTP request. Give youra
element arel
attribute set tonoreferrer
.Below your
.author
element, create a newdiv
element with the classsocial-icons
.Add five
a
elements within that newdiv
, and give them the followinghref
attributes.- The first
a
element should have anhref
set tohttps://www.facebook.com/freecodecamp
. - The second
a
element should have anhref
set tohttps://twitter.com/freecodecamp
. - The third
a
element should have anhref
set tohttps://instagram.com/freecodecamp
. - The fourth
a
element should have anhref
set tohttps://www.linkedin.com/school/free-code-camp
. - The fifth
a
element should have anhref
set tohttps://www.youtube.com/freecodecamp
.
- The first
Within each of your new
a
elements, add ani
element and give them the following classes:- Your first
i
element should have the classfab fa-facebook-f
- Your second
i
element should have the classfab fa-twitter
- Your third
i
element should have the classfab fa-instagram
- Your fourth
i
element should have the classfab fa-linkedin-in
- Your fifth
i
element should have the classfab fa-youtube
- Your first
Below your
.heading
element, create a newsection
element with theclass
set totext
. Within that, create ap
element with theclass
set tofirst-paragraph
and the following text:1
Soon the freeCodeCamp curriculum will be 100% project-driven learning. Instead of a series of coding challenges, you'll learn through building projects - step by step. Before we get into the details, let me emphasize: we are not changing the certifications. All 6 certifications will still have the same 5 required projects. We are only changing the optional coding challenges.
Create another
p
element below your.first-paragraph
element, and give it the following text:1
After years - years - of pondering these two problems and how to solve them, I slipped, hit my head on the sink, and when I came to I had a revelation! A vision! A picture in my head! A picture of this! This is what makes time travel possible: the flux capacitor!
Add a third
p
element at the end of your.text
element, and give it the following text:1
It wasn't as dramatic as Doc's revelation in Back to the Future. It just occurred to me while I was going for a run. The revelation: the entire curriculum should be a series of projects. Instead of individual coding challenges, we'll just have projects, each with their own seamless series of tests. Each test gives you just enough information to figure out how to get it to pass. (And you can view hints if that isn't enough.)
After the three
p
elements within your.text
element, create ablockquote
element. Within that, add anhr
element, ap
element with theclass
set toquote
, and a secondhr
element.Give the
.quote
element the textThe entire curriculum should be a series of projects
.Below your
blockquote
element, add anotherp
element with the following text:1
No more walls of explanatory text. No more walls of tests. Just one test at a time, as you build up a working project. Over the course of passing thousands of tests, you build up projects and your own understanding of coding fundamentals. There is no transition between lessons and projects, because the lessons themselves are baked into projects. And there's plenty of repetition to help you retain everything because - hey - building projects in real life has plenty of repetition.
Create a fifth
p
element at the end of your.text
element, and give it the following text:1
The main design challenge is taking what is currently paragraphs of explanation and instructions and packing them into a single test description text. Each project will involve dozens of tests like this. People will be coding the entire time, rather than switching back and forth from "reading mode" to "coding mode".
Create one final
p
element at the end of your.text
element and give it the following text:1
2
3
4
5
6Instead of a series of coding challenges, people will be in their code
editor passing one test after another, quickly building up a project.
People will get into a real flow state, similar to what they
experience when they build the required projects at the end of each
certification. They'll get that sense of forward progress right from
the beginning. And freeCodeCamp will be a much smoother experience.Below your
.text
element, create a newsection
element and give it aclass
oftext text-with-images
. Within that, create anarticle
element with aclass
set tobrief-history
, and anaside
element with theclass
set toimage-wrapper
.Within your
article
element, create anh3
element with theclass
set tolist-title
and the text ofA Brief History
. Below that, create ap
element with the textOf the Curriculum
. Then create aul
element with the classlists
.Within your
ul
element, create sixli
elements. Add anh4
element with aclass
set tolist-subtitle
and ap
element to each of yourli
elements.Then give the
h4
andp
elements the following text content, in order, with eachh4
using what’s on the left side of the colon, and eachp
using what’s on the right:V1 - 2014
:We launched freeCodeCamp with a simple list of 15 resources, including Harvard's CS50 and Stanford's Database Class.
V2 - 2015
:We added interactive algorithm challenges.
V3 - 2015
:We added our own HTML+CSS challenges (before we'd been relying on General Assembly's Dash course for these).
V4 - 2016
:We expanded the curriculum to 3 certifications, including Front End, Back End, and Data Visualization. They each had 10 required projects, but only the Front End section had its own challenges. For the other certs, we were still using external resources like Node School.
V5 - 2017
:We added the back end and data visualization challenges.
V6 - 2018
:We launched 6 new certifications to replace our old ones. This was the biggest curriculum improvement to date.
Within your
aside
element, create twoimg
elements, ablockquote
element, and a thirdimg
element. Give theblockquote
element aclass
set toimage-quote
.Within the
.image-wrapper
element, give your firstimg
element asrc
ofhttps://cdn.freecodecamp.org/testable-projects-fcc/images/random-quote-machine.png
, analt
ofimage of the quote machine project
, aclass
ofimage-1
, aloading
attribute set tolazy
, awidth
attribute of600
, and aheight
attribute of400
.Within your
.image-wrapper
element, give the secondimg
element asrc
ofhttps://cdn.freecodecamp.org/testable-projects-fcc/images/calc.png
, analt
ofimage of a calculator project
, aloading
attribute set tolazy
, aclass
set toimage-2
, awidth
attribute set to400
, and aheight
attribute set to400
.Within your
.image-wrapper
element, give your thirdimg
element asrc
ofhttps://cdn.freecodecamp.org/testable-projects-fcc/images/survey-form-background.jpeg
, analt
offour people working on code
, aloading
attribute oflazy
, aclass
set toimage-3
, awidth
attribute set to600
, and aheight
attribute set to400
.Within your
.image-quote
element, nest anhr
element, ap
element and a secondhr
element. Give thep
element aclass
set toquote
and the textThe millions of people who are learning to code through freeCodeCamp will have an even better resource to help them learn these fundamentals.
To start your CSS, normalize the CSS rules by targeting all elements with
*
, including the::before
and::after
pseudo-selectors.Set the
padding
andmargin
properties both to0
and set thebox-sizing
property toborder-box
.Create an
html
selector and give it afont-size
property set to62.5%
. This will set the default font size for your web page to 10px (the browser default is 16px).This will make it easier for you to work with
rem
units later, as2rem
would be 20px.Create a
body
selector. Set thefont-family
property toBaskervville
, with a fallback ofserif
. Set thecolor
property tolinen
and thebackground-color
property torgb(20, 30, 40)
.Create an
h1
selector, and set thefont-family
property toAnton
with the fallback ofsans-serif
.Create an
h2, h3, h4, h5, h6
selector. Give it afont-family
property set toRaleway
with a fallback ofsans-serif
.Create an
a
selector, and give it atext-decoration
property set tonone
and acolor
property set tolinen
.Now you are ready to start putting together the grid layout. CSS Grid offers a two-dimensional grid-based layout, allowing you to center items horizontally and vertically while still retaining control to do things like overlap elements.
Begin by creating a
main
selector and giving it adisplay
property set togrid
.Now you can style the layout of your grid. CSS Grid is similar to Flexbox in that it has a special property for both the parent and child elements.
In this case, your parent element is the
main
element. Set the content to have a three-column layout by adding agrid-template-columns
property with a value of1fr 94rem 1fr
. This will create three columns where the middle column is94rem
wide, and the first and last columns are both 1 fraction of the remaining space in the grid container.Use the
minmax
function to make your columns responsive on any device. Theminmax
function takes two arguments, the first being the minimum value and the second being the maximum. These values could be a length, percentage,fr
, or even a keyword likemax-content
.Wrap each of your already defined values of the
grid-template-columns
property in aminmax
function, using each value as the second argument. The first argument should be2rem
,min-content
, and2rem
respectively.To add space between rows in the grid layout, you can use the
row-gap
property. Give themain
selector arow-gap
property of3rem
.Your magazine will have three primary sections. You already set the overall layout in the
main
rule, but you can adjust the placement in the child rules.One option is the
grid-column
property, which is shorthand forgrid-column-start
andgrid-column-end
. Thegrid-column
property tells the grid item which grid line to start and end at.Create a
.heading
rule and set thegrid-column
property to2 / 3
. This will tell the.heading
element to start at grid line 2 and end at grid line 3.Create a
.text
selector and give it agrid-column
property set to2 / 3
.For additional control over the layout of your content, you can have a CSS Grid within a CSS Grid.
Set the
display
property of your.heading
selector togrid
.Now you can style the content of the
.heading
element with CSS Grid.The CSS
repeat()
function is used to repeat a value, rather than writing it out manually, and is helpful for grid layouts. For example, setting thegrid-template-columns
property torepeat(20, 200px)
would create 20 columns each200px
wide.Give your
.heading
element agrid-template-columns
property set torepeat(2, 1fr)
to create two columns of equal width.Give your
.heading
selector arow-gap
property set to1.5rem
.Remember that the
grid-column
property determines which columns an element starts and ends at. There may be times where you are unsure of how many columns your grid will have, but you want an element to stop at the last column. To do this, you can use-1
for the end column.Create a
.hero
selector and give it agrid-column
property set to1 / -1
. This will tell the element to span the full width of the grid.Give the
.hero
selector aposition
property set torelative
.You should remove the temporary
width
attribute before writing the CSS for your.hero-img
.Create an
img
selector and give it awidth
property set to100%
, and anobject-fit
property set tocover
.The
object-fit
property tells the browser how to position the element within its container. In this case,cover
will set the image to fill the container, cropping as needed to avoid changing the aspect ratio.Create a
.hero-title
selector and give it atext-align
property set tocenter
, acolor
property set toorangered
and afont-size
property set to8rem
.The subtitle also needs to be styled. Create a
.hero-subtitle
selector and give it afont-size
property set to2.4rem
, acolor
property set toorangered
, and atext-align
property set tocenter
.Create an
.author
selector and give it afont-size
property set to2rem
and afont-family
property set toRaleway
with a fallback ofsans-serif
.Create a
.author-name a:hover
selector and give it abackground-color
property set to#306203
.This will create a hover effect only for the
a
element within the.author-name
, showing the original freeCodeCamp green in the background.Create a
.publish-date
selector and give it acolor
property ofrgba(255, 255, 255, 0.5)
.Create a
.social-icons
selector. Give it adisplay
property set togrid
, and afont-size
property set to3rem
.The default settings for CSS Grid will create additional rows as needed, unlike Flexbox. Give the
.social-icons
selector agrid-template-columns
property set torepeat(5, 1fr)
to arrange the icons in a single row.If you wanted to add more social icons, but keep them on the same row, you would need to update
grid-template-columns
to create additional columns. As an alternative, you can use thegrid-auto-flow
property.This property takes either
row
orcolumn
as the first value, with an optional second value ofdense
.grid-auto-flow
uses an auto-placement algorithm to adjust the grid layout. Setting it tocolumn
will tell the algorithm to create new columns for content as needed. Thedense
value allows the algorithm to backtrack and fill holes in the grid with smaller items, which can result in items appearing out of order.For your
.social-icons
selector, set thegrid-auto-flow
property tocolumn
.Now the auto-placement algorithm will kick in when you add a new icon element. However, the algorithm defaults the new column width to be
auto
, which will not match your current columns.You can override this with the
grid-auto-columns
property. Give the.social-icons
selector agrid-auto-columns
property set to1fr
.Much like Flexbox, with CSS Grid you can align the content of grid items with
align-items
andjustify-items
.align-items
will align child elements along the column axis, andjustify-items
will align child elements along the row axis.Give the
.social-icons
selector analign-items
property set tocenter
.Give the
.text
selector afont-size
property set to1.8rem
and aletter-spacing
property set to0.6px
.Your
.text
element is not a CSS Grid, but you can create columns within an element without using Grid by using thecolumn-width
property.Give your
.text
selector acolumn-width
property set to25rem
.Magazines often use justified text in their printed content to structure their layout and control the flow of their content. While this works in printed form, justified text on websites can be an accessibility concern, for example presenting challenges for folks with dyslexia.
To make your project look like a printed magazine, give the
.text
selector atext-align
property set tojustify
.The
::first-letter
pseudo-selector allows you to target the first letter in the text content of an element.Create a
.first-paragraph::first-letter
selector and set thefont-size
property to6rem
. Also give it acolor
property set toorangered
to make it stand out.The other text has been shifted out of place. Move it into position by giving the
.first-paragraph::first-letter
selector afloat
property set toleft
and amargin-right
property set to1rem
.Create an
hr
selector, and give it amargin
property set to1.5rem 0
.To give the
hr
a color, you need to adjust theborder
property. Give thehr
selector aborder
property set to1px solid rgba(120, 120, 120, 0.6)
.Create a
.quote
selector. Give it acolor
property set to#00beef
, afont-size
property set to2.4rem
, and atext-align
property set tocenter
.To make the quote text stand out more, give the
.quote
selector afont-family
property set toRaleway
with a fallback ofsans-serif
.A quote is not really a quote without proper quotation marks. You can add these with CSS pseudo selectors.
Create a
.quote::before
selector and set thecontent
property to"
with a space following it.Also, create a
.quote::after
selector and set thecontent
property to"
with a space preceding it.Now it’s time to style your third
section
. Note that it has thetext
andtext-with-images
values for theclass
attribute, which means it is already inheriting the styles from your.text
rule.Create a
.text-with-images
selector and set thedisplay
property togrid
.You will need to have a column for text and a column for images. Give the
.text-with-images
selector agrid-template-columns
property set to1fr 2fr
. Also set thecolumn-gap
property to3rem
to provide more spacing between the columns.Give the
.text-with-images
selector amargin-bottom
property set to3rem
.Create a
.lists
selector and set thelist-style-type
property tonone
. This will get rid of the bullet points on the list items.Give the
.lists
selector amargin-top
property set to2rem
.Create a
.lists li
rule to target the list items within your.lists
element. Give it amargin-bottom
property set to1.5rem
.Create a
.list-title, .list-subtitle
selector and set thecolor
property to#00beef
.Time to style the last section of the magazine - the images.
The images are wrapped with an
aside
element using theimage-wrapper
class, so create an.image-wrapper
selector. Set thedisplay
property togrid
.The images should be within a two column, three row layout.
Give the
.image-wrapper
selector agrid-template-columns
property set to2fr 1fr
and agrid-template-rows
property set torepeat(3, min-content)
. This will give our grid rows that adjust in height based on the content, but columns that remain a fixed width based on the container.The
gap
property is a shorthand way to set the value ofcolumn-gap
androw-gap
at the same time. If given one value, it sets thecolumn-gap
androw-gap
both to that value. If given two values, it sets therow-gap
to the first value and thecolumn-gap
to the second.Give the
.image-wrapper
selector agap
property set to2rem
.The
place-items
property can be used to set thealign-items
andjustify-items
values at the same time. Theplace-items
property takes one or two values. If one value is provided, it is used for both thealign-items
andjustify-items
properties. If two values are provided, the first value is used for thealign-items
property and the second value is used for thejustify-items
property.Give the
.image-wrapper
selector aplace-items
property set tocenter
.Create an
.image-1, .image-3
rule and give it agrid-column
property set to1 / -1
. This will allow the first and third images to span the full width of the grid.Now that the magazine layout is finished, you need to make it responsive.
Start with a
@media
query foronly screen
with amax-width
of720px
. Inside, create an.image-wrapper
selector and give it agrid-template-columns
property of1fr
.This will collapse the three images into one column on smaller screens.
Create another
@media
query foronly screen
with amax-width
of600px
. Within, create a.text-with-images
rule and give it agrid-template-columns
property of1fr
.This will collapse your bottom text area into a single column on smaller screens.
Create a third
@media
query foronly screen
with amax-width
of550px
. Within, create a.hero-title
selector with afont-size
set to6rem
, a.hero-subtitle, .author, .quote, .list-title
selector with afont-size
set to1.8rem
, a.social-icons
selector with afont-size
set to2rem
, and a.text
selector with afont-size
set to1.6rem
.Create one final
@media
query foronly screen
with amax-width
of420px
. Within, create a.hero-title
selector with afont-size
property set to4.5rem
.Congratulations! Your magazine is now complete.
1 |
|
1 | *, |