The unusual actions displayed by the initial row of a grid container

In an effort to familiarize myself with front-end development, I took on the challenge of creating a simple calculator. Opting to utilize the grid-layout due to the static nature of the content and my desire for flexibility in button sizes both vertically and horizontally, I configured a 4x6 grid that allowed me to layout everything exactly as intended.

To gain better insight into my issue, I have shared a working example on Codepen.io. The problem I encountered is related to the variation in row heights within the grid-layout.

Below is a snippet of the CSS used:

.calculator-grid-container {
      height: 70vh;
      width: 60vw;
      max-width: 50vh;
      border-style: solid;
      border-width: 3px;
      border-color: white;
      background-color:#2e323a;
      border-radius: 1%;
      box-shadow: 0 0 10px whitesmoke;
      /* grid container config */
      display: grid;
      grid-template-columns: auto auto auto auto;
      grid-gap: 0px;
      padding: 10px;
    }

The buttons are rendered within a div using the specified class:

render() {
    return (
      <div className='calculator-grid-container'>
        <Display current={this.state.current} formula={this.state.formula}></Display>

        <Button name='AC' handleButtonPressed={this.handleButtonPressed}></Button>
        <Button name='/' handleButtonPressed={this.handleButtonPressed}></Button>
        <Button name='*' handleButtonPressed={this.handleButtonPressed}></Button>

        <!-- More Button Elements Here -->
      
      </div>
    );
  }

I am utilizing flex-layout on the buttons to ensure text alignment:

.button {
  display: flex;
  justify-content: center; 
  align-items: center; 
}

For example, each button has specific styling such as:

.AC-button {
  grid-column-start: 1;
  grid-column-end: 3;
}

The main issue arises with the first row containing two paragraph elements:

render() {
    return (
      <div className='display'>
        <p className='formula'>
          {this.props.formula}
        </p>
        <p className='current' id='display'>
          {this.props.current}
        </p>
      </div>
    );
  }

The CSS applied to these elements is causing inconsistencies in row heights and element sizing:

.display {
  background-color: black;
  text-align: right;
  font-family: digital;
  padding: 5px 10px 0 0;
  /* grid config */
  grid-column-start: 1;
  grid-column-end: 5;
  /* Applying flex-layout inside the div to align the p-elements */
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: right;
}

.formula {
  opacity: 0.5;
  overflow-wrap: break-word;
  word-wrap: break-word; 
}

.current {
  font-size: 46px;
}

The identified problems include inconsistent row heights, varying size based on font-size changes, and fluctuating size when the .formula element is empty.

If you have insights or solutions to address these issues, your assistance would be greatly appreciated!

View screenshots showcasing the discrepancies: https://i.sstatic.net/ALPuh.png https://i.sstatic.net/t6RTm.png

Answer №1

After considering the issues mentioned, I developed a solution that addresses all of them successfully. This fix ensures that all rows have fixed and equal heights, regardless of the content within the elements involved.

grid-template-rows: repeat(6, 1fr);

By setting up 6 rows with each occupying an equal fraction of available free space, we achieve consistent height across all rows (a note on using fr: free space calculations are done after considering non-flexible items).

The key aspect I had overlooked was specifying the requirement for uniform row heights.

Kudos to @arieljuod for highlighting the default margins associated with the p-tag!

p {
  margin-top: 0; /* remove default margins */
  margin-bottom: 0;  /* remove default margins */
  min-height: 25%; /* prevent height from dropping to 0 in empty p-tags */
}

I observed that even though the row height remained stable, the p-tag would collapse to a height of 0 when empty, leading to some erratic behavior where the lower p-element appeared to shift position. To counteract this, I included min-height: 25% in the p-tag styles, ensuring a minimum height of 25% of available space is maintained at all times.

Check out how the updated layout appears:

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

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

Swap the hyperlink and heading upon clicking

I need to implement a function where a sort bar changes URLs and titles on click. For example, when a link like this is clicked: <a href="http://localhost/11/affiliate/?post_type=affiliate&orderby=title&order=asc ">Title ASC</a> It sh ...

Is there a way to align this button perfectly with the textboxes?

https://i.sstatic.net/ODkgE.png Is there a way to align the left side of the button with the textboxes in my form? I'm using bootstrap 3 for this. Here is the HTML code for my form. I can provide the CSS if needed. Thanks. h1 { font-size:83px; ...

I designed an innovative contact form utilizing mail function, yet unfortunately, the emails generated from it persistently land in spam folders. Below is the code I

I have created a contact form using the mail function, but all the emails are ending up in the spam folder. Here is my code: <?php // CODE FOR SENDING EMAILS if(isset($_POST['submit'])){ $to = "<a href="/cdn-cgi/l/email-protectio ...

Overridden CSS rules

Having a slight CSS dilemma. Within my webpage's html, I have the following structure: <div class='box-div'> <div>Entry 1</div> <div class='hide'>Entry 2</div> </div> Here is my associated ...

Improving Page Load Speed with HTML Caching: Strategies for Enhancing Performance when over half of the data transferred is for navigation menus

I manage a complex and expansive website that contains a significant amount of repetitive HTML elements such as the navigation menu and top ribbon. Loading a single page on my site can be resource-intensive, with up to 300KB of data required, half of whic ...

Tips for Enhancing the Appearance of a List Input Box

While on my hunt for an auto-completing text field, I stumbled across lists. <label>Bad Habit <input list="badhabits" id="habits" name="habit"/> </label> <datalist id="badhabits"> <option value="alcoholics"> <option ...

Tips on customizing the appearance of React rendering components

<div> <h3>{this.props.product.name}</h3> <h3>{this.props.product.code}</h3> {this.renderColors()} <article> <div da ...

What is the best method for accessing and showcasing images in an ionic app?

Having trouble displaying images after executing the following command: ionic run ios The default ionicframework template is being used with the sidemenu, and no changes have been made to the app directory structure. An inline css is being used to speci ...

Tips on how to showcase various information in distinct tabs using VueJS

I am currently working on a website project utilizing the VueJS framework and Vuetify template. My goal is to showcase different content under separate tabs, however, I'm encountering an issue where the same content is being displayed in all three tab ...

The radio buttons are stuck and not changing their selection

Having a group of radio buttons with the same name, when one is checked, it automatically selects another one in the group. Here is my current code: <input name="a" type="radio"> <input name="a "type="radio" checked> JS $("input[type='r ...

How can we prevent an unstyled list from causing text to drop to the next row?

As I am not an expert developer, I am in the process of creating a basic menu card using bootstrap. To keep it simple, I decided to use an unstyled list and added a span with float: right to ensure that the price is always aligned to the right. However, I ...

Tips for showcasing heading and paragraph elements side by side in css?

I have a block of HTML code that includes a heading and two paragraphs: <div class="inter"> <h4>Note</h4> <p>To add/remove a dependent or to modify your spouse's insurer information, go to the My Life Events section and foll ...

Arranging a Vertical Element Within a Rotated Holder

Who would have thought that geometry would come into play when working with CSS? I need help figuring out how to perfectly cover a rotated container with an upright background image. The goal is to hide the rotation on the sides and maintain dynamic contro ...

Error: Attempting to access a method pg_fetch_object() that does not exist

I'm a beginner in PHP and I am trying to display a single column from a table named music in my database called composer. However, every time I run the code, I encounter the following error message: Fatal error: Call to a member function pg_fetch_obje ...

Understanding the res.render method in JavaScript can be a bit tricky at first

In my spare time, I have been immersing myself in coding lessons and have encountered some puzzling aspects of the code: Firstly, there is a confusion surrounding the action attribute in HTML Secondly, this particular piece of code is causing me some b ...

What is the best way to center a specific element within a flex container?

Is there a way to center a specific div within a flex container? Let me explain using some code: Take a look at this code snippet: <html> <head> <style> div.flex_container { display: flex; flex-direct ...

Convert several XML nodes to display in an HTML format

I am currently facing an issue where I am trying to display all the nodes of a specific tag in XML. Although it does filter the data correctly, only one node is showing up. Is there anyone who can assist me with this problem? Below is the XML content: < ...

Unveiled absolute positioned element disrupts page layout when revealed

I'm facing an issue where an absolutely positioned element is causing content to move when it's displayed. You can see the problem in action by clicking on Profile in this fiddle. Can anyone help me understand what is causing this behavior and h ...

Creating Dynamic Height for Div Based on Another Element's Height Using ReactJS and CSS

I'm attempting to set a fixed height for a div in order to enable overflow scrolling. However, I am encountering issues as I am using JavaScript within a useEffect hook to accomplish this task. The problem is inconsistent as sometimes the height is se ...

Label for the checkbox is covering other content on the screen in 1024 x 768

Here is the JSP code snippet: <h:field path="configuredChannels" required="true" code="admin.menu.channels"> <div class="row-fluid" data-channel-checkboxes="#"> <form:checkboxes element="div class=&apos ...