The Importance of the CSS Box Model

Similar documents
Styles, Style Sheets, the Box Model and Liquid Layout

Assignments (4) Assessment as per Schedule (2)

What is the Box Model?

Using CSS for page layout

Fundamentals of Web Technologies. Agenda: CSS Layout (Box Model) CSS Layout: Box Model. All HTML elements can be considered as a box or a container

Web Design and Implementation

Make a Website. A complex guide to building a website through continuing the fundamentals of HTML & CSS. Created by Michael Parekh 1

Page Layout. 4.1 Styling Page Sections 4.2 Introduction to Layout 4.3 Floating Elements 4.4 Sizing and Positioning

FLOATING AND POSITIONING

Welcome Please sit on alternating rows. powered by lucid & no.dots.nl/student

CSS Selectors. element selectors. .class selectors. #id selectors

Lab Introduction to Cascading Style Sheets

ITNP43: HTML Lecture 5

CS193X: Web Programming Fundamentals

Block & Inline Elements

<style type="text/css"> <!-- body {font-family: Verdana, Arial, sans-serif} ***set font family for entire Web page***

INFS 2150 Introduction to Web Development

INFS 2150 Introduction to Web Development

TAG STYLE SELECTORS. div Think of this as a box that contains things, such as text or images. It can also just be a

Web Design and Development ACS Chapter 12. Using Tables 11/23/2017 1

Chapter 1 Getting Started with HTML 5 1. Chapter 2 Introduction to New Elements in HTML 5 21

Web Site Design and Development Lecture 9. CS 0134 Fall 2018 Tues and Thurs 1:00 2:15PM

COMS 359: Interactive Media

CSS Box Model. Cascading Style Sheets

Client-Side Web Technologies. CSS Part II

Parashar Technologies HTML Lecture Notes-4

Internet Programming 1 ITG 212 / A

CSS: Cascading Style Sheets

the missing manual0 O'REILLY Third Edition David Sawyer McFarland Beijing Cambridge The book that should have been in the box Farnham

COMM 2555: Interactive Digital Communication / Spring 2018 Lab 9: Basic layout techniques with CSS

Certified CSS Designer VS-1028

VISA/APCO/STAC 2P61 WEBSITE CREATION Fall Term 2012

Creating Layouts Using CSS. Lesson 9

CSS3 Basics. From & CSS Visual Dictionary Learning Curve Books, LLC

CSS THE M\SS1NG MANUAL. David Sawyer McFarland. POGUE PRESS" O'REILLr Beijing Cambridge Farnham Köln Paris Sebastopol Taipei Tokyo

Adding CSS to your HTML

When you complete this chapter, you will be able to:

HTML and CSS a further introduction

2005 WebGUI Users Conference

Create a three column layout using CSS, divs and floating

Appendix D CSS Properties and Values

IDM 221. Web Design I. IDM 221: Web Authoring I 1

What do we mean by layouts?

Using Dreamweaver CS6

CSCU9B2: Web Tech Lecture 3

Positioning in CSS: There are 5 different ways we can set our position:

CSS for Page Layout Robert K. Moniot 1

Page Layout Using Tables

COMS 359: Interactive Media

ADDING CSS TO YOUR HTML DOCUMENT. A FEW CSS VALUES (colour, size and the box model)

3. Each of these mark examples contains an error. a. <input name= country value= Your country here. /> b. <checkbox name= color value= teal />

Introduction to WEB PROGRAMMING

COMSC-031 Web Site Development- Part 2

CSS. Selectors & Measurments. Copyright DevelopIntelligence LLC

epromo Guidelines DUE DATES NOT ALLOWED PLAIN TEXT VERSION

CSS: The Basics CISC 282 September 20, 2014

Building Page Layouts

READSPEAKER ENTERPRISE HIGHLIGHTING 2.5

BIM222 Internet Programming

ADOBE 9A Adobe Dreamweaver CS4 ACE.

USER GUIDE. MADCAP FLARE 2017 r3. QR Codes

HTML + CSS. ScottyLabs WDW. Overview HTML Tags CSS Properties Resources

Introduction to Web Design CSS Reference

CSS.

Introduction to Web Design CSS Reference

EECS1012. Net-centric Introduction to Computing. Lecture 5: Yet more CSS (Float and Positioning)

Wanted! Introduction. Step 1: Styling your poster. Activity Checklist. In this project, you ll learn how to make your own poster.

Dreamweaver CS3 Lab 2

GIMP WEB 2.0 MENUS. Web 2.0 Menus: Horizontal Navigation Bar with Dynamic Background Image

INFS 2150 Introduction to Web Development

INFS 2150 Introduction to Web Development

CSS Cascading Style Sheets

Session 3.1 Objectives Review the history and concepts of CSS Explore inline styles, embedded styles, and external style sheets Understand style

IMY 110 Theme 7 HTML Tables

Reading 2.2 Cascading Style Sheets

Cascading Style Sheets (CSS)

Cascading Style Sheets Level 2

Cascading Style Sheets for layout II CS7026

Creating Web Pages with SeaMonkey Composer

CSS Design and Layout Basic Exercise instructions. Today's exercises. Part 1: Arrange Page into Sections. Part 1, details (screenshot below)

CS7026 CSS3. CSS3 Graphics Effects

HTML and CSS COURSE SYLLABUS

Getting Started with Eric Meyer's CSS Sculptor 1.0

2. Write style rules for how you d like certain elements to look.

Class 3 Page 1. Using DW tools to learn CSS. Intro to Web Design using Dreamweaver (VBUS 010) Instructor: Robert Lee

The Benefits of CSS. Less work: Change look of the whole site with one edit

Lab 1: Introducing HTML5 and CSS3

8/19/2018. Web Development & Design Foundations with HTML5. Learning Objectives (1 of 2) Learning Objectives (2 of 2)

ACSC 231 Internet Technologies

recall: a Web page is a text document that contains additional formatting information in the HyperText Markup Language (HTML)

Graduating to Grid. An Event Apart Orlando

Produced by. Web Development. Eamonn de Leastar Department of Computing, Maths & Physics Waterford Institute of Technology

CSS: Layout Part 2. clear. CSS for layout and formatting: clear

To link to an external stylesheet, the link element is placed within the head of the html page:

There are 3 places you can write CSS.

Microsoft Expression Web Quickstart Guide

Chapter 3 Style Sheets: CSS

IMY 110 Theme 11 HTML Frames

1 of 7 11/12/2009 9:29 AM

Lab 2: Movie Review. overview.png background.png rottenbig.png rbg.png fresh.gif rotten.gif critic.gif

Transcription:

The Importance of the CSS Box Model Block Element, Border, Padding and Margin Margin is on the outside of block elements and padding is on the inside. Use margin to separate the block from things outside it, padding moves the contents away from the edges of the block. The difference becomes clear once you apply backgrounds and borders to an element. Unlike padding, margins are not covered by either the background or border because they are the space outside of the actual element. Remember that when you use padding, padding adds to the containers width/height. See example below: Margin and padding values are set clockwise, starting from the top. Page 1

Practical example: Here is an <h2> heading between two paragraphs. As you can see, the margin creates white space between the paragraphs, and the padding (where you see the background gray color) gives it some breathing room. Margin and Padding Values In the above example of the heading, the values for the margin and padding would be: margin: 15px 0 15px 0; padding: 15px 15px 15px 15px; To optimize this line of code further, using shorthand cuts down on repetitive code. Applying the shorthand technique would slim the code down to this: margin: 15px 0; /*--top and bottom = 15px right and left = 0 --*/ padding: 15px; /*--top, right, bottom and left = 15px --*/ Here is what the complete CSS would look like for this heading: h2 { background: #f0f0f0; border: 1px solid #ddd; margin: 15px 0; padding: 15px; Padding makes things grow Keep in mind that padding adds to the total width of your element. For example, if you had specified that the element should be 100 pixels wide, and you had a left and right padding of 10 pixels, then you would end up with 120 pixels in total. 100px (content) + 10px (left padding) + 10px (right padding) = 120px (total width of element) Margin, however, expands the box model but does not directly affect the element itself. This tip is especially handy when lining up columns in a layout! Page 2

2. Refresher About Floats Floats are a fundamental element in building CSS-based websites and can be used to align images and build layouts and columns. If you recall how to align elements left and right in HTML, floating works in a similar way. The float property specifies whether a fixed-width box should float, shifting it to the right or left, with surrounding content flowing around it. The float: left value aligns elements to the left and can also be used as a solid container to create layouts and columns. Let s look at a practical situation in which you can use float: left. The float: right value would align elements right, with surrounding elements flowing to the left. Page 3

Quick tip: Because block elements typically span 100% of their parent container s width, floating an element to the right knocks it down to the next line. This also applies to plain text that runs next to it because the floated element cannot squeeze in the same line. You can correct this issue in one of two ways: 1. Reverse the order of the HTML markup so that you call the floated element first, and the neighboring element second. 2. Specify an exact width for the neighboring element so that when the two elements sit side by side, their combined width is less than or equal to the width of their parent container. Page 4

Internet Explorer 6 (IE6) has been known to double a floated element s margin. So, what you originally specified as a 5-pixel margin becomes 10 pixels in IE6. A simple trick to get around this bug is to add display: inline to your floated element, like so:.floated_element { float: left; width: 200px; margin: 5px; display: inline; /*--IE6 workaround--*/ 3. Refresher on Alignment The days of using the <center> HTML tag are long gone. Let s look at the various ways of centeraligning an element. Horizontal Alignment You can horizontally align text elements using the text-align property. This is quite simple to do, but keep in mind when center-aligning inline elements that you must add display: block. This allows the browser to determine the boundaries on which to base its alignment of your element..center { text-align: center; display: block; /*--For inline elements only--*/ To horizontally align non-textual elements, use the margin property. If both margin-left and margin-right are auto, their used values are equal. This horizontally centers the element with respect to the edges of the containing block. Horizontal alignment can be achieved, then, by setting the left and right margins to auto. This is an ideal method of horizontally aligning non-text-based elements; for example, layouts and images. But when center-aligning a layout or element without a specified width, you must specify a width in order for this to work. To center-align a layout: Page 5

.layout_container { margin: 0 auto; width: 960px; To center-align an image (image with a class of center): img.center { margin: 0 auto; display: block; /*--Since IMG is an inline element--*/ Vertical Alignment You can vertically align text-based elements using the line-height property, which specifies the amount of vertical space between lines of text. This is ideal for vertically aligning headings and other text-based elements. Simply match the line-height with the height of the element. h1 { font-size: 3em; height: 100px; line-height: 100px; To vertically align non-textual elements, use absolute positioning. The trick with this technique is that you must specify the exact height and width of the centered element. With the position: absolute property, an element is positioned according to its base position (0,0: the top-left corner). In the image below, the red point indicates the 0,0 base of the element, before a negative margin is applied. By applying negative top and left margins, we can now perfectly align this element both vertically and horizontally. Page 6

Here is the complete CSS for horizontal and vertical alignment:.vertical { width: 600px; /*--Specify Width--*/ height: 300px; /*--Specify Height--*/ position: absolute; /*--Set positioning to absolute--*/ top: 50%; /*--Set top coordinate to 50%--*/ left: 50%; /*--Set left coordinate to 50%--*/ margin: -150px 0 0-300px; /*--Set negative top/left margin--*/ Page 7