Center a column using Twitter Bootstrap

Cover Image for Center a column using Twitter Bootstrap
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

How to Center a Column Using Twitter Bootstrap 3 😎🔥

<center>![](https://media.giphy.com/media/fXS8ltMnsvPQYFgYgL/giphy.gif)</center>

Are you struggling to center a column within a container using Twitter Bootstrap 3? Don't worry, we've got you covered! In this blog post, we'll address common issues and provide easy solutions to help you achieve the desired result. Let's get started! 💪💻

The Problem 😟

The question at hand is how to center a <div> with a class .centered within a container of 12 columns. You may be tempted to use an offset, but that approach may not give you the desired result. You don't want any extra empty space outside the <div>, and its contents should shrink proportionally. So how do you achieve this? 🤔

The Solution 🙌

To center a column within a container in Bootstrap 3, you can use the following steps:

  1. Add the necessary CSS class to the <div> element:

<div class="col-lg-1 col-lg-offset-5 centered">
  <!-- Your content here -->
</div>
  1. Define a custom CSS class called .centered in your CSS file:

.centered {
  background-color: red;
  margin-left: auto;
  margin-right: auto;
  float: none;
}
  1. Make sure to include the latest Bootstrap CSS in your HTML file:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

By setting margin-left and margin-right to auto and float to none, you effectively center the column within the container. This approach ensures that there are no extra empty spaces outside the <div> and that its contents shrink proportionally. 🎉

Example Usage 🌟

Here's an example to help you visualize how the above solution works:

<body class="container">
  <div class="col-lg-1 col-lg-offset-5 centered">
    <img data-src="holder.js/100x100" alt="" />
  </div>
</body>

In this example, the <div> with a size of one column is centered within the container. The background color is set to red for visibility purposes, but feel free to customize it to suit your needs. You can also add additional content inside the centered <div>, and it will remain perfectly centered within the container. 👍

Conclusion and Call-to-Action 🏁📢

Congratulations, you now know how to center a column using Twitter Bootstrap 3! We hope this guide was helpful and that you were able to achieve the desired result. If you have any further questions or need assistance, feel free to leave a comment below. Let's create amazing center-aligned layouts together! 🚀💫

Remember to share this post with your fellow developers who may find it useful. Happy coding! 💻❤️

<center>![](https://media.giphy.com/media/fTI9mBoWLef8k2Ri5M/giphy.gif)</center>


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello