How to center a responsive image in a container using Bootstrap

How can I properly center my banner and place the logo above it? Currently, both elements are not centered as desired.

View the live version here: http://codepen.io/anon/pen/waYBxB

HTML Code:

</head>
<body>

    <!-- LOGO HEADER -->
    <section>
        <div class="container">
            <div class="row">
                <div class="col-md-6"><img src="http://gvsfoundation.org/wp-content/uploads/2013/04/your-logo-here-27.png" class="Logo"></div>
                <div class="col-md-6"></div>
            </div>
        </div>
    </section>

    <!-- SLIDING BANNER -->
    <section class="Banner ">
        <div class="container midText">
            <div class="col-md-12 text-center hidden-xs"><img src="http://oi58.tinypic.com/eb61xd.jpg" class="img-responsive"></div>
        </div>
    </section>

    <!-- MENU BAR -->
    <section>
        <div class="container">
            <div class="col-md-12">
                <nav>
                    <ul>
                        <li><a href="?p=home">Home</a></li>
                        <li><a href="?p=home">Codepen.io</a></li>
                        <li><a href="?p=home">A longer button</a></li>
                        <li><a href="?p=home">This is my really long button</a></li>
                        <li><a href="?p=home">About Us</a></li>
                        <li><a href="?p=home">Contact</a></li>
                    </ul>
                </nav>
            </div>
    </section>

    <!-- MAIN CONTENT -->
    <section>
         <div class="container"  id="MainPage">
            <div class="row">
                <div class="col-md-9 MainContent"><h3>codepen ---- CODING HAS NEVER BEEN SO EASY!</h3><br> Notice how the white background is centered but not the image and the logo. How can I fix this?   </div>
                <div class="col-md-2">I AM IN THE BANNER SECTION </div>
            </div>
        </div>
    </section>
</body>
</html>

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

CSS Code:


    body {
        background-image: url(http://oi60.tinypic.com/2hhnpzn.jpg);
    }
    
    nav ul {
        list-style: none;
        overflow: hidden;
        height: 47px;
    }

    nav ul li a {
        color: #F9F7F7;
        background-image: linear-gradient(to top, #ff6351, #ffcc6e);
        padding: 8px 22px 8px 22px;
        display: block;
        text-align: center;
        font-size: 22px;
        float: left;
        margin-right: 5px;
        color: rgb(255, 255, 255);
        background-color: rgb(120, 120, 120);
        text-shadow: rgb(79, 79, 79) 1px 1px 3px;
    } 

    .Logo {
        margin: 15px;
    }

    #MainPage {
        background-color: white;    
    }

    .midText {
        text-align:center;
    }

    .MainContent {
        margin: 33px;
    }

Answer №1

To center the banner image on the page, simply add the following code in front of the image:

<section class="Banner ">
      <div class="container midText">
    <div class="col-md-12 text-center hidden-xs">
<center>
<img src="http://oi58.tinypic.com/eb61xd.jpg" class="img-responsive">
</center></div>
  </div>
    </section>

Answer №2

To achieve centered divs, utilize the column system provided by Bootstrap. Bootstrap rows consist of 12 column-widths, so ensure your row equals 12 columns.

Here is an example:

<section>
  <div class="container">
    <div class="row">
      <div class="col-md-4"></div>
      <div class="col-md-4"><img src="http://gvsfoundation.org/wp-content/uploads/2013/04/your-logo-here-27.png" class="Logo"></div>
      <div class="col-md-4"></div>
    </div>

You can have columns arranged as 4|4|4 for centering, or try other combinations like 3|6|3, 2|8|2, or 1|10|1. This approach should always keep your divs centered. Hope this explanation helps.

If my explanation is unclear, check out these visual examples: http://getbootstrap.com/examples/grid/

Answer №3

Since you are dealing with multiple breakpoints, have you considered setting the image width in pixels for better control? The discrepancy in aspect ratio between the image and the containing div could be causing the fit issue.

display: block;
max-width: 1024px;
margin: 0 auto;
height: auto;

As for the logo...

<div class="col-md-12"><img src="http://gvsfoundation.org/wp-content/uploads/2013/04/your-logo-here-27.png" class="Logo"></div>

.Logo {
display: block;
margin: 15px auto;}

Here's a quick fix to consider.

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

Tips on how to update the styling of an active link

http://jsfiddle.net/G8djC/2/ Looking to create a tabbed area where content changes based on the tab clicked. The Javascript function switches the link class to active upon clicking. However, struggling to change the color of the active tab beyond the firs ...

CSS3 animations with background-image for Firefox are an exciting way to enhance the

I am currently working on a keyframe animation project using CSS. The animation is running smoothly in Chrome with supported -webkit syntaxes. @-webkit-keyframes title_toggle { from { background-image:url(images/title.png); } 75%{ background-image:url(im ...

Troubles arise when hovering over the <strong> tag and the <h4> element

While hovering over my h4 tag in the table, everything functions correctly. However, when I hover over the strong tag inside the h4 element, the strong tag inherits the same hover effect as the h4 tag. The structure of each td element within the table is ...

Unlocking the Mysterious Realm of HTML

I recently stumbled upon a mysterious combination of HTML attributes: <div vanisheffect="true" blinkcolor="red" fadeopacity="0.8"> Have you ever encountered something like this? And more importantly, does it have any impact on specific browsers? Is ...

Checking the status of a checkbox after submitting using AngularJs

For my first application, I am utilizing AngularJs and JavaScript to display information from an API as checkboxes. Currently, the functionality is working well, but I am unsure how to validate if any checkbox options are checked with a submit button. Aft ...

Implementing HTML tags in C# code

Below is an example of HTML script: <h1> This is heading one </h1> <p> This is paragraph. </p> I would appreciate any recommendations on how to add <html></html> tags to the above script using C#. ...

Displaying numerous images/items in a Bootstrap 2.3 carousel

I have been working on a PHP-based website that utilizes Twitter Bootstrap 2.0. Currently, I am retrieving user information from a MySQL database and attempting to display them in separate frames or items within a carousel instead of all at once using a wh ...

Is it feasible to automate the cropping process with selenium?

Exploring a new challenge: simulating a cropping feature. I understand that replicating human cropping is impossible, but the image I intend to crop remains a fixed size each time I run the test. My goal is to establish the cropping WebElement at a constan ...

Having trouble getting CSS Animation to work in a React project?

I am currently developing a Next.js application using React. const partyButton = useRef(null) const handleParty = () => { setTimeout(() => { partyButton.current.classList.add('party-time'); console.log(party ...

What is the overlay feature in Material-UI dialogs?

I recently started using the React-Redux Material-UI package found at http://www.material-ui.com/#/components/dialog My goal is to implement a grey overlay that blankets the entire dialog element, complete with a circular loading indicator after the user ...

Is it possible for Websockets to receive data from Ajax requests?

I am currently working on creating a PHP and HTML5 websocket chat system. In this setup, one end will be a socket while the other end will be AJAX. Is it possible for PHP's socket_recv() function to receive data from sources like AJAX, MySQL, or any o ...

HTML5 Local Storage allows web developers to store data locally within

After saving a value in the local storage (HTML5) on page 1, I noticed that when I navigate to page 2, the values stored in the local storage disappear. Any insights or suggestions on what might be causing this issue? Upon reviewing my code, I am using th ...

Having Trouble with the Spacing Between Navbar and Page Content in Bootstrap?

Let's tackle the issue at hand: Here is the code snippet. A lot of it has been borrowed from examples and tutorials. <!-- Navigation --> <nav class="navbar navbar-inverse navbar-fixed-top"> <div class="container"> <!-- Brand a ...

Using JavaScript to dynamically set a background image without the need for manual hard-coding

My attempt was to showcase images as a background image upon mouseover event for the div section with id 'message' by manually coding JavaScript functions for each image like this: Here is the HTML code inside the body section <div id = "mes ...

Inject JavaScript Object Information into Bootstrap Modal

After iterating through each object and assigning its data to an individual div along with a button, I encountered an issue. When testing the buttons, I noticed that only the last object's data was displayed in all of the modal windows. Any suggestion ...

Creating a visually engaging parallax effect in two columns with differing lengths that align perfectly at the conclusion

Recently, I came across an interesting layout technique on Apple's website that involves two columns with different lengths. As you scroll down the page, one column slows down to align with the other so that they both meet at the bottom. You can chec ...

By default, use jQuery to load the content of a div

Upon page load, I am looking to display the content within #destiny2. This section includes text and images, as well as additional content for three categories. Initially, the first category should be shown without requiring a click. Subsequently, clicking ...

Ways to delete a property from an element that was originally included with jquery

On my jsp page, I am implementing a jQuery autocomplete feature for a text field with the id "mytextfield". jQuery(function(){ $("#mytextfield").autocomplete("popuppages/listall.jsp"); }); Sometimes, based on user input and pr ...

Creating various iterations of a single CSS animation

Attempting to animate multiple instances of a cross mark animation can be tricky. The animation works well with one instance, but struggles when trying to create more than one. The challenge lies in controlling the size and position of each cross animation ...

Switch the hue when altered

My document contains various input elements such as text, radio buttons, and checkboxes. I want each of these elements to change color when a change is made. This is the method I am currently using: $("document").on('click', 'change', ...