Button Styling in CSS

Página 16

CSS button styling

Buttons are widely used elements in user interfaces, whether in forms, menus or even calls to specific actions. They are one of the most common forms of interaction between the user and the application. Therefore, it is important that they are well styled so that they catch the user's attention and convey the correct message. There are several ways to style buttons in CSS, from simply changing the background color to using transition effects. Let's see some of the main techniques used:

Colors and borders

The most basic way to style a button is by defining colors and borders. You can set the background color, text color, border color, border size, and other properties. For example: ```css button { background-color: #007bff; color: #fff; border: 1px solid #007bff; border-radius: 4px; padding: 10px 20px; fontsize: 16px; } ``` In this example, we're setting the button's background color to blue (#007bff), the text color to white (#fff), a solid 1-pixel border to blue, and a border radius of 4 pixels. We are also setting an inner padding of 10 pixels vertically and 20 pixels horizontally, as well as a font size of 16 pixels.

Transition Effects

Another way to style buttons is through the use of transition effects. These effects can be applied when the user hovers over the button or when the button is clicked. For example: ```css button:hover { background-color: #0056b3; border-color: #0056b3; color: #fff; transition: all 0.3s ease; } button:active { background-color: #003b80; border-color: #003b80; color: #fff; transition: all 0.3s ease; } ``` In this example, we are defining that when the user hovers over the button, the background color and border color will change to a darker shade of blue (#0056b3), and the text color will change to white (#fff). When the button is clicked, the background color and border color will change back to an even darker shade of blue (#003b80), and the text color will remain white. We are also defining that these changes must occur in a 0.3 second transition, using the ease function to make the transition smoother.

Gradients

Another technique often used in button styling is the use of gradients. Gradients are a way to create a smooth transition between two or more colors. For example: ```css button { background: linear-gradient(to bottom, #007bff, #0056b3); color: #fff; border: 1px solid #007bff; border-radius: 4px; padding: 10px 20px; fontsize: 16px; } ``` In this example, we are defining a linear gradient that goes from light blue (#007bff) to a darker shade of blue (#0056b3), in the bottom-to-bottom direction. We're also setting the button's other basic properties, such as the text color, border, and border radius.

Icons

Finally, another way to style buttons is by adding icons. Icons can be added using special fonts (such as Font Awesome) or using images. For example: ```html ``` In this example, we're using Font Awesome to add a search icon to the button. The "fa fa-search" class is responsible for defining the icon, and the text "Search" is the text that will be displayed next to the icon. In summary, styling buttons in CSS is a very simple task, but it can make all the difference in the appearance of your application. With the techniques presented here, it is possible to create attractive and functional buttons that will help improve the user experience.

Now answer the exercise about the content:

_What is one of the most basic ways to style a button in CSS?

You are right! Congratulations, now go to the next page

You missed! Try again.

Next page of the Free Ebook:

17Image styling in CSS

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text