Learning CSS by Styling Buttons Like a Pro

CSS, or Cascading Style Sheets, is the backbone of web design. It allows us to style HTML elements and give life to our webpages. In this post, we’ll walk through a project where we replicate stylish buttons inspired by YouTube and Twitter, diving into fundamental CSS concepts along the way.

Setting Up the Project

To get started:

  1. Create a new file called buttons.html.
  2. Open it in your browser to preview changes as we go.

Below is our initial setup:

<!DOCTYPE html>
<html>
<head>
    <title>CSS Buttons</title>
</head>
<body>
    <button>Subscribe</button>
</body>
</html>

Here’s what the initial button looks like in the browser:

Initial HTML Button
Initial HTML Button

Adding Style with CSS

To style the button, we’ll add a <style> element inside the <head> section of our HTML:

<style>
    button {
        background-color: red;
        color: white;
        border: none;
        height: 36px;
        width: 105px;
        border-radius: 4px;
        cursor: pointer;
    }
</style>

After saving and refreshing, our button transforms:

Styled Subscribe Button
Styled Subscribe Button

Understanding the CSS Code

Here’s a breakdown of the CSS properties we used:

  • background-color: Sets the button’s background color.
  • color: Changes the text color.
  • border: Removes the border.
  • height and width: Define the button’s size.
  • border-radius: Rounds the corners for a sleek look.
  • cursor: Changes the mouse pointer to a hand when hovering over the button.

Adding a Second Button

Now, let’s add another button labeled “Join”:

<button class="subscribe-button">Subscribe</button>
<button class="join-button">Join</button>

We use the class attribute to apply specific styles to each button. Here’s the updated CSS:

<style>
    .subscribe-button {
        background-color: red;
        color: white;
        border: none;
        height: 36px;
        width: 105px;
        border-radius: 4px;
        cursor: pointer;
    }

    .join-button {
        background-color: white;
        color: blue;
        border: 2px solid blue;
        height: 36px;
        width: 105px;
        border-radius: 4px;
        cursor: pointer;
    }
</style>

This results in two buttons styled differently:

Subscribe and Join Buttons
Subscribe and Join Buttons

Using a Color Picker

To fine-tune colors, many code editors (like VS Code) offer built-in color pickers. Hover over a color value to adjust it precisely. For example, switching from red to an RGB value like rgb(200, 0, 0) helps us match the exact YouTube red.


Hover Effects

Let’s add some interactivity. Update the CSS to include hover states:

.subscribe-button:hover {
    background-color: darkred;
}

.join-button:hover {
    background-color: lightblue;
    color: white;
}

Now, hovering over each button gives instant feedback, making the design more engaging:


Conclusion

CSS is all about experimenting and learning through practice. By replicating real-world designs like YouTube and Twitter buttons, you not only understand CSS syntax but also gain the skills to tackle more complex projects. Don’t forget, when you’re stuck, Google is your best friend! Search for properties like “CSS rounded corners” or “CSS button hover” to find solutions quickly.

Let us know in the comments if you enjoyed this lesson and share your styled buttons with us!

watch this tutorial video for better understanding.

Ibad Ur Rahman
Ibad Ur Rahmanhttps://gadgetsfocus.com
Ibad Ur Rahman is a tech enthusiast and the lead editor at GadgetsFocus. With years of experience diving deep into consumer electronics, Ibad specializes in breaking down complex tech specifications into clear, actionable advice. His rigorous approach to aggregating real-world data and testing insights ensures that readers get the unvarnished truth about the latest smartphones, laptops, and smart home gadgets.

More from author

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Related posts

Advertisment

Latest posts

How to Factory Reset a Locked Windows 11 Laptop

The quick fix: To factory reset a Windows 11 laptop without the password, boot to the login screen. Hold down the **Shift** key on...

How to Stop Spam Texts and Calls on iOS 19

The quick fix: The fastest way to stop spam calls on your iPhone is to go to **Settings > Phone > Silence Unknown Callers**...

Samsung Galaxy Ring Finally Launches: Specs, Price, and Should You Buy One?

The quick verdict: The highly anticipated Samsung Galaxy Ring has officially launched, targeting users who want comprehensive sleep and fitness tracking without the bulk...