What is the best way to center text both vertically and horizontally within columns using Bootstrap 4?

Trying to figure out how to center text vertically and horizontally in a bootstrap 4 grid with two columns:

<div class="row" style="height:200px;background-color:grey">
   <div class="col-md-10">
      <span>Need help with vertical/horizontal alignment</span>
   </div>
   <div class="col-md-2">
      <span>Same issue here</span>
   </div>
</div>

Struggling to find a solution for this specific case even after checking stackoverflow answers.

Check out the fiddle for reference: http://jsfiddle.net/x1hphsvb/31/

Any suggestions on how to properly center the text?

Answer №1

Utilizing the most recent iteration (Bootstrap 4 alpha 6).

Employ the align-items-center class for vertical alignment, and text-center for horizontal alignment...

<div class="row text-center align-items-center" style="height:200px;background-color:grey">
   <div class="col-md-10">
      <span>How to centrally align this vertically/horizontally</span>
   </div>
   <div class="col-md-2">
      <span>This as well</span>
   </div>
</div>

https://www.codeply.com/go/vaGYMJHA3T

Answer №2

Make sure to include the classes text-center and align-middle on your spans.

To learn more about these classes, check out the documentation links below:

Typography:

Vertical Alignment:

If using the classes directly doesn't produce the desired effect, you have another option. Wrap the spans in divs, apply a class to them, and style the classes as shown here: http://jsfiddle.net/x1hphsvb/32/

.col-md-10{
  height: 200px;
}
.col-md-2{
  height: 200px;
}

.this-div {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.that-div {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.2/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/bootswatch/3.3.7/flatly/bootstrap.min.css" rel="stylesheet"/>
<div class="row" style="height:200px;background-color:grey">
   <div class="col-md-10">
      <div class="this-div">
      <span>How to center this vertically/horizontally</span>
      </div>
   </div>
   <div class="col-md-2">
      <div class="that-div">
      <span>This too</span>
      </div>
   </div>
</div>

Answer №3

To achieve both horizontal and vertical text centering, you can center the elements under both columns.

Simply add the following three classes to your code:

d-flex justify-content-center align-items-center

<head>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" crossorigin="anonymous">
</head>

<div class="row" style="height:200px;background-color:grey">
  <div class="col-md-10 d-flex align-items-center justify-content-center">
    <span>How to center this vertically/horizontally</span>
  </div>
  <div class="col-md-2 d-flex align-items-center justify-content-center">
    <span>This too</span>
  </div>
</div>

The first class will set the display property of the columns to flex.

Answer №4

By utilizing the CSS properties position:relative and position:absolute along with transform:translate(), you can achieve the desired layout effect.

To demonstrate this, I switched from using col-md to col-xs in order to better showcase the effect within the limited space of the snippet.

In addition, I included height:100% for both columns so that they inherit the height of their parent container, resulting in the vertical and horizontal center alignment of the span. This alignment remains consistent even when resizing the browser window.

.col-xs-10,
.col-xs-2 {
  position: relative;
}

.col-xs-10 span {
  position: absolute;
  top: 50%;
  left: 50%;
  transform:translate(-50%, -50%);
}

.col-xs-2 span {
  position: absolute;
  top: 50%;
  left: 50%;
  transform:translate(-50%, -50%);
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="row" style="height:200px;background-color:grey">
  <div class="col-xs-10" style="background-color:red;height:100%;">
    <span>How to center this vertically/horizontally</span>
  </div>
  <div class="col-xs-2" style="background-color:blue;height:100%;">
    <span>This too</span>
  </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

Are there more effective methods for determining the precise CSS values needed for a 3D transformation, rather than relying solely on trial and error?

Having a 3D iPhone vector, I am looking to create a sprite animation of screens on top of it. To achieve this, I am using a div as a mask over the iPhone, with the sprite background animating through transition and background-position. The iPhone is alrea ...

Changing or toggling the visibility of links once the week is finished

I have a total of 4 links available, but I want to display only one link at a time, highlighting a different lunch option every week. Once all 4 weeks have passed, the first lunch menu will be shown again, starting from Monday. <div class="wrapper& ...

The code functions properly when tested on a local server, but doesn't produce any results when

Currently, I am working on customizing a premade website and have been tasked with making a modification. The goal is to implement a confirmation box that appears when a button to delete a meeting task is clicked, rather than immediately deleting it. Afte ...

The HTML div element is not expanding to the full width of the screen on an iPhone device

Recently, I encountered an issue while testing my website on the iPhone. The footer was not covering the full width, especially on Safari browser. After some digging, I realized that the problem might be caused by an absolutely positioned DIV called #leaf- ...

Align navigation items in the center of the navigation container

I've been grappling with aligning the content of a nav-item inside a nav container in my project. I'm unable to apply a margin on the ul>li items as it doesn't adjust properly on different resolutions. I've tried using percentages, b ...

HTML/CSS - Troubleshooting Animation Issues

I am currently experimenting with different website animations and implementing responsive design. However, I encountered an issue when trying to apply these changes to my header, which also affects the .heading element. It seems that there is a problem w ...

Tips for positioning text on the left and right sides of a div in HTML styling

I am struggling with positioning two pieces of text within a div. Despite having some styling already in place, the text is currently displaying one after the other on the left-hand side. I want to position one piece of text to the left and another to the ...

What is the best way to create square editor tabs in Eclipse without using swt-border-radius?

The design of Eclipse's rounded and/or swooshing tabs is starting to feel outdated in today's modern era. I thought that by adding the following line in my default.css file, I could get rid of the rounded corners: swt-corner-radius: 0px Howeve ...

Missing Icons in Bootstrap Table显示

I am currently working on a bootstrap-table (version 1.18) that is built upon bootstrap version 4.5.2 Everything seems to be functioning correctly except for the icons within the table, although the functionalities they represent are still operational (su ...

How to set up Bootstrap with Symfony 4 using encore

I attempted to follow the instructions provided in the documentation, but unfortunately, it isn't working as expected. Documentation: https://symfony.com/doc/current/frontend/encore/simple-example.html Here is the code I am using: assets/js/app.js ...

Choose a variety of photos for each individual input file

I am facing a scenario where I need to allow users to upload 3 images using input[type="file"]. Each input should only accept 1 image, but if a user selects more than 1 image in the first input, I want to distribute them among the subsequent inputs. For in ...

The issue of why padding left is not functioning correctly in Bootstrap version 5.3.1

</head> <body> <header class="bg-info "> <div class="container"> <div class="row text-white"> <div class="col-md-6 p-3 pl-6 "> ...

Buttons to maximize, minimize, and close individual sections on a webpage

I am fairly new to front end development and I must say, I am absolutely enjoying every bit of it. Here is a little challenge that came my way. It may seem simple to some but it got me thinking. I have three sections on the right side of my website. I wa ...

Tips for designing a map similar to the one found on ikman.lk

Can anyone provide step-by-step instructions on how to design a map similar to the one featured on ikman.lk? ...

Discovering the dimensions of a disabled attribute within a table using XPath in Selenium with Java

I'm attempting to determine the number of columns in a specific table, but some are disabled - I'd like to know if it's possible to get the count without including the disabled ones (only counting the visible columns). As you can see in the ...

The previous user's selection of checkboxes will be disabled

https://i.stack.imgur.com/fH1g2.pnghttps://i.stack.imgur.com/Oiu6q.pngI am facing an issue where I want all the records (seats selected by the user) to be disabled, but the code provided only fetches the last record and disables it. <?php ...

Having trouble with submitting a form using @submit in Vue 3

I am facing an issue with the vuejs @submit.prevent feature in my login form. When I click the login button on my website, it does not work as expected. Even after adding a catch error in the function, the console still shows nothing. Can someone please as ...

Delay the standard equal height function in Bootstrap 4

Currently, I am working with multiple columns in Bootstrap using col-md-6, but I am experiencing an issue with them having equal heights. I do not want this behavior and I am using Bootstrap 4 for this project. Can anyone provide assistance with this? ht ...

Why are my Bootstrap nav-tabs not showing tab-content in MVC?

I'm dynamically creating tab navigation from controllers using a list. <div class=""row> <div class="col-xl-3"> <!-- Tabs nav --> <div class="nav flex-column nav-pills nav-pills-custom" id="v-p ...

AngularJS ng-repeat nested loop allows you to iterate through an array

I am currently working with the following JSON data: { "ListNhomThanhTra": [ { "ListKeHoachThanhTra": [ { "ListDoiTuong": [ { "MA_DV_DUOC_TT": "DT01", ...