Slightly askew text

I am currently in the process of developing a calendar using Bootstrap and React, but I have encountered an issue with the calendar's design. The text on the top row that displays the weekdays is not aligning perfectly with the "day" items below it. These "day" items will eventually be replaced with numbers instead of text.

After experimenting with the code, I believe the problem is related to the length of the text itself. However, I am struggling to find a solution to ensure everything lines up correctly.

Here is the code snippet:

.calendar {
  text-align: center;
}

.calendar--weekdays li {
  padding: 5px 20px;
}

.calendar--week li {
  padding: 5px 20px;
}

.calendar--row {
  display: flex;
  justify-content: center;
  flex-direction: row;
}

.calendar--row p {
  padding: 10px 15px;
  margin: 0px;
}

.event__viewer {
  text-align: center;
}
<head>
  <!--  Font Awesome  -->
  <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
</head>
<body>
  <div id="container" class="container">
    <div class="row">
      <div class="col-lg-6">
        <div class="calendar">
          <h1>My Calendar</h1>
          <div class="calendar">
            <div class="calendar--row calendar--weekdays">
              <p>Sun</p>
              <p>Mon</p>
              <p>Tue</p>
              <p>Wed</p>
              <p>Thur</p>
              <p>Fri</p>
              <p>Sat</p>
            </div>
            <div class="calendar--row calendar--week">
              <p>day</p>
              <p>day</p>
              <p>day</p>
              <p>day</p>
              <p>day</p>
              <p>day</p>
              <p>day</p>
            </div>
            <!-- Additional week rows -->
          </div>
          <div class="calendar--tools">
            <i class="fa fa-arrow-left" aria-hidden="true"></i>
            <span> Month </span>
            <i class="fa fa-arrow-right" aria-hidden="true"></i>
          </div>
        </div>
      </div>
      <div class="col-lg-6 event__viewer">
        <h1>Events</h1>
      </div>
    </div>
  </div>
</body>

Any suggestions or thoughts on how to address this issue?

Answer №1

When using the <p> tag, the width is determined by the text inside it, leading to misalignment due to varying widths of different <p> tags. To avoid this issue, setting a fixed width for all <p> tags or using tables would be beneficial.

.calendar {
  text-align: center;
}

.calendar--weekdays li {
  padding: 5px 20px;
}

.calendar--week li {
  padding: 5px 20px;
}

.calendar--row {
  display: flex;
  justify-content: center;
  flex-direction: row;
}

.calendar--row p {
  padding: 10px 15px;
  margin: 0px;
  width: 30px
}

.event__viewer {
  text-align: center;
}
<head>
  <!--  Font Awesome  -->
  <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
</head>
<body>
  <div id="container" class="container">
    <div class="row">
      <div class="col-lg-6">
        <div class="calendar">
          <h1>My Calendar</h1>
          <div class="calendar">
            <div class="calendar--row calendar--weekdays">
              <p>Sun</p>
              <p>Mon</p>
              <p>Tue</p>
              <p>Wed</p>
              <p>Thur</p>
              <p>Fri</p>
              <p>Sat</p>
            </div>
            <div class="calendar--row calendar--week">
              <p>day</p>
              <p>day</p>
              <p>day</p>
              <p>day</p>
              <p>day</p>
              <p>day</p>
              <p>day</p>
            </div>
            
            (remaining code/template here)
      

Answer №2

It seems odd that you're not utilizing a proper table to display the information. However, if you prefer to stick with your current markup and still want it to resemble a table, you can make adjustments as follows to resolve the alignment problem.

Important styling:

.calendar {
  display: table;
  margin: 0 auto;
  caption-side: bottom;
}
.calendar--row {
  display: table-row-group;
}
.calendar--row p {
  display: table-cell;
}
.calendar--tools {
  display: table-caption;
}

Check out the updated version below:

.calendar {
  text-align: center;
  caption-side: bottom;
  display: table;
  margin: 0 auto;
}

.calendar--weekdays li {
  padding: 5px 20px;
}

.calendar--week li {
  padding: 5px 20px;
}

.calendar--row {
  display: table-row-group;
}

.calendar--row p {
  padding: 10px 15px;
  margin: 0px;
  display: table-cell;
}

.event__viewer {
  text-align: center;
}

.calendar--tools {
  display: table-caption;
}
<head>
  <!--  Font Awesome  -->
  <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
</head>
<body>
  <div id="container" class="container">
    <div class="row">
      <div class="col-lg-6">
        <div class="calendar">
          <h1>My Calendar</h1>
          <div class="calendar">
            <div class="calendar--row calendar--weekdays">
              <p>Sun</p>
              <p>Mon</p>
              <p>Tue</p>
              <p>Wed</p>
              <p>Thur</p>
              <p>Fri</p>
              <p>Sat</p>
            </div>
            <div class="calendar--row calendar--week">
              <p>day</p>
              <p>day</p>
              <p>day</p>
              <p>day</p>
              <p>day</p>
              <p>day</p>
              <p>day</p>
            </div>
            <div class="calendar--row calendar--week">
              ...
              
<!-- Code truncated for brevity -->
              
              ...
              
            </div>
          </div>
          <div class="calendar--tools">
            <i class="fa fa-arrow-left" aria-hidden="true"></i>
            <span> Month </span>
            <i class="fa fa-arrow-right" aria-hidden="true"></i>
          </div>
        </div>
      </div>
      <div class="col-lg-6 event__viewer">
        <h1>Events</h1>
      </div>
    </div>
  </div>
</body>

Answer №3

The issue causing the misalignment of weekday titles with the columns below is due to them being separate DOM elements without any direct correlation (except for their box model positioning where <div>s stack beneath each other).

In essence, all rows are centered as specified. The padding in the <p> tags containing the weekday titles affects the overall width when centering the row, so the combination of character width and padding determines the center alignment. However, this calculation does not take into account the rows below it.

To establish a connection between the rows, one solution suggested by some is using a <table>. While effective, there is widespread advice against utilizing <table> for layout purposes.

Alternatively, setting a min-width on all columns can help align the content to the center in each "cell," ensuring consistency in width across all cells in each column.

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

Updating a secondary state array is possible by modifying a JavaScript array with setState()

In my React application, there is a grid where names can be selected. When a name is chosen, the app retrieves corresponding data from a database and displays rows of information related to that particular name. Each row is represented as an object stored ...

What is the method to recommend certain pre-written text to the user within the tiptap text editor interface?

When a user clicks on the tip-tap editor, I would like to provide them with helpful suggestions in the form of preloaded text. If the user finds a suggestion that they like, they can save it to their database by pressing Enter or the right arrow key butt ...

How can I manually close a Material-UI Modal?

Utilizing the Popover component, which functions similarly to a Modal, I am faced with a test scenario like so: test('should close popover when clicked two times', () => { userEvent.click(targetElem); let popover = screen.getByTestId ...

Dynamic animation that creates an underlined hover effect for text-wrapped elements

While exploring the website , I came across a cool underline hover animation for their links. What caught my attention was how they've managed to disable the underline effect when the text inside an <a> tag wraps and spans multiple lines. I&apo ...

What is the best way to open an HTML file with Python without restrictions?

My HTML file is quite large, containing 4,574 words and 57,718 characters. However, when I try to read it using the .read() command, it only displays 3,004 words and 39,248 characters when I export it. Is there a way to read and export it without any lim ...

Utilizing React with Typescript to create JSX text translation files

My task involves translating text stored in a file... ///translations.txt const TEXT: { [x: string]: { [y: string]: string } } = { en: { joinNow: <React.Fragment>Join <b>Now<b/></React.Fragment>, signUp: <React.Fragmen ...

Using VueJS to integrate a CSS file into a minified CSS bundle in webpack

I am working on a project with Vue.js and I want to add my personal CSS files to the minified CSS file that is generated from the styles in *.vue files. In my App.vue file, I have: <style lang="scss"> @import '/static/css/AdminLTE.min.css&ap ...

When I try running my JavaScript code in the console, it works perfectly fine. However, I encounter an error when I attempt

Recently, I added a cool JavaScript code to my website that changes the background of the landing page randomly. Here is the snippet: var bgImages = [ "url('assets/bg/front1.jpg')", "url('assets/bg/fro ...

What are the differences between using .val() and .innerHTML?

When working with JQuery and trying to access elements, I have noticed that the method for retrieving content differs depending on the element type. For example, if I have a form with a textarea and want to get the text inside of it, I would use $("textare ...

Create a React component using the value stored within an object

I am interested in creating an object: import React from "react"; import { Registration } from "../../"; const RouteObj = { Registration: { route: "/registration", comp: <Registration /> } }; export default RouteObj; Next, in a separat ...

After deploying on Heroku, the Mongodb, Reactjs, Express, and Node.js project is showing a blank page and experiencing 404 errors

My app is functioning locally, with routes working via Postman and the database being accessed in Robo 3t. However, when deployed on Heroku, the app only displays a blank page upon launch. The console shows the following error: image description Here is t ...

Trouble retaining dropdown selection in Yii TbGridView when using pager

I've encountered an issue with Yii (1.1.x) while using TbGridView to present a bootstrap grid view with filtering and paging. Everything seems to function correctly, except when I choose an option from the 'fulfilled' dropdown (Yes, No, or ...

Transmitting video through a local area network

I am seeking a solution to stream video content to a local network with potentially over 1000 viewers. The streaming will need to be accessible via web browsers such as Internet Explorer, Chrome, and Firefox, as not all users have internet access due to co ...

How can HTML text be displayed based on certain JavaScript conditions?

By modifying the style of the text, I was able to implement basic functionality for validating correct answers. However, my main query is whether it is feasible to display a specific phrase upon achieving a perfect score. Upon analyzing the provided sample ...

Background filling the entire screen with a fixed position behind the elements on the page and in front of

Here are the two sample images I am using to give you an idea. This represents the background of the body: This is the background for the fixed position div: Above these two elements lies the website content. In between them, there are some moving stars ...

Basic AJAX call for newcomers

As a beginner, I've been searching for some straightforward AJAX requests. However, all I can find are bits of code that just don't seem to work. Can anyone lend me a hand and point out what's going wrong here? This is my Client-side code: ...

Refresh the value of a JavaScript variable on the server using a script executed on the client side

Hello, I'm working on a webpage that utilizes JavaScript to find the location of my device. I am interested in updating the variables within the JavaScript code from a script that runs on my laptop. Is this even possible? Below is the code snippet: i ...

Dynamic flexibility for content scrolling across components

Creating a mobile-friendly terms of service page with a title, content, and two buttons is my current project. The specific requirements are: All components should not exceed 100% of the viewport to allow for scrolling The buttons need to stay sticky at t ...

Can a column in ReactJS be protected from sorting or excluded from the sorting process?

Currently, I am new to ReactJS and working on a project that involves using a Data Table library that I found here: react-data-table-component My goal is to prevent the first column from being re-arranged or sorted once other columns have been clicked f ...

Transform regular hyperlinks into image hyperlinks

Is there a way to convert regular link tags into image tags using PHP? For example, how can I change the following: <a href="images/first.jpg">This is an image.</a> <a href="images/second.jpg">This is yet another image.</a> ...