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

To achieve a centered alignment for the h1 and p elements within the division, follow the code snippet below:

<div id="outer" class="container">
 <div id="inner">
  <h1>Heading</h1>
  <p>Some content</p>
 </div>
</div>

Answer №1

To create a centered layout, simply set the parent div to display: flex and apply margin: auto; to the child element.

Take a look at the demonstration below:

#outer {
  width: 100%;
  height: 100vh;
  display: flex;
}

#inner {
  margin: auto;
}
<div id="outer" class="container">
  <div id="inner">
    <h1>Heading</h1>
    <p>Some content</p>
  </div>
</div>

Answer №2

<div id="wrapper" class="container text-center">
<div id="internal">
<h1>Title</h1>
<p>Description</p>
</div>
</div>

#wrapper{
min-height:100vh;
display:flex;
}

#internal{
margin:auto;
}

Kindly make the necessary adjustments to your code. Use display:flex & min-height:100vh for #wrapper to cover 100% of the screen's height. Apply margin:auto to #internal for centering the content.

Answer №3

Utilizing the power of flexbox, Bootstrap 4 makes it easy to structure elements in a flexible and responsive way. Adding the d-flex class sets the property display: flex, while align-items-center vertically aligns the content and justify-content-center horizontally centers it.

<div id="outer" class="container d-flex align-items-center justify-content-center">
    <div id="inner">
      <h1>Heading</h1>
      <p>Some content</p>
    </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

Adding Edit and Delete Functionality to MySQL Database using PHP

Struggling to implement edit and delete buttons in an HTML table populated with values from a MySQL database. The goal is to send the "eventID" to the relevant PHP page upon clicking these buttons. Any assistance on how to resolve this issue would be highl ...

AngularJS mistakenly deletes the incorrect item when using the indexOf method

For the purpose of honing my skills in AngularJS and Rails, I am currently developing a note-taking application which functions similar to a blog. Terms like post equate to note in this context. The left sidebar (controlled by Sidebar.js) fetches all item ...

Aligning multiple spans vertically within a div of varying height (ui-select-multiple)

I am currently using AngularJS ui-select's Multiselect feature. If you want to see how my multiselect looks, check it out here: http://plnkr.co/edit/zJRUW8STsGlrJ38iVwhI?p=preview When arranging spans in multiple lines, achieving nice vertical align ...

Building and saving an HTML webpage using Node.js: A step-by-step guide

Using node.js, I developed an application that gathers data in a MongoDB database. For example: name: John pagename: mypage links: [link1, link2, link3] The task at hand is to generate static HTML pages with the format pagename.mydomainname.com, and ins ...

Having trouble sending data to an API through jQuery: the function is not functioning properly

Currently, I am in the process of developing an HTML form that allows users to input values into text fields and submit them to an external API called ThingSpeak. This API then uses the received values to generate a plot displayed within an iframe. Althoug ...

Store the show/not show status of the collapse item-1 class using either local storage or session storage

Having trouble maintaining the state of my collapsible items after a page refresh. I'm using bootstrap 5. To keep the accordion item open, you need to add show to the class. <div role="tabpanel" data-bs-parent="#accordion-1" cla ...

Troubleshooting issue with JavaScript sorting function failing to arrange elements in ascending

I'm having trouble sorting numbers in ascending order using JavaScript. Here's the code snippet: <h2>JavaScript Array Sort</h2> <p>Click the button to sort the array in ascending order.</p> <button onclick="myFunctio ...

My AngularJs code seems to be throwing some errors

I recently started learning Angularjs and here is the code I have written: <!DOCTYPE html> <html> <head> <title>ng-Messages Service</title> <script src='https://ajax.googleapis.com/ajax/libs/jquery ...

Retrieve data from a text file using ajax and then return the string to an HTML document

Just starting out with ajax, I have a text file containing number values For example, in ids.txt, 12345 maps to 54321 12345,54321 23456,65432 34567,76543 45678,87654 56789,98765 Here is the Html code snippet I am using <html><body> < ...

Ways to include external JavaScript in an HTML document along with CSS styling

Check out this code snippet in HTML with embedded JavaScript. I'm trying to externalize the JS file, but it's not functioning as expected when I do so. How can I resolve this issue and ensure that the element moves upon clicking when the script i ...

Tips on eliminating unwanted gridlines in Chart.js?

After creating a chart using Chartjs, I am facing an issue with some pixels that I want to remove. To make it clearer, here is an image demonstrating the problem: https://i.sstatic.net/sSoK6.png Below is the code snippet responsible for generating this p ...

Retrieving information from a server within several sections of a Flask application

Currently working on a project with Python using Flask and Jinja2, I am exploring ways to integrate a sidebar. Within the HTML pages, there is this snippet: {% include "sidebar.html" %} My objective for the sidebar file is to showcase a section highlight ...

Tips for aligning a div in the center while adjusting screen size

My goal is to showcase a dashboard with a card layout. The desired layout can be seen https://i.sstatic.net/OWgpE.png: Here is a snippet from the HTML File: <div id="overallCanvasWrapper" class="statistic-container"> <p ng-show=" ...

How to Extract Text Content Excluding the Wrapping Tag from an Element Using JSoup

How can I take the text 'Some random text' and enclose it within a span tag? For example, if I have the following element: <div> Some random text 1 <a href="some_url">Go to Link</a> </div> <div> <spa ...

How can I make a recently added row clickable in an HTML table?

I have a table where each row is given the class ".clickablerow". I've set up an onclick function so that when a row is clicked, a dialog box appears allowing me to insert text above or below as a new row. The issue I'm facing is that even though ...

Issues encountered when attempting to parse a JSON object using jQuery

My PHP file is set up like this: <?php include('connection.inc.php'); ?> <?php header("Content-type: application/json"); ?> <?php $sth = mysql_query("select * from ios_appointments a join ios_worker w join ios_partners p wher ...

Arranging circles on top of each other creates a dark line along the edges with rounded

I find myself entangled in quite the enigmatic venture. My latest project involves crafting a mouse that doubles as a 'torch / searchlight'. Whenever there is a hover action on any text (inline elements, buttons, and so forth), its color transit ...

Execute with jQuery using Multiple Attribute Selector

I am attempting to input numeric values using a keyboard. My issue is as follows: the keyboard has an "Accept" button, and I have multiple text fields. I want to assign a different action for each text field. I attempted to use multiple attribute selector ...

Sending Arguments to Shiny RMarkdown

After spending several days attempting to import data into a shiny rmarkdown file, I finally decided to seek help here. I am exploring different methods to achieve my goal, but it's crucial that the final product can be accessed through a website. Th ...

`A problem with HTML element display in Internet Explorer versions 8 and 9 involving <p> and <h2> tags`

While working on my website, I encountered an issue with the way text is being rendered in <p> and <h2>. The footer of my site has three columns, but for some reason the middle column is displaying its <p> text within the <h2> text ...