Ensure that text remains centered within a resizable div without wrapping

My website features a sidebar menu that is resizable and has a collapse animation transition. Some of the text in this sidebar needs to be centered, but I've run into an issue - when using text-align: center; to format the text, it shifts around as you resize its container div due to the changing center point.

To address this problem, I resorted to setting white-space: nowrap; and manually adjusting the left padding to center the text. While this solution works well, the challenge arises with dynamically generated text that also requires centering. This means having to calculate the length of the text dynamically and adjust the padding accordingly, which doesn't seem like the right approach.

Is there a way to keep text centered in a div without shifting when the div is resized? It doesn't necessarily have to involve the use of the nowrap style.

You can view an illustration of the issue on jsfiddle here: https://jsfiddle.net/singerbradley/dvmauj3f/28/#&togetherjs=nvnJwReUA7

EDIT:

Let me clarify my goal - I want the text in the resizing div to remain centered even as the div is adjusted in width. Additionally, I prefer the text to appear cut off rather than wrap to the next line when the container shrinks. The desired outcome is demonstrated in the lower box, where the text remains stationary despite the container resizing (overflow: hidden; and white-space: nowrap; are used).

.enhance {
  border-color: red;
  border-style: solid;
  margin-top: 10px;
  margin-bottom: 10px;
  padding: 20px 0;
  width: 300px;
  overflow: hidden;
  transition: all .5s linear;
}

#wrap {
  text-align: center;
}

#nowrap {
  white-space: nowrap;
  text-align: center;
}

#wrap:hover {
  width: 50px;
}

#nowrap:hover {
  width: 50px;
}
<p style="width:500px;">
Hover over each box to see it in motion. The centered, line wrap text shifts with div resize.
</p>
<div id="wrap" class="enhance">This text has line wraps.</div>
<div id="nowrap" class="enhance">This text has no line wraps.</div>

Answer №1

To achieve this after making your edit, you will need to create a nested container and set its width equal to the parent container. Look for the <span> element inside the #nowrap div along with the additional CSS styles applied to it.

.enhance {
  border-color: red;
  border-style: solid;
  margin-top: 10px;
  margin-bottom: 10px;
  padding: 20px 0;
  width: 300px;
  overflow: hidden;
  transition: all .5s linear;
}

#wrap {
  text-align: center;
}

#nowrap {
  white-space: nowrap;
  text-align: center;
}

#nowrap span {
  display: block;
  width: 300px;
}

#wrap:hover {
  width: 50px;
}

#nowrap:hover {
  width: 50px;
}
<p style="width:500px;">
Hover over each box to see it in motion. The centered, line wrap text shifts with div resize.
</p>
<div id="wrap" class="enhance">This text has line wraps.</div>
<div id="nowrap" class="enhance"><span>This text has no line wraps.</span></div>

Answer №2

The text is now stable in its place, no more shifting around. I trust this will be beneficial.

.enhance {
  border-color: red;
  border-style: solid;
  margin-top: 10px;
  margin-bottom: 10px;
  padding: 20px 0;
  width: 300px;
  overflow: hidden;
  transition: all .5s linear;
}

.wrap {
  text-align: center;
}

#nowrap {
  white-space: nowrap;
}

#wrap:hover {
  width: 50px;
}

#nowrap:hover {
  width: 50px;
}
<div id="nowrap" class="enhance wrap">This text has line wraps.</div>
<div id="nowrap" class="enhance">This text has no line wraps.</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

JavaScript class name modifications are not functioning as expected

Testing a sprite sheet on a small web page I created. The code for each sprite in sprites.css is structured like this... .a320_0 { top: 0px; left: 0px; width: 60px; height: 64px; background: url("images/sprites.png") no-repeat -787 ...

Sort by label using the pipe operator in RxJS with Angular

I have a situation where I am using an observable in my HTML code with the async pipe. I want to sort the observable by the 'label' property, but I'm not sure how to correctly implement this sorting logic within the pipe. The labels can be e ...

What is the best approach for establishing a consistent font size and line height for a website using em units?

What is the optimal approach to ensure consistent typography across different browsers (font-size and line height) for an entire website with a fixed width of 970px, centered layout? It's common to receive designs from clients with varying font sizes ...

Enclose a pair of divs within a fresh div wrapper for better organization

Imagine you have the following: <div class="somediv"></div> <div class="somediv"></div> <div class="somediv"></div> <div class="somediv"></div> <div class="somediv"></div> <div class="somediv" ...

Accordion is having trouble expanding fully

My accordion is causing some trouble. When I try to open one section, both sections end up opening. I want it to toggle, so that only one section opens at a time based on my click. Switching the display from flex to column fixes the issue, but I don' ...

Troublesome tab key function in FireFox causing navigation issues

I'm facing an issue with the tab key focus on links in FireFox. Strangely, it's working fine in Chrome but in FireFox, it keeps looping within one element. For better understanding, I have created a demo showcasing this behavior specifically in ...

Condense: Attaching the footer firmly to the bottom of the webpage

After creating a website with Distill in R, I've realized that the footer tends to float halfway across the page when there isn't enough content. Is there a way to ensure that the footer stays fixed to the bottom of every page? ...

Hide the first dot indicator on the carousel when it is clicked

I've been experiencing a strange issue with my carousel - whenever I click on any of the dots, dot1 disappears. Upon inspecting the CSS, I found out that when clicking on a dot, it receives a display:none property from somewhere. Here is the code for ...

Retrieve the order in which the class names are displayed within the user interface

In the following code snippet, each div element is assigned a common class name. <div id="category_9" class="list_item" data-item_ids="[38]"</div> <div id="category_2" class="list_item" data-ite ...

ChakraUI failing to display on the screen

Exploring Chakra UI in my latest project has been an exciting journey for me. I made sure to install all the necessary dependencies and started importing components from the ChakraUI Docs. Initially, I successfully imported and rendered the button feature ...

What is causing the table to not be displayed in this Javascript program when it is used in a

I am currently experimenting with incorporating an infinite loop before the prodNum and quantity prompts to consistently accept user input and display the output in a table. Although the program is functional when executed, it fails to showcase the table ...

If an input type="file" is empty, then $this->form_validation->run() will return false

Whenever I make changes to my settings and leave the input type="file" empty, $this->form_validation->run() consistently returns false. Surprisingly, I haven't utilized setrules or required it. Below is the snippet of my code: public function ...

Using Angular to condense or manipulate an array

I am working with a JSON response that contains arrays of arrays of objects. My goal is to flatten it in an Angular way so I can display it in a Material Table. Specifically, I want to flatten the accessID and desc into a flat array like [ADPRATE, QUOCON, ...

Crafting artistic shapes using the Canny Edge Detection technique on Canvas

Can someone provide some guidance on using Canny Edge Detection to generate shapes in Canvas? ...

Guide on transforming the popup hover state into the active state in vue.js through a click event

My code currently includes a popup menu that shows up when I hover over the icon. However, I need to adjust it so that the popup changes its state from hover to active. Intended behavior: When I click the icon, the popup should appear and then disappear w ...

a practical guide on implementing overflow-x in the row class

How can I implement a row scroll overflow on the x-axis for columns 5, 6, and 7? <div class="container-fluid"> <div class="row"> <div class="col-4">col 1</div> <div class="col-4">col 2</div> ...

What is the best way to make the Bootstrap navbar stay fixed at the top of the

I am currently experiencing an issue with the Bootstrap navbar while using version 5.0. Whenever the Bootstrap navbar expands, the content below it also moves down, which is not the desired behavior. I want the content to remain in place. I tried set ...

CSS: Set the left position to 250px with the added feature of hiding any overflow

I am currently working on a sidebar that can be shown or hidden using JavaScript with CSS animations. To achieve this effect, I need to position the content div (#main) using left: 250px;. #main { left: 250px; position: relative; overflow: hidden; } ...

What is the best way to stack text boxes vertically in CSS/HTML?

How can I arrange these textboxes so that .textbox appears at the top, followed by textbox1 below? CSS .textbox { border: 1px solid #848484; -moz-border-radius-topleft: 30px; -webkit-border-top-left-radius: 30px; border-top-left-radius: ...

Avoiding Navbar Link Clicks due to Z-Index

Is it possible for the z-index or positioning of an element to hinder a user from clicking on a navbar link? When I visit my website , I find that I am unable to click on any of the links in the navigation bar. Could this be due to the z-index or position ...