The centrally-aligned flexbox div remains at a fixed size, preventing any text truncation within its nested elements

My goal is to truncate the lengthy text in the span with the id of first-item when the screen size decreases. However, I am encountering an issue where setting margins on the parent element prevents it from shrinking, thus causing the truncation not to work as intended. I suspect that this problem is related to a discussion on Why don't flex items shrink past content size?, but despite my efforts, I have been unable to resolve it.

body {
  display: flex;
  flex-direction: column;
}

main {
  display: flex;
  flex-direction: column;
  margin-left: auto;
  margin-right: auto;
}

#container {
  display: flex;
}

#first-item {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
<body>
  <main>
<div id="container">
  <span id="first-item">Long long long long long long long long long long long long long long long long long long long long long long long long text to be truncated</span>
  <span>Shorter text</span>
</div>
  </main>
</body>

UPDATE: After receiving a comment from @AmauryHanser pointing out a typo in my initial post, which has since been corrected. Despite the appearance of an ellipsis, the text can only shrink up to a certain point, indicating that the issue persists. Refer to the screenshot below for clarification.

https://i.sstatic.net/6c7ze.png

Answer №1

Remember to include max-width:100% in the main element

body {
  display: flex;
  flex-direction: column;
}

main {
  display: flex;
  flex-direction: column;
  max-width: 100%;
  margin: auto;
}

#container {
  display: flex;
}

#first-item {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
<body>
  <main>
    <div id="container">
      <span id="first-item">Long long long long long long long long long long long long long long long long long long long long long long long long text to be truncated</span>
      <span>Shorter text</span>
    </div>
  </main>
</body>

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

How to position a Navbar on top of a grid background structure

I am currently working on developing a responsive website, and I have encountered a challenge that I need help with. I am looking to create a header using a grid system, where I have divided the header into two parts: col-md-4 : For an Image col-md-7 : ...

Incorporating HTML elements within VBScript within a single document

Here is a code snippet that mixes VBScript and HTML: IF (x.name="name") THEN n=x.value response.write("<tr>") response.write("<th>Name:</th>") response.write("<td><input name=""n2"" value=" & n & ">< ...

Creating sequential numbers using jQuery

Recently, I worked on a script (credit to Chyno Deluxe) that generates a list of menu items based on what is written in the input box. However, I now have a new requirement which involves generating a sequence of numbers to be added continuously. Here is ...

Please explain the steps for creating a responsive grid layout

*{ margin: 0; margin-bottom: 200px; padding: 0; border: 0; outline: 0; font-size: 100%; vertical-align: baseline; background: transparent; } .grid-container { position:relative; display: inline-grid; grid-template-col ...

Ways to gather all the elements on each page

Currently engrossed in a web scraping endeavor for a car website utilizing Selenium. One potential roadblock I've encountered is that the code seems to only iterate over the initial car element on the page, instead of going through the entirety of ava ...

How to make text in HTML and CSS stay at the bottom of a div?

I am in the process of creating a website dedicated to my "Starcraft II" clan. My goal is to have the navigation bar display the text "ALLOYE" and remain fixed at the bottom of the screen. I attempted to achieve this using the following code: vertical-alig ...

How to centrally position an image within a cropped parent container using CSS

Is there a way to center an img within a parent div tag that has overflow: hidden set? I am looking to clip the image equally on both sides in order to display the middle portion. <div class='wrapper'> <img/> </div> The desi ...

What is causing my mobile version not to resize and adjust to fit the screen properly?

Currently utilizing Bootstrap 4 along with the viewport meta tag, %meta{:content => "width=device-width, initial-scale=1, shrink-to-fit=no", :name => "viewport"}/ Even after hiding all images with .hidden-xs-up, the display on Chrome mobile appear ...

Vertical Spacing in Bootstrap 3: Eliminating Gaps Between Divs

I am looking to eliminate the gap between certain div elements and the div positioned below it. This space is created when one div is taller than another in a column. For instance, take a look at this example: http://www.bootply.com/Hc2aO5o4bG Essential ...

What is the reason behind needing to restart the server each time I make a change to my React application?

I'm diving into my first project using React, stepping away from my usual tools of pure HTML, JavaScript, PHP, and Node.js. I decided to create a single-page application portfolio, but I've encountered a frustrating issue. Every time I make a cha ...

How should we arrange these components to optimize the overall aesthetic of this particular design?

My current progress on the code: Visit this link Desired design for reference: View design picture here I've experimented with position relative and margins, but these techniques have impacted the responsiveness of my webpage. What is the most eff ...

Is there a way for me to acquire the Amazon Fire TV Series?

I'm currently working on developing a web app for Amazon Fire TV, and I require individual authorization for each app. My plan is to utilize the Amazon Fire TV serial number for this purpose. However, I am unsure how to retrieve this serial using Jav ...

Is there a way to create an image gallery layout similar to Pinterest using CSS?

I am currently developing a dynamic PHP gallery that will feature thumbnails with the same width but varying heights. These thumbnails will be arranged from left to right, so I would prefer not to use a traditional five-column layout. I suspect that achiev ...

The words spill across the navigation bar

I am facing an issue with my navbar where the text overflows and creates extra space due to a white background. I have tried resolving this for a while but haven't been successful. I need help from someone knowledgeable in this area. Here are some im ...

All hyperlinks are displayed as a single entity

I have 5 image links displayed side by side, but when I hover over them, they act as one collective link. Can someone please assist me with resolving this issue? After updating my code to include the entire div, it seems that there is something within the ...

What is the best way to enlarge the image to a specific percentage?

Although I have never programmed in HTML or worked on web projects before, I am currently using a git repository where the project is client-server. This means I need to modify the code to suit my requirements, which includes working with HTML. My problem ...

The navigation bar triggers the opening of all icon menus in their designated positions at the same time

This code snippet represents the navigation bar for an admin user, featuring 3 icons: navigation menu, user menu, and manage button icons. The problem arises when clicking on any of these icons, as all dropdown items from each icon are displayed in their ...

Superceding Meta Character Encoding Statements

Have you ever wondered what happens when two older-style character encoding declarations are used in a webpage? Will the first declaration override the second, or vice versa? Or will they both be ignored and excluded from the encoding algorithm? For Ex ...

PHP allows for creating dropdown lists where the values are dynamically dependent on the selection of another dropdown list within the same form

Is there a way for me to implement this solution? The values in the dropdownlist are based on another dropdownlist within the same form. For example, a form with dropdownlists for car names and models of that car, along with a search button. Please take no ...

Resolving the Table Issue with 'onclick' in Javascript

Apologies for the lack of creativity in the title, I struggled to come up with something fitting. Currently, I am engaged in the development of a user-friendly WYSIWYG site builder. However, I have encountered an obstacle along the way. I've devised ...