There seems to be an issue with vertically centering row divs in Bootstrap

Struggling to center a row div vertically in an ASP.NET MVC project? Despite numerous attempts, the content remains at the top. Here's a quick fix with some eye-catching colors to illustrate the issue:

Site.css:

html, body {
    height: 100%;
}

#mmenu_screen > .row {
    min-height: 100vh;
}

/* Padding to prevent content from touching edges */
.body-content {
    padding-left: 15px;
    padding-right: 15px;
}

/* Prevent truncation of long terms in left column of horizontal description lists */
.dl-horizontal dt {
    white-space: normal;
}

/* Limit width of form input elements to 280px */
input,
select,
textarea {
    max-width: 280px;
}

.flag {
    width: 100px;
}

Layout.cshtml:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
</head>
<body>
    <div class="d-flex flex-column h-100" style="background-color: antiquewhite;">
        @RenderBody()
    </div>

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)
</body>
</html>

Index.cshtml:

<div class="container-fluid h-100 d-flex flex-column" style="background-color: aqua">
    <div class="row h-100" style="flex:1;">
        <div class="col-md-4">
            <div class="row">
                <div class="col-md-6">
                    @Html.LabelFor(model => model.Year)
                </div>
                <div class="col-md-6">
                    @Html.DropDownListFor(model => model.Year, new SelectList(Model.Years, "Value", "Text"), new { @class = "form-control year-dropdown", style = "font-size:120%;" })
                </div>
            </div>
            <div class="row p-2"></div>
            <div class="row">
                <div class="col-md-6">
                    Edad en el momento del accidente
                </div>
                <div class="col-md-6">
                    @Html.DropDownListFor(model => model.Year, new SelectList(Model.Ages, "Value", "Text"), new { @class = "form-control year-dropdown" })
                </div>
            </div>
        </div>
        <div class="col-md-8">
            <img src="~/Content/img/accident-1497298.jpg" alt="car_accident" class="img-fluid w-100" />
        </div>
    </div>
</div>

Your assistance is greatly appreciated.

https://i.sstatic.net/pEZqA.png

Edit:

Following advice from @Artūrs Orinskis (see code below), the content in the first column is now centered vertically relative to the image. However, the entire row containing the image still appears at the top as depicted in figure 2.

https://i.sstatic.net/qG8az.png

Edit 2:

Thanks to the guidance of @Artūrs Orinskis, I have found a solution that works perfectly. Sharing the final code below in case it can benefit others.

Final code:

<div class="container-fluid d-flex flex-column flex-fill">
        <div class="row" style="background-color: yellowgreen; height: 100vh">
            <div class="col-4 d-flex bg-success">
                <div class="d-flex flex-column flex-fill my-auto bg-info">
                    <div class="form-group">
                        <input class="form-control">
                    </div>
                    <div class="form-group">
                        <input class="form-control">
                    </div>
                </div>
            </div>
            <div class="col-8 d-flex bg-warning">
                <div class="d-flex flex-fill my-auto bg-info">
                    <img src="~/Content/img/accident-1497298.jpg" alt="car_accident" class="img-fluid w-100" />
                </div>
            </div>
        </div>
    </div>

https://i.sstatic.net/V6oFN.png

Answer №1

Consider utilizing this strategy.

Key elements of the design include:

  1. Ensure height is set to 100% only on the html & body tags
  2. Use bootstrap's .flex-fill for most elements to fully fit content within the viewport and .flex-column to span vertically across the page, aiding in vertical alignment control
  3. A blue background (.bg-info) is maintained to showcase how content expands from the center, spanning vertically as needed

body, html {
  height: 100%;
}

body {
  display: flex;
  flex-direction: column;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twbootstrap/5.1.2/css/bootstrap.min.css" rel="stylesheet"/>
<html>
  <body>
    <div class="container-fluid d-flex flex-column flex-fill">
      <div class="row flex-fill">
        <div class="col-4 d-flex bg-success">
          <div class="d-flex flex-column flex-fill my-auto bg-info">
            <div class="form-group">
              <input class="form-control">
            </div>
            <div class="form-group">
              <input class="form-control">
            </div>
          </div>
        </div>
        <div class="col-8 d-flex flex-column bg-warning">
          <div class="d-flex flex-fill my-auto bg-info">
            <img class="img-fluid w-100" src="data:image/svg+xml, %3Csvg viewBox='0 0 612 612' xmlns='http://www.w3.org/2000/svg' focusable='false'%3E%3Ctitle%3EBootstrap%3C/title%3E%3Cpath d='M510 8a94.3 94.3 0 0 1 94 94v408a94.3 94.3 0 0 1-94 94H102a94.3 94.3 0 0 1-94-94V102a94.3 94.3 0 0 1 94-94h408m0-8H102C45.9 0 0 45.9 0 102v408c0 56.1 45.9 102 102 102h408c56.1 0 102-45.9 102-102V102C612 45.9 566.1 0 510 0z'%3E%3C/path%3E%3Cpath fill='currentColor' d='M196.77 471.5V154.43h124.15c54.27 0 91 31.64 91 79.1 0 33-24.17 63.72-54.71 69.21v1.76c43.07 5.49 70.75 35.82 70.75 78 0 55.81-40 89-107.45 89zm39.55-180.4h63.28c46.8 0 72.29-18.68 72.29-53 0-31.42-21.53-48.78-60-48.78h-75.57zm78.22 145.46c47.68 0 72.73-19.34 72.73-56s-25.93-55.37-76.46-55.37h-74.49v111.4z'%3E%3C/path%3E%3C/svg%3E">
          </div>
        </div>
      </div>
    </div>
  </body>
</html>

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

I want to apply a transition property to my anchor tag so that it changes color smoothly when transitioning from being visited to being hovered over

Here is the HTML content for the Main Page: <body> <div class="container"> <header> <nav class="nav-items"> <ul> <li id="li_1"><a href="ind ...

Immediately set the width attribute of an HTML element using JavaScript

Can an element's width be dynamically set using the following code: <img id="backgroundImg" src="images/background.png" alt="" width="javascript:getScreenSize()['width']+px" height="1100px"/> In this code, getScreenSize() is a JavaSc ...

css: maximum width or maximum size

My website features images with a width of 720 pixels. I am looking for a way to ensure that these images are either 95% of the page's width (with a maximum width set at 95%), or 720px if the screen size is too large. This is to prevent distortion on ...

Error Encountered in Laravel 5.2 FormBuilder.php

Encountering Laravel 5.2 ErrorException in FormBuilder.php on line 1235 stating that the method 'style' does not exist. This error has me perplexed. I have already made updates to my composer.json file by adding "laravelcollective/html": "^5.2" ...

difficulties encountered when implementing a sticky footer with two distinct footer sections

Currently, I am teaching myself web development and reconstructing a website from scratch. The layout should resemble the following: ===========Navigation Bar=========== =========Website Content=========== ===========Footer #1============ ===========Fo ...

Tips for converting numerical values to visual elements in a JQUERY slider for images

I am looking to incorporate a similar image slider on my website, like the one showcased here: . However, I want the bottom content numbers to be replaced with smaller versions of the images in the slider instead. Does anyone have any suggestions on how I ...

Limit the amount of text displayed on the main section of the webpage

I am working on creating a simple webpage with three different sections styled like this: .top { position:absolute; left:0; right:0; height: 80px; } .left { position:absolute; ...

Waiting for all promises to resolve: A step-by-step guide

I need to make two different API calls and perform calculations based on the results of both. To wait for both promises to resolve, I am using Promise.all(). const getHashTagList = async () => { loader.start(); try { await getAllHashTag ...

Incorporating vertical-align:middle into complex divs

I am faced with the challenge of aligning three divs which contain different lengths of text vertically. After experimenting with the following code: #container{ height:200px; vertical-align:middle; display:table-cell; } #content{ height: ...

Unable to increase font size using CSS

I am struggling with adjusting the size of an h1 element along with other properties, but the only change I see is in the font family. Could it be the Bootstrap grid I am using causing this issue? I am still relatively new to Bootstrap. h1{ font-famil ...

Make sure to place the <i> tag within the <li> tag at the bottom to automatically resize the height

Currently, I am working on customizing my menu design with a mix of bootstrap and font awesome CSS styles. It would be easier to demonstrate the issue on a live page. My main objectives are two-fold: I want to position the chevron icon at the bottom with ...

Cursor customized image vanishes upon clicking anywhere on the page on a MAC in various web browsers

I am currently trying to set a custom image as the cursor using jQuery in this manner $(document).ready(function(){ $("body").css('cursor','url(http://affordable-glass.com/test/penguinSm.png),auto'); }); While this method works, ...

The HTML form label offers a choice between two alternatives

My usual method of styling forms involves organizing elements like this: <label for="CompanyNameTextBox"> <span>Company</span> <input name="CompanyNameTextBox" type="text" id="CompanyNameTextBox" /> </label> This set ...

Use JavaScript to identify the front layer

My goal is to adjust the z-index of a table cell using JavaScript to bring it to the top layer. I've discovered that the z-index property only works when the CSS 'position' is set to something other than normal. The issue I'm facing is ...

positioning a window.confirm dialog box in the center of the screen

I'm currently facing an issue with a dialog box that I have implemented successfully. However, I can't seem to get it centered on the page: <Button variant="danger" onClick={() => { if (window.confirm('Delete character?')) handle ...

Using CSS to layer a background image

Let's explore combining multiple layers as background images using HTML5 and CSS3: <div style="background-image: url(layer1.png)">...</div> To add the next layer, we can use the following CSS code: div { background-image: url('la ...

Ways to eliminate the gap between columns without using gutters

I implemented the code below to create two columns, but there seems to be some space between them. How can I remove this space, considering that I am using "no-gutter"? Check out the image here: https://ibb.co/80zTh60 <section id="incubator"> & ...

Utilizing Bootstrap's Multi-Grid System

I am currently working on implementing a Bootstrap grid design that resembles pic1.jpg. However, I am facing challenges in achieving the desired layout, as pic2.jpg shows the current scenario. Below, I have shared the code I am using. pic1.jpg :- ! pic2. ...

SignalR's DependencyResolver in Asp.Net is failing to resolve hubs

Our application is quite complex, with hubs spread across multiple dlls. The entire application uses DI with LightCore. However, we are currently facing an issue where SignalR does not appear to be utilizing the DependencyResolver Adapter that we have crea ...

What is the best way to eliminate vertical spacing among li elements in CSS

I am facing a challenge with my HTML code and styling, which is just an example: <div id="mn" style="margin-top:200px;"> <div class="first">1</div> <div class="second">2</div> & ...