What is the best way to center content without aligning text in the center using Bootstrap?

When using Bootstrap, I have divided my main page into two grids to enable content to stack vertically in mobile view. However, I am struggling to center the text horizontally and vertically within the grid without aligning the text to the center using the d-flex Bootstrap function. You can see what I am trying to achieve with my code here, and what I currently have here (note that the navbar text sizes are what I desire, although they appear larger in the 'I want' image). Below is the code I have implemented so far:

*, *::before, *::after {
    box-sizing: border-box;
}

body {
    margin: 0;
    padding: 0;
    background-color: #EECBD1;
    font-family: 'Jost', sans-serif;
    font-family: 'Inter', sans-serif;
    min-height: 100vh;
}

.heaven-logo {
    /* max-width: 50%; */
    height: auto;
    padding-left: 5px;
    padding-right: 5px;
}

.nav-link {
    font-family: 'Inter', sans-serif;
    font-weight: 400;
    font-size: 100%;
    color: #000;
    display: flex;
    padding-right: 15px;
    list-style: none;
}

.nav-link:hover {
    color: #fff;
}

.navbar-nav {
    margin-left: auto;
}

.landing-main-text {
    font-family: 'Jost', sans-serif;
    font-weight: 600;
    font-size: 500%;
    line-height: 1.1;
    margin: none;
}

.landing-desc-text {
    font-family: 'Inter', sans-serif;
    font-weight: 300;
    font-size: 200%;
}

.macaron-image {
    width: 25rem;
    height: auto;
}
<!DOCTYPE html>
<html>
    <head>
        <title>Macaron Heaven</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <!-- CSS tag -->
        <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c3a1acacb7b0b7b1a2b383f6edf3edf1">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
        <!-- Google fonts -->
        <link rel="preconnect" href="https://fonts.googleapis.com">
        <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
        <link href="https://fonts.googleapis.com/css2?family=Inter:wght@100;300;400;500&family=Jost:wght@600&display=swap" rel="stylesheet">
        <!-- Custom CSS -->
        <link rel="stylesheet" type="text/css" href="app.css">
    </head>
    <body>
        <!-- Navbar -->
        <nav class="navbar navbar-expand" id="navbar">
            <div class="container-fluid">
              <a class="navbar-brand" href="#">
                <img src="img/logo.png" class="heaven-logo">
              </a>
              <div id="navbarNav">
                <ul class="navbar-nav">
                  <li class="nav-item">
                    <a class="nav-link" href="#">Selection</a>
                  </li>
                  <li class="nav-item">
                    <a class="nav-link" href="#">About Us</a>
                  </li>
                  <li class="nav-item">
                    <a class="nav-link" href="#">Contact</a>
                  </li>
                </ul>
              </div>
            </div>
        </nav>
        <!-- Landing page -->
        <div class="container-fluid" id="landpage">
            <div class="row">
                <div class="col-sm-12 col-md-6 d-flex flex-column justify-content-center align-items-center">
                    <p class="landing-main-text">The real macaron.</p>
                    <p class="landing-desc-text">Sweet, colorful delicacy that you can't have just one of.</p>
                </div>
                <div class="col-sm-12 col-md-6 d-flex flex-column justify-content-center align-items-center">
                    <img src="img/macaron-image.svg" class="macaron-image">
                </div>
            </div>
        </div>

        <!-- JS tag -->
        <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cdafa2a2b9beb9bfacbd8df8e3fde3ff">[email protected]</a>/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
    </body>
</html>

Answer №1

Instead of using the class justify-content-center on the Div for your text and the Div for your image, consider this alternative approach....

The Div containing the image should not have the d-flex class, but should only include the necessary col information. Additionally, I recommend adding the img-fluid class to the <img> element to ensure the image spans the full width of its parent Div.

For the Div with the text, incorporate the classes d-flex align-items-stretch to ensure both columns have the same height.

Within the text Div, wrap both paragraphs in a separate Div and apply the class

class="d-flex align-self-center"
to center this new Div within the existing column.

If the paragraphs do not wrap correctly due to having multiple paragraphs, you can try adding the flex-wrap class to the new inner Div, resulting in

class="d-flex align-self-center flex-wrap"
.

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

Fetch several images simultaneously from a photo collection using JavaScript by generating a batch process

I need help with creating an image gallery that allows users to download multiple images by selecting them. The download should result in a zip file. I have checkboxes for selecting the images, but I'm struggling to figure out how to enable the downlo ...

Using jQuery to modify CSS attributes is proving to be more challenging than I anticipated

I am attempting to modify a CSS attribute using jQuery. I have used the following code: wrapperInner.css('overflow', 'visible'); However, the value remains unchanged. When I use an alert alert(wrapperInner.css('overflow')), ...

Adjustment of Facebook button positioning in floating bar on Firefox

My floating bar currently has 5 social buttons, but I've noticed that the Facebook button is shifting about 20 pixels to the left. Could this be an issue with the CSS elements? After inspecting the elements, I found that when I remove position: absol ...

The image on the landing page is not properly scaling to fit the size requirements

Currently utilizing Bootstrap Studio, I incorporated the code background-size:cover. While it works well in desktop mode, it unfortunately isn't very mobile-friendly. Below are screenshots for reference: Mobile View Example Desktop View Example ...

What is the most effective way to choose and give focus to an input using JavaScript or jQuery?

How do you use JavaScript or jQuery to focus on and select an input? This is the relevant snippet of my code: <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> </he ...

Keep a consistent square proportion within a horizontal adaptable design

I am currently working on creating a responsive material list view that consists of cards with images, text details, and user actions. The challenge I'm facing is that the height of the cards can vary due to the different textual details, and the widt ...

Removing the empty option in a select dropdown

I am facing an issue with the code below: <select id="basicInput" ng-model="MyCtrl.value"> <option value=""></option> <option value="1">1</option> <option value="2">2</option> <option value="3"& ...

Switching off toggle does not switch back to primary theme when clicking again for the second time

When I click the button for the first time, it changes the theme. However, when I click it a second time, nothing happens; it seems like it's stuck on the second theme forever. It should change themes with every click. Can someone please help me with ...

Passing information from Vue to a modal

I'm working with 3 components: TaskList, TaskItem, and TaskForm. Within the TaskList component, there is a loop that renders all the data in TaskItem. Each TaskItem has an edit button meant to open a TaskForm modal (using bootstrap) displaying the sp ...

Using CSS to overlay multiple text boxes onto an image at different positions

Can you help me solve this challenge? I have an image of a real property that will be randomly loaded into a DIV with a width of 200 pixels. The image needs to be resized to 200 pixels wide while maintaining its proportional height. Additionally, I have fo ...

Decreasing the gap between the header and main content in Bootstrap, and the spacing between sections and containers in Bootstrap 5 cards

Is there a way to decrease the gap between the header and main sections in a Bootstrap 5 cards class, as indicated by the black line marked as A? Also, how can I adjust the spacing, specifically line B, between the section container (or <div> tag) a ...

How to verify in HTML with Angular whether a variable is undefined

When it comes to the book's ISBN, there are instances where it may not be defined. In those cases, a placeholder image will be loaded. src="http://covers.openlibrary.org/b/isbn/{{book.isbn[0]}}-L.jpg?default=false" ...

Struggling with the grid layout in percentage and feeling perplexed

As I delve into the world of grid systems, I find myself grappling to comprehend it fully. The complexities can sometimes leave me feeling inadequate in my understanding. From what I gather, a 12-column grid system without any margins means that the 12th ...

Guide to automatically triggering overscrolling

Currently working on a kiosk webpage designed for an iPad. Discovered an app that enables fullscreen mode, but requires overscrolling at the bottom. Is there a method to automatically trigger this on my webpage? Attempted to achieve this using autoscroll ...

What causes the difference in CSS border-bottom-width rendering between IE and Chrome?

My ASP:MENU is styled with some CSS properties, such as setting the 'height' and 'line-height' of each element to 15px. This design appears fine in Internet Explorer but seems too cramped in Chrome. Additionally, a 'border-bottom-s ...

Is it possible to call componentDidMount() multiple times in React?

I am in the process of converting an HTML API to ReactJS. The original HTML API is as follows: <script src="//dapi.kakao.com/v2/maps/sdk.js?appkey=3199e8f198aff9d5aff73000faae6608"></script> <script> var mapContainer = document.getE ...

Is it possible to insert Ruby variables into HAML css using interpolation?

I am in need of a sequence of classes, such as .module1, .module2, ... module(n). My goal is to specify these classes using css, ruby, and HAML: :css .mod1 { background-image: url("#{extra.image}"); } Can I use Ruby variables to streamline ...

Develop a dynamic daily messaging system

I'm interested in incorporating a daily changing message feature on my website. My plan is to store all the messages in a MySQL database and have them displayed daily. Any guidance on how to achieve this would be highly appreciated! Thank you, I ha ...

How come my jQuery isn't updating the class of a specific element?

I apologize if this question seems too specific to my own project, but I am facing a dilemma. I am attempting to change the color of the title of the user's current page to orange when hovering over the page name in the navigation menu. In simpler ter ...

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 ...