Tips for vertically centering text within a textarea nested in a flexbox within a grid framework

Struggling to center text inside a textarea within a flexbox nested in a grid? Despite my efforts, I can't seem to crack it. Here's the Fiddle code snippet:

// Attempting to Center Text
const myDocumentAreaFiltersGlobalDisplay = document.getElementById("document_area_filters_global_display_id")

const myFiltersGlobalDisplayWrap = document.createElement("div");
myFiltersGlobalDisplayWrap.classList.add("myFiltersGlobalDisplayWrap");

// Creating TextArea Element
const myFiltersGlobalDisplay = document.createElement("textarea");
myFiltersGlobalDisplay.classList.add("myFiltersGlobalDisplay");
myFiltersGlobalDisplay.value = "CENTER ME PLEASE";

// Adding Reset Button
myFilterGlobalResetButton = document.createElement("button");
myFilterGlobalResetButton.textContent = "X";
myFiltersGlobalDisplayWrap.appendChild(myFiltersGlobalDisplay);
myFiltersGlobalDisplayWrap.appendChild(myFilterGlobalResetButton);

myDocumentAreaFiltersGlobalDisplay.appendChild(myFiltersGlobalDisplayWrap);
/* Styling for Grid Layout */
.document_grid {
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: 1fr;
  gap: 0px;
  grid-template-areas:
    "document_area_filters_global_display";
}

html,
body,
.document_grid {
  height: 100%;
  margin: 0;
}

textarea {
  border: 1 px solid grey;
  resize: none;
  width: 100%;
  padding-top: 0px;
  padding-bottom: 0px;
  overflow: hidden;
}

.myFiltersGlobalDisplayWrap{
  display: flex;
  flex-direction: row;
  align-items: center;
}

.myFiltersGlobalDisplay{
  line-height: 1;
  text-align: right;
  padding-right: 5px;
}
<div class="document_area_filters_global_display" id="document_area_filters_global_display_id"></div>

The goal is to set the textarea height to one line of text and ensure the button on the right matches the same height as the textarea.

Answer №1

After much consideration, I realized that using a textarea is not the best option for vertically centering text.

Therefore, I made the switch from a textarea to a div and also transitioned from a flexbox layout to a full grid layout to maintain consistency with the parent sections' layouts.

In the code snippet below, you can see that the content within the div is editable and the div will dynamically adjust its size based on the length of the text.

.color_green {
  background-color: lightgreen;
}

.document_area_filters_global_display {
  display: grid;
  width: 100%;
  grid-template-columns: 1fr auto;
  grid-template-rows: 1fr;
  grid-template-areas: 
  "filter_global_area reset_button_area";
}

.myFiltersGlobalDisplayWrap{
  grid-area: filter_global_area;
}

.myFiltersGlobalDisplay{
  margin-right: 5px;
  text-align: right;
}

.myFilterGlobalResetButtonWrap{
  grid-area: reset_button_area;
  height: 100%;
  justify-self: end;
  align-self: center;
}

.myFilterGlobalResetButton{
  height: 100%;
}
<div class="document_area_filters_global_display" id="document_area_filters_global_display_id">
  <div class="myFiltersGlobalDisplayWrap color_green">
    <div id="filters_global_display_id" class="myFiltersGlobalDisplay" contentEditable="true">Center me</div>
  </div>
  <div class="myFilterGlobalResetButtonWrap">
    <button class="myFilterGlobalResetButton" id="filters_global_display_reset_button_id">X</button>
  </div>
</div>

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

Update the class name by utilizing template literals

I'm currently in the process of mastering template literals as I have a project where I need to utilize them. However, I seem to be encountering an issue that I can't quite figure out. Here is the code that is currently working: const classes = ...

What is the best way to assign a value to an option element for ordering purposes?

My select element is being populated with fruits from a database, using the following code: <select name="fruitsOption" id="fruitsOptionId" ngModel #fruitRef="ngModel"> <option *ngFor="let fruit of fruits">{{fruit}}</option> </selec ...

Checkbox not working in Chrome [Version 101.0.4951.41 (Official Build) (64-bit)]

Update: It appears that the checkboxes in my project are not functioning correctly. Even when a standard checkbox was added to the HTML, it failed to respond to click events. I am currently developing a dat.gui menu for my THREE.JS project. You can find ...

Remove information from the database seamlessly without the need to refresh the page

I need assistance with deleting data from a database without having to refresh the page. While my current code works, it still requires the page to be refreshed after deleting a product. I am looking for a solution similar to this Below is the JavaScript ...

Trouble getting CSS and JavaScript to function properly with HTTPS?

It seems that the css and js files are functioning properly on , but not on . As a novice in web design and javascript, I am struggling to find a solution to this issue. Trying to link the CSS file with "//isdhh.org/css/style.css" has not resolved the pr ...

How can I verify an element's attribute while employing Event Delegation?

Is there a method to determine if a particular element has been activated using Event Delegation, based on its attribute, class, or ID? <ul> <li><button>Make the first paragraph appear</button></li> <li><butto ...

How to adjust the grid-gutter-width in Bootstrap 4.5 container?

If we want to change the $grid-gutter-width: 30px !default; variable specifically for the .container class, we can do so in the _variables.scss file. For example: .container { padding-left: 40px; padding-right: 40px; } Where can I make this custo ...

The Datalist HTML does not function properly with line breaks

<input type="text" list="req" name="req" style="width:350px; height:70px;"><datalist id="req"> <option value=""><option> <?php while($getreq = $requirements->fetch_array()){ ?> <option><?=$req = preg_r ...

Could React Router be the culprit behind the double rendering of my component?

Issue with Comments Rendering I am facing an issue where the comments component is rendering both on the index page and the item details page, instead of just the latter. I have a list of items where clicking on a detail link should take you to the item&a ...

What are the best ways to transfer information between functional components in a React application?

When working with React, data exchange between class-based components can be done using states and props in the following way: App.js import Name from './Name'; import React, { Component } from 'react' export class App extends Compo ...

Vue.JS has issued a warning indicating that the `util._extend` API is no longer supported. To address this, developers are advised to utilize Object

During the execution of a call API request, the Vue.JS application encountered an error. The API is hosted at Okta, and the request is successful when using cURL in the CLI. Error Message (node:82171) [DEP0060] DeprecationWarning: The `util._extend` API i ...

Show each text field individually in JavaScript

Is it possible to display text fields one by one upon button click? Initially, all text fields are hidden, but when the button is clicked, they should be displayed one after another with their visibility property set to visible? This is what I have attemp ...

What's the best way to trigger an event within a tag in a Vue.js component?

app.js Vue.component("popup",{ template : /*html*/` <div class="popup is-active" > <div class="popup-background"></div> <div class="popup-card"> <header class=" ...

A guide on displaying data in a table using a select dropdown in a pug file and passing it to another pug file

After creating a single page featuring a select dropdown containing various book titles from a JSON file, I encountered an issue. Upon selecting a book and clicking submit (similar to this image), the intention was for it to redirect to another page named ...

Switch up the placement of the boxes by moving them in different directions, such as

I've been attempting to recreate the button movement demonstrated in this link , but I'm having trouble achieving it. Using CSS animation, I can make the buttons move in a straight line, here's what I have so far: <div id="box" style=&ap ...

What is the reason for Bootstrap requiring both a class name and an ID name for collapse functionality?

When implementing the collapse effect in Bootstrap, why do buttons need to refer to the data-toggle=collapse class and the data-target=#collapseExample ID of the panel for the effect? Wouldn't simply targeting the ID be enough to toggle its state? Co ...

Adding attributes dynamically to input elements in a React render function

I currently have an input tag inside my render function which looks like this: <input id="time" type="time"> I am now looking to dynamically add the value attribute to it. Is there a way to achieve this in React? Thank you for your help in advance ...

The JavaScript syntax dollar sign

I am currently studying a JavaScript source code and it's my first time writing JavaScript. I find some of the syntax confusing. <script id="source" language="javascript" type="text/javascript"> $(function () { window.onload=function() ...

What is the best way to reorganize an object's properties?

Looking for a way to rearrange the properties of an existing object? Here's an example: user = { 'a': 0, 'b': 1, 'c': 3, 'd': 4 } In this case, we want to rearrange it to look like this: user = { &a ...

Tips for achieving an AJAX-like appearance in jQuery for my code

The question may seem a bit confusing, but here's the basic idea: I'm currently working on a JavaScript library and I want to incorporate some of jQuery's style. I have a function that will take in 3 parameters, and I want it to work simila ...