10 Simple CSS Code Examples You Can Learn in 10 Minutes

Posted on

Once you’ve started dabbling in HTML, you’ll probably be interested in adding more power to your web pages. CSS is the best way to do that. CSS lets you apply changes across your entire page without having to use lots of inline HTML styles. computer science computer science computer science computer science computer science

We’ll go over how to create an inline stylesheet so you can practice your CSS skills, and then we’ll move onto 10 simple examples that will show you how to do a few basic things. From there, your imagination is the limit!

Inline Stylesheet

Every HTML document contains a <head> tag. That head section is where your inline CSS stylesheet goes. Here’s what it’ll look like: computer science  computer science computer science computer science computer science

  All of your CSS declarations.

Put that at the top of your document, fill it with your CSS, and you’re set to go.

1. Easy Paragraph Formatting

The cool thing about styling with CSS is that you don’t have to specify a style every time you create an element. You can just say “all paragraphs should have this particular styling” and you’re good to go. Here’s an example of how you might do that. computer science computer science computer science computer science computer science

Let’s say you want every paragraph (that’s everything with a <p> HTML tag) on your page to be slightly larger than usual. And dark grey, instead of black. Here’s how you would do that with CSS: computer science computer science

p {
  font-size: 120%;
  color: dimgray;

That’s all there is to it. Now, whenever the browser renders a <p> paragraph, the text will inherit the size (120 percent of normal) and the color (“dimgray”).

If you’re curious as to which plain-text colors you can use, check out this CSS color listfrom Mozilla.

2. Change Letter Case

Okay, so now that we’ve seen how to make a change to every paragraph, let’s look at how we can be a bit more selective. Let’s create a designation for paragraphs that should be in small caps. Here’s how we’d do that:

p.smallcaps {
  font-variant: small-caps;

To make a paragraph that’s entirely in small caps, we’ll use a slightly different HTML tag. Here’s what it looks like:

<p class="smallcaps">Your paragraph here.</p>

As you can see, adding a dot and a class name to any specific element in CSS specifies a sub-type of that element defined by a class. You can do this with text, images, links, and just about anything else.

If you want to change the case of a set of text to a specific case, you can use these CSS lines:

text-transform: uppercase;
text-transform: lowercase;
text-transform: capitalize;

The last one capitalizes the first letter of every sentence. computer science computer science computer science

3. Change Link Colors

Let’s try changing the style of something other than a full paragraph. There are four different colors a link can be assigned: its standard color, its visited color, its hover color, and its active color (which it displays while you’re clicking on it). Here’s how we might change those: computer science computer science computer science

a:link {
  color: gray;

a:visited {
  color: green;

a:hover {
  color: rebeccapurple;

a:active {
  color: teal;

Note that each “a” is followed by a colon, not a dot. computer science computer science computer science

Each one of those declarations changes the color of a link in a specific context. There’s no need to change the class of a link to get it to change color. It will all be determined by the user and the state of the link.

Prev1 of 4Next

Leave a Reply

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