Skip to Main Content or Page Contents
This first tutorial will show you how to style with inline CSS the following HTML Elements
You will learn the basics of applying inline styles so that you Know:
This example is only to demonstrate the use of the some of the styling techniques of CSS rather than producing a result that you would use.
This is a p HTML element styled with inline CSS
Results such as this this will be achieved in easy steps and full explanation of the code.
It is assumed that you have a basic knowledge of HTML or have reached lesson 3 in our HTML tutorial
Starting with inline coding the tutorial will progress to the other places your code can be placed, as mentioned below.
If you want to style for example all <p> tags then use Internal Style Sheets or External Style Sheets
How to Use Inline CSS Style follows shortly
Open your HTML Editor and create a new HTML document and view the document in the HTML editors Source view window. Different editors have there own name for this window
The source code produced should look like:
Example
<!DOCTYPE> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> </head> <body> <p>Replace this line with your Page Content</p> </body> </html>
With the inline-css-exercise-01.htm still open in the Editors Code View window (Editors may give this window a different name)
Enter the following HTML Code Elements in between between the opening & closing <body> </body> tags
Copy/Paste or Enter the following
<h1>My Main Heading h1 only 1 allowed</h1> <p>Paragraph 1.</p> <p>Paragraph 2. This is a much longer paragraph so that it continues on more that 1 line. Observe what happens when the browser window is narrowed and widened. The effect changes depending on the settings for this paragraph.</p> <h2>My h2 Sub Heading. Sub Headings More than 1 allowed</h2> <p>Paragraph 2</p> <h3>My h3 Sub Heading</h3> <p>My third paragraph.Another longer paragraph so that it continues on more that 1 line. Observe what happens when the browser window is narrowed and widened. The effect changes depending on the settings for this paragraph.</p></p> <p>My third paragraph.</p>
Save it as inline-css-exercises.htm
It is important to save a new document as soon as it is created, so that the editor knows the documents location.
Regularly save your work
Open the saved file inline-css-exercises.htm in your browser and check that it is correct
Click here to view my inline-css-exercises.htm
<HTML-tag style ="property:value; ">This is my Content</tag>
<HTML-tag style ="property: value; property: value; property: value;">This is my Content</tag>
Exercise 1
Set the
The syntax to use is
property: value;
as follows:
background-color: lightgray;
background-color: yellow;
background-color: red;
Copy/Paste or Enter the following
<body style ="background-color: lightgray; "> <h1 style ="background-color: yellow; " >My Main Heading h1 only 1 allowed</h1> <p style ="background-color: red; " >Paragraph 1.</p>
Because only 1 body element is allowed you MUST replace the current body element with the new body element.
Because of Googles rules The same applies to the h1 heading
You cant add the
<p style ="background-color: red; " >Paragraph 1.</p>
just under the current Paragraph 1. Allowing you to see the original version with the new styled version
Examples of property:value; pairs
color: white;
or
color: #000;
background-color: black;
or
background-color: #000;
width: 30px
width: 50%
max-width: 95%
min-width: 30px
padding: 6px;
padding: 2px 4px;
padding: 1px 2px 3px 4px;
margin: 6px;
margin: 2px 4px;
margin: 1px 2px 3px 4px;
margin: 0 auto;
border: 3px dashed black;
font-family: Verdana, Geneva, sans-serif;
Any HTML Element. In this Lesson we will use the <body>, <p> paragraph and some of the headings <h1>, <h2> etc.
Only the curly brackets. They must be used.
Do not use ( ) or [ ]
There are 100s of different properties that style color (CSS uses the American spelling), sizes, fonts, text styles, borders, padding, margins, positioning etc.
Each property has a selection of values
E.g. Color has 140 different color names such as red, green, blue, yellow, cyan, magenta, white, black, lightgray etc. With a total of over 16 million colours using the hex color system e.g. #000000 to #FFFFFF or in shorthand where the hex number pair are the same #000 to #FFF
The Look of an Element
ALL Elements look like a box. In the centre is the content, this can be surrounded by padding, then a border, then on the outside 4 margins which is space between elements
All these sections can be styled with CSS
See a diagram of the Box Model
In this step we change the default text colour from black to blue, and change the colour of the background to white.
In the declaration the American spelling color is used.
This step will produce the following.
This is a p HTML tag styled with inline CSS
Copy The code you entered in Step 1 and add additional code within the <p> tag only & add some additional text as follows:
Save and check in a browser
The above code can also be written with additional optional white space as follows
Example
<p style="color: white; background-color: blue;">This is a p HTML tag styled with inline CSS</p>
This layout allows easier code explanation
A CSS style rule consists of a a selector, and one or more declarations:
style="your styling go here"
The above sample contains 2 style Declarations, for clarity these are shown below on separate lines
color: #900; background-color: #ff3;
>
Closes the HTML tag
HTML including inline CSS Code called rules
This is a p HTML tag styled with inline CSS
Border require a Width (thickness), a Style, and a colour
Border Widths are usually expressed in px (pixels), rem, em or as thin, medium, or wide.
The full range of widths are
none The shortest method is border: 0 . That's a Zero. Useful to use to remove an automatic blue border round an image used as an hyperlink
solid style="border-style: solid;
dotted style="border-style: dotted;
dashed style="border-style: dashed;
double style="border-style: double;
The following Border Styles give a 3D effect. Use the same border colour for all 4 sides for the best effect.
groove style="border-style: groove;
ridge style="border-style: ridge;
inset style="border-style: inset;
outset style="border-style: outset;
This is an HTML a <p> tag
tag with inline CSS That styles with red text, yellow background,
<p style="color: #009; background-color: #ff3; padding: 4px 0 12px 14px; width: 300px; font: 14px Arial, Helvetica, sans-serif; border-top: 3px solid #C90; border-right: 6px solid #FC3; border-bottom: 12px solid #C60; border-left: 24px solid #960; ">This is a <p> HTML tag with inline CSS </p>
<p style=" color: red; background-color: yellow; padding: 4px 0 12px 14px; width: 300px; font: 14px Arial, Helvetica, sans-serif; border-top: 3px solid #C90; border-right: 6px double #FC3; border-bottom: 12px groove #C60; border-left: 24px ridge #960;">
Fonts are the styles of typeface
In CSS you can select the fonts that you would like the browser to display. But and it's a big but those fonts must be available in the computer, tablet or phone for it to be displayed.
If it is not on the device then the device or browser default will be displayed
Remember that users can delete fonts from there device.
The following fonts are regarded as Web Safe Fonts but are not 100% safe
Followed by less safe ones
"" Place double barrel font names in double quotes as above
** Not available on Unix Systems
## Windows uses the very similar font Arial & not Helvetica
To attempt to minimise this problem then it is best to use a font family, a prioritized list of font family names and/or generic family names
where you state the font you would like to be displayed, a backup font or fonts, then as a final backup state wether you prefer a serif font or a sans serif (without serif). cursive, fantasy or monospace. A serif is the small curved bit at a numbers or letters tips.
<p style="font-family: times, serif; "> Sample text formatted with inline CSS. </p>
This technique is rather cumbersome and not covered in this tutorial.
Search Google for How to embed fonts
This site is hosted on Hostgator