Text not perfectly centered on the page

I'm working on a layout with 4 columns, and I want the first 3 columns to display text vertically. However, I'm having trouble aligning the text in the center of the column. Even after using justify-content and align-items properties, the text is still positioned to the right instead of the center. Can someone help me fix this issue?

body{
    margin:0;
    padding: 0;
    height: 100vh;
    width: 100vw;
}
.forAll{
    height: 100vh;
    display: flex;
    justfiy-content: center;
    align-items: center;
    text-align: center;
    padding: 0;

}
.verticalOption{
    transform: rotate(270deg);
    line-height: 10px;
}
.verticalOption a{
    text-decoration: none;
    color: white;
    font-size: 10px;
    line-height: 5px;
    white-space: nowrap;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<body>
    <div class="no-gutters">
        <div class="fluid-container d-flex flex-row-reverse">
            <div class="container col-xl-9 col-9 col1 forAll bg-primary"></div>

            <div class="container col-xl-1 col-1 col2 forAll bg-warning">
                <h1 class="verticalOption"><a href="#">Sample Text</a></h1>
            </div>

            <div class="container col-xl-1 col-1 col3 forAll bg-danger">
                <h1 class="verticalOption"><a href="#">Sample Text</a></h1>
            </div>

            <div class="container col-xl-1 col-1 col4 forAll bg-success">
                <h1 class="verticalOption"><a href="#">Sample Text</a></h1>
            </div>
        </div>
    </div>
</body>

Answer №1

By adding an additional flex container to the column, I have enhanced the control over alignment within the HTML nesting. The content of the column has been justified to the center and the h1 heading is now displayed inline-flex to eliminate any extra white-space.

Below is where all the classes have been incorporated:

<div class="container col-xl-1 col-1 col2 forAll bg-warning d-flex justify-content-center">
  <h1 class="verticalOption d-inline-flex m-0">
    <a href="#">Sample Text</a>
  </h1>
</div>

Demo

body{
    margin:0;
    padding: 0;
    height: 100vh;
    width: 100vw;
}
.forAll{
    height: 100vh;
    display: flex;
    justfiy-content: center;
    align-items: center;
    text-align: center;
    padding: 0;
}
.verticalOption{
    transform: rotate(270deg);
    line-height: 10px;
}
.verticalOption a{
    text-decoration: none;
    color: white;
    font-size: 10px;
    line-height: 5px;
    width: 200px;
    white-space: nowrap;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<body>
    <div class="no-gutters">
        <div class="fluid-container d-flex flex-row-reverse">
            <div class="container col-xl-9 col-9 col1 forAll bg-primary"></div>

            <div class="container col-xl-1 col-1 col2 forAll bg-warning d-flex justify-content-center">
                <h1 class="verticalOption d-inline-flex m-0"><a href="#">Sample Text</a></h1>
            </div>

            <div class="container col-xl-1 col-1 col3 forAll bg-danger d-flex justify-content-center">
                <h1 class="verticalOption d-inline-flex m-0"><a href="#">Sample Text</a></h1>
            </div>

            <div class="container col-xl-1 col-1 col4 forAll bg-success d-flex justify-content-center">
                <h1 class="verticalOption d-inline-flex m-0"><a href="#">Sample Text</a></h1>
            </div>
        </div>
    </div>
</body>

Answer №2

Simply apply text-align:center to your h1 element.

If that solution does not produce the desired result, it could be due to setting a fixed width for your a tag, which may exceed the container size of 200px.

Answer №3

To achieve the desired result, simply include the following two lines within the .verticalOption section:

vertical-align:center;
display:table-cell;

Make sure to remove the line containing justfiy-content: center;

Answer №4

Apologies for the error I made with justify-content: center - that was the root cause of the issue.

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

Designs for Creating Webpages

As a beginner in the coding world, I have little experience with HTML and Python. My goal is to create a personal webpage, starting with a template and customizing it to fit my needs. Any suggestions on where to find free templates or where to begin my s ...

Selenium automation tool encountering difficulty in finding an element within a dropdown menu

I encountered an issue with Selenium where it was able to click on the button that reveals a dropdown list, but unable to locate the list itself after it appears. Despite trying various methods such as find_element_by and Select module, along with switch_t ...

Considering the development of a web application similar to Canva

I'm interested in creating an app similar to Canva, but I'm not sure where to begin. I have a solid understanding of HTML and CSS, but my JavaScript skills are basic. I'm curious about the technologies they use. Is there a way to save HTM ...

Exploring Anchor Tags in Next.js

I'm having trouble using an anchor tag in Next.js Despite not receiving any console errors when setting it up and clicking the link, the page doesn't scroll to the specified id tag. A recent GitHub issue implies that a significant amount of cus ...

Concealing JW Player Video Link within Source Code

<div id="movie">...</div> <script> player("film").customize({ width:800, height:400, playlist: [{ file: "https://movielink.mp4", image: "https://moviepic.jpg", tracks: [{ file: "http://movi ...

Animating SVG from upper left corner to lower right corner

I am looking to create an animation on this SVG that starts from the top left and ends at the bottom right, similar to a gif. You can view the example I want to replicate in the following link: body { background: black } path { fill: white; } /* ...

Challenges encountered with Material-UI elements

Attempting to implement the http://www.material-ui.com/#/components/drawer (Docked Example) component from Material-UI in conjunction with ReactJS. An error is encountered with the "=" sign in this line: handleToggle = () => this.setState({open: !this ...

What is the best way to showcase an ajax response on an HTML webpage?

Apologies for the length of the code, but I'm looking to condense it while maintaining the same functionality. Is there an alternative approach or method that could achieve the same results? I receive data from PHP, which is then passed to JavaScript ...

Organize images with overlays to the center of the page

I need help centering a group of pictures (a 3x3 table) on my webpage. I was able to center them before adding image overlays, but now the images are showing up in the top left corner of the page. How can I group and center them, and how do I specify the i ...

jQuery - Event cannot be triggered on the 'deselect' or 'focusout' of <option> tag

Check out the demo here Hello there, I have a situation where I need an input box to appear below a select option, only if the user selects "Other". The code currently uses the .click() function on the option, but now I am trying to make the input box di ...

Error: The locator I used with the ID getUserById did not find any elements

Here is the code snippet from my HTML file: <h1>User list</h1> <button class="btn btn-primary" [routerLink]="'/register'">Register</button> <br> <br> <table class="table& ...

Enhance Animation Fluidity with HTML/CSS

I am experimenting with creating a floating animation for my iceberg logo by adjusting the top margin. Below is the CSS code I have implemented: img { height: 60px; padding: 5px; -webkit-animation: logofloat 1s ease-in-out infinite alternate; -moz ...

Is it possible to modify the background color of a checkbox?

I am looking to modify the background color of the checkbox itself, specifically the white square. I have spent a lot of time researching this topic but most articles suggest creating a div and changing its background color, which does not affect the whi ...

Instructions for adding up all the values in each column of an HTML table and showing the total in the footer of that specific column

Can someone help me with my HTML table setup? I have a table structure as shown below, and I am looking to add a "Total" row at the footer for each specific "quarter". "quarter1" "quarter2" "quarter3" "quarter4" 250000 115000 ...

Arranging date and time in jQuery based on AM/PM notation

I have written some JavaScript code to sort dates in ascending order (from newest to oldest). I have successfully sorted the dates, but I am having trouble sorting the time with AM or PM using a 12-hour format. I can do it in a 24-hour format, but not in ...

Is there a way for me to duplicate a complex element multiple times within the same page?

As an illustration, let's say I have a social tab located in my header that I would like to duplicate in the footer. This tab is comprised of various buttons with SVG images on top, event listeners linked to button IDs, and CSS styling. One option is ...

retrieve data with the help of DOMDocument

I'm currently working on extracting a specific value from the HTML snippet below using DOMDocument: <h3> <meta itemprop="priceCurrency" content="EUR">€ <meta itemprop="price" content="465.0000">465 </h3> The value ...

Modify the text or background shade of the Bootstrap date picker

I have successfully integrated the bootstrap date picker (view figure below) and it is functioning as expected. The code appears like this: <div class="input-group date"> <input name="departure" type="text" class="form-control" id="departure" ...

Unable to show the name of the chosen file

After customizing the input [type = file], I successfully transformed it into a button with a vibrant green background. Here is the code snippet that enabled this transformation: <style> #file { height:0px; opacity:0; } ...

Guide: "Changing an HTML element's class by utilizing its id attribute"

This is the HTML code snippet I am working with: <li class="treeview" id="account_management"> I need to select the element with the id of "account_management" and update its class from "treeview" to "treeview active" in order to ...