Basics of CSS

codemonkey

Code monkey not crazy, just proud.
Reaction score
66
Part I - Foundation

Want to learn the basics of CSS? Well you have come to the right place.

CSS is a language structured to make HTML easier to control and more powerful.

Usage

To use CSS you can use an external file or embed it straight into an HTML file.

External
Pros: Easy to manipulate lots of pages
Cons: Requires ability to make .css files

To use an external file you need to create a new file, let's call it stylesheet.css in the same directory as your HTML file. All the CSS code should go in stylesheet.css.

In the HTML file you need to put the following code below the <head> tag.

Code:
<link rel="stylesheet" href="stylesheet.css" type="text/css" />

Put that on every page you want the CSS to affect.

Internal
Pros: Embedded directly into the HTML file.
Cons: Can only affect one page, to affect multiple you have to copy and paste code into each page.

To use CSS inside the HTML document you need to go to the <head> section of your HTML document and right below it add:

Code:
<style type="text/css">
</style>

In between the <style> tag is where your CSS code goes.

CSS Comments

To make a comment you use /* */ like this:

Code:
/* This is a comment in CSS, it does not affect anything in the page */

Modifying Tags

Now we come to some practical stuff, modifying tags.

Code:
p
{
color: red;
}

What this does is make the content of every <p> tag red.

p - defines the tag used (you can do this with any tag, except <a> which is special and we will get to later)
{ - declares the starting of attributes
color - declares that the font color of the <p> tag is going to be modified (this is called an CSS attribute)
: - shows that the attribute is over, and the value is the next thing coming up
red - the value for the "color" attribute
; - ends the statement

Common Attributes

Here's a list of some common attributes:
color - changes the font color
text-align - changes the alignment of the text (center, left, right)
font-size - changes the font size
font-family - changes the font type (Arial, Tahoma, etc.)
font-weight - changes the amount of boldness (bold, bolder, lighter)
background-color - changes the background color
font-style - changes the font style (italic, normal, oblique)

Measurement Attributes

When declaring the value of an attribute that involves measurement (font-size, width, height) you have to include a unit UNLESS the value is 0.

Common Units are px (pixel) and cm (centimeter).

Example Usage:
Code:
b /* Bold tag */
{
font-size: 24px; /* notice there is NO SPACE between the number and the unit */
}

Multi-Value Attributes

Some CSS attributes require more than one value.

For example:

Code:
ul /* unordered list element */
{
border: 1px solid black;
color: red;
}

As seen in the above, each value is separated by a space.
1px - Thickness of border
solid - Type of border (solid, dotted, ridge, inset, outset, groove, dashed, double)
black - Color of border

Borders

Borders are very unique when it comes to CSS besides having a multi-value attribute.

With borders you can specify a border on only certain sides, for example:

Code:
p
{
border-top: 1px solid black;
border-right: 1px solid black;
border-left: 1px solid black;
}

That adds a border side to each side except the bottom. You could set the values to be different to have a multi-colored border, or something like that.

Here's the easier and shorter way though to do the same exact thing as the previous example.

Code:
p
{
border: 1px solid black;
border-bottom: none;
}

All this does is make a border on all sides, then removes the border on the bottom to produce the same effect as before, however you save a couple bytes of space.

Part II - Classes and IDs
Classes and IDs make it possible to use CSS to change something, without changing all of a tag.

Classes

Code:
.CoolRedClass /* The period, ".", before "CoolRedText" indicates that it is a class you are defining, not putting a tag name  */
{
color: red;
font-style: italic;
}

Code:
<div class="CoolRedClass">This text is red (and italic)</div>

<p class="CoolRedClass">This text is also red (and italic)</p>

<div>This text is not red or italic</div>

<p>This text is not red or italic</p>

The CSS only affects tags that have the attribute class="class", not all tags.

Classes should be used when the class will be used more than one time, example:

Code:
<div class="myclass">Ahoy world!</div>
<div class="myclass">wuts up?</div>

However, it's not necessary for a class to be used more than once.

Identifiers (IDs)

IDs are practically identical to Classes, except they have a different method of usage, and you can only use an ID once.

Code:
#CoolRedID /* The number sign, "#", before "CoolRedID" indicates that it is a ID you are defining, not putting a tag name  */
{
color: red;
font-style: italic;
}

Code:
<p id="CoolRedID">This text is red and italic.</p>

Remember: Only use IDs ONCE.

You might be wondering why you would ever want to use an ID instead of a class, and that is because of JavaScript; I won't go into the details but it is good practice.

Tag Specific Classes

If you want to make a Class only be able to affect one tag, or have different effects on different tags so that this would happen:

Code:
<p class="myclass">This text is red</p>
<div class="myclass">This text is blue, yet it has the same class as the previous</div>

You can do this with Tag Specific Classes.

Here's the code for it:

Code:
p.myclass /* p is the tag and myclass is the class */
{
color: red;
}

div.myclass
{
color: blue;
}

___

This concludes part two of Basics of CSS.

Part III - Links and Events

In this part we learn how to modify links and make dynamic content with hover event.

Modifying Links Attributes

Links are a bit different to modify then other tags.

To modify a link you need to use this code:

Code:
a:link
{
/*All CSS attributes here */
}


/*for a class use:*/

a.myclass:link
{
/* attributes here */
}

And if you want to change how it looks when visited or when it's hovered upon:

Code:
a:link
{
color: white;
}

a:visited
{
color: gray;
}

a:hover
{
color: blue;
}

You can also use :hover with other tags besides <a>, for example:

Code:
p:hover
{
background-color: yellow;
}

That gives a highlighting effect when you scroll over the contents of a <p> tag.

___

This concludes part three of the Basics of CSS.

Part IV - Misc.

Asterisk
The asterisk (*) can be used to fill in CSS attributes for all tags that don't already have them specified.

For example:

Code:
*
{
color: red;
}

div
{
color: blue;
}

Code:
<p>This text is red</p>
<div>This text is blue</p>

Multiple Classes

You can use multiple classes on an element by seperating them with a space, for example:
Code:
.redcolor
{
color: red;
}

.bluebackground
{
background-color: blue;
}

Code:
<div class="bluebackground redcolor">This has a blue background and is red.</div>

Quick CSS

There is a third way of using CSS directly in the HTML, for those who want CSS to effect only one specific instance of a tag.

Here's how it works:

Code:
<div style="color: red; /*other css properties*/">This text is red</div>
<div>This text is not red</div>

As you can see it only affects one tag.

Attribute and Value List

You can find a great list with all attributes and values here.

___

This concludes the Basics of CSS tutorial by me, thanks for reading!

You may not redistribute this tutorial without my permission.
 

UndeadDragon

Super Moderator
Reaction score
447
I think you should also add that you can assign the child of a parent elements' style, so you don't have to give everything classes and ids. Example:

Code:
<html>
<head>
<title>CSS Test</title>
<style type="text/css">
#container
{
border: 1px;
}
#container p
{
color: #f00;
}
</style>
</head>
<body>
<div id="container">
   <p>This is red text</p>
</div>
</body>
</html>
 

codemonkey

Code monkey not crazy, just proud.
Reaction score
66
I think I'll make a more advanced tutorial with that, and link to that at the end of this one.

It would also have advanced selectors and some CSS3 thrown in.
 

JerseyFoo

1/g = g-1
Reaction score
40
#container p ~ any paragraph within element of id

#container > p ~ child paragraph of element of id

But old IE doesn't do it right. Writing Efficient CSS

And you can't have a tutorial on CSS without addressing id-spec, come on.
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • Varine Varine:
    How can you tell the difference between real traffic and indexing or AI generation bots?
  • The Helper The Helper:
    The bots will show up as users online in the forum software but they do not show up in my stats tracking. I am sure there are bots in the stats but the way alot of the bots treat the site do not show up on the stats
  • Varine Varine:
    I want to build a filtration system for my 3d printer, and that shit is so much more complicated than I thought it would be
  • Varine Varine:
    Apparently ABS emits styrene particulates which can be like .2 micrometers, which idk if the VOC detectors I have can even catch that
  • Varine Varine:
    Anyway I need to get some of those sensors and two air pressure sensors installed before an after the filters, which I need to figure out how to calculate the necessary pressure for and I have yet to find anything that tells me how to actually do that, just the cfm ratings
  • Varine Varine:
    And then I have to set up an arduino board to read those sensors, which I also don't know very much about but I have a whole bunch of crash course things for that
  • Varine Varine:
    These sensors are also a lot more than I thought they would be. Like 5 to 10 each, idk why but I assumed they would be like 2 dollars
  • Varine Varine:
    Another issue I'm learning is that a lot of the air quality sensors don't work at very high ambient temperatures. I'm planning on heating this enclosure to like 60C or so, and that's the upper limit of their functionality
  • Varine Varine:
    Although I don't know if I need to actually actively heat it or just let the plate and hotend bring the ambient temp to whatever it will, but even then I need to figure out an exfiltration for hot air. I think I kind of know what to do but it's still fucking confusing
  • The Helper The Helper:
    Maybe you could find some of that information from AC tech - like how they detect freon and such
  • Varine Varine:
    That's mostly what I've been looking at
  • Varine Varine:
    I don't think I'm dealing with quite the same pressures though, at the very least its a significantly smaller system. For the time being I'm just going to put together a quick scrubby box though and hope it works good enough to not make my house toxic
  • Varine Varine:
    I mean I don't use this enough to pose any significant danger I don't think, but I would still rather not be throwing styrene all over the air
  • The Helper The Helper:
    New dessert added to recipes Southern Pecan Praline Cake https://www.thehelper.net/threads/recipe-southern-pecan-praline-cake.193555/
  • The Helper The Helper:
    Another bot invasion 493 members online most of them bots that do not show up on stats
  • Varine Varine:
    I'm looking at a solid 378 guests, but 3 members. Of which two are me and VSNES. The third is unlisted, which makes me think its a ghost.
    +1
  • The Helper The Helper:
    Some members choose invisibility mode
    +1
  • The Helper The Helper:
    I bitch about Xenforo sometimes but it really is full featured you just have to really know what you are doing to get the most out of it.
  • The Helper The Helper:
    It is just not easy to fix styles and customize but it definitely can be done
  • The Helper The Helper:
    I do know this - xenforo dropped the ball by not keeping the vbulletin reputation comments as a feature. The loss of the Reputation comments data when we switched to Xenforo really was the death knell for the site when it came to all the users that left. I know I missed it so much and I got way less interested in the site when that feature was gone and I run the site.
  • Blackveiled Blackveiled:
    People love rep, lol
    +1
  • The Helper The Helper:
    The recipe today is Sloppy Joe Casserole - one of my faves LOL https://www.thehelper.net/threads/sloppy-joe-casserole-with-manwich.193585/
  • The Helper The Helper:
    Decided to put up a healthier type recipe to mix it up - Honey Garlic Shrimp Stir-Fry https://www.thehelper.net/threads/recipe-honey-garlic-shrimp-stir-fry.193595/

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top