Achieving a vertical center alignment for a button within a panel header using Bootstrap

I'm currently creating an HTML page using Bootstrap 4. I have inserted a button into a panel header:

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

My goal is to center the button (and the title) within the panel head. How can I accomplish this?

<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet" />
<div class="container p-4 ">
  <div class="card">
    <div class="card-header">
      <p class="float-left display-4">Administrator</p>
      <a href="#" class="btn btn-primary float-right" style="align-items: center" routerLink="../list" role="button">
        <i class="fa fa-arrow-left"></i>&nbsp;Indietro</a>
    </div>
    <div class="card-body">
      <form>
        <div class="form-group">
          <label for="email">Email:</label>
          <input type="email" class="form-control" id="email">
        </div>
        <div class="form-group">
          <label for="pwd">Password:</label>
          <input type="password" class="form-control" id="pwd">
        </div>
        <div class="form-group">
          <label for="displayName">Display name:</label>
          <input type="displayName" class="form-control" id="displayName">
        </div>
        <button type="submit" class="btn btn-primary float-right">Salva</button>
      </form>
    </div>
  </div>
</div>

Thank you

Answer №1

Consider implementing display: flex; on your card header, a feature provided by bootstrap. Instead of using float-right, try utilizing ml-auto to position the button to the right (When using flex properties, float is unnecessary). Hopefully, this code snippet proves to be helpful.

<!-- 
  Bootstrap docs: https://getbootstrap.com/docs
-->
<div class="container p-4 ">

  <div class="card">

    <div class="card-header d-flex flex-row align-items-center">
      <p class="float-left display-4">Administrator</p>
      <a href="#" class="btn btn-primary ml-auto" style="align-items: center" routerLink="../list" role="button">

    <i class="fa fa-arrow-left"></i>&nbsp;Indietro</a></div>
    <div class="card-body">
      <form>
        <div class="form-group">
          <label for="email">Email:</label>
          <input type="email" class="form-control" id="email">
        </div>
        <div class="form-group">
          <label for="pwd">Password:</label>
          <input type="password" class="form-control" id="pwd">
        </div>

        <div class="form-group">
          <label for="displayName">Display name:</label>
          <input type="displayName" class="form-control" id="displayName">
        </div>

        <button type="submit" class="btn btn-primary float-right">Salva</button>
      </form>
    </div>
  </div>
</div>

Answer №2

To center your card-header content, simply apply the following CSS properties: display flex and align-items center.

.card-header {
    padding: .75rem 1.25rem;
    margin-bottom: 0;
    background-color: rgba(0,0,0,.03);
    border-bottom: 1px solid rgba(0,0,0,.125);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

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

Expanding the capabilities of the kohana framework with additional pages

As I delve into the world of kohana documentation, I'm facing a bit of confusion. While it's clear that I need to define the index page in controllers like public $template = "index";, I am unsure about how to add other files such as html, php as ...

Positioning a div element directly beneath another div and aligning it to the left

Currently, I am working on a project using ASP.NET MVC 4. In terms of the user interface, particularly in relation to the _Layout, it is a very basic setup that will be consistent across all pages. While utilizing bootstrap 3, the website is primarily desi ...

Utilizing AJAX to dynamically update div content within an overlay

Can someone please help me figure out what is going on with my code. I have 3 links, each with a data-action and an ID number. When I click on the links, nothing happens. It seems like it's not fetching the data from my ajax request (action.php). H ...

Is it possible to adjust the height of sibling divs within a sequence to match the tallest div?

Do you have a set of inline div containers with hardcoded widths but varying content heights? Is there a way to ensure that all divs maintain the same height without risking content overflowing from its parent container? I've experimented with inher ...

Resize background image to perfectly fit within div dimensions

One of my divs has a background image that is getting clipped: <div style='text-align:center;background-image: url(/media/img_1_bg.jpg);background-repeat:no-repeat;width:450px;height:900px;' id="mainpage" align="center"> Is there a soluti ...

Switch Up Your Page Title Instantly Using an Anchor Tag Click

I am looking to use Javascript to change the title of a page when a specific anchor tag is clicked. <a href="index.html" title="Home">Home</a> <a href="about.html" title="About Us">About Us</a> Is it possible to achieve this by ad ...

The issue of the background image not displaying correctly within a Bootstrap container arises

I'm having trouble adding a background image to my website using Bootstrap 4 and external CSS (internal CSS isn't working for me either). I want the background image to be behind all rows in a container, not just behind one specific row. Could ...

What are the steps for implementing the Ionic 2 Pulling Refresher feature in my application?

Hey everyone, I'm currently working on developing a mobile app using AngularJS 2/Ionic2. I am facing an issue with implementing a pull-to-refresh feature in my app. We followed the steps outlined in this link, and while we were able to get the page to ...

Ways to verify if options have been chosen or not?

if(isset($_POST["marketing"])){ foreach($_POST as $key => $value){ if(strlen($value)<1) continue; echo "<option value='".$value."' selected >".$value."</option>"; } I have a PHP script that dynamically cre ...

Align the logo in the center of the navbar with items placed on either side

Looking for a way to center my brand logo within my navbar with menu items positioned around it? Preferably without the use of javascript? I've tried different methods, including adjusting the Bootstrap navbar styling, but haven't found a perfect ...

How to center a position:absolute div inside a position:relative parent div

Regarding: http://jsfiddle.net/WdGdd/3/ I am wondering if it is achievable to center a div with position:absolute within its parent div with position:relative? The goal is to create a button that displays one of two possible values. The width of the butt ...

Having trouble finding errors in Python using Selenium?

I encountered an error while using selenium in conjunction with python: selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/main/article/section/form/div[1]/div[2]/ ...

At 400 pixels screen size, the buttons in the appBar component are spilling out

In my appBar component, I have three buttons that appear fine on large screens. However, when the screen size reaches 400px, they start stretching out of the appBar. Here is how they look on large screens: https://i.sstatic.net/l3nJM.png And this is how ...

Retrieve values from HTML 5 data attributes using PHP

Recently, I created a form that contains <input id="user-id" type="text" data-user-id="" name="message" placeholder="type and send..." class="form-control"> The form includes the data-user-id HTML5 data attribute. I'm looking for a way to acce ...

Are Bootstrap pills, tabs, and dropdowns the perfect combination or a questionable pairing?

While researching Bootstrap documentation, I noticed that pills with dropdowns and pills as tab bars are both mentioned. But I wonder if it's possible to combine the two functionalities. My goal is to create a pill-style navigation bar for different s ...

Unexpected Output: Checkbox returning incorrect value using jQuery and PHP

I'm currently facing an issue where I'm trying to retrieve the value (true or false) of a checkbox and update the database for each row within a while loop. The problem is that the checkboxes only seem to respond to changes based on the first row ...

HTML Multiple Selection to Communicate with an Ajax URL

Imagine we had an HTML multi-select element. How can we construct an ajax request URL using the selected values from the multi-select? Since I am working with YUI dataTable, traditional form serialization may not be viable (and wouldn't be preferable ...

Ensuring elements are correctly positioned

I am struggling to make the images in the following code align horizontally across the top and also need to align the h1's in the same way. I have tried using the vertical-align property, but it seems to behave oddly to me. HTML: <div class=&apos ...

HTML and JavaScript Implementation of a Score Rating System

Creating a review center with input range sliders has been quite an interesting project for me. Each slider ranges from 1 to 10, displaying a corresponding number on the side followed by /10. Utilizing a JavaScript function, I was able to successfully upda ...

Using CSS absolute/relative positioning in a flyout menu: Utilizing specific parent properties (such as left, top, width, vertical/horizontal alignment) while disregarding others

I recently encountered a common issue in CSS involving flyout menus with sub-menus that are shown on hover. When attempting to position the sub-menu directly below the parent list item, I used position:relative; on the parent item and position:absolute;top ...