Struggling to align text within an HTML section using Bootstrap across various screen sizes

I am struggling to center a section of text on my website across all screen sizes. The problem is that as the screen size increases, the text stays at the top, leaving empty space at the bottom and ruining the design.

Below, I have included the code and screenshots showing how the text remains at the top instead of being centered.

In the code, I have identified the part responsible for adjusting the padding from the top of the page. However, I am unsure how to make it center across all screen sizes. Any help would be appreciated.

body {
    width: 100%;
    height: 100%;
    font-family: Lora,"Helvetica Neue",Helvetica,Arial,sans-serif;
    color: #fff;
    background-color: #000;
}

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

h1,
h2,
h3,
h4,
h5,
h6 {
    margin: 0 0 35px;
    text-transform: uppercase;
    font-family: Montserrat,"Helvetica Neue",Helvetica,Arial,sans-serif;
    font-weight: 700;
    letter-spacing: 1px;
}

p {
    margin: 0 0 23px;
    font-size: 18px;
    line-height: 1.5;
}

@media(min-width:768px) {
    p {
        text-align: justify;
        text-justify: inter-word;
        margin: 0 0 20px;
        font-size: 20px;
        line-height: 1.6;
    }
}

a {
    color: #3bc692;
    -webkit-transition: all .2s ease-in-out;
    -moz-transition: all .2s ease-in-out;
    transition: all .2s ease-in-out;
    outline: 0!important;
}
@media(min-width:768px) {
    .adjust-this {
        padding-left: 55px; padding-right: 55px;
    }
}

.content-section {
    padding-top: 85px;
}

@media(min-width:767px) {
    .content-section {
        padding-top: 50px; /*This is where you can adjust padding for the section*/
        width: 100%;
        height: 100%;
    }
}

.content-section p {
    opacity: 0.9;
    font-family: 'Domine';
    font-size: 19px;
    font-weight: 500;
}

a:hover,
a:focus {
    text-decoration: none;
    color: #1d9b6c;
}

.greeting {
    padding-top: 5px;
    margin-bottom: 20px;
}
<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>About</title>

    <!-- Bootstrap Core CSS -->
    <link href="css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom CSS -->
    <link href="about.css" rel="stylesheet">

    <!-- Custom Fonts -->
    <link href="font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
    <link href="http://fonts.googleapis.com/css?family=Open Sans:400,700,400italic,700italic" rel="stylesheet" type="text/css">

</head>


<body>
 <!-- About Section -->
    <article id="about" class="content-section">
        <div class="container">
            <div class="row">
                <div class="col-sm-10 col-sm-offset-1 text-left adjust-this">
                    <h2 class="text-center greeting">Hi there!</h2>
                    <p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000
                    </p>
                    <p>
                    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

                    </p>
                    <p>
                    It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.                     
                    </p>

                    <p>
                      It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.
                      </p>
                </div>
            </div>
        </div>
    </article>
    </body>
https://i.sstatic.net/N5ZN7.png

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

Answer №1

Bootstrap doesn't have a built-in way to vertically center content inside a div. You are essentially stuck with setting padding-top to achieve vertical alignment, which may not be responsive on different screen sizes. Without specifying a height for your container, options like vertical-align: middle or flexbox centering won't work. However, if you do set a height, those methods could work well.

If you want to stick with your current approach, you can adjust the top padding based on screen size using media queries:

@media(min-width: /*Specify screen size here*/ ) {
    .content-section {
        padding-top: 200px; /*add required top padding here*/
    }
}

For a more complex solution, you could use jQuery to calculate window height and apply top padding as a percentage of that height. Add this code snippet at the beginning of your <body> tag:

<script>
$(document).ready(function() {
    var getPadding = function() { 
        $("#about").css("padding-top",function () {
            return (window.innerHeight*.15 + "px");
        });
    };
    getPadding();
    $( window ).resize(function() {
        getPadding();
    });
});
</script>

Here is a link to a CodePen example demonstrating this concept:

http://codepen.io/egerrard/pen/BLxBBw

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

Execute the button click function repeatedly at specific time intervals using Javascript

I have a submit button on my page that uses the POST method to send a value. <button id="log" class="btn bg-purple" formmethod=post value="2" name="start" formaction=start_server.php>Get Log</button> Clicking this button retrieves a log file ...

Enable my textbox to interpret the html img tag

Is there a way to display emoji images instead of emoji symbols when inserting them into my textbox? For example, can I show the image representation of ':)' instead of just the symbol itself? ...

Why is Google's No Captcha ReCaptcha failing to function properly

Seeking assistance with a PHP issue - I have implemented the no captcha reCAPTCHA widget, but I am struggling with a code snippet I found online. Whenever I try to submit an email, I receive a prompt to check the captcha, whether I click the box or not. F ...

What is the most effective method for sharing features while maintaining unique aesthetics?

In the process of developing a Next.js website, I've encountered a challenge: I have 3 forms that function in similar ways but have different styles. Instead of duplicating code for each form, I'm looking for a way to build the form structure on ...

Is your overlaying div functioning properly in Chrome and Firefox, but encountering issues in Internet Explorer?

I am facing a challenge with overlaying two divs on top of an image. My objective is to create a simple lightbox where when the user hovers over the left or right side, the cursor changes to a pointer and arrows appear. Below is the code I am using: HTML: ...

What could be causing the load() function in my AJAX to not work properly in this code?

I am trying to implement a script that loads a navigation menu (ul element) from one HTML page to a specific div on another page. The ul element has a unique id, and I am using this id to load only the ul element and not the entire HTML page it is a part ...

Inquiries regarding CSS consolidation in the latest version of Rails, Rails 7

Starting a new Rails 7 application with Bootstrap CSS and JS bundling, along with CSS bundling, has resulted in the following structure: app/assets/builds, app/images/foo.jpg, app/assets/stylesheets/application.bootstrap.scss I have utilized Yarn to add B ...

Issue with the scope of Bootstrap Accordion

Observing that the event triggers on a single Bootstrap accordion can affect all other accordions on the same page, I am wondering if there is a way to isolate this behavior without altering the markup or using $(this). Any suggestions? Check out this exam ...

Bootstrap 4's compacted navigation bar

Is it possible to create a collapsible navbar in my code? I am facing an issue where the navbar does not collapse when viewed on mobile devices. I tried using a button to toggle the visibility but it did not work as expected – the navbar either disappear ...

The dual-column format is experiencing technical difficulties

Having some trouble creating a two column layout in an HTML page. I've posted my code on jsfiddle http://jsfiddle.net/bobbyfrancisjoseph/eFMpJ/35/ Struggling to set a top margin for the inner container, even though I've specified it in the CSS ...

Tips for updating the text of an HTML element without deleting its children using JavaScript

Currently, I am implementing a code snippet to translate elements that contain the data-i18next attribute: const elementsToTranslate = document.querySelectorAll('[data-i18next]'); for (let i = 0; i < elementsToTranslate.length; i++) { ele ...

Exploring the functionality of Virtual Studio Design view while developing asp.net applications with compelling CSS design

When I use an external CSS to style my controls, the layout appears disorganized in the Design View of VS2008. Every time I make a modification to my CSS, I am unable to preview it in design view. Instead, I have to run the web page and open my browser to ...

Firefox hovers with a height of 0 effect activated

Hey there, I'm experiencing an issue in Firefox. When I attempt to use a hover effect on a div to change the height to 0, it's behaving strangely. Is there a way to make it function like it does in Chrome? Here is the code snippet: CSS : #squa ...

What is the best way to stack columns on small screens in a single row?

While utilizing the default bootstrap col classes, I noticed that when I reduce the grid to mobile size, the columns span the entire width of the row. This results in a long vertical list of items on mobile devices, even though there is sufficient space on ...

Tinybox is not built to execute any JavaScript code within its environment

I have been utilizing Tinybox (http://www.scriptiny.com/2009/05/javascript-popup-box/) to handle my popup functionalities. However, I've noticed that when I load a page in the popup that contains a script tag, it does not function properly. Any sugg ...

Is there a way to have the font size in an H1 adjust dynamically to fill the entire width of the H1 using just CSS?

My h1 Element <h1>Title Of Element<h1> The h1 has a width of 200px h1{width:200px;} Although the h1 is indeed 200px wide, the text inside does not fully utilize that width. I expected setting font-size to 100% h1{font-size:100%;} However, t ...

Unfortunately, the input type number does not allow for the removal of decimal points

I need assistance with modifying the HTML code below. I want to remove the decimal point from the input field. Could anyone please provide instructions on how to accomplish this? <input type="number" min="0" step="1" ng-pattern="/^(0|[1-9][0-9]*)$/" ...

What's the best way to ensure the BODY element occupies the full height of the HTML element?

Currently, I have implemented the sticky footer example code from Twitter Bootstrap. The sticky footer is functioning correctly, but my goal now is to extend the body (or a div) to occupy the remaining space by matching the height of the html element and a ...

Manipulating SVG filter attributes with CSS: A comprehensive guide

In my quest to master CSS animation, I found myself needing to manipulate the values of SVG filter attributes. While attempting to reference a CSS variable within the specularConstant attribute, I encountered an error from the browser. Is it feasible to ...

PHP A more organized method for passing button values when the value is specifically 0

Hey there, I've come across a situation where I had to make some tweaks to my code to get it working. I'm curious if there is a cleaner solution out there that I might be missing, or if my workaround is actually the best approach for solving this ...