我想將網格中flexbox的文本區域內的文本居中。我努力去實現這一點,但(對我來說)似乎是不可能的。小提琴:
// Prepare Filters Global Display
const myDocumentAreaFiltersGlobalDisplay = document.getElementById("document_area_filters_global_display_id")
const myFiltersGlobalDisplayWrap = document.createElement("div");
myFiltersGlobalDisplayWrap.classList.add("myFiltersGlobalDisplayWrap");
// myFiltersGlobalDisplay = document.getElementById("filters_global_display_id");
const myFiltersGlobalDisplay = document.createElement("textarea");
myFiltersGlobalDisplay.classList.add("myFiltersGlobalDisplay");
myFiltersGlobalDisplay.value = "CENTER ME PLEASE";
myFilterGlobalResetButton = document.createElement("button");
myFilterGlobalResetButton.textContent = "X";
myFiltersGlobalDisplayWrap.appendChild(myFiltersGlobalDisplay);
myFiltersGlobalDisplayWrap.appendChild(myFilterGlobalResetButton);
myDocumentAreaFiltersGlobalDisplay.appendChild(myFiltersGlobalDisplayWrap);
/* 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>
最后,我得出結論,如果你想讓文本垂直居中,文本區不是正確的選擇。
所以我從textarea換成了div。我還將flexbox布局改為全網格布局,以便與父部分的布局更加一致。 在下面的代碼片段中,div中的內容是可編輯的,并且div將根據文本長度自動增長。
.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>