Home  >  Article  >  Web Front-end  >  css button settings

css button settings

WBOY
WBOYOriginal
2023-05-27 09:23:072620browse

In web design, buttons are a common interactive element. CSS (Cascading Style Sheets) is a language used to describe the style of web pages. Through CSS, we can set the appearance and interactive effects of buttons. In this article, we’ll explore how to style buttons using CSS.

1. Basic button style setting

We can use CSS properties to set the button style. The following is an example:

button {
  background-color: #4CAF50; /* 背景色 */
  border: none; /* 边框大小 */
  color: white; /* 文字颜色 */
  padding: 15px 32px; /* 内边距 */
  text-align: center; /* 文字居中 */
  text-decoration: none; /* 文字下划线 */
  display: inline-block; /* 显示方式 */
  font-size: 16px; /* 字体大小 */
  margin: 4px 2px; /* 外边距 */
  cursor: pointer; /* 鼠标指针样式 */
}

This code sets a basic button style, like the following:

css button settings

2. Further setting of button style

We can use more CSS properties to style buttons. For example:

Hover effect

When the mouse pointer hovers over the button, we can use the :hover pseudo class to set the style of the button . The following is an example:

button:hover {
  background-color: #3e8e41; /* 悬停时背景色 */
}

This code will set the background color of the button to green, which will take effect when the mouse pointer hovers over the button. The result is as shown below:

css button settings

Active effect

When the button is clicked, we can use the :active pseudo-class to set the style of the button. The following is an example:

button:active {
  background-color: #4CAF50; /* 点击时背景色 */
  box-shadow: 0 5px #666; /* 阴影效果 */
  transform: translateY(4px); /* 按钮向下移动 4 像素 */
}

This code will set the background color of the button to green, and add a shadow effect and downward movement animation effect to the button, which will take effect when the button is clicked. The result is as shown below Shown:

css button settings

##Disabled effect

When the button is disabled, we can use

:disabled pseudo Class to style the button. Here is an example:

button:disabled {
  opacity: 0.6; /* 降低按钮的透明度 */
  cursor: not-allowed; /* 禁止鼠标点击 */
}

This code will reduce the transparency of the button and disable mouse clicks. When the button is disabled, it takes effect, and the result is as shown below:

css button settings

The above is the basic method of setting button style using CSS. By using different attributes and pseudo-classes, we can create a variety of button styles and interaction effects to meet various visual and interactive needs.

The above is the detailed content of css button settings. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:html this usageNext article:html this usage