Flexbox button with 3 styles and variations

WHAT TO KNOW - Sep 7 - - Dev Community

<!DOCTYPE html>





Flexbox Buttons: 3 Styles & Variations

<br> body {<br> font-family: sans-serif;<br> margin: 0;<br> padding: 20px;<br> }<br> h1, h2, h3 {<br> margin-bottom: 10px;<br> }<br> .container {<br> display: flex;<br> justify-content: space-around;<br> align-items: center;<br> margin-bottom: 20px;<br> }<br> .button {<br> display: inline-block;<br> padding: 10px 20px;<br> border: none;<br> border-radius: 5px;<br> font-weight: bold;<br> text-decoration: none;<br> color: white;<br> cursor: pointer;<br> transition: background-color 0.3s ease;<br> }<br> .button-primary {<br> background-color: #007bff;<br> }<br> .button-secondary {<br> background-color: #6c757d;<br> }<br> .button-tertiary {<br> background-color: #28a745;<br> }<br> .button:hover {<br> opacity: 0.8;<br> }<br> .code-block {<br> background-color: #f0f0f0;<br> padding: 10px;<br> border-radius: 5px;<br> margin-bottom: 20px;<br> }<br>



Flexbox Buttons: 3 Styles & Variations



Flexbox, a powerful CSS layout module, offers a flexible and efficient way to design and style buttons. This article explores three distinct button styles and their variations, showcasing the versatility of Flexbox in crafting visually appealing and interactive elements.


  1. The Classic Button

A classic button is characterized by its simple structure, often with a solid background color, rounded corners, and a distinct call-to-action text. Let's create a basic button using Flexbox.

HTML

  <button class="button button-primary">
   Click Me
  </button>


CSS


.button {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 10px 20px;
  border: none;
  border-radius: 5px;
  font-weight: bold;
  text-decoration: none;
  color: white;
  cursor: pointer;
  background-color: #007bff; /* Primary color */
  transition: background-color 0.3s ease;
}

.button:hover {
  opacity: 0.8;
}


Primary


Secondary


Tertiary



In this example, we utilize Flexbox to center the text horizontally and vertically within the button. The

justify-content: center

property aligns the text to the center of the button, while

align-items: center

aligns it vertically. We also add hover effects to enhance the visual feedback.


  1. The Gradient Button

Gradient buttons add a touch of visual depth and modern appeal. We can easily incorporate gradients into our Flexbox buttons using CSS.

CSS

.button-gradient {
  background-image: linear-gradient(to right, #007bff, #6200ee);
  /* Customize gradient colors */
}


Gradient



By applying a

linear-gradient

to the button's background, we create a smooth transition between two colors, making the button stand out. Experiment with different gradient directions and color combinations to achieve the desired effect.


  1. The Icon Button

Icon buttons combine visual elements with text, providing a clear indication of their purpose. Flexbox allows us to easily align icons and text within the button.

HTML

  <button class="button button-icon">
   <i class="fas fa-search">
   </i>
   <span>
    Search
   </span>
  </button>


CSS


.button-icon {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 15px;
}

.button-icon i {
  margin-right: 10px;
}





Search




Here, we use

justify-content: space-between

to distribute the icon and text evenly within the button. We also add some spacing between them using

margin-right

. Remember to include the necessary font icons (e.g., Font Awesome) for the icon to appear correctly.



Variations



Flexbox offers further flexibility in customizing button styles. Let's explore some variations:



Rounded Buttons



By adjusting the

border-radius

property, you can create buttons with different levels of roundness.


.button-rounded {
  border-radius: 50px;
}


Rounded



Elevated Buttons



To create a raised button effect, we can use box-shadow.


.button-elevated {
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
}


Elevated



Full-Width Buttons



To make a button occupy the entire width of its container, we can use

width: 100%

.


.button-full-width {
  width: 100%;
}




Full Width








Conclusion





Flexbox provides a powerful and versatile framework for designing button styles. By leveraging its properties, you can create a wide range of buttons that are visually appealing, interactive, and adaptable to different layouts. Remember to use consistent styling and consider accessibility when designing buttons. Experiment with different variations, colors, and effects to create buttons that enhance the user experience of your web application.




. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .