@Sarah
2015-12-27T23:27:23.000000Z
字数 14754
阅读 1090
css
Taking up space
Cool, right? Each HTML element gets its own box to live in.
As you saw, the outermost box of each element went all the way across the page. This is why until now, your HTML elements have been sitting on top of one another: by default, they take up the full width of the page.
We can change all this with the first positioning property we'll learn: the display property. We'll learn about four possible values.
block: This makes the element a block box. It won't let anything sit next to it on the page! It takes up the full width.
inline-block: This makes the element a block box, but will allow other elements to sit next to it on the same line.
inline: This makes the element sit on the same line as another element, but without formatting it like a block. It only takes up as much width as it needs (not the whole line).
none: This makes the element and its content disappear from the page entirely!
Instructions
Let's get cracking. Set the display of all
* {
border: 1px dashed blue;
}
div {
height: 50px;
width: 100px;
border: 2px solid black;
border-radius: 5px;
/*Add your CSS here!*/
}
#one {
background-color: #FF0000;
}
#two {
background-color: #0000FF;
}
#three {
background-color: #FFD700;
}
#four {
background-color: #308014;
}
Good work! If you didn't notice much of a difference, don't worry. Our
As mentioned, any element that comes in as a block (say, a paragraph) will automatically take up the full width of the page, no matter how much or how little content you put in.
If we specify a display of inline-block, however, our blocks are still blocks, but will be able to sit next to each other on the same line.
Instructions
Change the display property of all your
div {
height: 50px;
width: 100px;
border: 2px solid black;
border-radius: 5px;
/*Add your CSS here!*/
display: inline-block;
}
Did you see that? Your
The inline-block value allows you to put several block elements on the same line. The inline value places all your elements next to one another, but not as blocks: they don't keep their dimensions.
Instructions
Try it and see! Set all your
div {
height: 50px;
width: 100px;
border: 2px solid black;
border-radius: 5px;
/*Add your CSS here!*/
display: inline;
}
The good news is, inline places all your elements on a single line. The bad news is that it doesn't maintain their "box"ness: as you saw, all your
The inline display value is better suited for HTML elements that are blocks by default, such as headers and paragraphs.
Finally, we'll try out the display value none. As you might expect, this prevents the page from displaying the selected element. As you might not expect, this removes the selected element from the page entirely, including any children and any content. Poof! Gone! (But not gone forever—changing the display value away from none will bring everything back.)
Instructions
Give it a whirl! Set all your
div {
height: 50px;
width: 100px;
border: 2px solid black;
border-radius: 5px;
/*Add your CSS here!*/
display: none;
}
Now that you understand more about the display property and the box model, we can delve into the details of how each individual box behaves.
Check out the diagram in the Result tab (it's the one from the first exercise in this lesson). As you can see, each box is made of layers. From the outermost to the innermost:
The margin is the space around the element. The larger the margin, the more space between our element and the elements around it. We can adjust the margin to move our HTML elements closer to or farther from each other.
The border is the edge of the element. It's what we've been making visible every time we set the border property.
The padding is the spacing between the content and the border. We can adjust this value with CSS to move the border closer to or farther from the content.
The content is the actual "stuff" in the box. If we're talking about a
element, the "stuff" is the text of the paragraph.
You'll see abbreviations like TM, TB, and TP in the diagram. These stand for "top margin," "top border," and "top padding." As we'll see, we can adjust the top, right, left, and bottom padding, border, and margin individually.
Instructions
Study the diagram in the Result tab until you're comfortable with where the padding, border, and margin go. Then hit Save & Submit Code to start adjusting these properties!
Let's start with our margins. Adjusting our margins not only moves our element relative to other elements on the page, but also relative to the "walls" of the HTML document.
For instance, if we take an HTML element with a specific width (such as our
Instructions
Try it out! Set our div's margin property to auto to center our div on the page.
* {
border: 1px dashed black;
}
div {
height: 50px;
width: 100px;
border: 2px solid black;
border-radius: 5px;
background-color: #308014;
margin:auto;
}
If you want to specify a particular margin, you can do it like this:
margin-top: /some value/
margin-right: /some value/
margin-bottom: /some value/
margin-left: /some-value/
You can also set an element's margins all at once: you just start from the top margin and go around clockwise (going from top to right to bottom to left). For instance,
margin: 1px 2px 3px 4px;
will set a top margin of 1 pixel, a right margin of 2, a bottom of 3, and a left of 4.
Instructions
Remove our div's margin: auto; on the CSS tab. Using whichever method you like best, give it a top margin of 20px, a right margin of 50px, a bottom margin of 10px, and a left margin of 5px.
Well done! You can see how fine-tuning your margins will help you place elements where you'd like them to be on the page.
We've worked with borders before, but it never hurts to have extra practice.
Instructions
Change your div's border to 4 pixels wide and solid, with the hex color #FF0000.
Good! Let's adjust the padding. Remember, the padding is the space between your border and your innermost layer: the actual content.
Padding can be set in two ways, just like your margins. You can either select them individually, like this:
padding-top: /some value/
padding-right: /some value/
padding-bottom: /some value/
padding-left: /some-value/
Or select them all in one declaration, like this:
padding: value value value value;
You should also know that if you want your padding to be the same for all four sides, you can declare that value only once. padding: 10px will give your HTML element 10 pixels of padding on all sides.
Instructions
Go ahead and use your preferred method to give your div padding of 40px on all sides.
Did you see that? Your
When you give CSS a positive padding or margin value, it puts that space between the element and its reference: for instance, if you have a
If you want to move an element in the other direction, you can give CSS a negative value: margin-left: -20px will move the element twenty pixels to the left.
Instructions
Give your
Cool, right? You can move HTML elements clear off the page with negative margins values.
Time for a quick review to make sure you've got a handle on all this margin and padding stuff!
Instructions
We've put a
Give that div a border of 1px solid black.
Give it a background color of #CC0000.
Set its top margin to 10px, its right margin to 5px, its bottom margin to 5px, and its left margin to 50px.
Set its top padding to 0px, its right padding to 30px, its bottom padding to 0px, and its left padding to 10px.
/*Add your CSS below!*/
div{
border: 1px solid black;
background-color:#CC0000;
margin: 10px 5px 5px 50px;
padding:0px 30px 0px 10px;
}
Okay! So we know how our individual elements are constructed. But how do we determine where they go on the page?
One way is to use floats. When you float an element on the page, you're telling the webpage: "I'm about to tell you where to put this element, but you have to put it into the flow of other elements." This means that if you have several elements all floating, they all know the others are there and don't land on top of each other.
You can think of the HTML page as sort of like a sea, and floating elements as boats on it: all the boats have positions on the sea, and they all see and steer clear of each other.
(Some of the positioning methods we'll learn in upcoming sections can accidentally drop elements on top of each other.)
Instructions
Let's get started. Set your div's float property to right!
Q&
div {
height: 300px;
width: 100px;
border: 2px solid black;
border-radius: 5px;
background-color: #308014;
/*Add your CSS here!*/
float:right;
}
Good! As you saw, your div moved over to the right side of the page.
Instructions
Move it back by changing your div's float from right to left!
As you may have already guessed, we can use floated elements to naturally divide our pages into different sections. Try it!
Instructions
Set your
to float to the left.
div {
height: 300px;
width: 300px;
border: 2px solid black;
border-radius: 5px;
background-color: #308014;
/*Add your CSS here!*/
float:right
}
p {
font-family: Verdana, sans-serif;
font-size: 20px;
width: 280px;
/*Add your CSS here!*/
float:left;
}
Unfortunately, we sometimes mix large floating elements with non-floating ones, and elements do end up on top of each other.
See your footer (the blue bit between the two columns)? It's stuck back there because we haven't told it something very important: to clear the other elements on the page!
If you tell an element to clear: left, it will immediately move below any floating elements on the left side of the page; it can also clear elements on the right. If you tell it to clear: both, it will get out of the way of elements floating on the left and right!
The syntax is what you're used to:
element {
clear: /right, left, or both/
}
Instructions
Tell the div with the ID #footer to clear both.
#footer {
height: 50px;
background-color: #69D2E7;
/*Add your CSS here!*/
clear:both;
}
Great work so far! Now that you understand positioning elements with float, let's move on to slightly more complex positioning methods.
If you don't specify an element's positioning type, it defaults to static. This just means "where the element would normally go." If you don't tell an element how to position itself, it just plunks itself down in the document.
Instructions
Check out the
The first type of positioning is absolute positioning. When an element is set to position: absolute, it's then positioned in relation to the first parent element it has that doesn't have position: static. If there's no such element, the element with position: absolute gets positioned relative to .
To show you how this works, we've set the #outer div to have absolute positioning. This means that when you position the #inner div, it will be relative to #outer. (If #outer had the default positioning of static, then #inner would get positioned relative to the entire HTML document.)
Instructions
Try it out: set #inner's position to absolute and give it a margin-left of 20px.
div {
height: 100px;
width: 100px;
border-radius: 5px;
border: 2px solid black;
}
#inner {
height: 75px;
width: 75px;
background-color: #547980;
/*Add your CSS here!*/
position:absolute;
margin-left:20px;
}
#outer {
height: 1500px;
width: 150px;
background-color: #45ADA8;
position: absolute;
margin-left: 100px;
}
Good! Did you notice how the #inner div moved 20 pixels in from the edge of the #outer div? That's absolute positioning at work.
Relative positioning is more straightforward: it tells the element to move relative to where it would have landed if it just had the default static positioning.
If you give an element relative positioning and tell it to have a margin-top of 10px, it doesn't move down ten pixels from any particular thing—it moves down ten pixels from where it otherwise would have been.
Instructions
Give it a try: change #inner's position to relative and give it a margin-left of 200px.
#inner {
height: 75px;
width: 75px;
background-color: #547980;
/*Add your CSS here!*/
position:relative;
margin-left:200px;
}
Perfect! See? This positioning stuff's not so hard.
Finally, fixed positioning anchors an element to the browser window—you can think of it as gluing the element to the screen. If you scroll up and down, the fixed element stays put even as other elements scroll past.
Instructions
Set #inner's position to fixed, then scroll up and down a bit. It stays put even as #outer moves out of the frame!
Great work—you've learned a lot about CSS positioning! We've covered:
The CSS box model
Display values, including block, inline-block, inline, and none
Margins, borders, and padding
Positioning elements with float
Giving elements absolute, relative, and fixed positioning
Check out the website we've started in the Result tab. Do you recognize it? It's the demo we showed you in the first CSS lesson!
It doesn't look quite the same, though. This is because much of the crucial display and positioning CSS we used has been removed. Your job? Add it back in!
Instructions
There's a navigation bar here somewhere, but it's lost due to display problems! On the CSS tab, give #navbar a position of fixed and a top margin of -10px.
Good work! The navigation bar is all stacked up, however, instead of being laid out horizontally.
Instructions
Fix this by:
1. Setting li's display value to inline;
2. Giving it 5 pixels of padding all around.
li {
/*Add your CSS here!*/
border: 2px solid #000000;
font-family: Futura, Tahoma, sans-serif;
color: #ffffff;
border-radius: 5px 5px;
background-color: #cc0323;
display:inline;
padding:5px;
}
Good work! You want to make sure everything floats nicely, however. Your footer is currently stuck behind your other elements!
Instructions
Inside the #left selector, set float: left and width: 45%
Inside the #right selector, set float: right and width: 45%
Inside the #footer selector, set clear: both
body {
background-color: #b7d1c4
}
h2 {
font-family: Verdana;
font-weight: bold;
text-align: center;
padding-top: 25px;
padding-bottom: 25px;
color: #acd1b2;
}
img {
height: 170px;
width: 170px;
box-shadow: rgba(0,0,0,0.2) 10px 10px;
}
#navbar {
/*Add your CSS here!*/
left:50%;
margin-left:-254px;
position:fixed;
margin:-10px;
}
#header {
position: relative;
top: -10px;
background-color: #3c4543;
border-top-left-radius: 15px;
border-top-right-radius: 15px;
}
ul{
list-style-type: none;
position: fixed;
margin: -10px;
}
li {
/*Add your CSS here!*/
border: 2px solid #000000;
font-family: Futura, Tahoma, sans-serif;
color: #ffffff;
border-radius: 5px 5px;
background-color: #cc0323;
display:inline;
padding:5px;
}
#left{
/*Add your CSS here!*/
float:left;
width:45%;
}
p {
font-family: Tahoma;
font-size: 1em;
}
#right{
/*Add your CSS here!*/
float:right;
width:45%;
}
table {
border: #000000 1px solid;
background-color: #acd1b2;
float: right;
margin-right: 10px;
border-bottom-right-radius: 15px;
border-bottom-left-radius: 15px;
}
td {
height: 75px;
width: 75px;
}
td img {
height: 75px;
width: 75px;
box-shadow: none;
}
th {
font-family: Verdana;
font-size: 1em;
font-weight: normal;
color: #3c4543
}
#bottom_left{
border-bottom-left-radius: 15px;
}
#bottom_right{
border-bottom-right-radius: 15px;
}
#footer{
/*Add your CSS here!*/
position: relative;
bottom: -20px;
border-bottom-left-radius: 15px;
border-bottom-right-radius: 15px;
height: 75px;
background-color: #3c4543;
clear:both;
}
#button{
border: 2px solid #000000;
float:left;
position: relative;
left: 229px;
bottom: -20px;
border-radius: 5px;
background-color: #cc0323;
height: 30px;
width: 150px;
}
#button p{
position: relative;
bottom: 10px;
font-size: 0.8em;
color: #acd1b2;
text-align: center;
}
.bold{
font-family: tahoma;
font-weight: bold;
font-size: 1.2em;
font-variant: small-caps;
color: #ffffff;
}
You'll be using your CSS skills to style an eye-catching resume. You'll need to call on your knowledge of CSS selectors, classes/IDs, margins, padding, borders, and positioning to make everything look exactly the way you want it to.
That said, this project will be relatively hands-off. We've put a demo resume in the editor (check the Result tab) for you to use as a template, but you should feel free to style your resume the way you'd like.