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.

More from author

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Related posts

Advertisment

Latest posts

The Data-Driven Revolution: How Data Science Fuels AI in Mobile App Development

I. Introduction Hook: "Ever wondered how your favorite music app knows exactly what you want to hear, or how your shopping app seems to predict...

AI-Powered Code Completion: Boosting Your Web Dev Speed (Beyond the Basics)

Introduction: "Imagine staring at a blank screen, the looming deadline casting a long shadow over your coffee-fueled coding session. You're knee-deep in a complex React...

Exploring AI Frameworks for Web Development: TensorFlow.js and More

Introduction: "The world of web applications is rapidly evolving, with artificial intelligence (AI) becoming an increasingly integral component. From personalized user experiences to automated tasks,...