What is the method for placing an image beside text in javascript?

I need help creating a Sign In button similar to the one on YouTube, with an image on the left and text on the right.

After attempting the code below, I realized that the image does not display as intended when executed.

How can I successfully position the image to the left of the text?

document.getElementById('profileItem').style.width='150px';
       document.getElementById('profileItem').style.borderRadius='10px';

       document.getElementById('profileItem').style.background='none';
       // document.getElementById('profileItem').style.background='url(images/SignUpBackground.png)';
       // document.getElementById('profileItem').style.backgroundSize='cover';
       // document.getElementById('profileItem').style.backgroundRepeat='no-repeat';
       document.getElementById('profileItem').style.border='1px solid #787CF5';
       // document.getElementById('profileItem').innerHTML="Sign In";
       document.getElementById('profileItem').style.color="#787CF5";
       document.getElementById('profileItem').style.fontSize="20px";
       var btn = document.getElementById("profileItem");
       btn.setAttribute("src","images/defaultProfImg.png")
       var title = document.createElement("h4");
       title.innerHTML = "Sign In";
       btn.appendChild(title);
.profileItem {
  height: 66px;
  width: 66px;
  background-image: url("../images/defaultProfImg.png");
  /* background-color: blue; */
  background-repeat: no-repeat;
  background-size:cover;
  border: 1px solid #383838;
  right: 10px;
  position: fixed;
  border-radius: 40px;
  margin-top: 7px;
  cursor: pointer;
}

.profileItem h4 {
  margin: auto;
  text-align: center;
  vertical-align: middle;
}
<div class="myAccountAndUploadDiv" id="myAccountAndUploadDiv">
        <button class="profileItem" id="profileItem"></button>
      </div>

Answer №1

There are various methods to achieve this goal, but my favorite is utilizing the flex display property.

.myButton {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: .5em 0.75em;
  cursor: pointer;
}

.myButton img {
  width: 24px;
  padding-right: .75em;
}

.myButton .text {
  padding-left: .75em;
}
<button type="button" class="myButton">
  <img role="presentation" src="https://www.iconexperience.com/_img/o_collection_png/green_dark_grey/512x512/plain/user.png" alt="sign in icon">
  <span>Sign In</span>  
</button>

Link to jsFiddle

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

broken alignment of right-aligned Bootstrap navbar

I need help with displaying a navbar on my webpage. I want the right side of the navbar to show different elements based on whether the user is logged in or not. If the user is not logged in, it should display "Login" and "Register," and if they are logged ...

Ajax fails to provide a response

Hey there, I'm trying to save the response from an HTML page and store the content of that page. However, every time I try, I keep getting an error message. What am I doing incorrectly? <script type="text/jscript"> var page = ""; $.aj ...

Utilizing JADE in conjunction with ng-class

I'm having trouble getting this expression to work properly in my JADE nav-bar. It looks correct to me. li(ng-class="{'active' : true}"): a(href='#') INFO SHEET What am I missing here? The class doesn't get applied as expect ...

Unexpected display behavior observed with Material UI tooltips

In my project, I am using material-ui with react. My goal is to change the tooltip (each containing an icon) when a specific condition is met. ToolTipWrapper.js import React from "react"; import { Tooltip } from "@material-ui/core"; import { CheckCircle ...

Organizing Angular Material Styles using Namespacing

In an attempt to develop reusable components with Angular 1.4.3 and Angular-Material 1.0.5, the goal is to seamlessly integrate these components across various applications. However, a challenge arises as the Angular Material CSS contains styling rules th ...

Comparing layout options: Tables vs Lists for user registration forms

I need to create a user registration form with a classic two-column layout, where labels are on the left and fields are on the right. The labels can be displayed in either English or French, resulting in varying label lengths. When it comes to structuring ...

What is the best way to showcase images along with their captions in a horizontal layout

I am facing an issue with displaying the last four images at the end of the page horizontally. When there is no caption, I can use the display:flex property to achieve this layout, but as soon as I add a caption, the images are displayed vertically instead ...

Generate a div dynamically and incorporate a function that triggers on click event dynamically

In my current project, I am faced with a challenge due to laziness. My goal is to automatically generate menu buttons that are linked to different sections of a page using raw JavaScript. The issue arises when the body content of my site is loaded from an ...

Module 'esprima' not found

I am currently working on developing an Abstract Syntax Tree program in JavaScript using Jet brains IDE. I have encountered an issue when running the program, receiving the error message Cannot find module esprima. The settings for nodejs seem to be corr ...

What could be causing my Parse Cloud Code request to be marked as 'unauthorized'?

Today I attempted to execute Parse cloud code from an AngularJS app for the very first time. Upon doing so, I encountered a Parse.Error 'unauthorized' message in my console.log. Despite having initialized Parse in my application and JS keys, I se ...

Dynamic JavaScript Total Calculation

I have been struggling to update the total price for specific options. Even though the code was created by someone else and has worked fine for many users, I can't seem to make it work correctly. Any assistance would be greatly appreciated. Here is t ...

Select the first item that is visible and chosen

Currently, I am working with a select list: <option ng-repeat="hour in Hours" value="{{hour.Value}}" ng-show="filterEvent($index)" ng-selected="hour.Value == EventDate || $first"> {{hour.Text}} </opti ...

Using nested AngularJS functions in Firefox for enhanced functionality

Check out this simple fiddle I've created. It should display an alert when you press enter in the textbox. The functionality works in the latest versions of IE and Chrome, but not in Firefox: fiddle Here's the HTML code: <div ng-app="app" ng ...

What might be causing my Vue unit test to overlook a function?

I am in the process of creating unit tests for my component dateFormat.js using Jest. The focus is on testing the function formatDateGlobal. Here is an excerpt from the test: import DateFormat from '../dateFormat'; describe('dateFormat.js& ...

"Make sure to specify Safari input field as an email and mark

I am experiencing an issue with a contact form in my HTML/PHP code. Everything seems to be working fine, but when using the SAFARI browser, the form fails to validate if I try to submit without filling out all input fields. For example, my form includes: ...

Tips for making a multiselect dropdown menu using bootstrap

I am working on a JavaScript application that parses data and displays it to users in a table generated by JavaScript. I am looking for a simple way to allow users to choose which fields to display in the table using a dropdown list or something similar. I ...

Modal obstructing BsDatePicker

<!-- The Server Modal --> <div class="modal" id="serverModal"> <div class="modal-dialog" style="max-width: 80%;overflow-y: initial !important;" > <div class=" ...

Iterating recursively through a tree structure to update properties using Mongoose

I have a unique structure resembling a tree that I've set up to store comments. Each "comment" acts as a node with a "parent" property linking it to another "comment" node. Additionally, I've included a "replyCount" field on each node to keep tra ...

Learn how to dynamically chain where conditions in Firebase without prior knowledge of how many conditions will be added

Currently, I am working on a project using Angular and firebase. My goal is to develop a function that can take two arguments - a string and an object, then return an Observable containing filtered data based on the key-value pairs in the object for a spe ...

Using 'require' within a nested directive that relies on the parent directive in AngularJS

Implementing a sub directive to be utilized in multiple directives is my current challenge. These parent directives share a common controller that has useful methods for updating scope variables within these directives: (potentially changing controllers ...