Symbol adjacent to button with multiple lines

My objective is to design a button with multi-line text and an icon positioned centrally next to it for both lines. Refer to the screenshot below for clarification:

https://i.sstatic.net/ODPI2.png

Despite my efforts, I am encountering difficulty in centering the button. The icon consistently appears beside the first line of the button instead of the second.

Below is the code snippet for my button:

.btn {
  position: relative;
  overflow: hidden;
  transform: translate3d(0, 0, 0);
  font-family: "Ubuntu", sans-serif;
  font-weight: 600;
  border: transparent;
  padding: 6px 16px;
  transition: background-color 100ms ease;
  outline: none !important;
  border-radius: 4px;
  line-height: 28px;
  background-color: #FBBC06;
  font-size: 18px;
  color: #022753;
  box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.35);
}

.btn:before {
  font-family: 'Material Icons';
  content: 'shopping_cart';
  font-feature-settings: 'liga';
  font-size: 50px;
}
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<button class="btn">
    <span>von günstigstem Anbieter<br> in den Warenkorb legen</span>
</button>

The current alignment shows that the icon is only centered with the first line in the button, whereas I desire it to be vertically centered between both lines.

I have attempted to remove the span tags, but the outcome remains consistent.

Answer №1

If you want to center align the icon vertically, you can utilize position: absolute. I made some modifications to your CSS code. Please see below:

.btn {
        position: relative;
        overflow: hidden;
        transform: translate3d(0, 0, 0);
        font-family: "Ubuntu", sans-serif;
        font-weight: 600;
        border: transparent;
        padding: 6px 16px 6px 70px;
        transition: background-color 100ms ease;
        outline: none !important;
        border-radius: 4px;
        line-height: 28px;
        background-color: #FBBC06;
        font-size: 18px;
        color: #022753;
        box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.35);
      }

      .btn:before {
        font-family: 'Material Icons';
        content: 'shopping_cart'; 
        font-feature-settings: 'liga'; 
        font-size: 50px; 
        position: absolute; 
        top: 50%; 
        transform: translateY(-50%); 
        width: 50px; 
        left: 16px;
      }
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<button class="btn">
    <span>von günstigstem Anbieter<br> in den Warenkorb legen</span>
</button>

Answer №2

Incorporating the CSS property display: inline-block for .btn>span results in aligning both lines alongside the icon. To achieve vertical centering, you have the option to utilize vertical-align.

.btn>span { display: inline-block }

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

How can I align text to the left within a dropdown menu?

I'm currently working on a simple HTML and CSS dropdown menu. Although I have managed to hide and show it, I am struggling to align the text inside the dropdown to the left. Can anyone help me figure out what I'm doing wrong? Please take a look ...

Do you know of a more streamlined approach to formatting this SCSS code?

Currently working on a Saleor Site and diving into the world of Django and SASS for the first time. While crafting my own styling rules in SCSS files, I've noticed some repetitive code that could potentially be streamlined. It seems like there should ...

Creating an overlay with CSS hover on a separate element and the ::before pseudo class

Issue: I am struggling to display the overlay when hovering over the circle element, rather than just the image itself. I have attempted to achieve this using CSS, but can't seem to make it work as intended. Any guidance and examples using JavaScript ...

Guide to inserting an HTML file into an article's content

After downloading an extension from freefrontedit and uploading it to my server in the directory /accordeon, I successfully accessed it by pointing my browser to . However, I am now faced with the challenge of loading the index.html into my Joomla article ...

How do I view a wildcard in my ID selector using jQuery?

When I want to initially hide various content scattered around the page, I usually use the following jQuery code: $('#objective_details, #time_estimate_details, #team_members_details, #resources_details').hide(); Is there a method to utilize a ...

What could be causing my Bootstrap Carousel to malfunction?

Currently, I am developing a MEAN stack application with Angular 7. In this project, I have incorporated a Bootstrap Carousel, but unfortunately, it seems to be malfunctioning. #slider .item { height: 500px; } .carousel-caption { font-size: 18px; } ...

Flip the direction of this vertical CSS motion blur effect

Is there a way to modify this code so that the object can have a vertical motion blur instead of a horizontal one? I want it to move from top to bottom and then back up. Can anyone assist me with this? http://jsfiddle.net/db8gr4y6/ #outer { posit ...

Interactive image grid with adjustable description field per image upon selection

My goal is to create a grid of images with a single text field below the grid. This text field should display the description of the image that was last clicked. The grid is implemented using floating divs within a main div, as shown in the code snippet be ...

Angular's Motion Module

Incorporating Angular-5 into my project has introduced a plethora of animations on various pages. While the utilization of jQuery provides a straightforward approach for adding and removing classes to DOM elements, it is frequently advised against using jQ ...

When using jQuery, the PHP variable value is not retrieved from the DIV data-id attribute, instead it just displays the variable name

I currently have the following code: <div class= "obutton feature2" data-id=".$bookID."><button>Reserve Book</button></div> <div class="modal-box"></div> Although this button triggers the jQuery script, the modal box ...

Bone structure template - optimizing list spacing with CSS

Currently, I am attempting to float my navigation bar further towards the left by at least 200px, closer to the end of the line visible below. However, every time I add a margin or padding, it causes the elements within the navigation bar to stack on top o ...

Guide on crafting a scrollable and touch-responsive grid using CSS and JavaScript

I am seeking guidance on creating a grid with a variable number of columns and rows. It should be contained within a separate div and should not interfere with other objects or alter the parent's size. The grid needs to be square and I am currently u ...

What is causing .attr('title') to retrieve the title of the element before?

UPDATE Flexslider now includes several callback spaces for use, making this functionality possible. after: function(){}, //Callback: function(slider) - Fires after each slider animation completes Thank you to everyone who contributed! LIVE CO ...

Using :nth-child on elements with the same class name does not produce the desired result

I attempted to utilize the :nth-child selector to target the first, third, and fifth elements with the class name "personal", but it did not work as expected. Is there an alternative method to specifically select these elements sharing the same class? I ha ...

What is the best way to retrieve the values of a select element from LEVEL 4 within the form submission of LEVEL 3?

To enhance readability, the intricate code has been abstracted. Within our Angular 2 project, we are working with a component called <top-component> (LEVEL 1): <top-component> </top-component> This component includes a template known a ...

Showing various divisions based on the selected option from a dropdown menu

My goal is to have forms display below a select tag based on the number selected by the user. I am attempting to achieve this using JQuery, but it's currently not functioning as expected: Select tag:- <label>How many credit cards do you have:* ...

Here is a unique rewrite:"Adjusting the prop of a Material UI Button component depending on screen size breakpoints can be achieved by utilizing

While using the Material UI Button component, I encountered an issue with conditionally determining the variant of the button based on screen size. Specifically, I want the variant to be 'outlined' on medium and larger screens, and no variant at ...

Menu drop down offset in Internet Explorer 7

Having some difficulty with a dropdown menu specifically in IE7. The menu functions correctly in all other browsers, but in IE7 it seems to be misaligned for some reason. Any suggestions or insights would be greatly appreciated. Please refer to the menu co ...

When a button is clicked, the event is not triggered within a FirefoxOS application

Despite functioning perfectly on Firefox for desktop, my application encounters issues when tested on my ZTE Open C (Firefox OS 1.3). Pressing the button does not trigger any action, while onmouseup and onclick events work flawlessly. Even attempts to bin ...

Striving to convert Celsius to Fahrenheit using ReactJS

I am currently attempting to convert Celsius into Fahrenheit within this specific div. Despite being fairly new to React, I’m finding it challenging to determine where exactly to place the conversion calculation. return ( <div className="app& ...