Design a range of background buttons

I'm currently working on creating a button with a dual background design, where one part features an icon with a fixed width and the other part displays normal text.

My current progress looks like this:

.button {
  position: relative;
  display: inline-block;
  background-color: red;
  height: 50px;
  line-height: 50px;
  padding-left: 30px;
}

span {
  background-color: blue;
  position: absolute;
  left: 0;
  width: 30px;
  text-align: center;
}
<link href="https://use.fontawesome.com/releases/v5.8.2/css/all.css" rel="stylesheet"/>

<div class="button">
  <span>x</span> Test
</div>


<div class="button">
  <span><i class="fas fa-address-card"></i></span> Test
</div>

The issue with my current approach is that a thin 1px border appears at the bottom, making it look unstable. How can I achieve this desired effect in a cleaner manner? Thank you

Answer №1

Adjust the span's height to fill the entire container

.button {
  position: relative;
  display: inline-block;
  background-color: red;
  height: 50px;
  line-height: 50px;
  padding-left: 30px;
}

span {
  background-color: blue;
  position: absolute;
  left: 0;
  height:100%;
  width: 30px;
  text-align: center;
}
<link href="https://use.fontawesome.com/releases/v5.8.2/css/all.css" rel="stylesheet"/>

<div class="button">
  <span>x</span> Test
</div>


<div class="button">
  <span><i class="fas fa-address-card"></i></span> Test
</div>

Answer №2

You have the option to utilize display: grid for positioning the items, enabling both vertical and horizontal centering. Implement a linear-gradient to manage colors effortlessly. This approach is inherently responsive and will adapt seamlessly to any dimensions you assign to height or width within .button.

.button {
  display: grid;
  grid-template-columns: 1fr 1fr;
  align-items: center;
  text-align: center;
  background: linear-gradient(to right, lightblue 50%, lightcoral 50%);
  
  /* styling */
  width: 100px;
  height: 30px;
}
<link href="https://use.fontawesome.com/releases/v5.8.2/css/all.css" rel="stylesheet" />

<div class="button">
  <span>x</span> 
  Test
</div>

<br>

<div class="button">
  <i class="fas fa-address-card"></i>
  Test
</div>

If you desire specific dimensions for the icon-part or label-part, simply modify the grid-template-columns to something like grid-template-columns: 25% 75%; as well as adjust the linear-gradient accordingly (

background: linear-gradient(to right, lightblue 25%, lightcoral 75%)
).

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue with border in a nested table within an HTML table

I am faced with an issue in styling nested tables. My goal is to have borders on both tables, but I specifically need only the bottom border of the inner table without the top, right, and left borders. The default border style can be achieved using the fol ...

Can CSS Grid be substituted for Material UI's grid component?

Exploring the Material-UI Grid Component, I have found that it is built on flexbox and offers great functionality. However, my curiosity lies in comparing it to CSS Grid, which I find equally user-friendly. Despite my preference for CSS Grid, I am hesita ...

What is the most effective way to monitor the duration of video playback?

Whenever a user watches a video, I aim to initiate 2 AJAX calls. The first call should trigger when the user finishes watching the entire video or if the time played is equal to or greater than the duration of the video (considering users may rewind). time ...

css pentagon malfunctioning

Recently, I was assisting a friend in creating a pentagon using CSS - it turned out to be more challenging than I expected. If you take a look at this link, you'll see what I mean. It consists of 5 triangles created with CSS and then rotated and alig ...

Establish the appropriate height for the input field

Having an input class as shown below: <input type="number" class="inv_1"/> I attempted to adjust the height using CSS: .inv_1 { width: 50%; height: 32px; } Expected result: input tag with a height of 32px. Actual result: ...

What is the best way to prevent text-decoration from being applied to icons when utilizing font-awesome inside a hyperlink?

I recently began using FontAwesome and it's been going well. However, I have a question about using it with an anchor tag that has text-decoration:none, and on hover text-decoration:underline. Whenever I hover over the link, the icon also receives the ...

Loading images based on specified search parameters in PHP

As I work on my project, I am faced with the task of loading a specific image from a local folder after the user clicks the search button. The current code that I have in place is: <!-- [SEARCH FORM] --> <form method="post"> <h1> S ...

Adjust the border color of an image when a radio button option is selected

I manage an online shop using WooCommerce that includes a filter for selecting product colors. The available color options are black, blue, and white. When customers choose a color option, only the products in that specific color will be displayed automati ...

Column content merging in BS layout

Currently, I am exploring how to implement a two-column layout with the left column serving as a scrollable navbar. At this early stage of development, please excuse any imperfections in the code! One issue that arose involved using the .sidebar class wit ...

Can content that is already encoded be further Html Encoded?

I am working with ASP.Net 4.0 using MVC 2. I am receiving user-generated content that may or may not already be Html Encoded. I came across an article at , which was informative, but I need a method to ensure the content is encoded without double encoding. ...

The modal is functioning properly on Firefox and Internet Explorer, but it is experiencing issues

Having an issue with my modal not functioning properly in Chrome. When I click on it, only the background fades and both the before and after content load in the Chrome Dev tools simultaneously with no visible content in between. Here is a link to the Cod ...

Creating a horizontal slideshow for multiple divs can be achieved through various methods,

I'm feeling a little lost when it comes to creating a slideshow similar to this image: (Symbian^3 music menu) https://i.sstatic.net/oK0F2.png Can anyone provide me with a straightforward code example or a helpful resource to achieve something simila ...

Is it possible to refresh the chat-box using PHP?

I recently created a chat box application using PHP, but I'm facing an issue with its automatic reload functionality. Is there a way to implement auto-reload in PHP itself, or would it be better to restructure the system to utilize AJAX? Additionally, ...

arrange the css styles in angular

Currently working with Angular and looking to adjust the CSS specificity on the webpage displayed in the link below: https://i.sstatic.net/X3MHW.png The goal is to load 2 before 1. Any suggestions on how to achieve this? ...

Locate the immediate parent element and assign a class to it using jQuery

CSS: <div class="views-row views-row-10 views-row-even views-row-last"> <div class="sonuc"> <span>text1</span> </div> </div> <div class="views-row views-row-11 views-row-even views-row-last"> ...

What changes should I make to my code in order to incorporate an additional level of submenu to my CSS-based dropdown menu?

Hello and thank you for your help, I currently have some CSS code that is working well for 3 levels of dropdown menus, but I am now in need of it to also support a 4th level. When I try to add the extra level by copying the 3rd level code and making adjus ...

Enhance User Experience with a Multi Step Form incorporating a Progress Bar, designed with jQuery and CSS3. Resolve issues with

I downloaded a similar "Multi Step Form with Progress Bar using jQuery and CSS3". I managed to display the fieldsets with divs for additional forms or images, so showing the content in the fieldset is not an issue. However, the problem lies with the actua ...

Ways to adjust the height of an element using the ::before selector in javascript

Within the realm of styling, there exists an element adorned with the .bg class. Its aesthetic instructions are described as follows: .bg { width:100%; } .bg:before { position: absolute; content: ''; background: rgba(0,0,0,0.5) ...

Tips for preventing font distortions caused by transform-rotation

I have implemented the transform-rotation property in CSS to rotate a div and use it as fixed Facebook and Twitter links on my webpage. However, I have noticed that this rotation is distorting my current text font. When I remove the rotation, the text retu ...

Preventing a user with the same name as the one in the table from being added by utilizing a jQuery contains check proved

Check out my fiddle here: http://jsfiddle.net/jd5pgdz2/4/ I encountered an issue when attempting to add a user with the same name as one already in my table (the displayed users are static in the HTML). Despite using 'contains' to check if the u ...