Aligning text vertically within Bootstrap 4 divs, specifically those containing the btn class, without specifying a fixed height, while utilizing CSS Grid instead of flexbox

I'm working on a project with a CSS Grid containing Bootstrap 4 divs with btn tags. The issue I'm facing is that the divs are stretching vertically, and I need to vertically align the text in the middle.

While I know I could use flexbox for this, it's not an option in this particular project.

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

For reference, here is an example: https://jsfiddle.net/fredhors/0sw2uLay/5/

.grid-wrapper {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(20vmax, 1fr));
  grid-gap: 0.5rem;
  height: 100vh;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="grid-wrapper">
  <div class="btn btn-primary">Content A</div>
  <div class="btn btn-warning">Content B</div>
  <div class="btn btn-danger">Content C</div>
  <div class="btn btn-success">Content D</div>
</div>

Answer №1

If you need to centrally align the entire btn, simply apply align-items: center on the grid-wrapper - check out the demo below:

.grid-wrapper {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(20vmax, 1fr));
  grid-gap: 0.5rem;
  height: 100vh;
  align-items: center; /* vertically centered */
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="grid-wrapper">
  <div class="btn btn-primary">Content A</div>
  <div class="btn btn-warning">Content B</div>
  <div class="btn btn-danger">Content C</div>
  <div class="btn btn-success">Content D</div>
</div>

If you wish to stretch the background and achieve vertical alignment within the btn, consider using flexbox for the btn - refer to the demo below:

.grid-wrapper {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(20vmax, 1fr));
  grid-gap: 0.5rem;
  height: 100vh;
}

.grid-wrapper > .btn {
  display: flex;
  align-items: center;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="grid-wrapper">
  <div class="btn btn-primary">Content A</div>
  <div class="btn btn-warning">Content B</div>
  <div class="btn btn-danger">Content C</div>
  <div class="btn btn-success">Content D</div>
</div>


I understand that I could use flexbox but unfortunately that is not an option for this project.

If utilizing flexbox is not feasible, consider employing a pseudo element to center the text in the grid items - have a look at the demo below:

.grid-wrapper {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(20vmax, 1fr));
  grid-gap: 0.5rem;
  height: 100vh;
}

.grid-wrapper .btn:after {
  display: inline-block;
  vertical-align: middle;
  content: '';
  height: 100%;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="grid-wrapper">
  <div class="btn btn-primary">Content A</div>
  <div class="btn btn-warning">Content B</div>
  <div class="btn btn-danger">Content C</div>
  <div class="btn btn-success">Content D</div>
</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

Changing the bootstrap popover location

I'm looking to customize the position of a Bootstrap popover that appears outside of a panel. Here's my setup: HTML: <div class="panel"> <div class="panel-body"> <input type="text" id="text_input" data-toggle="popover ...

Having trouble mastering the art of Mobile First Responsive Design

I'm encountering some challenges with implementing a "mobile first" design strategy. After reading multiple articles and the Mobile First (2011) book, it seems like the process involves creating the mobile version of a website before developing the de ...

Struggling with aligning two divs side by side on an HTML page

Recently, I've been experimenting with Electron and attempting to create 2 divs side by side. Despite trying various alignment options found here, nothing seems to be working for me. Here's the code I have so far: Code body, html { hei ...

Preventing links from moving when an icon appears on hover: A simple guide

I'm currently working on adding a paw icon next to a link when it's hovered over. I want the icon to be initially invisible and only appear when the link is hovered over. The issue I'm facing is that when I hover over a link, the other links ...

Tips for implementing a method to switch CSS properties of a main container by using a checkbox within its child element in a Svelte component

It took me a while to figure this out, but I still feel like my implementation is not ideal. I'm confused as to why things break when I remove the checkedActivities.has(activity) ? "checked" : "unchecked", because I thought TypeScr ...

Swapping out an external CSS library file for a locally-saved alternative

Currently, I am importing a CSS library directly into my main CSS file like this: @import url(https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.8.2/css/all.min.css); However, I would like to store it locally in my project and import it from its folde ...

Aligning icons side by side using only CSS styling

I'm having trouble positioning icons next to each other horizontally and they keep stacking on top of one another. Can you please review my CSS code to see if I made a mistake? Here is the CSS: #social { top:20px; left:30px; height:32px; width:200p ...

Issues arising with VueJS array props when integrating Bootstrap

I am attempting to display two divs next to each other while iterating through props (which are essentially an array) in VueJS. When I have just a single element, everything functions properly. However, as soon as I include the v-for directive, the element ...

What is the purpose of using multiple css classes together?

In my project, I have implemented CSS modules which allow me to use multiple classes. The variable open is a boolean that determines whether or not the Paper should shift. <Paper className={`${classes.paper} ${open && classes.paperShift}`}> Questio ...

How does setting margin: 0 auto horizontally center text on an image when no width is defined?

While trying to horizontally center text on a hero image, I used margin: 0 auto without specifying a width. Surprisingly, this trick worked even though many sources claim it only works with a specified width. My theory is that the default image width may b ...

Tips on Utilizing If/Else Conditions in HTML Table TD_TAGS

Can someone please explain how to implement an if/else condition within this <td> tag? <td data-label="Status">{{$t('Status'+item.cStatus)}}</td> This is what I am trying to achieve: if(item.cStatus == NW){ color:red ...

Selecting a navigation tab will change the displayed div, refresh the content, and set a default tab selection

Hey there, I'm diving into the world of ASP and CSS concepts and have run into a basic problem. My navigation bar currently looks like this: <li><a href="#Div1">Division 1</a></li><br> <li><a href="#Div2">Div ...

HTML5 CSS and Rails theme

I am currently utilizing a free HTML5 template found at this URL: Despite my efforts, I am unable to successfully implement the background images from the main.js file (bg01.jpg, bg02.jpg, bg03.jpg). How should I correctly reference these image files wit ...

What is the best way to toggle content visibility using Bootstrap 5 buttons?

How can I achieve a toggle functionality for two buttons in Bootstrap 5, where clicking one button displays its content while hiding the content of the other button? For instance, when button 1 is clicked, its content should appear while hiding the content ...

Struggling with color styling within link_to tags in Rails 4 - having trouble getting inline styling to show up correctly

<%= link_to "Sign In", new_user_session_path, :method => :get, style: "color:white" %> <h3>Read More - <%= link_to 'Article', action: :articles, style: "color: purple" %></h3> <%= link_to 'Click to Read More Abo ...

What is the best way to adjust width in Bootstrap for responsiveness?

Is there a way to change the width of div elements using only Bootstrap for responsiveness? I want the div elements to increase their width to 50% when the screen width is reduced to around 600px, but I can't find a breakpoint for the w-* class. I a ...

CSS hover effect with multiple image overlays on one area upon mouseover

I've been working on a map project that involves changing the color of multiple areas upon hovering using CSS. I've set up the main div with position:relative, and positioned the images inside absolutely to create hover effects by transitioning o ...

Showing Transition Effect in Vue for Bootstrap Modal Form

I'm struggling to implement a simple Fade-in / Fade-out transition when displaying a Bootstrap Modal form in Vue. The form displays properly but without the desired transition effect. Can anyone help me identify what I'm doing wrong? Thank you in ...

Is it necessary to bring in CSS for the following page?

Should CSS be imported and used across multiple pages in a web application? Is the page structure outlined below considered acceptable, or is there a standard practice to follow? /pages /home index.js index.module.css /cart ...

What are the best ways to incorporate EMs and percentages into responsive web design?

As I delve into the world of responsive design, I am eager to understand the most effective practices when it comes to utilizing em and percentage units of measurement. In a scenario where I set the font-size of the body to 10px, could this potentially le ...