Responsive Web Dedign(十三)
freeCodeCamp —- Responsive Web Dedign
Learn CSS Grid by Building a Magazine
Begin with your standard HTML boilerplate. Add a
DOCTYPEdeclaration, anhtmlelement specifying this page is in English, aheadelement, and abodyelement.Add a
<meta>tag with the appropriatecharsetand a<meta>tag for mobile responsiveness within theheadelement.Add a
titleelement with the textMagazine, alinkelement for thehttps://fonts.googleapis.com/css?family=Anton%7CBaskervville%7CRaleway&display=swapfont stylesheet, alinkfor thehttps://use.fontawesome.com/releases/v5.8.2/css/all.cssFontAwesome stylesheet, and alinkfor your./styles.cssstylesheet.Your font stylesheet will load three separate fonts:
Anton,Baskervville, andRaleway.Within your
body, create amainelement. Then in that element, create asectionwith aclassset toheading.Within your
.headingelement, create aheaderelement with theclassset tohero.In that element, create an
imgelement with thesrcset tohttps://cdn.freecodecamp.org/platform/universal/fcc_meta_1920X1080-indigo.png, thealtset tofreecodecamp logo, and theclassset tohero-img.The
loadingattribute on animgelement can be set tolazyto 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
imgelement aloadingattribute set tolazy.After your
imgelement, add anh1element with theclassset tohero-titleand the text set toOUR NEW CURRICULUM, followed by apelement with theclassset tohero-subtitleand 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
widthattribute to theimgelement, with a value of400.You will remove this later on when you have worked on the CSS.
After your
headerelement, create adivwith theclassset toauthor.Within that
div, create apelement with theclassset toauthor-nameand give it the textBy freeCodeCamp. Wrap thefreeCodeCampportion in anaelement with thehrefset tohttps://freecodecamp.org, and thetargetset to_blank.Below that, add a second
pelement with the classpublish-dateand the textMarch 7, 2019.The
RefererHTTP 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 therelattribute tonoreferreromits this information from the HTTP request. Give youraelement arelattribute set tonoreferrer.Below your
.authorelement, create a newdivelement with the classsocial-icons.Add five
aelements within that newdiv, and give them the followinghrefattributes.- The first
aelement should have anhrefset tohttps://www.facebook.com/freecodecamp. - The second
aelement should have anhrefset tohttps://twitter.com/freecodecamp. - The third
aelement should have anhrefset tohttps://instagram.com/freecodecamp. - The fourth
aelement should have anhrefset tohttps://www.linkedin.com/school/free-code-camp. - The fifth
aelement should have anhrefset tohttps://www.youtube.com/freecodecamp.
- The first
Within each of your new
aelements, add anielement and give them the following classes:- Your first
ielement should have the classfab fa-facebook-f - Your second
ielement should have the classfab fa-twitter - Your third
ielement should have the classfab fa-instagram - Your fourth
ielement should have the classfab fa-linkedin-in - Your fifth
ielement should have the classfab fa-youtube
- Your first
Below your
.headingelement, create a newsectionelement with theclassset totext. Within that, create apelement with theclassset tofirst-paragraphand 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
pelement below your.first-paragraphelement, 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
pelement at the end of your.textelement, 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
pelements within your.textelement, create ablockquoteelement. Within that, add anhrelement, apelement with theclassset toquote, and a secondhrelement.Give the
.quoteelement the textThe entire curriculum should be a series of projects.Below your
blockquoteelement, add anotherpelement 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
pelement at the end of your.textelement, 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
pelement at the end of your.textelement 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
.textelement, create a newsectionelement and give it aclassoftext text-with-images. Within that, create anarticleelement with aclassset tobrief-history, and anasideelement with theclassset toimage-wrapper.Within your
articleelement, create anh3element with theclassset tolist-titleand the text ofA Brief History. Below that, create apelement with the textOf the Curriculum. Then create aulelement with the classlists.Within your
ulelement, create sixlielements. Add anh4element with aclassset tolist-subtitleand apelement to each of yourlielements.Then give the
h4andpelements the following text content, in order, with eachh4using what’s on the left side of the colon, and eachpusing 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
asideelement, create twoimgelements, ablockquoteelement, and a thirdimgelement. Give theblockquoteelement aclassset toimage-quote.Within the
.image-wrapperelement, give your firstimgelement asrcofhttps://cdn.freecodecamp.org/testable-projects-fcc/images/random-quote-machine.png, analtofimage of the quote machine project, aclassofimage-1, aloadingattribute set tolazy, awidthattribute of600, and aheightattribute of400.Within your
.image-wrapperelement, give the secondimgelement asrcofhttps://cdn.freecodecamp.org/testable-projects-fcc/images/calc.png, analtofimage of a calculator project, aloadingattribute set tolazy, aclassset toimage-2, awidthattribute set to400, and aheightattribute set to400.Within your
.image-wrapperelement, give your thirdimgelement asrcofhttps://cdn.freecodecamp.org/testable-projects-fcc/images/survey-form-background.jpeg, analtoffour people working on code, aloadingattribute oflazy, aclassset toimage-3, awidthattribute set to600, and aheightattribute set to400.Within your
.image-quoteelement, nest anhrelement, apelement and a secondhrelement. Give thepelement aclassset toquoteand 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::beforeand::afterpseudo-selectors.Set the
paddingandmarginproperties both to0and set thebox-sizingproperty toborder-box.Create an
htmlselector and give it afont-sizeproperty 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
remunits later, as2remwould be 20px.Create a
bodyselector. Set thefont-familyproperty toBaskervville, with a fallback ofserif. Set thecolorproperty tolinenand thebackground-colorproperty torgb(20, 30, 40).Create an
h1selector, and set thefont-familyproperty toAntonwith the fallback ofsans-serif.Create an
h2, h3, h4, h5, h6selector. Give it afont-familyproperty set toRalewaywith a fallback ofsans-serif.Create an
aselector, and give it atext-decorationproperty set tononeand acolorproperty 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
mainselector and giving it adisplayproperty 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
mainelement. Set the content to have a three-column layout by adding agrid-template-columnsproperty with a value of1fr 94rem 1fr. This will create three columns where the middle column is94remwide, and the first and last columns are both 1 fraction of the remaining space in the grid container.Use the
minmaxfunction to make your columns responsive on any device. Theminmaxfunction 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-columnsproperty in aminmaxfunction, using each value as the second argument. The first argument should be2rem,min-content, and2remrespectively.To add space between rows in the grid layout, you can use the
row-gapproperty. Give themainselector arow-gapproperty of3rem.Your magazine will have three primary sections. You already set the overall layout in the
mainrule, but you can adjust the placement in the child rules.One option is the
grid-columnproperty, which is shorthand forgrid-column-startandgrid-column-end. Thegrid-columnproperty tells the grid item which grid line to start and end at.Create a
.headingrule and set thegrid-columnproperty to2 / 3. This will tell the.headingelement to start at grid line 2 and end at grid line 3.Create a
.textselector and give it agrid-columnproperty set to2 / 3.For additional control over the layout of your content, you can have a CSS Grid within a CSS Grid.
Set the
displayproperty of your.headingselector togrid.Now you can style the content of the
.headingelement 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-columnsproperty torepeat(20, 200px)would create 20 columns each200pxwide.Give your
.headingelement agrid-template-columnsproperty set torepeat(2, 1fr)to create two columns of equal width.Give your
.headingselector arow-gapproperty set to1.5rem.Remember that the
grid-columnproperty 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-1for the end column.Create a
.heroselector and give it agrid-columnproperty set to1 / -1. This will tell the element to span the full width of the grid.Give the
.heroselector apositionproperty set torelative.You should remove the temporary
widthattribute before writing the CSS for your.hero-img.Create an
imgselector and give it awidthproperty set to100%, and anobject-fitproperty set tocover.The
object-fitproperty tells the browser how to position the element within its container. In this case,coverwill set the image to fill the container, cropping as needed to avoid changing the aspect ratio.Create a
.hero-titleselector and give it atext-alignproperty set tocenter, acolorproperty set toorangeredand afont-sizeproperty set to8rem.The subtitle also needs to be styled. Create a
.hero-subtitleselector and give it afont-sizeproperty set to2.4rem, acolorproperty set toorangered, and atext-alignproperty set tocenter.Create an
.authorselector and give it afont-sizeproperty set to2remand afont-familyproperty set toRalewaywith a fallback ofsans-serif.Create a
.author-name a:hoverselector and give it abackground-colorproperty set to#306203.This will create a hover effect only for the
aelement within the.author-name, showing the original freeCodeCamp green in the background.Create a
.publish-dateselector and give it acolorproperty ofrgba(255, 255, 255, 0.5).Create a
.social-iconsselector. Give it adisplayproperty set togrid, and afont-sizeproperty set to3rem.The default settings for CSS Grid will create additional rows as needed, unlike Flexbox. Give the
.social-iconsselector agrid-template-columnsproperty 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-columnsto create additional columns. As an alternative, you can use thegrid-auto-flowproperty.This property takes either
roworcolumnas the first value, with an optional second value ofdense.grid-auto-flowuses an auto-placement algorithm to adjust the grid layout. Setting it tocolumnwill tell the algorithm to create new columns for content as needed. Thedensevalue 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-iconsselector, set thegrid-auto-flowproperty 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-columnsproperty. Give the.social-iconsselector agrid-auto-columnsproperty set to1fr.Much like Flexbox, with CSS Grid you can align the content of grid items with
align-itemsandjustify-items.align-itemswill align child elements along the column axis, andjustify-itemswill align child elements along the row axis.Give the
.social-iconsselector analign-itemsproperty set tocenter.Give the
.textselector afont-sizeproperty set to1.8remand aletter-spacingproperty set to0.6px.Your
.textelement is not a CSS Grid, but you can create columns within an element without using Grid by using thecolumn-widthproperty.Give your
.textselector acolumn-widthproperty 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
.textselector atext-alignproperty set tojustify.The
::first-letterpseudo-selector allows you to target the first letter in the text content of an element.Create a
.first-paragraph::first-letterselector and set thefont-sizeproperty to6rem. Also give it acolorproperty set toorangeredto make it stand out.The other text has been shifted out of place. Move it into position by giving the
.first-paragraph::first-letterselector afloatproperty set toleftand amargin-rightproperty set to1rem.Create an
hrselector, and give it amarginproperty set to1.5rem 0.To give the
hra color, you need to adjust theborderproperty. Give thehrselector aborderproperty set to1px solid rgba(120, 120, 120, 0.6).Create a
.quoteselector. Give it acolorproperty set to#00beef, afont-sizeproperty set to2.4rem, and atext-alignproperty set tocenter.To make the quote text stand out more, give the
.quoteselector afont-familyproperty set toRalewaywith 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::beforeselector and set thecontentproperty to"with a space following it.Also, create a
.quote::afterselector and set thecontentproperty to"with a space preceding it.Now it’s time to style your third
section. Note that it has thetextandtext-with-imagesvalues for theclassattribute, which means it is already inheriting the styles from your.textrule.Create a
.text-with-imagesselector and set thedisplayproperty togrid.You will need to have a column for text and a column for images. Give the
.text-with-imagesselector agrid-template-columnsproperty set to1fr 2fr. Also set thecolumn-gapproperty to3remto provide more spacing between the columns.Give the
.text-with-imagesselector amargin-bottomproperty set to3rem.Create a
.listsselector and set thelist-style-typeproperty tonone. This will get rid of the bullet points on the list items.Give the
.listsselector amargin-topproperty set to2rem.Create a
.lists lirule to target the list items within your.listselement. Give it amargin-bottomproperty set to1.5rem.Create a
.list-title, .list-subtitleselector and set thecolorproperty to#00beef.Time to style the last section of the magazine - the images.
The images are wrapped with an
asideelement using theimage-wrapperclass, so create an.image-wrapperselector. Set thedisplayproperty togrid.The images should be within a two column, three row layout.
Give the
.image-wrapperselector agrid-template-columnsproperty set to2fr 1frand agrid-template-rowsproperty 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
gapproperty is a shorthand way to set the value ofcolumn-gapandrow-gapat the same time. If given one value, it sets thecolumn-gapandrow-gapboth to that value. If given two values, it sets therow-gapto the first value and thecolumn-gapto the second.Give the
.image-wrapperselector agapproperty set to2rem.The
place-itemsproperty can be used to set thealign-itemsandjustify-itemsvalues at the same time. Theplace-itemsproperty takes one or two values. If one value is provided, it is used for both thealign-itemsandjustify-itemsproperties. If two values are provided, the first value is used for thealign-itemsproperty and the second value is used for thejustify-itemsproperty.Give the
.image-wrapperselector aplace-itemsproperty set tocenter.Create an
.image-1, .image-3rule and give it agrid-columnproperty 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
@mediaquery foronly screenwith amax-widthof720px. Inside, create an.image-wrapperselector and give it agrid-template-columnsproperty of1fr.This will collapse the three images into one column on smaller screens.
Create another
@mediaquery foronly screenwith amax-widthof600px. Within, create a.text-with-imagesrule and give it agrid-template-columnsproperty of1fr.This will collapse your bottom text area into a single column on smaller screens.
Create a third
@mediaquery foronly screenwith amax-widthof550px. Within, create a.hero-titleselector with afont-sizeset to6rem, a.hero-subtitle, .author, .quote, .list-titleselector with afont-sizeset to1.8rem, a.social-iconsselector with afont-sizeset to2rem, and a.textselector with afont-sizeset to1.6rem.Create one final
@mediaquery foronly screenwith amax-widthof420px. Within, create a.hero-titleselector with afont-sizeproperty set to4.5rem.Congratulations! Your magazine is now complete.
1 |
|
1 | *, |