What is the best way to align text vertically in a Bootstrap 4 button?

I have been searching on Google for an answer to this question, but so far I have not found a solution. I am currently working on a set of page controls and I am struggling to center my text vertically within the buttons.

Below is my current html:

<div class="row input-group align-items-center justify-content-center" data-bind="visible: pageCount > 1">
  <button class="btn btn-sm mx-1" data-bind="click: gotoPage.bind($data, 0)">&laquo;</button>
  <button class="btn btn-sm mx-1" data-bind="click: gotoPage.bind($data, page() - 1)">&lsaquo;</button>
  <div class="ml-1" style="width:4rem">
    <input class="text-right form-control form-control-sm" type="number" data-bind="value: page" />
  </div>
  <span class="mx-1" data-bind="text: ' / ' + (pageCount) "></span>
  <button class="align-middle btn btn-sm mx-1" data-bind="click: gotoPage.bind($data, page() + 1)">&rsaquo;</button>
  <button class="btn btn-sm mx-1" data-bind="click: gotoPage.bind($data, pageCount)">&raquo;</button>
</div>

The data-bind attributes are my knockout bindings, just so you know.

This is how it appears in Chrome:

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

As you can see, the guillemets are slightly below the vertical center of the buttons. I have tried applying various Bootstrap classes like align-items-center, align-middle, text-center, etc. to both the parent div and the buttons, but without success.

Answer №1

There seems to be an issue with the HTML entities containing default spaces. Check out the example below:

.btn span {
 border: 1px solid red;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">

<h3>With Html entity</h3>
  <button role="button" class="btn btn-sm mx-1 d-inline-flex align-items-center" data-bind="click: gotoPage.bind($data, 0)"><span>&laquo;<span></button>


<h3>Without Html entity</h3>
  <button role="button" class="btn btn-sm mx-1 d-inline-flex align-items-center" data-bind="click: gotoPage.bind($data, 0)"><span>A</span></button>
<button role="button" class="btn btn-sm mx-1 d-inline-flex align-items-center" data-bind="click: gotoPage.bind($data, 0)"><span>a</span></button>

Answer №2

I have successfully implemented a snippet using bootstrap-4 cdn and it is displaying perfectly. Here is the code snippet:

<link href="https://use.fontawesome.com/releases/v5.0.11/css/all.css" rel="stylesheet"/>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" />
<div class="row input-group align-items-center justify-content-center">
  <button class="btn btn-sm mx-1"><i class="fas fa-angle-double-left"></i></button>
  <button class="btn btn-sm mx-1"><i class="fas fa-angle-left"></i></button>
  <div class="ml-1" style="width:4rem">
    <input class="text-right form-control form-control-sm" type="number" />
  </div>
  <span class="mx-1"></span>
  <button class="align-middle btn btn-sm mx-1"><i class="fas fa-angle-right"></i></button>
  <button class="btn btn-sm mx-1"><i class="fas fa-angle-double-right"></i></button>
</div>

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

Focus on the image displayed through instafeed.js

Using the Instafeed.js plugin to create a dynamic Instagram feed. Everything is working smoothly, but I'm struggling to center the images being retrieved by Instafeed. Despite trying various methods, I can't seem to get it to display as desired. ...

Create a vibrant stroke and conclude with a circular marker beneath the content with CSS

Can you create a vibrant line with a dot underneath text using CSS, similar to the example shown in the image? The underline should begin with some spacing or padding. .underline { text-decoration: underline; text-underline-offset: 10px; displa ...

The width of the Div is set to 100%, yet there is still some empty space visible on

I am having trouble setting the background of my code to display a picture that is 300px in height and 100% in width. However, when I set the width to 100%, there are about 15px gaps on each side. I could adjust the width to 1000px, but that presents issue ...

The validation for props is incomplete as the parameter 'match.params.id' is not present

I am currently working on a project and following a tutorial as I am still a beginner in this field However, my code is generating an error specifically at console.log(props.match.params.id); The error message reads: 'match.params.id' is missin ...

The dropdown feature in Bootstrap 4 seems to be malfunctioning when used in a project created with create-react-app

Currently, my project is utilizing create-react-app and I am in the process of incorporating a drop-down feature. In order to integrate Bootstrap, I took the CSS file source code, pasted it into an SCSS file, and then used node-sass-chokidar to generate ...

Creating a loop in a column-based carousel without using JavaScript

I'm currently working on developing a carousel using JavaScript or jQuery. I've attempted the code snippet below, but I'm having trouble getting it to display in 3 or 4 columns. While I understand that Bootstrap can handle this easily, I&apo ...

Tips on utilizing commas as text input within an HTML input array

Is there a way to pass a comma in an input field to the server using input arrays? The server currently receives comma-separated input values from an array of inputs, which causes it to consider any commas within the input as separators. For example: HTM ...

How can we show a React JS component when clicked, without using the App.js file for display?

How can I display a component onclick in React JS without using UseState in a file other than App.js? This code will be in App.js var [clicks, setClicks] = useState(false) return( <> {clicks && &l ...

Another option instead of using the .siblings() method would be

Is there an alternative to using the .siblings() method in jQuery for traversing through divs of a specific class in separate container divs? How can this be achieved with the following code: HTML: <div id="container1"> <div id="1"></di ...

Troubleshooting Issue with Absolute Positioning and Hover Effect on Material UI Button

I have a div containing a hidden button and an inner div filled with graphs and text. Occasionally, I need to blur the inner div and make the button float on top in the center of the blurred section, similar to the layout seen on subscription prompts found ...

Is there a way to ensure that the size of the second div box can adjust dynamically in CSS?

Is there a way to make multiple boxes expand dynamically in size when one of them contains more words? Currently, the problem is that only the box with more text expands while the others remain unchanged. I want all the boxes to follow the same size dynami ...

Is the "footer" div automatically nested within the "main" div if they are not nested in the code?

Within my code structure, I currently have the following: <html> <head> ... </head> <body> <div id="body"> <div id="main"> ... </div> <div id="foot ...

Implement a callback function to dynamically generate variables and display them within an HTML element

Apologies in advance for any errors I may make as I am a beginner when it comes to javascript, jquery, and json. I have a script that retrieves data from a json file and displays it on a webpage using javascript, jquery, ajax (I believe), and json. When ...

The Bond Between Django and Bootstrap

In order to create a development road map for my Django project, I have chosen PyCharm Pro as my IDE and Bootstrap Studio as my mock up tool. Interestingly, none of the existing public projects have a calendar that meets my requirements, so I will need to ...

Encountering difficulties with the mobile display of a Wordpress theme

I am currently working with the megashop theme on WordPress and I have a few queries that need addressing: Is there a way to modify a specific setting exclusively for views smaller than desktop size, without impacting the desktop view? For example, the m ...

What is the alternative way to display two tables side by side in HTML without relying on the "style" tag?

For my project, I have created two tables and I am looking for a way to display them side by side without relying on inline styles. Does anyone have any suggestions on how to achieve this? ...

What steps can I take to ensure the browser only shows the page once it has completely loaded?

I find it frustrating when webpages load slowly and you can see the process happening. In my opinion, it would be more satisfying to wait until everything is fully loaded - scripts, images, and all - before displaying the page. This brings up two questio ...

What are the steps to viewing an image or video file in full screen mode on a web browser?

Is there a way to play video/image files in full screen mode on a browser? How can I achieve this? ...

Making the Select Tag function as an on-click event in JQuery

So I currently have a tab set up that functions as on click links when viewed on desktop. However, for tablet and mobile devices, I need it to be transformed into a select drop down list while maintaining the same functionality. Below is the code used for ...

How to ensure the height of an image in a Bootstrap flex row matches the

I just updated to the latest Bootstrap 5.0 beta version and I'm working with this html code snippet: <div> <div class="d-flex flex-row"> <img src="img/funny.png" alt=""> ...