SlideShare a Scribd company logo
CSS Layout Basics
Starting with a new HTML document, let’s just start with our divs.

       <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
           "http://www.w3.org/TR/html4/strict.dtd"
           >
       <html lang="en">
       <head>
           <title>Let's Learn Layouts!</title>
       </head>
       <body>
          <div id="container">

           <div id="header”><div id=“logo”></div></div>

           <div id=”content”></div>

           <div id=”right_column”></div>

           <div class="feature”></div>
           <div class="feature”></div>
           <div class="feature”></div>

           <div id="footer">THIS IS THE FOOTER</div>

          </div> <!--end of container div-->
       </body>
       </html>
Add content (text, images) to the divs.



  <body>
     <div id="container">
                                                                                                       HTML
      <div id="header"><div id="logo"><img
  src="http://openclipart.org/image/90px/svg_to_png/170921/tikigiki_abstract-element-029.png"></div>THIS IS THE
  HEADER</div>

      <div id="content"><h1>Headline</h1>
        <p>Halvah powder oat cake. <a href="#">Pie oat cake</a> candy halvah cookie topping. Tootsie roll
  toffee faworki cookie ice cream. Jujubes jelly-o caramels. Dragée bear claw biscuit bear claw. Bear claw
  brownie chupa chups ice cream gummi bears sweet. Toffee jujubes topping. Oat cake cake lemon drops jelly
  beans marshmallow marzipan cheesecake lollipop tart.</p>
     <p>Halvah powder oat cake. Pie oat cake candy halvah cookie topping. <a href="#">Tootsie roll toffee</a>
  faworki cookie ice cream. Jujubes jelly-o caramels. Dragée bear claw biscuit bear claw. Bear claw brownie
  chupa chups ice cream gummi bears sweet. Toffee jujubes topping. Oat cake cake lemon drops jelly beans
  marshmallow marzipan cheesecake lollipop tart.</p></div>

  <div id=”right_column”></div>

     <div class="feature”></div>
     <div class="feature”></div>
     <div class="feature”></div>

  <div id="footer">THIS IS THE FOOTER</div>

  </div> <!--end of container div-->
  </body>
  </html>
All the HTML with no styling
This is what it looks like if we preview the HTML document. The borders shows how the divs react
without any styling. Notice that by default they are displaying 100% the width of the page. That is
because by default, divs are block-level elements.
Create a stylesheet (CSS) file and link the file to your HTML page. Let’s begin by adding style to the
body and to the #container div.


  body{
      font-family: Futura;
  }

  #container{
      width:800px;
      margin: 0 auto;
      background-color: #FAFCE8;
  }




 Notice that you do NOT need a # on
 the body tag. The body is not a div, it
 is a root element.
Also notice that margin: 0 auto; centers the #container div to the viewport & removes any space
between the very top of the page and the container.
Next, let’s style the #header div.

  #header{                           Because the image is in a div, it is taking up the entire
      height: 80px;
      border:1px solid blue;
                                     width of the parent div, #container and pushing the
      text-align: center;            “THIS IS THE HEADER” text down.
      padding: 10px;
      margin-bottom: 5px;
      }
This can easily be fixed by adding “float:left;” to the #logo div.

   #logo{
       float:left;
       }




   In the normal flow, each block element (div, p, h1, etc.) stacks on top of each other vertically, from the top of the
   viewport down. Floated elements are first laid out according to the normal flow, then taken out of the normal
   flow and sent as far to the right or left (depending on which value is applied) of the parent element. In other
   words, they go from stacking on top of each other to sitting next to each other, given that there is enough room
   in the parent element for each floated element to sit. This behavior is crucial to remember as you build your
   websites.

   http://www.alistapart.com/articles/css-floats-101/
Now let’s style the #content div.

  #content{
      float: left;
      width: 595px;
      border:1px solid red;
  }
When we highlight the #right_column div we see that because it is not styled, the div still has its 100% width. The
content within the div is being pushed by the content in the #content div.
Add styling to the #right_column div.

  #right_column{
      width: 190px;
      border: 1px solid green;
      float: right;
  }




Notice the width of #right_column is
small enough to fit in the 800px width
we created for the #container div.
This is what happens if the width of
#right_column is too big to fit both the
#content div and #right_column. It get’s
pushed out and by default, the other
divs below #right_column move up.

This is why it is important to give your
divs widths.

Heights are not always necessary – it
depends on whether you want a
specific height to the div OR if you want
the content within your div to
determine the size of the div.
Now we add our .feature styles.

   .feature{
       float: left;
       width: 150px;
       height: 100px;
       border: 1px solid grey;
       margin-right: 10px;
       margin-top: 10px;
       margin-bottom: 10px;
       padding: 5px;
   }




Because the .feature has a
float, the #footer div has moved
up. It helps to think that by
default, HTML elements are like
magnets that will stick to the left of
the page.
We can solve this problem once we style the #footer div.


       #footer{
           clear: both;
           height: 50px;
           border: 1px solid purple;
           text-align: center;
       }




 The clear property
 specifies which sides of
 an element where
 other floating elements
 are not allowed.
Let’s add a little love to the text in the .feature divs so that it doesn’t look broken.

  .feature h3 {
      margin-bottom: 2px;
      margin-top: 0;
  }

  .feature span{
      font-size: 11px;
  }




By default heading tags have a
margin top and a margin
bottom. By specifying the
margin in the h3 tag we
overrode the default.
Now we are left with styling the little details. I want that image to be in the center of the
#right_column div. So let’s add a div around the image tag in our HTML page, with a little inline
styling.


  <div style=”display:block; margin-left: auto; margin-right: auto; width:
  175px;"><img src=“images/johnny_automatic_open_book.svg" height="90"></div>


                                                                 Sometimes it is not the text that needs to
                                                                 be centered, but the block as a whole.
                                                                 Or, phrased differently: we want the left
                                                                 and right margin to be equal. The way to
                                                                 do that is to set the margins to 'auto'. This
                                                                 is normally used with a block of fixed
                                                                 width, because if the block itself is
                                                                 flexible, it will simply take up all the
                                                                 available width.

                                                                 http://www.w3.org/Style/Examples/007/c
                                                                 enter.en.html
Now let’s style the li’s within the #right_column div so they aren’t so big…

  #right_column li{
      font-size: 12px;
  }




 “GREAT!” The smaller font
 allows for the first feature
 enough room to move up! We
 can solve this one of two ways.

 1. We can give the
 #right_column a specific height
 so that it pushes the .feature div
 down. OR…
2. We can nest our .feature divs in a div with an inline style of “clear:both;”

    <div style="clear: both;">
  <div class="feature"><h3>feature1</h3><span>Tootsie roll toffee faworki cookie ice cream.</span></div>
  <div class="feature"><h3>feature2</h3><span>Tootsie roll toffee faworki cookie ice cream.</span></div>
  <div class="feature"><h3>feature3</h3><span>Tootsie roll toffee faworki cookie ice cream.</span></div>
    </div>



                                                                   Here we have all the divs highlighted. You
                                                                   can see the features “living” in their new
                                                                   div – on separate row.
One last detail left! Adding colors to our links in the #content div.

  #content a:link, #content a:visited{
      color: #333;
  }

  #content a:focus, #content a:hover, #content a:active{
      color: green;
  }




                                                                        The first link is highlighted to show the
                                                                        “hover” state.
Finished HTML
Finished CSS

More Related Content

PDF
Layout with CSS
PDF
Creating a Webpage from a Template
PDF
Three quick accessibility tips for HTML5
PPT
Chapter4
PDF
CSS pattern libraries
PDF
CSS - OOCSS, SMACSS and more
PPTX
10 WordPress Theme Hacks to Improve Your Site
KEY
HTML CSS & Javascript
Layout with CSS
Creating a Webpage from a Template
Three quick accessibility tips for HTML5
Chapter4
CSS pattern libraries
CSS - OOCSS, SMACSS and more
10 WordPress Theme Hacks to Improve Your Site
HTML CSS & Javascript

What's hot (20)

PDF
BEM Methodology — @Frontenders Ticino —17/09/2014
DOCX
Html n css tutorial
PDF
Modular HTML, CSS, & JS Workshop
PPT
HTML & CSS Workshop Notes
KEY
The Cascade, Grids, Headings, and Selectors from an OOCSS Perspective, Ajax ...
PDF
Progressive Prototyping w/HTML5, CSS3 and jQuery
PDF
Using Core Themes in Drupal 8
PDF
Html5 ux london
PDF
Learn SUIT: CSS Naming Convention
PPTX
About Best friends - HTML, CSS and JS
PPT
Web Design 101
PPTX
Supercharged HTML & CSS
PDF
Pamela - Brining back the pleasure of hand-written HTML - Montréal Python 8
PDF
Prototyping w/HTML5 and CSS3
PDF
Shaping Up With CSS
PPTX
Introduction to HTML5
PPT
Haml, Sass and Compass for Sane Web Development
PPT
Web 2.0 - what is it?
PPTX
Page layout with css
DOCX
BEM Methodology — @Frontenders Ticino —17/09/2014
Html n css tutorial
Modular HTML, CSS, & JS Workshop
HTML & CSS Workshop Notes
The Cascade, Grids, Headings, and Selectors from an OOCSS Perspective, Ajax ...
Progressive Prototyping w/HTML5, CSS3 and jQuery
Using Core Themes in Drupal 8
Html5 ux london
Learn SUIT: CSS Naming Convention
About Best friends - HTML, CSS and JS
Web Design 101
Supercharged HTML & CSS
Pamela - Brining back the pleasure of hand-written HTML - Montréal Python 8
Prototyping w/HTML5 and CSS3
Shaping Up With CSS
Introduction to HTML5
Haml, Sass and Compass for Sane Web Development
Web 2.0 - what is it?
Page layout with css
Ad

Viewers also liked (20)

PPTX
Acc 626 slidecast - Forensics for IT
PPTX
Basketball game
PPT
270611%20bezoek%20roosendaal[1]
PPTX
Cooll usersguide 5
PPS
Silver
PDF
Bab i pendahuluan
PPT
Expo ingles
DOCX
Los videojuegos
PDF
SAP FICO COMPLETE PACKAGE
PDF
CONCESSÕES E PPPs NO GOVERNO TEMER:ARTIGO MAURICIO PORTUGAL RIBEIRO
PDF
"Fashions fade, style is eternal"
PPTX
Cooll usersguide 1
PPT
Corespring
PPTX
6 Development Tools we Love for Mac
PDF
PDF
Startgids Accounthouder
PPT
Overview of CTG3 and our tools
PPT
Shifts in the music industry
PPTX
Ukita june 2011pptx
Acc 626 slidecast - Forensics for IT
Basketball game
270611%20bezoek%20roosendaal[1]
Cooll usersguide 5
Silver
Bab i pendahuluan
Expo ingles
Los videojuegos
SAP FICO COMPLETE PACKAGE
CONCESSÕES E PPPs NO GOVERNO TEMER:ARTIGO MAURICIO PORTUGAL RIBEIRO
"Fashions fade, style is eternal"
Cooll usersguide 1
Corespring
6 Development Tools we Love for Mac
Startgids Accounthouder
Overview of CTG3 and our tools
Shifts in the music industry
Ukita june 2011pptx
Ad

Similar to CSS Layout Tutorial (20)

PPTX
Castro Chapter 11
PPTX
Layouts
PDF
Class 3 create an absolute layout with css abs position (aptana)
PDF
Web Layout
PPT
Introduction to HTML
PDF
The Future of CSS Layout
PPTX
How to create a Liquid three column CSS layout
PDF
BasicCSSFlowTutorial
PDF
BasicCSSFlowTutorial
PDF
Class 4 handout two column layout w mobile web design
PDF
Style Your Site Part 1
PPT
Chapter6
PDF
Introduction to Frontend Development - Session 2 - CSS Fundamentals
PPTX
PPTX
Liquid column layout final
PPTX
Cascading Style Sheets CSS
PDF
GDI Seattle Intro to HTML and CSS - Class 3
PPT
Chapter 6 - Web Design
PPTX
Designing for the web - 101
PPT
Web Designing Bugs - Fixes By Nyros Developer
Castro Chapter 11
Layouts
Class 3 create an absolute layout with css abs position (aptana)
Web Layout
Introduction to HTML
The Future of CSS Layout
How to create a Liquid three column CSS layout
BasicCSSFlowTutorial
BasicCSSFlowTutorial
Class 4 handout two column layout w mobile web design
Style Your Site Part 1
Chapter6
Introduction to Frontend Development - Session 2 - CSS Fundamentals
Liquid column layout final
Cascading Style Sheets CSS
GDI Seattle Intro to HTML and CSS - Class 3
Chapter 6 - Web Design
Designing for the web - 101
Web Designing Bugs - Fixes By Nyros Developer

More from hstryk (16)

PPTX
CSS - Text Properties
PPT
Lesson 3 - HTML & CSS Part 2
PPTX
Lesson1 - Fall 2013
PPTX
Lesson2
PPTX
CSS3 Transitions
PPTX
Introduction to CSS3
PPTX
Sprites rollovers
PDF
Building a Website from Planning to Photoshop Mockup to HTML/CSS
PPTX
Lecture4
PPTX
Tutorial1 - Part 2
PDF
Tutorial1
PDF
Project1
PPTX
Lesson 3
PPTX
Lesson2
PPTX
Lesson1
PDF
Heather Strycharz - Resume
CSS - Text Properties
Lesson 3 - HTML & CSS Part 2
Lesson1 - Fall 2013
Lesson2
CSS3 Transitions
Introduction to CSS3
Sprites rollovers
Building a Website from Planning to Photoshop Mockup to HTML/CSS
Lecture4
Tutorial1 - Part 2
Tutorial1
Project1
Lesson 3
Lesson2
Lesson1
Heather Strycharz - Resume

Recently uploaded (20)

PPTX
12. Community Pharmacy and How to organize it
PDF
Urban Design Final Project-Context
PDF
Phone away, tabs closed: No multitasking
PPTX
Complete Guide to Microsoft PowerPoint 2019 – Features, Tools, and Tips"
PPTX
building Planning Overview for step wise design.pptx
PPTX
Special finishes, classification and types, explanation
PDF
Facade & Landscape Lighting Techniques and Trends.pptx.pdf
PDF
Integrated-2D-and-3D-Animation-Bridging-Dimensions-for-Impactful-Storytelling...
PPTX
An introduction to AI in research and reference management
PPTX
AD Bungalow Case studies Sem 2.pptxvwewev
PPTX
Causes of Flooding by Slidesgo sdnl;asnjdl;asj.pptx
DOCX
The story of the first moon landing.docx
PPTX
AC-Unit1.pptx CRYPTOGRAPHIC NNNNFOR ALL
PPTX
Wisp Textiles: Where Comfort Meets Everyday Style
PDF
GREEN BUILDING MATERIALS FOR SUISTAINABLE ARCHITECTURE AND BUILDING STUDY
PPT
Machine printing techniques and plangi dyeing
PPTX
YV PROFILE PROJECTS PROFILE PRES. DESIGN
PDF
Benefits_of_Cast_Aluminium_Doors_Presentation.pdf
PDF
Quality Control Management for RMG, Level- 4, Certificate
PPTX
ANATOMY OF ANTERIOR CHAMBER ANGLE AND GONIOSCOPY.pptx
12. Community Pharmacy and How to organize it
Urban Design Final Project-Context
Phone away, tabs closed: No multitasking
Complete Guide to Microsoft PowerPoint 2019 – Features, Tools, and Tips"
building Planning Overview for step wise design.pptx
Special finishes, classification and types, explanation
Facade & Landscape Lighting Techniques and Trends.pptx.pdf
Integrated-2D-and-3D-Animation-Bridging-Dimensions-for-Impactful-Storytelling...
An introduction to AI in research and reference management
AD Bungalow Case studies Sem 2.pptxvwewev
Causes of Flooding by Slidesgo sdnl;asnjdl;asj.pptx
The story of the first moon landing.docx
AC-Unit1.pptx CRYPTOGRAPHIC NNNNFOR ALL
Wisp Textiles: Where Comfort Meets Everyday Style
GREEN BUILDING MATERIALS FOR SUISTAINABLE ARCHITECTURE AND BUILDING STUDY
Machine printing techniques and plangi dyeing
YV PROFILE PROJECTS PROFILE PRES. DESIGN
Benefits_of_Cast_Aluminium_Doors_Presentation.pdf
Quality Control Management for RMG, Level- 4, Certificate
ANATOMY OF ANTERIOR CHAMBER ANGLE AND GONIOSCOPY.pptx

CSS Layout Tutorial

  • 2. Starting with a new HTML document, let’s just start with our divs. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd" > <html lang="en"> <head> <title>Let's Learn Layouts!</title> </head> <body> <div id="container"> <div id="header”><div id=“logo”></div></div> <div id=”content”></div> <div id=”right_column”></div> <div class="feature”></div> <div class="feature”></div> <div class="feature”></div> <div id="footer">THIS IS THE FOOTER</div> </div> <!--end of container div--> </body> </html>
  • 3. Add content (text, images) to the divs. <body> <div id="container"> HTML <div id="header"><div id="logo"><img src="http://openclipart.org/image/90px/svg_to_png/170921/tikigiki_abstract-element-029.png"></div>THIS IS THE HEADER</div> <div id="content"><h1>Headline</h1> <p>Halvah powder oat cake. <a href="#">Pie oat cake</a> candy halvah cookie topping. Tootsie roll toffee faworki cookie ice cream. Jujubes jelly-o caramels. Dragée bear claw biscuit bear claw. Bear claw brownie chupa chups ice cream gummi bears sweet. Toffee jujubes topping. Oat cake cake lemon drops jelly beans marshmallow marzipan cheesecake lollipop tart.</p> <p>Halvah powder oat cake. Pie oat cake candy halvah cookie topping. <a href="#">Tootsie roll toffee</a> faworki cookie ice cream. Jujubes jelly-o caramels. Dragée bear claw biscuit bear claw. Bear claw brownie chupa chups ice cream gummi bears sweet. Toffee jujubes topping. Oat cake cake lemon drops jelly beans marshmallow marzipan cheesecake lollipop tart.</p></div> <div id=”right_column”></div> <div class="feature”></div> <div class="feature”></div> <div class="feature”></div> <div id="footer">THIS IS THE FOOTER</div> </div> <!--end of container div--> </body> </html>
  • 4. All the HTML with no styling
  • 5. This is what it looks like if we preview the HTML document. The borders shows how the divs react without any styling. Notice that by default they are displaying 100% the width of the page. That is because by default, divs are block-level elements.
  • 6. Create a stylesheet (CSS) file and link the file to your HTML page. Let’s begin by adding style to the body and to the #container div. body{ font-family: Futura; } #container{ width:800px; margin: 0 auto; background-color: #FAFCE8; } Notice that you do NOT need a # on the body tag. The body is not a div, it is a root element.
  • 7. Also notice that margin: 0 auto; centers the #container div to the viewport & removes any space between the very top of the page and the container.
  • 8. Next, let’s style the #header div. #header{ Because the image is in a div, it is taking up the entire height: 80px; border:1px solid blue; width of the parent div, #container and pushing the text-align: center; “THIS IS THE HEADER” text down. padding: 10px; margin-bottom: 5px; }
  • 9. This can easily be fixed by adding “float:left;” to the #logo div. #logo{ float:left; } In the normal flow, each block element (div, p, h1, etc.) stacks on top of each other vertically, from the top of the viewport down. Floated elements are first laid out according to the normal flow, then taken out of the normal flow and sent as far to the right or left (depending on which value is applied) of the parent element. In other words, they go from stacking on top of each other to sitting next to each other, given that there is enough room in the parent element for each floated element to sit. This behavior is crucial to remember as you build your websites. http://www.alistapart.com/articles/css-floats-101/
  • 10. Now let’s style the #content div. #content{ float: left; width: 595px; border:1px solid red; }
  • 11. When we highlight the #right_column div we see that because it is not styled, the div still has its 100% width. The content within the div is being pushed by the content in the #content div.
  • 12. Add styling to the #right_column div. #right_column{ width: 190px; border: 1px solid green; float: right; } Notice the width of #right_column is small enough to fit in the 800px width we created for the #container div.
  • 13. This is what happens if the width of #right_column is too big to fit both the #content div and #right_column. It get’s pushed out and by default, the other divs below #right_column move up. This is why it is important to give your divs widths. Heights are not always necessary – it depends on whether you want a specific height to the div OR if you want the content within your div to determine the size of the div.
  • 14. Now we add our .feature styles. .feature{ float: left; width: 150px; height: 100px; border: 1px solid grey; margin-right: 10px; margin-top: 10px; margin-bottom: 10px; padding: 5px; } Because the .feature has a float, the #footer div has moved up. It helps to think that by default, HTML elements are like magnets that will stick to the left of the page.
  • 15. We can solve this problem once we style the #footer div. #footer{ clear: both; height: 50px; border: 1px solid purple; text-align: center; } The clear property specifies which sides of an element where other floating elements are not allowed.
  • 16. Let’s add a little love to the text in the .feature divs so that it doesn’t look broken. .feature h3 { margin-bottom: 2px; margin-top: 0; } .feature span{ font-size: 11px; } By default heading tags have a margin top and a margin bottom. By specifying the margin in the h3 tag we overrode the default.
  • 17. Now we are left with styling the little details. I want that image to be in the center of the #right_column div. So let’s add a div around the image tag in our HTML page, with a little inline styling. <div style=”display:block; margin-left: auto; margin-right: auto; width: 175px;"><img src=“images/johnny_automatic_open_book.svg" height="90"></div> Sometimes it is not the text that needs to be centered, but the block as a whole. Or, phrased differently: we want the left and right margin to be equal. The way to do that is to set the margins to 'auto'. This is normally used with a block of fixed width, because if the block itself is flexible, it will simply take up all the available width. http://www.w3.org/Style/Examples/007/c enter.en.html
  • 18. Now let’s style the li’s within the #right_column div so they aren’t so big… #right_column li{ font-size: 12px; } “GREAT!” The smaller font allows for the first feature enough room to move up! We can solve this one of two ways. 1. We can give the #right_column a specific height so that it pushes the .feature div down. OR…
  • 19. 2. We can nest our .feature divs in a div with an inline style of “clear:both;” <div style="clear: both;"> <div class="feature"><h3>feature1</h3><span>Tootsie roll toffee faworki cookie ice cream.</span></div> <div class="feature"><h3>feature2</h3><span>Tootsie roll toffee faworki cookie ice cream.</span></div> <div class="feature"><h3>feature3</h3><span>Tootsie roll toffee faworki cookie ice cream.</span></div> </div> Here we have all the divs highlighted. You can see the features “living” in their new div – on separate row.
  • 20. One last detail left! Adding colors to our links in the #content div. #content a:link, #content a:visited{ color: #333; } #content a:focus, #content a:hover, #content a:active{ color: green; } The first link is highlighted to show the “hover” state.