Responsive Web Dedign(六)
freeCodeCamp —- Responsive Web Dedign
Learn CSS Flexbox by Building a Photo Gallery
Begin with your standard HTML boilerplate. Add a
DOCTYPE
declaration, anhtml
element, ahead
element, and abody
element.Add the
lang
attribute to the opening<html>
tag withen
set as the value.Within your
head
element, add ameta
tag with thename
set toviewport
and thecontent
set towidth=device-width, initial-scale=1
.Also add a
meta
tag with thecharset
set toUTF-8
.Within your
head
element, add atitle
element with the text set toPhoto Gallery
, and alink
element to add yourstyles.css
file to the page.Add a
header
element within thebody
element and assign a class ofheader
to it.Inside the
header
, create anh1
withcss flexbox photo gallery
as the text.Below your
.header
element, create a newdiv
element and assign it aclass
ofgallery
. Thisdiv
will act as a container for the gallery images.Inside that
.gallery
element, create nineimg
elements.Next, give each
img
asrc
attribute according to its order in the document. The firstimg
should have asrc
ofhttps://cdn.freecodecamp.org/curriculum/css-photo-gallery/1.jpg
. The rest should be the same, except replace the1
with the number theimg
is in the document.Normalize your box model by creating a
*
selector and setting thebox-sizing
property toborder-box
as the value.Your images are too big. Create a
.gallery img
selector to target them. Give them all awidth
of100%
and amax-width
of350px
so they shrink as needed but don’t get too big.Also set the
height
property to300px
to keep your images a uniform size.Remove the margin from your
body
element, set thefont-family
tosans-serif
, and give it abackground-color
of#f5f6f7
as the value.Align your
.header
text in the center. Make the text uppercase using thetext-transform
property withuppercase
as the value.Give it a padding of
32px
on all sides. Set the background to#0a0a23
and the text to#fff
as the color values.Add a
border-bottom
with4px solid #fdb347
as 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
display
property offlex
. This will make the element a flex container. Any direct children of a flex container are called flex items.Create a
.gallery
selector and make it a flex container.Flexbox has a main and cross axis. The main axis is defined by the
flex-direction
property, 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-direction
ofrow
on the.gallery
element.The
flex-wrap
property determines how your flex items behave when the flex container is too small. Setting it towrap
will 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-content
property determines how the items inside a flex container are positioned along the main axis, affecting their position and the space around them.Give your
.gallery
selector ajustify-content
property withcenter
as the value.The
align-items
property positions the flex content along the cross axis. In this case, with yourflex-direction
set torow
, your cross axis would be vertical.To vertically center your images, give your
.gallery
selector analign-items
property withcenter
as the value.Give your
.gallery
selector apadding
property set to20px 10px
to create some space around the container.Then, give it a
max-width
of1400px
and add amargin
of0 auto
to 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-fit
property to determine how images should behave.Give your
.gallery img
selector theobject-fit
property and set it tocover
. This will tell the image to fill theimg
container while maintaining aspect ratio, resulting in cropping to fit.Your images need some space between them.
The
gap
CSS shorthand property sets the gaps, also knowns as gutters, between rows and columns. Thegap
property and itsrow-gap
andcolumn-gap
sub-properties provide this functionality for flex, grid, and multi-column layout. You apply the property to the container element.Give your
.gallery
flex container agap
property with16px
as the value.Smooth out your images a bit by giving the
.gallery img
selector aborder-radius
property with10px
set as the value.The
::after
pseudo-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 samewidth
as 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: center
on the flex container.Example:
1
2
3
4.container::after {
content: "";
width: 860px;
}Create a new selector using an
::after
pseudo-element on the.gallery
element. Add acontent
property set to an empty string""
and350px
set for thewidth
property.The
alt
image 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
alt
attribute to all nine of your cat images to describe them. Use a value at least five characters long for each.
1 |
|
1 | * { |