Responsive Web Dedign(六)
freeCodeCamp —- Responsive Web Dedign
Learn CSS Flexbox by Building a Photo Gallery
Begin with your standard HTML boilerplate. Add a
DOCTYPEdeclaration, anhtmlelement, aheadelement, and abodyelement.Add the
langattribute to the opening<html>tag withenset as the value.Within your
headelement, add ametatag with thenameset toviewportand thecontentset towidth=device-width, initial-scale=1.Also add a
metatag with thecharsetset toUTF-8.Within your
headelement, add atitleelement with the text set toPhoto Gallery, and alinkelement to add yourstyles.cssfile to the page.Add a
headerelement within thebodyelement and assign a class ofheaderto it.Inside the
header, create anh1withcss flexbox photo galleryas the text.Below your
.headerelement, create a newdivelement and assign it aclassofgallery. Thisdivwill act as a container for the gallery images.Inside that
.galleryelement, create nineimgelements.Next, give each
imgasrcattribute according to its order in the document. The firstimgshould have asrcofhttps://cdn.freecodecamp.org/curriculum/css-photo-gallery/1.jpg. The rest should be the same, except replace the1with the number theimgis in the document.Normalize your box model by creating a
*selector and setting thebox-sizingproperty toborder-boxas the value.Your images are too big. Create a
.gallery imgselector to target them. Give them all awidthof100%and amax-widthof350pxso they shrink as needed but don’t get too big.Also set the
heightproperty to300pxto keep your images a uniform size.Remove the margin from your
bodyelement, set thefont-familytosans-serif, and give it abackground-colorof#f5f6f7as the value.Align your
.headertext in the center. Make the text uppercase using thetext-transformproperty withuppercaseas the value.Give it a padding of
32pxon all sides. Set the background to#0a0a23and the text to#fffas the color values.Add a
border-bottomwith4px solid #fdb347as the value.Flexbox is a one-dimensional CSS layout that can control the way items are spaced out and aligned within a container.
To use it, give an element a
displayproperty offlex. This will make the element a flex container. Any direct children of a flex container are called flex items.Create a
.galleryselector and make it a flex container.Flexbox has a main and cross axis. The main axis is defined by the
flex-directionproperty, which has four possible values:row(default): horizontal axis with flex items from left to rightrow-reverse: horizontal axis with flex items from right to leftcolumn: vertical axis with flex items from top to bottomcolumn-reverse: vertical axis with flex items from bottom to top
The axes and directions will be different depending on the text direction. The values shown are for a left-to-right text direction.
Try the different values to see how they affect the layout.
When you are done, set an explicit
flex-directionofrowon the.galleryelement.The
flex-wrapproperty determines how your flex items behave when the flex container is too small. Setting it towrapwill allow the items to wrap to the next row or column.nowrap(default) will prevent your items from wrapping and shrink them if needed.Make it so your flex items wrap to the next row when they run out of space.
The
justify-contentproperty determines how the items inside a flex container are positioned along the main axis, affecting their position and the space around them.Give your
.galleryselector ajustify-contentproperty withcenteras the value.The
align-itemsproperty positions the flex content along the cross axis. In this case, with yourflex-directionset torow, your cross axis would be vertical.To vertically center your images, give your
.galleryselector analign-itemsproperty withcenteras the value.Give your
.galleryselector apaddingproperty set to20px 10pxto create some space around the container.Then, give it a
max-widthof1400pxand add amarginof0 autoto center it.Notice how some of your images have become distorted. This is because the images have different aspect ratios. Rather than setting each aspect ratio individually, you can use the
object-fitproperty to determine how images should behave.Give your
.gallery imgselector theobject-fitproperty and set it tocover. This will tell the image to fill theimgcontainer while maintaining aspect ratio, resulting in cropping to fit.Your images need some space between them.
The
gapCSS shorthand property sets the gaps, also knowns as gutters, between rows and columns. Thegapproperty and itsrow-gapandcolumn-gapsub-properties provide this functionality for flex, grid, and multi-column layout. You apply the property to the container element.Give your
.galleryflex container agapproperty with16pxas the value.Smooth out your images a bit by giving the
.gallery imgselector aborder-radiusproperty with10pxset as the value.The
::afterpseudo-element creates an element that is the last child of the selected element. You can use it to add an empty element after the last image. If you give it the samewidthas the images it will push the last image to the left when the gallery is in a two-column layout. Right now, it is in the center because you setjustify-content: centeron the flex container.Example:
1
2
3
4.container::after {
content: "";
width: 860px;
}Create a new selector using an
::afterpseudo-element on the.galleryelement. Add acontentproperty set to an empty string""and350pxset for thewidthproperty.The
altimage attribute should describe the image content. Screen readers announce the alternative text in place of images. If the image can’t be loaded, this text is presented in place of the image.To complete the project, add an
altattribute to all nine of your cat images to describe them. Use a value at least five characters long for each.
1 |
|
1 | * { |