Responsive Web Dedign(五)
freeCodeCamp —- Responsive Web Dedign
Learn the CSS Box Model by Building a Rothko Painting
By now, you should be familiar with the basic elements an HTML page should have.
Set up your code with a
DOCTYPEdeclaration, anhtmlelement with the language set to English, aheadelement, and abodyelement.Within the
headelement, add ametatag which sets thecharsettoUTF-8, and atitleelement with the valueRothko Painting.Within the
bodyelement, add animgelement with asrcofhttps://cdn.freecodecamp.org/curriculum/css-box-model/diagram-1.png.In the CSS box model, every HTML element is treated as a box with four areas.
Imagine you receive a box from your favorite online retailer – the content is the item in the box, or in our case, a header, paragraph, or image element.
Change the
srcattribute in the<img>fromhttps://cdn.freecodecamp.org/curriculum/css-box-model/diagram-1.pngtohttps://cdn.freecodecamp.org/curriculum/css-box-model/diagram-2.png.The content is surrounded by a space called padding, similar to how bubble wrap separates an item from the box around it.
Think of the border like the cardboard box your item was shipped in.
Change the
srcattribute tohttps://cdn.freecodecamp.org/curriculum/css-box-model/diagram-3.pngMargin is the area outside of the box, and can be used to control the space between other boxes or elements.
Here the bottom element has a larger top margin, pushing it further down the page.
Now that you understand the CSS box model, let’s get started on the Rothko painting.
Remove the
<img>element.Add a
divelement in thebody.Set the
classattribute equal tocanvas. For example,<div class="my-div">.This will act as the canvas for your painting.
Before you can start styling the
divyou added, you need to link your CSS to your HTML.Add a
linkelement to link yourstyles.cssfile. Set thehreftostyles.css, and remember to set therelattribute tostylesheet.Time for CSS.
Even though your
<div>has no text, it’s still treated as a box with content. Write a CSS rule that uses the.canvasclass selector and set itswidthto 500 pixels. Here’s a CSS rule that sets the width of the classcardto 300 pixels:1
2
3.card {
width: 300px;
}Add the
heightproperty with the value600pxto your.canvasrule.Change the
background-colorof the canvas to#4d0f00.Every painting needs a frame.
Wrap the
.canvaselement in anotherdiv. Give thatdivtheframeclass.Write a new rule using the
.frameclass selector.Use the
bordershorthand declaration to give the.frameelement a solid, black border with a width of50px.The frame is much too wide.
In
.frame, set itswidthto 500 pixels.Use padding to adjust the spacing within an element.
In
.frame, use thepaddingshorthand property to increase the space between the.frameand.canvaselements by50px. The shorthand will increase space in the top, bottom, left, and right of the element’s border and canvas within.Use margins to adjust the spacing outside of an element.
Using the
marginproperty, give the.frameelement vertical margin of20px, and horizontal margin ofauto. This will move the frame down 20 pixels and horizontally center it on the page.Add a new
divelement inside of your.canvaselement.Give the new
divtheclassattribute with a value ofone. This will be your first rectangle.Write a new rule that targets
.oneand set itswidthto 425 pixels.Now set the
heightfor.oneto 150 pixels.Set the
background-colorof.oneto#efb762.Use margins to position the
.oneelement on the canvas.Add the shorthand
marginproperty with a vertical margin of20pxand a horizontal margin ofauto.Now
.oneis centered horizontally, but its top margin is pushing past the canvas and onto the frame’s border, shifting the entire canvas down 20 pixels.Add
paddingof1pxto the.canvaselement to give the.oneelement something solid to push off of.Adding 1 pixel of padding to the top, bottom, left, and right of the canvas changed its dimensions to 502 pixels x 602 pixels.
Replace the
paddingproperty withoverflowset tohidden- changing the canvas back to its original dimensions.Add another
divwith aclassvalue oftwojust below youroneelement. This will be your second rectangle.Create a new CSS rule using the
.twoselector and set itswidthto 475 pixels.Set the
heightof the.twoelement to 200 pixels.Set the
background-colorof the.twoelement to#8f0401.Center the
.twoelement by setting itsmargintoauto.Create a new
divwith aclassvalue ofthreeright under the.twoelement. This will be your third rectangle.You don’t always have to use pixels when sizing an element.
Create a new rule,
.three, and set itswidthto91%.Set the
heightof.threeto28%.Change the
background-colorof.threeto#b20403.Center the
.threeelement on the canvas by setting itsmargintoauto.It’s helpful to have your margins push in one direction.
In this case, the bottom margin of the
.oneelement pushes.twodown 20 pixels.In the
.twoselector, usemarginshorthand property to set top margin to0, horizontal margin toauto, and bottom margin to20px. This will remove its top margin, horizontally center it, and set its bottom margin to 20 pixels.The colors and shapes of your painting are too sharp to pass as a Rothko.
Use the
filterproperty toblurthe painting by2pxin the.canvaselement.Here’s an example of a rule that add a 3px
blur:1
2
3p {
filter: blur(3px);
}Create a rule that targets both
.oneand.twoand increase theirblureffect by 1 pixel.Increase the
blurof.threeby 2 pixels.The rectangles are too small and their edges don’t have the soft quality of a painting.
Increase the area and soften the edges of
.oneby setting itsbox-shadowto0 0 3px 3px #efb762.The rectangles are too small and their edges don’t have the soft quality of a painting.
Increase the area and soften the edges of
.oneby setting itsbox-shadowto0 0 3px 3px #efb762.Add a
box-shadowto.threewith the values0 0 5px 5px #b20403.The corners of each rectangle are still too sharp.
Round each corner of the
.oneelement by 9 pixels, using theborder-radiusproperty.Use the
border-radiusproperty on the.twoselector, to set its top-left radius and bottom-right radius to8px, and top-right radius and bottom-left radius to10px.The
border-radiusproperty accepts up to four values to round the top-left, top-right, bottom-right, and bottom-left corners.Round the top-left corner of
.threeby 30 pixels, the top-right by 25 pixels, the bottom-right by 60 pixels, and bottom-left by 12 pixels.Rotate each rectangle to give them more of an imperfect, hand-painted look.
Use the
transformproperty on the.oneselector torotateit counter clockwise by 0.6 degrees.Rotate the
.twoelement clockwise by 0.4 degrees.Rotate
.threecounter clockwise by 0.2 degrees.With this final step, your Rothko painting is now complete.
1 |
|
1 | .canvas { |