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

The click event fails to provide $event upon being clicked

Within my HTML structure in an angular 7 application, I have the following setup: My goal is to trigger the GetContent() function when any text inside the div is clicked. Strangely, when clicking on the actual text, $event captures "Liquidity" correctly. ...

How can I programmatically adjust the center of a Google Maps v3 map?

I have a table with 2 columns. The first column contains a div with a Google map, while the second column has a panel that changes its width after a few seconds. On the map, there is a marker placed. Issue: When I click on a button that triggers setCente ...

Changing the Values of Variables in an npm Package within SvelteKit

I have been working on a SvelteKit component library where I define variables for components like background-color and font-family in a CSS file. Inside the ./dist/index.js file of my library, I export the CSS using `import './main.css'`. When in ...

Utilizing Conditional Styling for an Array of Objects within a Textarea in Angular 6

My array contains objects structured as follows: list =[ { name:"name1", value:true } { name:"name2", value:false } { name:"name3", value:true } { name:"name4", value:false ...

Add space between the first and second rows in the grid gallery display

.gallery{ display: grid; grid-template-columns: repeat( auto-fit, minmax(250px, 1fr) ); grid-template-rows: repeat( auto-fit, minmax(250px, 1fr) ); } view grid image here Could you assist me with identifying the spacing issue between the first and second ...

In Firefox, the image width may vary compared to the one that was originally inserted

Greetings to all! I have encountered an issue while using jcrop with Firefox as my browser. It seems that the image size does not match the actual dimensions, allow me to elaborate. The image appears to be 20% larger than its displayed size, despite being ...

Ways to send a POST variable to PHP without refreshing the webpage

Is there a way to pass a simple variable from a text-box on a different page to PHP without reloading it? I've been informed that Ajax is required for this task, but I'm unsure of how to go about implementing it. Can someone provide me with a sam ...

Implementing a Moving Background Image with CSS3 or jQuery

I am facing an issue with a background image that has a file size of 4+ MB in PNG format. My website is a single page website and I want to use this background image, but I'm unsure how to go about it. Is there a way to reduce the size of the image s ...

Utilize text wrapping to ensure a fixed maximum height for content display

I am in need of a div that contains text spanning multiple lines, with both a fixed width and a maximum height. Currently, I have applied the CSS property overflow: hidden;. However, my issue arises when the last line of text exceeds the maximum height of ...

The header row in HTML tables sometimes vanishes unexpectedly after sorting the table

Upon filtering the table, I noticed that the header disappears sporadically. The issue is that the table header row should remain in place regardless of whether or not the characters used for filtering are present in the table rows. In Example 1: When fil ...

Having trouble getting the on:dblclick event to trigger on a row within a list using S

I am currently using Sveltestrap and I am in need of a double click handler for a row: <ListGroupItem on:dblclick={handler(params)}> <Row> <Col><Icon name=........</Col> ... </Row> </ListGroupItem> Int ...

Customize the background color of Material UI input components on standard labels

Currently using react material ui and interested in changing the background color of a standard input. The issue arises when the label is overridden by the background color when the input is not selected. Input selected Input not selected This is my att ...

In HTML, a Bootstrap row appears on top of another row when viewing on mobile devices

I have implemented a Bootstrap HTML section that consists of a container and two main rows. Here is the code snippet: <link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"> <style& ...

Position the background in CSS according to the class name

There are a total of 8 icons, with 4 in blue and 4 in grey. Therefore, product 1 has both a blue icon and a grey icon. When the user has product 1, they will see the blue icon (with an added class on the container called .gotProduct), and when they don&ap ...

Tips on personalizing the default html template in nuxt

Struggling to customize the default page from my Nuxt app due to limited documentation. Link to Documentation I'm interested in extracting only certain elements like the title or links in the head section. For example, how can I access just the li ...

I'm having trouble getting Jquery's insertAfter() function to function properly

After creating a parent div and using jQuery's insertAfter() method to insert additional divs, an unexpected issue arises. The first record goes to the bottom, while subsequent records are inserted above it. Below is the function in question: functi ...

Utilizing AJAX for XML data parsing

I need help with formatting XML data into a table. The code I've written isn't working as expected. The XML data is structured in branches, causing it to not display correctly. Can someone assist me in fixing this issue? <!DOCTYPE html> &l ...

Automatically align a Div to the right within an ngFor directive in an Angular 6 application

<div class="row"> <div class="col-lg-4 col-xs-6" *ngFor="let client of clients;index as i"> <!-- small box --> <div class="small-box bg-dashboard-box" > <div class="inner"> <div class="text-black"> ...

An easy way to enable mobility for BootstrapDialog on mobile devices

Currently, I am utilizing the library available at https://github.com/nakupanda/bootstrap3-dialog in order to create a dialog box. However, my issue arises when viewing the dialog on mobile devices where it exceeds the screen size. On desktops, adjusting t ...

Loading HTML content in Electron using the renderer process JS

During the development phase, I found that the jquery load method for loading HTML views in Electron was functioning smoothly: app.appContainer.load('html/homePage/homePage.html', () => { app.addListenerToHomePage(false) }); I have individua ...