Trouble aligning image in the middle with bootstrap

After attempting the solutions proposed in previous posts, I find myself sharing my amateur code where the embedded image refuses to center and I am at a loss as to why. The image is meant to be positioned in the middle of a login screen/box; it worked for the individual who conducted the demonstration, yet despite tweaking his CSS, I have only been able to marginally improve its placement. However, the image still remains off-center. Can anyone shed some light on this issue?

HTML

<!DOCTYPE html>

<html lang="en">
    <head>
        <!-- Standard Meta Tags -->

        <!-- Bootstrap & Related Links-->

        <link rel="stylesheet" href="static/styles.css">

        <title>Project</title>
    </head>
    <body>
        <!-- cont is the entire box; changing its CSS changes its position on the page -->
        <div class="cont">
            <!-- box is the area that contains the image, username, and password fields, but not the button bar and login buttons -->
            <div class="box">
                <!-- Creates the entire top row with close button and three circular buttons
                <div class="row top">
                    <div class="left">
                        <i class="fa fa-times close"></i>
                    </div>
                    <div class="right">
                        <i class=" fa fa-circle but one"></i>
                        <i class=" fa fa-circle but two"></i>
                        <i class=" fa fa-circle but three"></i>
                    </div>
                </div>
                -->
                <div class="row middle sg">
                    <div class="row pic sg">
                        <div class="col-md-4 col-md-offset-4">
                            <img src="static/fleur.jpeg" alt="fleur-de-lis" class="photo">
                        </div>
                    </div>
                    <form action="#" class="form-horizontal form">
                        <div class="input-group y">
                            <span class="input-group-addon"><i class="fa fa-user use"></i></span>
                            <input type="text" class="form-control" placeholder="Username">
                        </div>
                        <div class="input-group">
                            <span class="input-group-addon"><i class="fa faunlock-alt use"></i></span>
                            <input type="password" class="form-control" placeholder="Password">
                        </div>
                    </form>
                </div>
                <div class="row base sg">
                    <h2 class="text-center login">Login</h2>
                </div>
            </div>
        </div>
    </body>
</html>

CSS

html, body {
    width: 100%;
    height: 100%;
}

.cont {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
}

.box {
    background-color: #2B2B35;
    width: 400px;
    border-radius: 10px;
}

.top {
    width: 100%;
    background-color: #24242E;
    margin: 0;
    border-radius: 10px 10px 0 0;
    padding: 0 15px;
}

.left {
    float: left;
}

.right {
    float: right;
}

.close {
    padding: 18px 0;
    font-size: 20px;
    color: #fff;
}

.but {
    padding: 18px 0 18px 5px;
    font-size: 20px;
}

.but:hover {
    cursor: pointer;
}

.one {
    color: #F4CB61;
}

.two {
    color: #DB5594;
}

.three {
    color: #6451E8;
}

.photo {
    width: 200px;
    border-radius: 50%;

}

.sg {
    display: block;
    margin-left: auto;
    margin-right: auto;
    align-items: center;
    justify-content: center;

}

.pic {
    margin: 40px 0 30px 0;
}

.form {
    padding: 0 40px 40px 40px;
}

.login {
    padding: 12px 0;
    margin: 0;
}

.base {
    background-color: #3FA752;
    border-radius: 0 0 10px 10px;
    color: #fff;
}

.base:hover {
    cursor: pointer;
}

.base h2{
    font-weight: 600;
    font-size: 26px;
}

.user {
    color: #ccc;
}

.y {
    padding-bottom: 15px;
}

input[type=text],
input[type=password] {
    background: 0, 0;
    border: 0;
    box-shadow: none;
    border-radius: 0;
    border-bottom: 1px solid #9446B6;
}

.input-group-addon {
    background: 0 0;
    border: 0;
    border-radius: 0;
    border-bottom: 1px solid #9AA6B6;
}

.use {
    color: #9AA6B6;
}

input[type=text],
input[type=password]:focus{
    box-shadow: none !important;
    color: #FF3F3F;

}

Answer №1

Last Updated:

To resolve the issue, it is recommended to remove the classes class="col-md-4 col-md-offset-4" and instead add a class to center the image.

html, body {
    width: 100%;
    height: 100%;
}

.cont {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
}
  
// Remaining CSS code here
<html lang="en">
    <head>
        <!-- Standard Meta Tags -->

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

<link rel="stylesheet" href="static/styles.css">

        <title>Project</title>
    </head>
    <body>
        <div class="cont">
            // Remaining HTML code here
        </div>
    </body>
</html>

It's important to note that images are by default inline-block elements and should be centered using text-align on the parent element. To center images as block elements, set their display property to 'block' and specify a width along with setting both left and right margins to 'auto'.

.container {
  width: 100%;
}

.tex-center {
  text-align: center;
}

#inline-example {
  display: inline-block; /* Default value*/
}

#block-example {
  display: block;
  max-width: 200px;
  margin: 0 auto 0 auto;
}
<h3>Inline centered image</h3>
<div class="container tex-center"><img id="inline-example" src="https://placeimg.com/200/200/nature"></div>
<h3>Block centered image</h3>
<div class="container"><img id="block-example" src="https://placeimg.com/200/200/people"></div>

If encountering similar CSS issues, utilizing browser developer tools like Chrome's DevTools can help identify problems such as unwanted margins or padding caused by specific classes like col-md-4 col-md-offset-4 in Bootstrap grids. Removing conflicting classes can often lead to a resolution.

https://i.sstatic.net/kPhuU.jpg

Answer №2

Give this a shot:

.picture {
    width: 250px;
    border-radius: 50%;
    display: block;
    margin: auto;
}

To properly center an image within a div, make sure to set the display property to block and use margin: auto; on the img element.

Answer №3

When referring to the image with the code

<img src="static/fleur.jpeg" alt="fleur-de-lis" class="photo">
, it is recommended that you include the following in your 'photo' class:

display:block;
margin: 0 auto;

Ensure that other CSS rules do not interfere with these lines, allowing your image to be centered within your col-element.

Answer №4

Your image is positioned incorrectly due to column classes being used. To resolve this issue, remove the column classes (even the entire div) and use the following CSS:

.photo {
    display: block; // set element as block
    margin: 0 auto; // center horizontally with auto margins
    width: 200px;
    border-radius: 50%;
}

For example implementation, visit: https://codepen.io/themeler/pen/QqNpWv

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

Creating two adjacent squares can be achieved by arranging columns and rows in a specific way

Looking to create a website with a gallery block but running into some issues. I only want to utilize Bootstrap along with my custom CSS for the finishing touches. The layout should consist of 2 squares next to each other, with the left square being 2 colu ...

Looking for a way to use JSON data in a dropdown menu to trigger a

After fetching a JSON file to fill my dropdown menu, I want each selected area to redirect to a specific URL. However, I'm unsure how to achieve this. Do I need to use JavaScript to trigger the onclick event for the URL? HTML: <div class="btn ...

What is the best method to add a background image to a short div element?

For instance, here is some example HTML code: <div id="container_background"> <div id="content"> <p> #container </p> </div> </div> This is the CSS code that applies to the above HTML ...

Aligning the Bootstrap navbar to the right causes the items to shift to the left

UPDATE: The issue has been resolved. It turns out that the navbar was not extending to 100% width and reaching the edges of the screen. I am currently facing a challenge with a Bootstrap 4 navbar, similar to issues I encountered with the previous version. ...

Is there a variance in window.innerWidth between Chrome and Firefox?

body, html { position:absolute; width:100%; height:100%; margin: 0; padding: 0; overflow: hidden; } When using window.innerWidth, there is a discrepancy between the values returned by Firefox and Chrome. Firefox return ...

Use Bootstrap to effortlessly arrange columns horizontally in a row

I'm facing a scenario where I need to arrange the columns in a row horizontally. The challenge is to ensure that the row expands horizontally to fit the columns. I attempted to use white-space: nowrap on the row, remove floats on columns, and set the ...

Employed AJAX for webpage updates, but seeking guidance on URL modification

I recently followed a tutorial on AJAX from http://www.w3schools.com/ajax/default.asp Currently, I am using this knowledge to update my page asynchronously. However, I also want the URL to be dynamically generated based on the new content. When a user cl ...

Is there a way to pass an HTMLDivElement as a child in React components?

Scenario Currently, I am in the process of developing a React application (rails-react) where the main component is called GameTracker. Within this parent component, there are two child components: EquipmentPanel and PinnedPanels. To achieve a specific fu ...

I'm looking to mirror images within the Bootstrap framework - any suggestions on how to achieve this using jQuery

When hovering over the image, it should flip to reveal text or a link: <div class="clearfix visible-xs"></div> <div class="col-md-3 col-sm-3 col-xs-6"> <div class="speaker-item animated hiding" data-animation="fade ...

React Vertical Scroll Carousel

Looking to create a website section that remains fixed while scrolling through a carousel vertically. Once the last slide is reached, scrolling will continue navigating the page rather than changing carousel slides. Unsure how to achieve this. Desiring a ...

Choose a tag in order to connect to a different page when an event occurs in Rails

Working on my first complete stack rails application! I am trying to implement a select dropdown menu that will direct users to a specific show page within the app. Each option represents a different college with its own individual show page. I am struggl ...

Difficulty with button click functionality in Selenium IDE

Hi there, I am trying to use the clickAndWait xpath=//button[contains(.,'Next')] command in Selenium IDE, but despite showing up in the test, it is not clicking on the button labeled 'Next'. The button I am referring to has an id of pmc ...

Error: The object does not have a method called 'to top scroller' and it is causing an uncaught type error

A plugin was obtained from the following link: http://www.jqueryscript.net/other/jQuery-Plugin-For-Smooth-Scroll-To-Top-Bottom-scrollToTop.html.i and the code was attached below in the index.html file. Several jQuery plugins were attempted, but none work ...

Developing shapes using CSS and jQuery

I am struggling to create the exact contour as shown in the image. Despite trying, I have not been successful in achieving it. I used a lot of HTML child tags in my attempt, but I would prefer to keep it minimal. Is there a simpler way to accomplish this? ...

Tips for confirming the successful loading of the reCAPTCHA JS file prior to form submission

My issue involves a recaptcha and a registration form. Let me share with you a simplified version of a handler: //my_script.js document.getElementById("my_form").onsubmit = function(e) { e.preventDefault(); grecaptcha.execute(); var grecap_resp = g ...

Is it possible to save the location of a draggable box using CSS3?

I'm trying to figure out how to store the position of a box in a fluid sortable row. Essentially, I want the position to be remembered when a user drags a box from category 1 to category 2, even after refreshing the page. Below is the HTML code for t ...

The white-space property disrupts the normal width of elements

My initial goal was to stack two elements together side-by-side, both taking the full available height. One element has a fixed width of 200px with an image (70px wide) inside, while the other element should only fit one line of text, clipping any overflow ...

Utilize jQuery to enclose nested elements within a parent element

There is a Markup presented below, where this HTML content is being generated from Joomla CMS. I am seeking a jQuery method to reorganize this HTML structure without modifying its PHP code. <div class="item column-1"> <div class="page-header"& ...

Issue an alert and refresh the webpage when a file extension upload script is detected

Here is the JavaScript code I'm using to restrict the file type extension during uploads: function TestFileType( fileName, fileTypes ) { if (!fileName) return; dots = fileName.split(".") //get the part AFTER the LAST period. fileType = "." + dots[do ...

The clearfix feature fails to function properly with overflow:auto

I could really use some help with my CSS skills: Here is the HTML code I am working with: <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Cluster Test</title> <link rel="stylesheet" href= ...