Center and align items vertically within a Bootstrap 4 div, or align them to the

Using Bootstrap 4, the .top block should span 100% width and the full height of the screen. The .top-title should be centered on the screen, while the .btn-scroll-down should appear at the bottom of the screen.

The desired page layout can be viewed at this link: https://i.sstatic.net/2nfqQ.png

HTML

<!-- top -->
        <div class="top js-top">
            <div class="container h-100">
                <div class="row h-100">

                    <div class="col-lg-12">
                        <h1 class="top-title">
                            <span class="top-title-1">text</span>
                            <span class="top-title-2">title</span>
                            <span class="top-title-3">text</span>
                        </h1>
                        <a href="" class="btn-redirect link-go-to-match">go</a>

                        <a href="#" class="btn-scroll-down js-btn-scroll-down"></a>
                    </div>
                </div>
            </div>
        </div>  
    <!-- /top -->

CSS

.btn-redirect {
    display: block;
    width: 301px;
    height: 57px;   
    /* Remaining CSS properties here... */
}

.btn-scroll-down {
    position: absolute;     
    /* Remaining CSS properties here... */
}

.top
{
    overflow: hidden;
    height: 100%;
    min-height: 100%;    
    /* Remaining styles here... */
}
.top .top-title
{    
    /* Remaining styles here... */
} 
/* Additional CSS classes omitted for brevity */

Link to JSFiddle example

However, on smaller screens, the .btn-redirect overlaps with other text elements as shown in this image: https://i.sstatic.net/1N7f6.png

Efforts to use Bootstrap 4's flex properties have not been successful in resolving the issue. Is there an alternative method to achieve the desired layout?

Answer №1

Implement the flexbox approach. Integrate a flexbox utility class for the container to occupy full height. This particular flex-fill class will be part of Bootstrap 4.1, as mentioned in this commit.

.flex-fill {
    flex: 1 1 auto;
}

Visit this link for more details.

<div class="top js-top vh-100 bg-danger text-white d-flex">
    <div class="container flex-fill">
        <div class="row h-100 align-items-center py-1">
            <div class="col-lg-12 h-100 flex-fill d-flex flex-column align-items-center">
                <div class="my-auto text-center">
                    <h1 class="top-title">
                        <span class="top-title-1">text</span>
                        <span class="top-title-2">title</span>
                        <span class="top-title-3">text</span>
                    </h1>
                    <a href="" class="btn btn-redirect link-go-to-match">go</a>
                </div>
                <a href="#" class="btn-scroll-down js-btn-scroll-down"></a>
            </div>
        </div>
    </div>
</div>

Transform the col-lg-12 into a flexbox column by adding d-flex flex-column. Then, group the h1 and go button within a div. Utilize my-auto to center the div vertically, allowing the button to naturally align at the bottom.

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

Looking to center the numbers in my ordered list within a border box - any tips on how to achieve this?

I'm attempting to create a numbered order list with a circular border around it. The goal is to have the number of the list item centered within the circular border and the entire circle centered within the paragraph. Currently, both the number and th ...

Can a linked checkbox be created?

Is it possible to create a switch type button that automatically redirects to a webpage when turned on by clicking a checkbox? I'm working on implementing this feature and would like to know if it's feasible. ...

Guide on exchanging the position of items in the second row of a grid

Could someone assist me in achieving a layout like the one shown in this grid? The desired result can be seen in this link: https://ibb.co/XbW025V. Here is my code for reference: https://jsfiddle.net/o0zjuyqb/1/ <div class="container"> ...

Assigning a value to a hidden field using a Bootstrap radio button

I'm having some difficulty setting the value of a hiddenfield using a Bootstrap radio button. Below is the code for the button group with the radio buttons and the hiddenfield. <div class="myRow"> <div class="smallCol"> ...

Hidden overflow and identification in URL causes content to be invisible and suddenly appear at the top of the page

I'm encountering a strange issue with containers that have overflow:hidden and when the page URL includes an id. The content gets pushed up and becomes invisible. This problem arises when I apply padding and negative margin at the bottom to create equ ...

Looking to transform an SVG into another?

After searching for an hour, I finally turned to SO and stumbled upon a tutorial on SVG shape morphing at https://css-tricks.com/svg-shape-morphing-works/. The tutorial instructs to create two SVGs with the same number of points and then provides code like ...

What are some effective methods for dynamically styling elements during iteration?

Need assistance with styling dynamic elements during iteration Located on the test.phtml page <?php foreach($tests => $test) { ?> <li class="<?php echo $test['class'] ?>"> <a href="#" title=""> Test In ...

Is there a way to smoothly toggle a class using fadeToggle in jQuery?

Currently, I'm utilizing the fadeToggle() function to switch between the visibility of selected elements. Upon page load, all elements in question are assigned the 'invisible' class (which applies the CSS property display: none;). I aim to ...

What is the best method to determine when 20% of a "<section>" element is within the user's view?

Looking at the layout, I have two sections that take up the entire page. My goal is to detect when the second section is 20% visible while scrolling, and then smoothly scroll to lock that section in place so it occupies the full space. This action should a ...

Shine a spotlight on the submit button when an input field is in focus in an

Embarking on my journey into front-end web development, I encountered a task of creating a subscription form. My goal is to make the subscribe button stand out when a user clicks on the input field. Is it possible to achieve this effect using CSS alone, wi ...

Adapt the size of HTML video tags based on the width of the screen and level of

Within the site (developed with React, utilizing Typescript and Tailwind), there exists a component that allows users to watch videos (constructed using the HTML Video tag). Additionally, beneath the video, there are various dropdowns and buttons. However, ...

Menu positioned offscreen in the bottom right corner

Trying to position the following code on the right side of the nav-bar, but it's only displaying half of the menu correctly. No additional CSS has been added, this is using the default material design lite code. The issue seems to be with my CSS http ...

Tips for resizing a browser window using an HTML element

I decided to add a unique feature to my website - a handle in the bottom right corner that users can grab and drag to the left to see how the design adapts to different browser window sizes (Responsive Design). To implement this, I included the following ...

What is the best way to adjust the size of an image within a div to

Here we go again... I've been searching for a solution but can't seem to find one. I have a container that occupies the left 50% of the screen. Inside this container, I want an image to fill the space without stretching. The current CSS is: heig ...

Is it possible to add additional text to an input field without modifying its existing value?

I have a numerical input field labeled "days" that I want to add the text " days" to without altering the actual numerical value displayed. <input type="number" class="days" (keyup)="valueChanged($event)"/> Users should only be able to edit the num ...

The Power of HTML Floating Elements

<nav> <div class="row"> <img src="resources/img/logo-white.png" alt="logo" class="logo"> <ul class="main-nav"> <li><a href="#">Food delivery</a></li> <li ...

organize multiple blocks in the middle of the page (html)

This code is unique and belongs to me. <html> <head><title>HOME||~All About EDM~</title> </head> <body > <center><img src="edm1.jpg" alt="edm" width="950" height="250"></body></center> <c ...

Is it possible to restrict the content of a DIV to only display horizontally

Within my div, I have a collection of elements that I would like to align horizontally. Initially, I tried using display : inline;, but encountered an issue where each element caused a line break when reaching the width of the div. I am in search of a CSS ...

Aligning images and input fields vertically with relative measurements

I'm looking for a way to vertically position two images, one on the left and one on the right, so that they are centered between labels and input fields within a containing div. Is it achievable using only relative lengths? Check out this jsfiddle fo ...

Having trouble with your jQuery animation on a div?

Check out this jsFiddle example: http://jsfiddle.net/uM68j/ After clicking on one of the links in the demo, the bar is supposed to smoothly slide to the top. However, instead of animating, it immediately jumps to the top. How can I modify the code to ac ...