Aligning buttons vertically using Bootstrap 4 and styling them as anchors

I am currently trying to create a layout using Bootstrap 4 with three buttons, one large button on the left and two smaller buttons stacked on top of each other to the right. I am using btn-groups to achieve this layout. My goal is to have the large button on the left occupy the full column height (equal to the sum of the heights of the two smaller buttons) and have all the text centered within the buttons, like so:

<div class="btn-group btn-block">
  <button type="button" class="btn btn-primary p-5 w-50">1</button>
  <div class="btn-group-vertical w-50">
    <button type="button" class="btn btn-secondary p-5">2</button>
    <button type="button" class="btn btn-success p-5">3</button>
  </div>
</div>

However, when I replace the buttons with anchors like so:

<div class="btn-group btn-block">
  <a href="#" role="button" class="btn btn-primary p-5 w-50">1</a>
  <div class="btn-group-vertical w-50">
    <a href="#" role="button" class="btn btn-secondary p-5">2</a>
    <a href="#" role="button" class="btn btn-success p-5">3</a>
  </div>
</div>

The "1" no longer aligns vertically, even though the anchor tag still occupies the correct space. I have tried various tricks to solve this issue, but none have produced the desired result (I could center the left button, but it wouldn't occupy the full column).

What am I missing when transitioning from buttons to anchors? While I could use onclick or other methods to work around this, I am interested in understanding what changes when moving from buttons to anchors.

[EDIT: Although this question was flagged as a duplicate of other vertical-align queries, it specifically addresses the differences encountered when switching from a button to an anchor tag. @Bdeering provided a perfect answer by suggesting that the issue may be related to losing flex behavior when using anchor tags.]

Answer №1

There seems to be an issue with the alignment of the text, but there is a simple solution using custom styles.

To center the text inside each anchor, you can add the following CSS code:

a.btn {
  display: flex;
  align-items: center;
  justify-content: center;
}

(Alternatively, you can use a style tag in your HTML)

<style>
  a.btn {
    display: flex;
    align-items: center;
    justify-content: center;
  }
</style>

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

What could be causing my toggle button to not trigger my responsive menu in JavaScript?

How can I make the navigation menu open when clicking the hamburger menu in the first list item? I tried using JavaScript to target the `nav-list` class in the second list item, but nothing happens on click. Initially, I had the hamburger icon and links i ...

HTML and JQUERY elements transitioned

I am currently working with a pre-existing code that was developed by someone else. The website is built using PHP, HTML, and JQUERY (WordPress with WooCommerce). My task involves inserting tabs into an existing section of the website. However, I am facin ...

Limit pasted content in an Angular contenteditable div

Is there a way to limit the input in a contenteditable div? I am developing my own WYSIWYG editor and want to prevent users from pasting content from external websites and copying styles. I want to achieve the same effect as if the content was pasted into ...

Numerous Divs Expanding to Fit Window Size

I successfully managed to make one div expand to the size of the window, filling the screen completely. However, I am facing an issue where the other divs are overlapping each other and only displaying thing 3. How can I ensure that they do not overlap and ...

Achieve equal heights and vertical alignment for Bootstrap 4 accordion header with floated left and right divs

Building a Bootstrap 4 accordion, I encountered an issue. In the button header, I have two divs with one floating left and the other floating right. The right div contains an image, while the left div contains a title and a subtitle. Everything works perf ...

What is the best way to retrieve a relative HTML table value?

I am facing an issue with getting the value from the second column in an HTML table when a button in the fourth column is clicked. Here is how I am creating the table: function loadTableData() { for (i=0; i < 7; i++) { let row = table.insert ...

Which media type should be designated for a page that includes a combination of PHP, HTML, CSS, and JavaScript/jQuery content, with a .php file extension?

When working with a PHP file that includes HTML, PHP, CSS, and JavaScript, what content type should be set in the header() function? header('Content-type: text/html; charset=utf-8'); I noticed that when I view the source code of the i-frame, th ...

How do I ensure that my absolutely positioned element is centered in this specific scenario?

I've been working on centering an element within its parent container, and I'm having trouble ensuring it remains centered responsively. Here's what I have so far: HTML <div class="example"> <span class="element1">Element ...

Unraveling the enigmatic duality of self-closure with Bootstrap 4

In the small div element, I need to incorporate multiple tooltip functionalities. Furthermore, as this div has an "overflow: auto" attribute, Popper.js automatically ensures that the tooltips remain within the boundaries of the element. In such situation ...

Pop-up box with hyperlink

I successfully integrated multiple modals into my webpage, using a template from w3school that I customized. Now, I am curious to see if it is possible for each modal to have its unique link. For example, when I open Modal 4, the URL would change to myweb ...

Tips for showcasing information stored in a JSON file: Input a value into the field provided, then utilize AngularJS to retrieve and display the corresponding data based on that specific value

https://plnkr.co/edit/iZqalOkrpSnjIzwvGchO?p=preview Is there a way to retrieve the data associated with each serial number entered in the search box? What elements might be missing from the current code? { "SerialNumbers": { "451651": [{ ...

Exploring the implementation of id tags in Bootstrap CSS

Is there a way to make this specific code only apply when the id is set to myCarousel? I am using Bootstrap for this project. I attempted to use the #myCarousel tag, but it didn't have the desired effect. .carousel { margin-top: -20px; height: 6 ...

Why is the combination of the overlay image, logo, and text not producing the desired outcome?

After adjusting the logo's resolution from 3400x1716 to 99x50 and overlapping it with a picture while adding text, I set the image dimensions to 300 height and 100% width to fill out the page. However, when looking at my code, the logo and text that w ...

Choose a specific tuple within an Angular application

Within my Angular application, I am working with a list of schedule objects stored in an array called availableSchedules. These schedules are displayed using ngFor directive. My goal is to implement a functionality where clicking the checkbox on the left ...

Tab panels are shown by Bootstrap 5 tabs

I am using Bootstrap 5 to design a layout that changes based on the width of the screen. When the screen width is greater than or equal to 992 pixels, a table is displayed in the sidebar and some text is shown in the main content area. However, if the scre ...

Disable image responsiveness for a specific id or class

I am in search of a way to eliminate the responsiveness of images within a particular div. I don't have any frameworks, only plain CSS that adjusts image sizes. Below is the code snippet for all image tags: img { max-width: 100%; height: auto; } Ho ...

Position the Close Button within the CSS Cookie Consent Popup in perfect alignment

Having trouble aligning the close X button in my cookie consent popup to the center? I added a margin-top to adjust it, but I'm looking for a better solution. <div class="alert cookiealert" > By using our website you agree to our C ...

Firefox has a bug when inserting an unordered list

I'm currently in the process of creating a WYSIWYG editor and I am focusing on incorporating unordered lists. I am utilizing document.execCommand('insertUnorderedList') for this functionality. Check out the demo below: div[contenteditable ...

Having trouble with video playback in the HTML5 media player on Codeigniter platform

I'm attempting to incorporate a video into my web application using the HTML5 media player within the Codeigniter framework. Below is the code snippet I have been working on: public function view($settings, Screen $screen) { ...

most effective method for recycling dynamic content within Jquery mobile

My jQuery mobile app has a requirement to reuse the same content while keeping track of user selections each time it is displayed. I have successfully created the necessary html content and can append it to the page seamlessly. The process goes something ...