ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Internet & the Web

A Tutorial Series CSS for Beginners: Border, Outline, Margins, Padding & More!

Updated on March 21, 2014

Recap of Last Tutorial

In my last tutorial and the first in this series entitled, "A Tutorial Series CSS for Beginners: Formatting Text, Links & Lists'" we looked extensively at color, text alignment of paragraphs, text decoration: underline, overline, and "strike through"; text transformation (e.g. all characters to uppercase, the formatting of links (e.g.) when hovering, and list formatting. We did not get to fonts which we will cover briefly next. Most of what was in the least tutorial was basic and straightforward. It more or less amounts to being familiar with what options are available and which work for you in your implementation.

Focus for this Tutorial: Outline, Margin, Border, and Padding

The properties of outline, margin, border, and padding along with the HTML element content are referred to as a box. In fact all HTML elements can be considered boxes. So what we are looking at with the various CSS properties in this tutorial is referred to as the CSS box model.Property values for outline, margin, border, and padding (along with content) determine the space requirement for the element, Placement of elements is important in making the page attractive and noteworthy. Thus, making corr

In fact, it is so fundamental that when display a page with the “inspect element” to step through the actual code of a page. an illustration of the box at that point in the page is presented, as the snapshot illustrates.

ect assesment as to whether the elements will "fit" is essential.

A HTML Element Box from an "Inspect Element"

Size determines everything. Incorrect calculations of element size causes "skewing" when placing elements. "Bad sized" provisions for padding, margin, and borders causes the page to look 'crowded" and "too busy"
Size determines everything. Incorrect calculations of element size causes "skewing" when placing elements. "Bad sized" provisions for padding, margin, and borders causes the page to look 'crowded" and "too busy"
The highlighted text is illustrated as the box in the preceding image. Knowing size and placement is so important that the "inspect element" invocation makes such illustrations available for debugging.
The highlighted text is illustrated as the box in the preceding image. Knowing size and placement is so important that the "inspect element" invocation makes such illustrations available for debugging.

The Five Components of Element Size


The five components which contribute to element size are:

  • Margin- An area around the outline which is devoid of a background color. The margin is transparent
  • Outline - An outline is a line that is drawn outside the border to enhance the border. The outline properties specify the style, color, and width of an outline.
  • Border - A border surrounds the padding and content. The border color is inherited from the color property of the box.

  • Padding -Clears an area around the content. The padding is affected by the background color of the box.

  • Content -The text and image area.


The snapshot illustrates an example where all five elements are specified.

An Example of Margin, Outline, Border, Padding, & Content

We set margin and padding which are not visible. The outline, border, and content are the visible items.
We set margin and padding which are not visible. The outline, border, and content are the visible items.

Calculating the Space Requirements for an Element

There is only so much space, so much “real estate” on a page. It becomes important to know how you want a page to look taking into account the total size of the elements. For example you have two elements A and B which you would like to have sit horizontally together side by side. If the width of the two combined exceeds the page width, the second element will be shifted down.

A Space Calculation Example

Suppose the element had the following properties:

width: 200px;

height 200px;

padding: 10 px;

margin: 10px;

border: 5px solid green;

The computation for the total area would be

width = height since our parameters for padding, margin, and border are equal on all sides. (This need not be the case, different values can be specified for each of the four sides (top, bottom, left, and right).

width = {width of element} + 2 X (width of padding, left and right) + 2 X (margin) + 2 X (border)

therefore, we arrive at a width (and height) of 250px. The element size is 250px by 250 px.

Our calculation is was a little bit less involved than the general calculation where borders, padding, and margins, and outline might not been identical. In general one would have:

Total element width = width + left padding + right padding + left border + right border + left margin + right margin + left outline + right outline

and

Total element height = height + top padding + bottom padding + top border + bottom border + top margin + bottom margin + left outline + right outline

Borders Come in Many "Flavors"

There are 8 types of borders currently defined. The are used by specifying the border-style value. The styles are named: dashed, dotted, double, solid, inset, outset, grooved, ridge.

Border-styles :

  • Border width is specified in pixels or to a value of thick, thin, or medium.
  • Border color. can also be set to transparent.
  • border-style-top, -right, -left, - bottom The values can be specified individually as such border-style-top, border-style-right, eyc. However there is a short cut method. One specify border-style with up to four values.


Depending on how many values are specified. The way the values are determined for top, bottom, left, and right are as follows:

border-style: value1 value2 value3 value4

  • top border is value1
  • right border is value2
  • bottom border is value3
  • left border is value4

border-style: value1 value2 value3;

  • top border is value1

  • right and left borders are value2

  • bottom border is value3

border-style: value1 value2;

  • top and bottom borders are value1
  • right and left borders are value2


border-style: value1;

  • all four borders are value1.

If you have a uniform border (all side the same) then you can specify border simply as

border: <width> <style> <color> ; on one line. Example:

border: 8px solid blue;

Border Styles

A look at the code which generated the border styles in the following snapshot.
A look at the code which generated the border styles in the following snapshot.
Examples of border styles.
Examples of border styles.

Outline

Outline

An outline is a line that is drawn outside the borders to make the element "stand out".The outline properties specify the color, style, and width.


As with border there is a shorthand way to describe outline properties

outline <color>, style, width

where:

width – thin, medium, thick, px

style – same 8 as border

color – rgb, hex, name invert (Yes. colors can be inverted.)


Padding


The padding clears an area around the content of an element. The padding is affected by the background color of the element.

As with top, right, bottom, and left padding can be changed independently using separate properties: padding-top, padding-right, etc. . A shorthand padding property can also be used, to change all paddings at once. Padding can be specified as a size as in the example which follows or as a percentage of the "container" element., the outlined or bordered area

padding-top:25px;
padding-bottom:25px;
padding-right:50px;
padding-left:50px;

The snapshot illustrates a padding example given in percentages.


Padding Examples: No Specific Padding and Padding as %-age

The first p[paragraph has no specific padding. The area anount it is reserved. In the second paragraph padding has been specified as a percentage.
The first p[paragraph has no specific padding. The area anount it is reserved. In the second paragraph padding has been specified as a percentage.

CSS Font Properties

There are four items to remember about fonts. There are font families such as Times New Roman, Tahoma, Arial which are popular. There are also generic fonts which are named monospace or serif.

When targeting a specific element such as a paragraph. It is a good idea to specify more than one font. In this way, if the preferred font is not available, the "fallback" font can be used. Beside the choice of font one can specify a font-style of normal, italic, or oblique (almost like italic, but used less frequently. Font size is nowadays mainly stated in terms of px (with dx, density pixels being common for mobile) but one can use em, which is a measure equal to the current font size. Thus 2em, for example is twice font size. We will discuss "em" further when we look at various units of measurement in CSS.

Illustrating Some Font Properties

In this example we override the default sizes for the heading. We introduce the notion of font-family for the paragraph. An we use "em" as the unit of measure for the font-size. 1em = current fornt size, so this paragraph is illustrated as 1.8 times
In this example we override the default sizes for the heading. We introduce the notion of font-family for the paragraph. An we use "em" as the unit of measure for the font-size. 1em = current fornt size, so this paragraph is illustrated as 1.8 times

Wrap Up and What's Next

In this tutorial we covered the five components which make up the area that is reserved form a HTML element. HTML elements are considered CSS boxes when the are formatted. These calculations are vital when one starts to layout a web page as we will see in the near future. We briefly touched on the subject of fonts. Choosing"safe" fonts which are likely to be present on all machines is a good practice. I am currently working on a hub which addresses typographic elements is some detail.

In the next tutorial, now that we have the concept of HTML boxes and how to size them, we will start looking at aspect of page layout.


Apprise Us About Your Satisfaction With This Tutorial

5 out of 5 stars from 1 rating of Were you satisfied with the information you received?

Comments

    0 of 8192 characters used
    Post Comment

    • profile image

      Johanna 4 years ago

      I think its a good article

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: "https://hubpages.com/privacy-policy#gdpr"

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)