Menu

Log in

Sign up

From beginner to master of web design, coding, infrastructure operation, business development and marketing

  • COURSES
  • HTML & CSS Introduction
  • HTML & CSS Coding with AI
  • Linux Introduction
  • Docker Basics
  • Git & GitHub Introduction
  • JavaScript Coding with AI
  • Django Introduction
  • AWS Basics
  • Figma Introduction
  • SEO Tutorial for Beginners
  • SEO with AI
  • OTHERS
  • About
  • Terms of Service
  • Privacy Policy

© 2024 D-Libro. All Rights Reserved

HTML & CSS IntroductionChapter 17. CSS: Styling Components

Buttons

Buttons

Customizing Buttons with HTML & CSS

There are several approaches to creating buttons using different tags (<a>, <button>, <input>). Functionalities are different by tag; but by customizing CSS, you can achieve the same style regardless of your tag choices.

1. Buttons with the <a> tag

Using the <a> tag is the simplest approach to creating a button. This approach can be used when you want to create a button to add a link to another page; however, you cannot use this approach for the button used to submit form data. As the <a> tag has an end tag, you can insert an icon into the button design.

Syntax

You can use the syntax below. Add a class for the button styling. You can use any class name. "btn" is often used for the button class name. To open the link in a different tab, you can also add the target="_blank" attribute.

Buttons with the <a> tag - syntax

Styling

To style a button, you need to define many properties:

  • For background and border design
    • background-color
    • border (style, width, color)
    • border-radius
  • For text design
    • color
    • font-size
    • font-family
    • line-height
    • text-decoration)
  • For size, spacing, and layout
    • height
    • width
    • padding
    • margin
    • display (including related properties)
Buttons with the <a> tag - styling

You can decide background-color, border and border-radius flexibly based on your design choice; however, you may need to carefully manage text, size, spacing, and layout-related properties as they are important to organize the button design structure.

Text related property

color, font-size and font-family are straightforward. You can decide based on your design choice; however, you need to pay attention to text-decoration and line-height.

As the <a> tag shows underline by default, usually, you want to set text-decoration: none to remove the underline.

The line-height property is used to adjust the vertical position of the text. We usually set the same height as the height of the button so that you can place the text in the center vertically. When you use Flex Box approach explained below, you don't need to set line-height as you need to adjust text position using the align-content property.

Size, spacing, and layout-related property

As <a> is an inline element, you cannot set its sizes. To set sizes, you need to change the display property. Usually, there are two ways to do it.

Flex Box

You can use Flex Box to style buttons. In this approach, you need to set display: flex at both the parent element and the <a> element. You can also adjust the position of the button using justify-content in the parent element.

To adjust the text position in a button, you can use justify-content and align-items in the <a> element itself.

Here is an example of the code for a button using the Flex Box approach with the <a> tag. To add a hyperlink, you can replace the # part with a URL.

<a> tag button – Flex Box
<!--HTML-->
<div style="display: flex; justify-content: center;">
  <a href="#" class="btn-flex">Click Here!</a>
</div>

<!--CSS-->
<style>
  .btn-flex{
    background-color:#2197A2;
    color: white;
    border-style: none;
    border-radius: 25px;
    width: 200px;
    height: 50px;
    font-size: 1.5rem;
    font-family: roboto, sans-serif;
    text-decoration: none;
    display: flex;
    justify-content: center;
    align-items: center;
  }
</style>

Inline-blok

Using flex: inline-block at the <a> element level is another approach. In this case, you don't need to set the flex property at the parent level but you may need to set text-align to define the position of the button.

For the alignment of the position of the text in a button, you can use text-align for the horizontal position and line-height for the vertical position. To place the text in the center vertically, you can use the same height as the height of the button.

Here is an example of the code for a button using the inline-block approach with the <a> tag.

<a> tag button – Inline-block
<!--HTML-->
<div style="text-align: center;">
  <a href="#" class="btn-inline-block">Click Here!</a>
</div>

<!--CSS-->
<style>
  .btn-inline-block{
    background-color: #2197A2;
    color: white;
    border-style: none;
    border-radius: 25px;
    width: 200px;
    height: 50px;
    font-size: 1.5rem;
    font-family: roboto, sans-serif;
    text-decoration: none;
    display: inline-block;
    text-align: center;
    line-height: 50px;
  }
</style>

2. Buttons with the <button> tag

For the buttons to handle forms, you can use the <button> tag. You can also use the <button> tag to set a hyperlink but this approach is different from the one of the <a> tag.

Syntax

The syntax of the <button> tag is slightly more complicated than that of the <a> tag. To add a hyperlink, you need to use the onclick attribute like in the example below.

Buttons with the <button> tag - syntax

Styling

Unlike the <a> tag, you can set the width and height for the <button> tag; however, using the Flex Box approach may be easier to style buttons with the <button> tag. As there is no default underline in the <button> element, you don't need to set text-decoration: none.

Here is an example of the code for a button that uses the <button> tag approach.

<button> tag button
<!--HTML-->
<div style="display: flex; justify-content: center;">
  <button onclick="location.href=`#`" class="btn">Here!</button>
</div>

<!--CSS-->
<style>
  .btn{
    background-color:#2197A2;
    color: white;
    border-style: none;
    border-radius: 25px;
    width: 200px;
    height: 50px;
    font-size: 1.5rem;
    font-family: roboto, sans-serif;
    display: flex;
    justify-content: center;
    align-items: center;
  }
</style>

3. Buttons with the <input> tag

For the buttons to handle forms, you can also use the <input> tag. As the <input> tag doesn't have the end tag, you cannot nest the <img> element to insert an icon. The <input> tag was commonly used before the <button> tag was introduced; however, as you can achieve the same results using the <button> tag, it is better to use the <button> tag.

Syntax

As there is no end tag, you need to put the text in the button using the value attribute.

Buttons with the <input> tag - syntax

Styling

You can use the same approach to styling as the one used for the <button> element styling.

Practice

Objective:
Create and style buttons with different approaches

1. Create a new HTML file for this chapter

  • Create a copy of the chapter16.html file and change the name to chapter17.html.
  • Change the <title> section to 17. Creating and Styling Components.
  • Also, delete the existing content of the <body> element that was created in the previous chapter.
  • Add the <h1> tag to show the chapter title of this page at the top of the page.
  • The code should look like the one below.
chapter17.html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!--Google Font-->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100;0,300;0,400;0,500;0,700;0,900;1,100;1,300;1,400;1,500;1,700;1,900&display=swap" rel="stylesheet">
    <!--Custom CSS-->
    <link rel="stylesheet" href="css/practice.css">
    <title>17. Creating and Styling Components</title>
  </head>
  <body>
    <h1>Chapter 17. Creating and Styling Components</h1>
  </body>
</html>

2. Update the body section

Add the code below in the <body> section of the HTML file. We are creating seven sets of buttons with different approaches.

  • Using the <a> tag with Flex Box
  • Using the <a> tag with Inline-block
  • Using the <button> tag with Flex Box
  • Using the <input> tag with Flex Box
  • Adjusting colors with a white background
  • Adjusting size to make a smaller button
  • Adjusting shape to create a previous button and a next button
chapter17.html
<h2 style="text-align: center;">Buttons</h2>
<p style="text-align: center;">The a tag with Flex Box</p>
<div style="display: flex; justify-content: center; padding-bottom: 20px;">
  <a href="main-page.html" class="btn-flex">Button 1</a>
</div>
<p style="text-align: center;">The a tag with Inline-block</p>
<div style="text-align: center; padding-bottom: 20px;">
  <a href="main-page.html" class="btn-inline">Button 2</a>
</div>
<p style="text-align: center;">The button tag with Flex Box</p>
<div style="display: flex; justify-content: center; padding-bottom: 20px;">
  <button onclick="location.href=`main-page.html`" class="btn-flex">Button 3</button>
</div>
<p style="text-align: center;">The input tag with Flex Box</p>
<div style="display: flex; justify-content: center; padding-bottom: 20px;">
  <input type="button" onclick="location.href=`main-page.html`" value="Button 4" class="btn-flex">
</div>
<p style="text-align: center;">White background</p>
<div style="display: flex; justify-content: center; padding-bottom: 20px;">
  <a href="main-page.html" class="btn-white-bg">Button 5</a>
</div>
<p style="text-align: center;">Small size</p>
<div style="display: flex; justify-content: center; padding-bottom: 20px;">
  <a href="main-page.html" class="btn-small">Button 6</a>
</div>
<p style="text-align: center;">Custom shapes</p>
<div style="display: flex; justify-content: center; padding-bottom: 20px;">
  <div class="btn-group">
    <a class="btn-previous">←</a>
    <a class="btn-next">→</a>
  </div>
</div>
<hr>

3. Create a new CSS file for component styling and add code

Create a component.css file and add new code for styling the buttons.

component.css
/* Chapter 17. CSS: Styling Components */

/* Buttons */
.btn-flex{
  background-color:#2197A2;
  color: white;
  border-style: none;
  border-radius: 25px;
  width: 200px;
  height: 50px;
  font-size: 1.5rem;
  font-family: roboto, sans-serif;
  text-decoration: none;
  display: flex;
  justify-content: center;
  align-items: center;
}

.btn-inline{
  background-color:#2197A2;
  color: white;
  border-style: none;
  border-radius: 25px;
  width: 200px;
  height: 50px;
  font-size: 1.5rem;
  font-family: roboto, sans-serif;
  text-decoration: none;
  display: inline-block;
  text-align: center;
  line-height: 50px;
}

.btn-white-bg{
  background-color:white;
  color: #2197A2;
  border: solid 2px #2197A2;
  border-radius: 25px;
  width: 200px;
  height: 50px;
  font-size: 1.5rem;
  font-family: roboto, sans-serif;
  text-decoration: none;
  display: flex;
  justify-content: center;
  align-items: center;
}

.btn-small{
  background-color:#2197A2;
  color: white;
  border-style: none;
  border-radius: 15px;
  width: 150px;
  height: 30px;
  font-size: 1rem;
  font-family: roboto, sans-serif;
  text-decoration: none;
  display: flex;
  justify-content: center;
  align-items: center;
}

.btn-group{
  display: flex;
  justify-content: center;
  list-style: none;
  gap: 5px;
}

.btn-previous{
  background-color:#2197A2;
  color: white;
  border-style: none;
  width: 60px;
  height: 25px;
  font-size: 1rem;
  font-family: roboto, sans-serif;
  border-radius: 25px 0px 0px 25px;
  text-decoration: none;
  text-align: center;
  line-height: 25px;
}

.btn-next{
  background-color:#2197A2;
  color: white;
  border-style: none;
  width: 60px;
  height: 25px;
  font-size: 1rem;
  font-family: roboto, sans-serif;
  border-radius: 0px 25px 25px 0px;
  text-decoration: none;
  text-align: center;
  line-height: 25px;
}

4. Check the result with a browser

Open chapter17.html with a browser. You can see various button designs.

You can also check the sample result here (Demo Site).

link


You can also learn this topic offline. Click AmazonKindle.

More Topics to Explore

How To Use Figma Prototype Features

How To Use Figma Prototype Features

Position Property in CSS: Position Absolute and Relative

Position Property in CSS: Position Absolute and Relative

Understanding CSS Syntax: Selectors and Declarations

CSS Syntax

Optimizing Line Height and Letter Spacing for Readability

line-height and letter-spacing

How To Use Figma Prototype Features

How To Use Figma Prototype Features

Position Property in CSS: Position Absolute and Relative

Position Property in CSS: Position Absolute and Relative

Understanding CSS Syntax: Selectors and Declarations

CSS Syntax

Optimizing Line Height and Letter Spacing for Readability

line-height and letter-spacing

Tags:

Button

Component

HTML & CSS Introduction
Course Content

Chapter 1. Overview of Website Development

How Websites Work?

Designing, Building and Publishing Websites

Designing Websites

Building Websites – Frontend and Backend Coding

Web App vs. Website

Frontend Coding

Web Framework and CMS

Publishing Websites (Hosting Services)

Chapter 2. Preparing for Website Coding

Two Key Tools to Start Coding Websites

How Browsers Display Web Pages

File Name Rules

Website Directory Structure

Absolute Path vs. Relative Path

Chapter 3. HTML Basics

What Is HTML?

HTML Element

Attribute

HTML Document Structure

Layout Semantics

Heading and Paragraph Tag

Text-Level Semantics

Space, Line-break and Tag in Content

Comments in HTML Document

Chapter 4. HTML: Add Links and Images

Overview of Adding Links and Images

Embed Images – <img>

Image File Format

Add Hyperlinks – <a>

Add Hyperlinks to Images

Add Hyperlinks to Specific Location on Web Page

Link Tag – <link>

Script Tag – <script>

Chapter 5. HTML: Create Lists and Tables

Create Lists

Create Tables

Combine Table Cells

Chapter 6. HTML: Create Forms

Create Forms

Create Text Input Forms and Submit Button

Radio Button and Checkbox

Select Box

Labels

Auto Complete and Disabled

Chapter 7. Bridging HTML and CSS

Block Element vs. Inline Element

Nesting Elements – Parent Elements and Child Elements

Div vs. Span

Global Attribute – Class, ID and Style

Accordion – <Details> and <Summary>

Chapter 8. CSS Basics

What Is CSS?

CSS Syntax

Where To Type CSS?

CSS Basic Selectors

Descendant Selector

Inheritance

Specificity

Reset CSS

Browser Developer Tools for CSS

Comments in CSS Document

Chapter 9. Web Design Basics

Design Element Representation in HTML and CSS

Key Design Points by CSS

Length

Color Code – HEX and RGB

Color Theme

Chapter 10. CSS: Sizing and Spacing

CSS Box Model

width and height

padding

margin

Margin and Padding for Specific Side

margin: auto

box-sizing

Chapter 11. CSS: Styling Text and Images

Text Styling Properties

font-size

color

font-family

Web Font and Google Font

font-weight and font-style

text-decoration

line-height and letter-spacing

text-align

vertical-align

Styling Images

float: left and right

Chapter 12. CSS: Styling Backgrounds

background-color

background-image

background-size

background-repeat

background-position

background-attachment

background (Multiple Properties)

Chapter 13. CSS: Styling Borders and Drawing Lines

border-style

border-color

border-width

border-radius

Border (Multiple Properties)

Borders on Specific Side

Border Radius on Specific Side

Chapter 14. CSS: Layout – Key Concepts and Display Property

Layout Before and After

Layout Key Design Points

Display Property

inline, block and inline-block

display: none

Chapter 15. CSS: Layout – Flex Box

Flex Box – display: flex

flex-direction

Main Axis and Cross Axis

flex-wrap

justify-content

align-items

align-content

align-self

flex-grow

flex-shrink

flex-basis

margin: auto with Flex Box

Inline Flex Box

Nested Flex Box

Chapter 16. CSS: Styling Lists

List Styling Properties

list-style-type

list-style-image

list-style-position

Chapter 17. Creating and Styling Components

Components and Layout

Buttons

Cards

Top Bar

Footer and Bottom Bar

Chapter 18. Completing Website Development

Website Structure Design

Live Server

Home (Landing) Page Development

Main (List) Page Development

Content (Detail) Page Development

Chapter 19. Publishing Websites

Key Steps to Publish Websites

Domain and DNS Server

Favicon

GitHub Pages

Chapter 20. Supplemental Topics

Horizontal Rule – <hr>

Line Break – <br>

Reserved Characters and HTML Entities

Non-breaking Space – &nbsp;

Chapter 1. Overview of Website Development

How Websites Work?

Designing, Building and Publishing Websites

Designing Websites

Building Websites – Frontend and Backend Coding

Web App vs. Website

Frontend Coding

Web Framework and CMS

Publishing Websites (Hosting Services)

Chapter 2. Preparing for Website Coding

Two Key Tools to Start Coding Websites

How Browsers Display Web Pages

File Name Rules

Website Directory Structure

Absolute Path vs. Relative Path

Chapter 3. HTML Basics

What Is HTML?

HTML Element

Attribute

HTML Document Structure

Layout Semantics

Heading and Paragraph Tag

Text-Level Semantics

Space, Line-break and Tag in Content

Comments in HTML Document

Chapter 4. HTML: Add Links and Images

Overview of Adding Links and Images

Embed Images – <img>

Image File Format

Add Hyperlinks – <a>

Add Hyperlinks to Images

Add Hyperlinks to Specific Location on Web Page

Link Tag – <link>

Script Tag – <script>

Chapter 5. HTML: Create Lists and Tables

Create Lists

Create Tables

Combine Table Cells

Chapter 6. HTML: Create Forms

Create Forms

Create Text Input Forms and Submit Button

Radio Button and Checkbox

Select Box

Labels

Auto Complete and Disabled

Chapter 7. Bridging HTML and CSS

Block Element vs. Inline Element

Nesting Elements – Parent Elements and Child Elements

Div vs. Span

Global Attribute – Class, ID and Style

Accordion – <Details> and <Summary>

Chapter 8. CSS Basics

What Is CSS?

CSS Syntax

Where To Type CSS?

CSS Basic Selectors

Descendant Selector

Inheritance

Specificity

Reset CSS

Browser Developer Tools for CSS

Comments in CSS Document

Chapter 9. Web Design Basics

Design Element Representation in HTML and CSS

Key Design Points by CSS

Length

Color Code – HEX and RGB

Color Theme

Chapter 10. CSS: Sizing and Spacing

CSS Box Model

width and height

padding

margin

Margin and Padding for Specific Side

margin: auto

box-sizing

Chapter 11. CSS: Styling Text and Images

Text Styling Properties

font-size

color

font-family

Web Font and Google Font

font-weight and font-style

text-decoration

line-height and letter-spacing

text-align

vertical-align

Styling Images

float: left and right

Chapter 12. CSS: Styling Backgrounds

background-color

background-image

background-size

background-repeat

background-position

background-attachment

background (Multiple Properties)

Chapter 13. CSS: Styling Borders and Drawing Lines

border-style

border-color

border-width

border-radius

Border (Multiple Properties)

Borders on Specific Side

Border Radius on Specific Side

Chapter 14. CSS: Layout – Key Concepts and Display Property

Layout Before and After

Layout Key Design Points

Display Property

inline, block and inline-block

display: none

Chapter 15. CSS: Layout – Flex Box

Flex Box – display: flex

flex-direction

Main Axis and Cross Axis

flex-wrap

justify-content

align-items

align-content

align-self

flex-grow

flex-shrink

flex-basis

margin: auto with Flex Box

Inline Flex Box

Nested Flex Box

Chapter 16. CSS: Styling Lists

List Styling Properties

list-style-type

list-style-image

list-style-position

Chapter 17. Creating and Styling Components

Components and Layout

Buttons

Cards

Top Bar

Footer and Bottom Bar

Chapter 18. Completing Website Development

Website Structure Design

Live Server

Home (Landing) Page Development

Main (List) Page Development

Content (Detail) Page Development

Chapter 19. Publishing Websites

Key Steps to Publish Websites

Domain and DNS Server

Favicon

GitHub Pages

Chapter 20. Supplemental Topics

Horizontal Rule – <hr>

Line Break – <br>

Reserved Characters and HTML Entities

Non-breaking Space – &nbsp;