CSS Tutorial

Posted by

CSS Tutorial

What is CSS?

CSS stands for Cascading Style Sheets. Css is standard language for styles defining on web pages. Although CSS is said to be widely know for its HTML document application which can be defining styles for any type of structured document format (such as XML for example).

Styles are used in CSS properties. Example you can able to set font properties such as size, colors, style, background images, border styles, and many more.

 

CSS History

CSS is one of major changes done in CSS level 1 and has became a W3C recommendation at December 1996. CSS1 states that CSS language as well as simple visual formatting model. CSS2 states to be a W3C recommendation on May 1998 which builds on CSS1 adds a support for media specific style sheets such as printers and aural devices, downloadable fonts, element positioning and tables.

CSS3 is said to be currently under development and includes more advanced features stated as animations, background gradients, multi-column layouts, border images, and more.

HTML vs CSS

Its not really stated as “HTML vs CSS”. In HTML and css is said to be the best one for friends. CSS is supplementary one to HTML. Here what each will perform.

HTML

Purpose of HTML is to provide document structure and meaning. This is said to be true introduction of HTML5.You can use HTML to specify what elements move on to the page such as headings, tables, paragraphs, images and many more. Each elements as well as their attribute values and attributes do have certain meaning.

 

 

CSS

You can able to use CSS to specify how HTML elements look but now they look as they are presented. You can use CSS to offer styles for speech output. You can write an HTML documents which is said to be concerned with its presentation then use CSS to perform how it will be presented with any given context. You can able to change the CSS without having any change to HTML. In other words you can use plug a style sheet into an HTML document and HTML document is said to take on styles of stylesheet.

Example

Before

Here’s an example that uses HTML only. No CSS involved.

<!DOCTYPE html>

<html>

<head>

<title>Example</title>

</head>

<body>

<main>

<h1>HTML Page</h1>

<p>This is a basic web page.</p>

</main>

</body>

</html>

Result:

HTML Page

This is a basic Web Page

 

 

 

 

After

In this example, we add some CSS.

 

<!DOCTYPE html>

<html>

<head>

<title>Example</title>

<style>

main {

width: 200px;

height: 200px;

padding: 10px;

background: beige;

}

h1 {

font-family: fantasy, cursive, serif;

color: olivedrab;

border-bottom: 1px dotted darkgreen;

}

p {

font-family: sans-serif;

color: orange;

}

</style>

</head>

<body>

<main>

<h1>HTML Page</h1>

<p>This is a basic web page.</p>

</main>

Result:

HTML Page

This is a basic Web Page

 

All we did was add the following code to the second example:

<style>

main {

width: 200px;

height: 200px;

padding: 10px;

background: beige;

}

h1 {

font-family: cursive;

color: olivedrab;

border-bottom: 1px dotted darkgreen;

}

p {

font-family: sans-serif;

color: orange;

}

</style>

Just go ahead and “cut and paste” the <style> tags and everything between them from second example into first example inorder to see how this affects both examples.

This create an HTML document without CSS, but it will probably look like it was created back in 1995!

Next we’ll look at the CSS syntax.

CSS Syntax — How to Code CSS

This page always describes the basic syntax which can use to apply CSS to any website.

CSS synatx will consists of set of rules which you can able to use to apply CSS for any website.

CSS syntax consists of various set of rules. These rules is said to have about 3 parts such as selector, value, a property.

You cant need to remember this order to code up CSS. Once start coding CSS you can do without thinking “This is a selector” or thats a property. This will begin to make best tutorial once you study up the examples on to this page.

Syntax:

selector { property: value }

The selector represents the HTML element that you want to style. For example:

h1 { color: blue }

This code fetches the browser to input out all occurences of HTML <h1> element in blue.

Grouping Selectors

You can able to apply many style to many selectors as you wish. Have a separate selectors with comma.

h1, h2, h3, h4, h5, h6 { color: blue }

 

Applying Multiple Properties

Inorder to apply more than one property just separate each declaration with semi colon.

h1 { color:blue; font-family: arial, helvetica, “sans serif” }

 

 

 

Readability

You can able to make CSS code to be more readable by spreading up your style declarations across multiple lines. You can even indent your code if you like it. This doesnt affect how code has rendered which makes it easier to read.

 

h1 {

color: blue;

font-family: arial, helvetica, “sans serif”;

font-size: 150%;

}

Now you have learned about CSS syntax but how do you use it in the syntax is the simple term in your website are included in the next topic.

 

How to add CSS to a Webpage

As it comes to adding CSS to your document as you have a choice of methods. One method which states out as the most common one.

There are about four ways of adding CSS into webpage: declare inline, embed to head of your document, linked to an external CSS file, import to CSS file.

 

Inline Styles

This inline styles, style sheet information is directly to HTML element. Its defined the style once, applying style against all element states as <p> tag which can add style directly to specific element you want to apply to

For example:

 

<p style=”color:olive;font-size:24px;”>HTML Styles with CSS</p>

Inline styles is said to be convenient and quick way to add up your styles to an HTML document in this method to be used sparingly. Just adding inline styles all over website to make it much more difficult to maintain. Small change is said to be major undertaking style which can be applied to many pages all over the website.

 

Embedded Styles

You can also add CSS to one part of document which will be used on top. Allows you to style up an element on page from a single place. Embedding the CSS information within <style> tags in the head of your document.

For example just place up the following code between the <head> tags of your HTML document:

 

<style>

body {

background-color: darkslategrey;

color: azure;

font-size: 1.1em;

}

h1 {

color: coral;

}

#intro {

font-size: 1.3em;

}

.colorful {

color: orange;

}

</style>

Here is the best example where code fits to an HTML document:

 

<!DOCTYPE html>

<html>

<head>

<title>My Example</title>

<style>

body {

background-color: darkslategrey;

color: azure;

font-size: 1.1em;

}

h1 {

color: coral;

}

#intro {

font-size: 1.3em;

}

.colorful {

color: orange;

}

</style>

</head>

<body>

<h1>Embedded Styles</h1>

<p id=”intro”>Allow you to define styles for the whole document.</p>

<p class=”colorful”>This has a style applied via a class.</p>

</body>

</html>

 

 

CSS Class Selectors

CSS classes is said to give extra flexibility is said to specify elements are to receive particular style.

Here we have learned about selectors. You can recall that selectors is said to be the things which will apply style against. In this examples, selectors were all HTML elements. Example, we decided to make <h1> element blue.

Now this works as well if you want all headings to be blue. But what will you do if you want some headings to be in blue? You want color of your headings to reflect section of website that you are in.

Sounds like you need to use classes!

 

In Css, classes is said to allow you a style which is given class of an element. To do this, you must link element to style by declaring a style for a class, assigning that class to element.

 

CSS Class Syntax

You must delcare a CSS class with the help of dot followed by class name. You can make a class name for youself. After class name you can just enter values or properties that you want to assign up your class.

.class-name { property:value; }

Want to use same class name for multiple ements as with a various style where you can prefix the dot with HTML element name.

For example:

p.large { font-size: 2em; }

 

CSS Class Example

 

<!DOCTYPE html>

<title>Example</title>

<style>

div.css-section {

border:1px dotted red;

padding: 20px;

}

p.css-section {

color:green;

}

</style>

<div class=”css-section”>CSS Class</div>

<p class=”css-section”>CSS classes can be very useful</p>

You can now also select classes or element which can be nested within another class or element. Example, div.css-section p is said to select all <p> elements which can be nested within <div> element that uses .css section class.

Its one of the major benefit of doing that this to apply a class to each and every instance of an element when its nested within another element that uses a class

 

Here’s an example:

<!DOCTYPE html>

<title>Example</title>

<style>

div.css-section {

border:1px dotted red;

padding: 20px;

}

div.css-section p {

color:green;

}

</style>

<div class=”css-section”>CSS Class

<p>CSS classes can be very useful</p>

</div>

 

Class ID Selector

You can able to assign a unique identifier to HTML element, style up that element by referencing its unique identifier. This will help to allow you to define a style which can be used by element to assign the ID

Any type of HTML element will have id attribute applied to it. Value of this attribute is elements unique identifier. Example: h1 id=”mainHeading”> gives that element a unique identifier of mainHeading.

We can reference that ID from our CSS code inside.

 

CSS ID Syntax

Syntax for declaring CSS ID is same for classes, except instead of using a hash (#) and a dot.

The syntax for declaring a CSS ID is the same as for classes, except that instead of using a dot, you use a hash (#).

#id-name { property:value; }

This is stated to similar to classes, Want to use same id name for multiple elements as each has a different style where you can prefix the has to HTML element name.

p#intro { font-size: 2em; }

 

CSS ID Example

<!DOCTYPE html>

<title>Example</title>

<style>

div#css-section {

border:1px dotted red;

padding: 20px;

}

</style>

<div id=”css-section”>

This lucky div has ID…

</div>

<div>

This poor div has no ID…

</div>

 

IDs vs Classes

Given Ids and classes are similar to be wondering one to use. This depends on situation.

 

When to use classes

You should use classes when style is said to be applied multiple times on the same page. Example, you can have many <h1> elements which need the same style applied.

When to use Ids

When use Ids only upon one element on page which is to have style applied and or you need a unique identifier for element. Example, you must assign ID to a <nav> tag which is at your left menu. Styles for ID contains background color, position, size, properties and many more. You must not have any other element on page to use this specific style.

This is another useful one about Ids which you can use Document Object Model (DOM) to refer them. This will enable you to use JavaScript/DHTML techniques to build more interactive website.

 

More Selectors

CSS includes many more selectors which you can use selecting element for styling.

Here are many things that you can provide with selectors:

Select these elements where you have a certain attribute.

Select certain element which has certain attribute with certain value.

Select specific element but when its inside another which has specific element.

Just select only First child of specific element.

Select only the last child of a specific element.

You can even specify which child (eg, the 3rd child, or the 4th, the 5th… etc).

Select an element when its being hovered over

Here, we look at the various font properties. These are the properties in the font namespace — those that include the word font at the start of their name.

 

CSS FONT PROPERTIES

CSS font properties enable you to change look for your text. For example, you can assign a font family, apply italic or bold formatting, change up the size and more.

 

CSS Font Family

Font Family property allows you set up the font family.

 

<!DOCTYPE html>

<title>Example</title>

<style>

p {

font-family: Georgia, Garamond, serif;

}

</style>

 

<p> text is said to be rendered in garamond, georgia or default serif font which totally depeneds on font user’s system. </p>

Font family property helps to accept different lists font families. This is stated as not all users to have same fonts installed on their computer. Therefore as developer or designer can provide list of fonts, starting up your first choice and ending with your last choice.

If user dont have your first choice font, second choice is said to be used for user. Second choice, third choice to be used and so on.

 

Its perfect practive to make last choice a generic font family stated as serif or sans-serif which depends on what you want a font

It’s good practice to make your last choice a generic font family such as serif or sans-serif (depending on whether you want a font with serifs or not). This way, if none of the other choices are on the user’s computer, their computer will choose a font that is on their computer, based on the generic family that you provide.

CSS Font Size

This enables to set up the size of text. For info on to the possible values, to see font size page

<!DOCTYPE html>

<title>Example</title>

<style>

p {

font-size: 30px;

}

</style>

<p>This text is using a font size of 30 pixels.</p>

 

CSS Font Size Adjust

This property enables you to adjust up the x height to make fonts as legible. For more info, just see font size adjust page.

 

<!DOCTYPE html>

<title>Example</title>

<style>

p {

font-size: 12px;

font-size-adjust: 0.58;

}

</style>

<p>This text is using a font-size-adjust value.</p>

 

CSS Font Stretch

This property lies on user’s system to have condensed or expanded version of font being used. For all possible values to font stretch page.

 

<!DOCTYPE html>

<title>Example</title>

<style>

p {

font-stretch: ultra-expanded;

}

</style>

<p>If your system is said to have an expanded version of the font being used, this text will be stretched.</p>

 

CSS Font Style

Font style property used for specifying italic text.

 

<!DOCTYPE html>

<title>Example</title>

<style>

p {

font-style: italic;

}

</style>

<p>This text is in italics.</p>

 

CSS Font Variant

This enables you to set your text to use small caps. Just see font variant.

 

<!DOCTYPE html>

<title>Example</title>

<style>

p {

font-variant: small-caps;

}

</style>

<p>This Text Is Using Small Caps.</p>

 

CSS Font Weight

This enables to set up your text to bold.

<!DOCTYPE html>

<title>Example</title>

<style>

p {

font-weight: bold;

}

</style>

<p>This text is bold.</p>

 

CSS Font Property

Font property is said to be shorthand property which enables to set up all front properties to go.

 

<!DOCTYPE html>

<title>Example</title>

<style>

p {

font: italic small-caps bold 20px Georgia, Garamond, serif;

}

</style>

<p>The styles for this text has been specified with the ‘font’ shorthand property.</p>

 

CSS Text Properties:

This page has properties in the text namespace properties with word text into their name as well as some other related properties

CSS text properties have various CSS font properties were there are many other properties will assist into styling up your text. Example you can change the color of text, add decoration properties, align text and many more.

In CSS, text will be styled with the properties listed below. This list will learn how to use CSS text property where it looks like a browser.

 

CSS Text Color

For more information to see color property

<!DOCTYPE html>

<title>Example</title>

<style>

p {

color: olive;

}

</style>

<p>This CSS text color is olive</p>

 

CSS Text Align

For more information, see text align property.

<!DOCTYPE html>

<title>Example</title>

<style>

p {

text-align: right;

}

</style>

<p>This CSS text is aligned right</p>

 

CSS Text Indent

Indents are said to be first line of paragraph. For more info just see the text indent property

<!DOCTYPE html>

<title>Example</title>

<style>

p {

text-indent: 50px;

}

</style>

<p>This text is said to be indented by 50 pixels. First line of paragraph is said to be indented by 50 pixels but the following lines must not be indented. Text is said to wrap before you see the indent and hence all this text!</p>

 

CSS Letter Spacing

For more information see the letter spacing property.

<!DOCTYPE html>

<title>Example</title>

<style>

p {

letter-spacing: 5px;

}

</style>

<p>This text has letter spacing applied</p>

 

CSS Word Spacing

For more information just see word spacing property

 

<!DOCTYPE html>

<title>Example</title>

<style>

p {

word-spacing: 50px;

}

</style>

<p>This text has word spacing applied</p>

 

 

 

CSS Text Decoration

For more information, just see text decoration property.

 

<!DOCTYPE html>

<title>Example</title>

<style>

.overline {

text-decoration: overline;

}

.line-through {

text-decoration: line-through;

}

.underline {

text-decoration: underline;

}

a:link {

text-decoration: none;

}

</style>

<p> class=”overline”>This text has a line over the top</p>

<p class=”line-through”>This text has a line through the middle</p>

<p class=”underline”>This text has a line underneath</p>

<a href=”/css/” target=”_blank”>This hyperlink has no underline</a>

 

 

 

 

CSS Text Transform

 

For more information, see the text-transform property.

 

<!DOCTYPE html>

<title>Example</title>

<style>

.uppercase {

text-transform: uppercase;

}

.lowercase {

text-transform: lowercase;

}

.capitalize {

text-transform: capitalize;

}

</style>

<p class=”uppercase”>This text has been transformed to uppercase</p>

<p class=”lowercase”>THIS TEXT HAS BEEN TRANSFORMED TO LOWERCASE</p>

<p class=”capitalize”>this text has been capitalized.</p>

 

CSS Text Direction

 

For more information, see the direction property.

Editor

Preview

 

<!DOCTYPE html>

<title>Example</title>

<style>

p {

direction: rtl;

}

</style>

<p>This text is running from right to left. This can be useful for languages where the text runs from right to left. Not so useful for english though…</p>

 

CSS unicode-bidi

This information to see unicode-bidi property. This conjunction with director property to determin the text direction. Possible values are normal, embed, bidi-override, and inherit.

 

<!DOCTYPE html>

<title>Example</title>

<style>

p {

direction: rtl;

unicode-bidi: bidi-override;

}

</style>

<p>This text is running from right to left. This can be useful for languages where the text runs from right to left. Not so useful for english though…</p>

 

 

 

CSS Text Shadow

This more information to see text shadow property

<!DOCTYPE html>

<title>Example</title>

<style>

p {

text-shadow: 2px 2px 8px orange;

}

</style>

<p>If your browser supports the CSS text-shadow property, this text will have a shadow.</p>

 

CSS White Space

CSS White Space tells up the browser to handle up white space. Possible values are normal, pre, and nowrap.

For more information, see the white-space property.

<!DOCTYPE html>

<title>Example</title>

<style>

p {

white-space: pre;

}

</style>

<p>This text has a line break

(actually, it has

many line breaks!)

and the

white-space pre setting

tells the browser to honor it

just like

the HTML pre tag.

</p>

 

CSS Height and Width Properties

CSS includes width and height properties to help you specify size of your elements.

 

Height and width Properties

This applies to all HTML elements except non replaced inline elements, column and table columns groups.

You can use a fixed height (i.e. pixels) or a percentage height. For more info, see height and width.

<!DOCTYPE html>

<title>Example</title>

<style>

div {

height: 150px;

width: 75px;

background-color: gold;

}

</style>

<div>

This div has height and width applied.

</div>

 

max-height and max-width Properties

This enables you to constrain width or height of an element to maximum value. For more information to see max-height and max-width.

<!DOCTYPE html>

<title>Example</title>

<style>

div {

max-height: 150px;

max-width: 75px;

background-color: gold;

}

</style>

<div>

This div has ‘max-height’ and ‘max-width’ applied.

</div>

 

min-height and min-width Properties

This enables you to constrain height and width for an element to minimum value. For more info to see min width and min height.

<!DOCTYPE html>

<title>Example</title>

<style>

div {

min-height: 150px;

min-width: 75px;

background-color: gold;

}

</style>

<div>

This div has ‘min-height’ and ‘min-width’ applied.

</div>

Just note that width and height can be affected by padding and borders. We cover later in this tutorial, and show you how you can deal with this issue.

 

The following CSS background codes demonstrate the various CSS properties you can use to style the background of any HTML element.

 

 

 

CSS Background Color

To set the background color for an element just use background-color-property.

 

<!DOCTYPE html>

<title>Example</title>

<style>

div {

padding: 20px;

background-color: yellow;

}

</style>

<div>

This ‘div’ has a background color applied.

</div>

 

CSS Background Image

Inorder to add CSS Background Image to an element to use background-image property.

<!DOCTYPE html>

<title>Example</title>

<style>

div {

padding: 70px;

background-image: url(“/pix/samples/bg2.png”);

}

</style>

<div>

This ‘div’ has a background image applied.

</div>

 

 

CSS Background Repeat

This determins whether background image repeats or not. On the possible values, see background repeat.

<!DOCTYPE html>

<title>Example</title>

<style>

div {

padding: 70px;

background-image: url(“/pix/samples/bg2.png”);

background-repeat: no-repeat;

}

</style>

<div>

The background image does not repeat.

</div>

 

CSS Background Position

This determins the background image position. For more information, see background position.

<!DOCTYPE html>

<title>Example</title>

<style>

div {

padding: 70px;

background-image:url(“/pix/samples/bg2.png”);

background-repeat:no-repeat;

background-position: 100px;

}

</style>

<div>

The background image is positioned 100 pixels in from the left.

</div>

 

CSS Background Attachment

This determines whether background image is said to scroll with outer container or not. For more information just see the background attachment.

 

<!DOCTYPE html>

<title>Example</title>

<style>

div {

padding: 70px;

height: 70px;

width: 150px;

background-image: url(“/pix/samples/bg2.png”);

overflow: auto;

background-attachment: fixed;

}

</style>

<div>

The background image is fixed – it doesn’t scroll with its outer container. This example uses the CSS overflow property to force the box to scroll when there’s too much text to fit in the box.

</div>

 

Shorthand Code

You can able to use background property to set up all background properties once. For more info, just see the background.

<!DOCTYPE html>

<title>Example</title>

<style>

div {

padding: 70px;

height: 70px;

width: 150px;

background: url(“/pix/samples/bg2.png”) repeat fixed;

overflow: auto;

}

</style>

<div>

This paragraph tag is said to have styled using “background” property which is said to be shorthand for setting up multiple properties of an HTML element.

</div>

CSS background codes demonstrate the various CSS properties which will style the background of any HTML element.

 

 

CSS Background Color

Set background color of element use background color property.

<!DOCTYPE html>

<title>Example</title>

<style>

div {

padding: 20px;

background-color: yellow;

}

</style>

<div>

This ‘div’ has a background color applied.

</div>

 

CSS Background Image

Inorder to add background image for an element just use background image property.

 

 

 

<!DOCTYPE html>

<title>Example</title>

<style>

div {

padding: 70px;

background-image: url(“/pix/samples/bg2.png”);

}

</style>

<div>

This ‘div’ has a background image applied.

</div>

 

CSS Background Repeat

This determins whether background image repeats or not. Just check the background repeat.

<!DOCTYPE html>

<title>Example</title>

<style>

div {

padding: 70px;

background-image: url(“/pix/samples/bg2.png”);

background-repeat: no-repeat;

}

</style>

<div>

The background image does not repeat.

</div>

 

 

 

CSS Background Position

This determines the background image position. For more information, see background-position.

<!DOCTYPE html>

<title>Example</title>

<style>

div {

padding: 70px;

background-image:url(“/pix/samples/bg2.png”);

background-repeat:no-repeat;

background-position: 100px;

}

</style>

<div>

The background image is positioned 100 pixels in from the left.

</div>

 

CSS Background Attachment

This determines whether background image helps to scroll with many outer container. For more information just check background attachment.

 

<!DOCTYPE html>

<title>Example</title>

<style>

div {

padding: 70px;

height: 70px;

width: 150px;

background-image: url(“/pix/samples/bg2.png”);

overflow: auto;

background-attachment: fixed;

}

</style>

<div>

The background image is said to be fixed – this doesn’t scroll with its outer container. This example uses the CSS overflow property to force the box to scroll when there’s too much text to fit in the box.

</div>

 

Shorthand Code

You can able to use background property to set all the background properties once. For information check for background.

<!DOCTYPE html>

<title>Example</title>

<style>

div {

padding: 70px;

height: 70px;

width: 150px;

background: url(“/pix/samples/bg2.png”) repeat fixed;

overflow: auto;

}

</style>

<div>

This paragraph tag is said to be styled using the ‘background’ property, which is shorthand for setting multiple properties for an HTML element.

</div>

 

CSS Border properties

CSS helps to set up styles for border on HTML element provides you in setting up border styles on one or more sides of an element.

There are some examples to demonstrate various CSS properties where you can able to use to apply styles to the border of HTML element.

 

 

 

Setting Borders on all Sides

Inorder to set up border styles for all sides of element then use border-width, border-color properties and border-style. You can also use boarder property to set up properties at once.

border-width, border-style, and border-color

<!DOCTYPE html>

<title>Example</title>

<style>

div {

padding: 20px;

border-width: 1px;

border-style: solid;

border-color: orange;

}

</style>

<div>

This ‘div’ has border styles applied using the border-width, border-style, and border-color properties.

</div>

 

The border Property

Border property is said to be shorthand for setting up border-width, border-style, and border-color.

<!DOCTYPE html>

<title>Example</title>

<style>

div {

padding: 20px;

border: 1px solid orange;

}

</style>

<div>

This ‘div’ has border styles applied using the ‘border’ property.

</div>

 

Border Styles

Borders is said to have following styles.

<!DOCTYPE html>

<title>Example</title>

<style>

div {

padding: 5px;

margin: 10px;

text-align: center;

border-color: orange;

border-width: 4px;

}

.solid {

border-style: solid;

}

.dotted {

border-style: dotted;

}

.dashed {

border-style: dashed;

}

.double {

border-style: double;

}

.groove {

border-style: groove;

}

.ridge {

border-style: ridge;

}

.inset {

border-style: inset;

}

.outset {

border-style: outset;

}

.hidden {

border-style: hidden;

}

</style>

<div class=”solid”>This ‘div’ has a border style of ‘solid’.</div>

<div class=”dotted”>This ‘div’ has a border style of ‘dotted’.</div>

<div class=”dashed”>This ‘div’ has a border style of ‘dashed’.</div>

<div class=”double”>This ‘div’ has a border style of ‘double’.</div>

<div class=”groove”>This ‘div’ has a border style of ‘groove’.</div>

<div class=”ridge”>This ‘div’ has a border style of ‘ridge’.</div>

<div class=”inset”>This ‘div’ has a border style of ‘inset’.</div>

<div class=”outset”>This ‘div’ has a border style of ‘outset’.</div>

<div class=”hidden”>This ‘div’ has a border style of ‘hidden’.</div>

 

Setting Borders for Each Side

If you dont need any border settings to be applied for all four sides or if you want each side to have various styles applied then you can able to use following properties:

 

Explicit Properties

border-bottom-color

border-bottom-style

border-bottom-width

border-left-color

border-left-style

border-left-width

border-right-color

border-right-style

border-right-width

border-top-color

border-top-style

border-top-width

 

<!DOCTYPE html>

<title>Example</title>

<style>

div {

padding: 20px;

border-bottom-width: 4px;

border-bottom-style: double;

border-bottom-color: orange;

}

</style>

<div>

This ‘div’ has a bottom border.

</div>

 

Shorthand Properties

Following properties provide you more conside way of specifying border properties one each side.

border-bottom

border-left

border-right

border-top

 

 

 

Example:

<!DOCTYPE html>

<title>Example</title>

<style>

div {

padding: 20px;

border-bottom: 4px double orange;

}

</style>

<div>

This ‘div’ has a bottom border.

</div>

 

Border Radius

You can also give borders rounder corners by using border radius property.

<!DOCTYPE html>

<title>Example</title>

<style>

div {

padding: 20px;

border: 1px solid orange;

border-radius: 8px;

}

</style>

<div>

Rounded corners.

</div>

 

 

 

 

 

CSS Margin properties:

In these examples will demonstrate various CSS properties where you can make use to apply margins to any HTML element.

Margins are defined as the space around the element. CSS margins are specified in a similar manner to borders as they can able to set indvidually for all sides or each side at once.

Setting Margins on all Sides

 

<!DOCTYPE html>

<title>Example</title>

<style>

div {

border: 1px solid orange;

}

div > p {

background: gold;

margin: 20px;

}

</style>

<div>

<p>

This text has a margin of 20 pixels on all four sides. It is nested within a div with a border to make it easier to see the effect of the margin.

</p>

</div>

 

Setting up Margins for Each Side

If you dont need margin settings to be applied at all four sides then you want to use each side to have different margins applied so that you can use following properties:

 

 

margin-top

margin-right

margin-bottom

margin-left

 

Example:

<!DOCTYPE html>

<title>Example</title>

<style>

p {

background: gold;

}

.left-margin {

margin-left: 60px;

}

</style>

<p class=”left-margin”>

This text has a left margin of 60 pixels.

</p>

<p>

This text has no left margin set.

</p>

 

Shorthand Property

This method used as shorthand property for setting up margin right, margin top, margin botton and margin left in the one place. This method was said to be quicker one. This also uses less code rather than previous method. Its same property that we used in our first example (i.e. the margin property). The only difference is that we apply multiple values against it.

 

 

<!DOCTYPE html>

<title>Example</title>

<style>

div {

border: 10px solid lightblue;

}

div > p {

border: 1px dotted orange;

margin: 20px 50px 10px 30px;

}

</style>

<div>

<p>

This text has a different sized margin for each side. It is nested within a ‘div’ with a border to make it easier to see the effect of the margin.

</p>

</div>

 

Variations

You dont want to offer various values for all four sides. You can provide more than three values.

If there is only one value then this applies to all sides. There are two values, top and bottom margins which are set to the first value and right and left margins are set to second one. If there are three more values, top is set to the first value, left and right are set up to second and bottom is set up to the third. If there are four more values then apply to right, top, bottom and left

Examples

margin:10px;

All four sides have a margin of 10 pixels.

margin:10px 20px;

Top and bottom have a margin of 10 pixels.

Right and left have a margin of 20 pixels.

margin:10px 20px 30px;

Top is 10px

Left and right are 20px

Bottom is 30px

margin:10px 20px 30px 40px;

Top is 10px

Right is 20px

Bottom is 30px

Left is 40px

 

CSS Padding properties

Padding states the space between element border and its content. CSS padding is stated just like margins which can be set individually for each sides or on all sides at once.

Set the Same Padding on all Sides

This example helps in padding up shorthand property inorder to set up the same padding on all sides of element.

 

<!DOCTYPE html>

<title>Example</title>

<style>

div {

background: gold;

padding: 20px;

}

div > p {

background: white;

}

</style>

<div>

<p>

Padding of 20 pixels on all four sides.

</p>

</div>

 

Setting Padding for Each Side

If you dont need the same padding settings to be applied on four sides or you want each side to have various padding then you can use the following properties:

padding-top

padding-right

padding-bottom

padding-left

 

<!DOCTYPE html>

<title>Example</title>

<style>

div {

background: gold;

padding-top: 5px;

padding-right: 60px;

padding-bottom: 15px;

padding-left: 30px;

}

div > p {

background: white;

}

</style>

<div>

<p>

Different padding on each side.

</p>

</div>

 

 

Shorthand Property

Padding property is shorthand for padding right, padding top, padding bottom and padding left.

<!DOCTYPE html>

<title>Example</title>

<style>

div {

background: gold;

margin: 20px;

padding: 5px 60px 15px 30px;

}

div > p {

background: white;

}

</style>

<div>

<p>

Different padding on each side.

</p>

</div>

 

Variations

With margin you dont need to provide various values on all four sides. You can provide more than three values.

If there is one value then it applies to all sides. If there are two values then top and botton paddings are set to first value amd right and left paddings are set to second.

If there are three values, top is set towards first value, the left and rights are set to be the second and botton is set upto third.

If there are four value then apply it top the top, right, bottom, and left, respectively.

padding:10px;

All four sides have padding of 10 pixels.

padding:10px 20px;

Top and bottom have padding of 10 pixels.

Right and left have padding of 20 pixels.

padding:10px 20px 30px;

Top is 10px

Left and right are 20px

Bottom is 30px

padding:10px 20px 30px 40px;

Top is 10px

Right is 20px

Bottom is 30px

Left is 40px

Padding, Borders, and the Box Model

 

Just apply padding and borders to element which can affect size of element. If you set element to be at 100 pixels wide, then apply padding of 10 pixels, the padding said to increase the width to be 120 pixels.

This is only because default CSS box model adds upto borders and padding to original box size. It looks at specified height and width which adds padding and borders to height and width.

<!DOCTYPE html>

<title>Example</title>

<style>

div {

width: 150px;

height: 50px;

}

.padding {

padding: 20px;

background: gold;

}

.no-padding {

padding: 0;

background: limegreen;

}

</style>

<div class=”padding”>

Padding

</div>

<div class=”no-padding”>

No padding

</div>

 

<!DOCTYPE html>

<title>Example</title>

<style>

div {

width: 150px;

height: 50px;

box-sizing: border-box;

}

.padding {

padding: 20px;

background: gold;

}

.no-padding {

padding: 0;

background: limegreen;

}

</style>

<div class=”padding”>

Padding

</div>

<div class=”no-padding”>

No padding

</div>

This will keep the box specified for width or height that draws the padding and border inside that specified height and width.

Many developers to change box model across all HTML elements and psuedo-elements.

You can do this with the following code.

*, *:before, *:after {

box-sizing: border-box;

}

 

How to style lists with CSS

HTML list is said to have a number of various styles applied using CSS. Here are some commonly used ones.

List Style Type

This determines the bullet looks lile. On the possible values to see list style type page.

<!DOCTYPE html>

<title>Example</title>

<style>

ul {

font-size: 1.5em;

list-style-type: circle;

}

</style>

<ul>

<li>List item one</li>

<li>List item two</li>

<li>List item three</li>

</ul>

 

List Style Image

For more information on using images for bullet points to see list-style-image.

 

 

<!DOCTYPE html>

<title>Example</title>

<style>

ul {

font-size: 22px;

list-style-image: url(“/pix/samples/bullet1.png”);

}

</style>

<ul>

<li>List item one</li>

<li>List item two</li>

<li>List item three</li>

</ul>

 

List Style Position

This determines bullet located inside lists containing box or outside. For more info, see list style position.

 

<!DOCTYPE html>

<title>Example</title>

<style>

ul {

font-size: 1.5em;

}

.inside {

list-style-position: inside;

}

.outside {

list-style-position: outside;

}

</style>

<ul class=”inside”>

<li>List item one</li>

<li>List item two</li>

<li>List item three</li>

</ul>

<ul class=”outside”>

<li>List item one</li>

<li>List item two</li>

<li>List item three</li>

</ul>

 

List Style

List style property is said to shorthand for selling list properties.

<!DOCTYPE html>

<title>Example</title>

<style>

ul {

font-size: 1.5em;

list-style: square inside;

}

</style>

<ul>

<li>List item one</li>

<li>List item two</li>

<li>List item three</li>

</ul>

 

 

 

Unstyled Lists

By default, most browsers will style lists at certain way. Example most lists is said to have bullet point or any other list market for each item. This lists will typically have margin and padding applied and list marker removed.

Unstyled lists are used often in website navigation and many other places where any list market, margins and padding is applied to get in its way.

 

<!DOCTYPE html>

<title>Example</title>

<style>

ul {

font-size: 22px;

margin: 0;

padding-left: 0;

list-style: none;

}

</style>

<ul>

<li>Apples</li>

<li>Oranges</li>

<li>Watermelons</li>

</ul>

 

Inline Lists

You cana ble to take further steps and turn unstyled list towards inline list. You can also do this by setting display:inline-block; against the li elements.

<!DOCTYPE html>

<title>Example</title>

<style>

ul {

font-size: 22px;

margin: 0;

padding-left: 0;

list-style: none;

}

ul > li {

display: inline-block;

}

</style>

<ul>

<li>Apples</li>

<li>Oranges</li>

<li>Watermelons</li>

</ul>

 

CSS Positioning

Just use CSS positioning inorder to specify how elements are positioned and where to be located on the page.

 

The term “CSS positioning” refers using position property to specify how elements are positioned. Example you can specify whether you want element positioned relative to its natural position at the page, in an absolute position at a fixed position and many more.

 

Relative Positioning

Inorder to perform relative positioning at CSS to use position:relative;followed by desired offset from top, right, bottom or left.

<!DOCTYPE html>

<title>Example</title>

<style>

div {

position: relative;

left: 50px;

background-color: gold;

width: 90px;

}

</style>

<p>Here is the normal flow of content…</p>

<div>

This div has relative positioning.

</div>

<p>…and here is more content…</p>

 

Absolute Positioning

Inorder to perform absolute positioning CSS against use position property. You can use position:absolute; followed by the desired offset.

<!DOCTYPE html>

<title>Example</title>

<style>

div {

position: absolute;

top: 100px;

left: 60px;

background-color: gold;

width: 90px;

padding: 20px;

}

</style>

<div>

This div is absolutely positioned 100 pixels from the top and 60 pixels from the left of its containing block.

</div>

 

 

 

Fixed Positioning

Fixed positioning allows to fix up position for an element on the page of scrolling.

<!DOCTYPE html>

<title>Example</title>

<style>

.absolute {

position: absolute;

top: 500px;

left: 60px;

height: 2000px;

background-color: limegreen;

}

.fixed {

position: fixed;

top: 10px;

left: 10px;

background-color: gold;

width: 90px;

padding: 20px;

}

</style>

<div class=”absolute”>

This tall div is absolutely positioned 500 pixels from the top and 60 pixels from the left of its containing block.

</div>

<div class=”fixed”>

This div is using a fixed position of 100 pixels from the top and 60 pixels from the left of its containing block. When this page scrolls, this box will remain in a fixed position – it won’t scroll with the rest of the page. Go on – SCROLL!

</div>

 

 

CSS Float

You can now use “Float” element at the right or left as other content flows around it.

CSS float property enables to determine where position for an element relative to other element on to the page. When to use float property to other element will wrap around the element you applied the float to.

Float Left Example

At this example, we set <div> element to float:left, results at the rest of content.

<!DOCTYPE html>

<title>Example</title>

<style>

.float-left {

float: left;

padding: 20px;

margin: 0 10px 10px 0;

background: gold;

}

</style>

<div class=”float-left”>

Float left

</div>

<p>This text rearranges itself to flow around the element that is floated left. </p>

<p>I added a margin to the right and bottom of the floated box so that this text doesn’t run right up against it. Play around with the property values to see how it changes the layout.</p>

 

Float Right Example

Example, we set a <div> element which helps to float:right, results at the rest of content wrapping around it.

At this case just keep bottom margin which move to right margin to left of box

 

<!DOCTYPE html>

<title>Example</title>

<style>

.float-right {

float: right;

padding: 20px;

margin: 0 0 10px 10px;

background: gold;

}

</style>

<div class=”float-right”>

Float right

</div>

<p>This text rearranges itself to flow around the element that is floated right. </p>

<p>I added a margin to the left and bottom of the floated box so that this text doesn’t run right up against it. Play around with the property values to see how it changes the layout.</p>

 

Aligning Boxes Side by Side

 

<!DOCTYPE html>

<title>Example</title>

<style>

div {

float: left;

padding: 20px;

}

.gold {

background: gold;

}

.limegreen {

background: limegreen;

}

.orange {

background: orange;

}

</style>

<div class=”gold”>

Gold

</div>

<div class=”limegreen”>

Limegreen

</div>

<div class=”orange”>

Orange

</div>

 

CSS Layers

You can now use CSS to add “layers for an elemnt so that an element can able to sit onto front or behind other elements.

In CSS, layers is said to refer on applying the z-index property to elements that overlap with each other.

Z-index property when used at conjunction with the position property, enables you to specify that element to appear on top at the event on overlap. An overlap can occur when position propertu is often desirable when creating advanced layouts.

 

<!DOCTYPE html>

<title>Example</title>

<style>

div {

width: 80px;

height: 100px;

position: relative;

}

.infront {

background-color: red;

top: 10px;

left: 80px;

z-index: 2;

}

.behind {

background-color: gold;

top: -60px;

left: 35px;

z-index: 1;

}

</style>

<div class=”infront”></div>

<div class=”behind”></div>

 

Layers with Opacity

You can able to use opacity propertu to provide some transparency between layers

 

<!DOCTYPE html>

<title>Example</title>

<style>

div {

width: 80px;

height: 100px;

position: relative;

}

.infront {

background-color: red;

top: 10px;

left: 80px;

z-index: 2;

opacity: 0.3;

}

.behind {

background-color: gold;

top: -60px;

left: 35px;

z-index: 1;

}

</style>

<div class=”infront”></div>

<div class=”behind”></div>

CSS flexbox and grid

This can be used for creating website layouts and positioning items on a web page. Here’s an overview. CSS flexbox and grid are two different layout models optimized for UI design. Flexbox is also known as flexible box layout which deals with one dimensional layouts where you can able to layout and align elements vertically or horizontally. Grid is also known as grid layout as it step further and allows to build two dimensional layouts where you can lay and align elements vertically and horizontally.

Here are some examples to demonstrate the concept of flexbox and grid layout.

Flexbox

Flexbox has a number of properties which allow to define a flexible box as well its item inside it.

First you can declare flex contaner by display applying: flex or display:inline-flex to it. This establishes a new flex formatting context for its contents.

 

<!DOCTYPE html>

<title>Example</title>

<style>

.outer-container {

font: 16px sans-serif;

background: gold;

color: white;

height: 200px;

display: flex;

}

.box {

padding: 10px;

flex: 1 0 auto;

}

.larger {

background: yellowgreen;

flex: 3 0 auto;

}

</style>

<div class=”outer-container”>

<div class=”box”>Box</div>

<div class=”box larger”>Larger Box</div>

<div class=”box”>Box</div>

<div class=”box larger”>Larger Box</div>

</div>

 

Here we specify normal box “grow factor” is 1 and the larger box is 3. Grow factor specifies how much flex item grows relative to rest flex item at flex container.

Grid Layout

Grid works similar to flexbox, grid container helps to establish a new grid formatting new context for contents.

<!doctype html>

<title>Example</title>

<style>

body {

display: grid;

grid-template-areas:

“header header header”

“nav article ads”

“footer footer footer”;

grid-template-rows: 60px 1fr 60px;

grid-template-columns: 20% 1fr 15%;

grid-gap: 10px;

height: 100vh;

margin: 0;

}

header, footer, article, nav, div {

padding: 20px;

background: gold;

}

#pageHeader {

grid-area: header;

}

#pageFooter {

grid-area: footer;

}

#mainArticle {

grid-area: article;

}

#mainNav {

grid-area: nav;

}

#siteAds {

grid-area: ads;

}

</style>

<body>

<header id=”pageHeader”>Header</header>

<article id=”mainArticle”>Article</article>

<nav id=”mainNav”>Nav</nav>

<div id=”siteAds”>Ads</div>

<footer id=”pageFooter”>Footer</footer>

</body>

 

Leave a Reply

Your email address will not be published. Required fields are marked *