The hidden overflow property in CSS and floating elements

I need help creating a list of items with a cross icon in the middle on the right-hand side. I tried using an anchor with padding, floated it to the right, and relatively positioned it. The width and height of the icon are known.

Each item should also have text centered vertically, but the challenge is that the text can vary in length. I want to prevent any overflow or wrapping, ensuring the text does not overlap the icon and extends up to its edge.

Additionally, each item's width should expand to fit its parent while maintaining a fixed height.

For reference, you can view an example here.

div {
  background-color: red;
  padding-right: 50px;
  display: inline-block;
  width: 80%;
  vertical-align: middle;
  height: 50px;
}

p {
  background-color: yellow;
  height: 20px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  display: inline-block;
  max-width: 100%;
}

a {
  background-color: green;
  float: right;
  position: relative;
  padding: 10px;
  top: 6px;
  right: -44px;
}
<div><p>This is a short paragraph.</p><a>X</a></div>
<br /><br />
<div><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p><a>X</a></div>

If I remove the -44px;, the issue is clearer, but I'm unsure how to address it. It seems like such a minor problem, but I can't figure it out.

My question is: How can I ensure the cross stays in place without moving down? I want it to always resemble the first div.

Answer №1

To achieve the desired layout, consider using positioning instead of floating elements. Apply relative positioning to the outer div and absolute positioning to the x element. By placing the x 50% down from the top of the container's height and then adjusting it back up half of its height with a negative margin, you can center it vertically.

div {
  background-color: red;
  padding-right: 50px;
  display: inline-block;
  height: 50px;
  position: relative;
  max-width: 100%;
  box-sizing: border-box;
}

p {
  background-color: yellow;
  height: 20px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 100%;
}

a {
  background-color: green;
  position: absolute;
  padding: 10px;
  top: 50%;
  right: 4px;
  margin-top: -19px;
}
<div><p>This is a short paragraph.</p><a>X</a></div>
<br /><br />
<div><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p><a>X</a></div>

Answer №2

Is this information helpful?

div {
 background-color: blue;
 padding-left: 30px;
 display: inline-block;
 width: 70%;
 vertical-align: center;
 height: 40px;
position: relative;
 }

p {
 background-color: orange;
 height: 15px;
 overflow: hidden;
 text-overflow: ellipsis;
 white-space: nowrap;
 display: inline-block;
 max-width: 90%;
}

a {
 background-color: pink;
   float: left;
   position: absolute;
   padding: 5px;
   top: 10px;
   right: 5px;
 }

I adjusted the positioning of the div and aligned the icon to float from the left side. Let me know if this meets your requirements...

Answer №3

https://jsfiddle.net/qp4903m2/9/

Examine the code provided in the link above. One key adjustment to note is the removal of the padding-right property on the div and the right property on the a elements (steps 1 & 3). Instead, focus on setting the max-width of the p element to be equal to the full width minus 50px (step 2), allowing space for additional content like a cross icon.

div {
  background-color: red;
  /* padding-right: 50px; */ /* Step 1 */
  display: inline-block;
  width: 80%;
  vertical-align: middle;
  height: 50px;
}

p {
  background-color: yellow;
  height: 20px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  display: inline-block;
  max-width: calc(100% - 50px); /* Step 2 */
}

a {
  background-color: green;
  float: right;
  position: relative;
  padding: 10px;
  top: 6px;
  /* right: -44px; */ /* Step 3 */
}
<div>
  <p>
  This is a short paragraph.
  </p>
  <a>X</a>
</div>
<br />
<br />
<div>
  <p>
  Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
  </p>
  <a>X</a>
</div>

Answer №4

To improve the layout, it is recommended to make the following adjustments: set the div position to relative, change the a tag position to absolute with a right offset of 5px instead of -44px. Check out the live demonstration here

  div {
  background-color: red;
  padding-right: 50px;
  display: inline-block;
  width: 80%;
  vertical-align: middle;
  height: 50px;
  position: relative; /*set position to relative */
}

p {
  background-color: yellow;
  height: 20px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  display: inline-block;
  max-width: 100%;
}

a {
  background-color: green;
  float: right;
  position: absolute; /*Change from relative to absolute */
  padding: 10px;
  top: 6px;
  right: 5px;/*Update right offset to 5px*/
}
<div>
  <p>
  This is a short paragraph.
  </p>
  <a>X</a>
</div>
<br />
<br />
<div>
  <p>
  Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
  </p>
  <a>X</a>
</div>

Answer №5

By keeping the width fixed and expanding the text on hover, I have found a potential solution. Does this meet your needs?

div {
  background-color: red;
  padding-right: 50px;
  display: inline-block;
  width: 80%;
  vertical-align: middle;
  height:auto;
}

p {
  background-color: yellow;
  height: 20px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  display: inline-block;
  max-width: 100%;
}

.wrapR
{   
 max-width:80%;
    text-overflow: ellipsis;
    white-space: nowrap;
    overflow: hidden;
}

.wrapR:hover { 
 background-color: yellow;
overflow: visible;
}

a {
  background-color: green;
  float: right;
  position: relative;
  padding: 10px;
  top: 6px;
  right: -44px;
}

Answer №6

If you have a fixed width for element "X," it is possible to limit the maximum width of adjacent text to prevent any overlap or movement of "X."

div {
      background-color: blue;
      padding-right: 30px;
      display: inline-block;
      width: 70%;
      vertical-align: middle;
      height: 60px;
    }

    p {
      background-color: orange;
      height: 25px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      display: inline-block;
      max-width: calc(100% - 20px);
    }

    a {
      background-color: purple;
      float: right;
      position: relative;
      padding: 15px;
      top: 8px;
      right: -40px;
      width:12px;
    }

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

Web page adjusts to fit entire screen on iOS devices

After creating a mobile version of my webpage that functions well on Android and small browser windows, I encountered an issue with iOS. When users pinch the screen on iOS devices, it zooms out to fit the entire page, which is not ideal for a long page lik ...

In Vue, applying CSS styles to D3's SVG elements may not work as expected when using the scoped attribute

Here is the code snippet of my component: <template> <div id="something" class="card"> </div> </template> const height = 200; const width = 200; let graph = d3 .select('#something') .append('svg') ...

Pseudo Elements: The Transformative Power of Before and After

JS Fiddle Looking for some CSS help: <div id="strip"></div> This is my setup: width: 100%; height: 130px; background: grey; I'm trying to add a pseudo-element after the strip, positioned at the bottom. #strip:after { content: "" ...

Adding CSS files to your Flask Application - A Step-By-Step Guide

I'm working with a flask application that is functioning properly, but I would like to enhance it by incorporating my own custom CSS files. My project structure is as follows: my_app -app.py static/ -style.css templates/ ...

Retrieve the width of an element once the browser has finalized its dimensions

I am facing an issue with centering a pop-up box perfectly within the window using CSS properties. The code for the pop-up box styling is as follows: #pop_up { position: fixed; display: inline-block; border: 2px solid green; box-shadow: 0p ...

How to eliminate cell borders in a Vaadin table

I recently set up a table in Eclipse using Vaadin for assistance. After some trial and error, I successfully removed the borders of the table with the following line of code: tblResetButton.addStyleName(Reindeer.TABLE_BORDERLESS) ; However, there is sti ...

The CSS grid-template-area feature is not functioning properly in web browsers

Struggling with creating a responsive CSS grid layout for different screen sizes. I want to have five divs on desktop screens and adjust the layout for smaller screens. As a newbie in web development, I'm finding it challenging to get it right. .fir ...

How to create a see-through background using three.js

I am new to working with three.js and recently came across a codepen that caught my attention. However, I am facing difficulties while trying to change the background color. After exploring various questions related to this issue, I attempted to add { alp ...

When a nested CSS Grid is inserted into an HTML table element, it causes horizontal overflow

     Generating an invoice dynamically with a CSS grid poses some challenges. To maintain a specific format, I need to define the number of rows and columns in advance along with column widths. Using template rows and columns for this purpose restrict ...

Prevent Column Breaks in Bootstrap by Setting Minimum Width

I have the following code and I am trying to create two columns with a minimum width for mobile browser support. <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"> <div class="container"> ...

What is the best way to ensure that the positioning of the text adapts to

Whenever I adjust the screen size, the text in front of the slider moves. I utilized the grid system in Bootstrap 5.2 to position it. My goal is to keep the text in its original position relative to the carousel when changing the screen size. Here is my co ...

Fixed top Bootstrap navbar - featuring a logo on the left and navigation links on the right

Having trouble creating a responsive fixed navbar with logo and navlinks on both sides. I removed all Bootstrap elements from the code I'm sharing here, can someone help me out? I need a solution using Bootstrap to ensure everything is responsive. Th ...

Animate elements using fixed position without unexpected jumps

Trying to create an animation that expands a box to full screen. However, when I set the position to fixed before the animation begins, the box quickly moves up to the upper left corner. Is there a way to make it expand from its original location instead? ...

Troubleshooting Bootstrap 4/CSS: How to prevent bullet points from overlapping with input fields?

I am struggling with an unordered list where each list item contains an input element, and I am using Bootstrap framework. Let me share my code snippet: <html> <head> <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/boo ...

CSS code for creating a triangle underneath a menu item

Given that my client has presented me with a site design featuring a standard menu containing a downward triangle on the active item, I am feeling clueless about how to proceed. What would be the most effective approach for achieving this effect? https:// ...

Custom HTML select element not triggering the onchange event

I found a code snippet on https://www.w3schools.com/howto/tryit.asp?filename=tryhow_custom_select that demonstrates a custom select input with an onchange event. However, I am facing an issue where the onchange event does not get triggered when I change th ...

Tips for creating a centered Reindeer

My reindeer code seems to be a bit off-center. Even though I followed a tutorial, my reindeer is not aligning properly as shown in the tutorial. Here is all the CSS code for the reindeer: .reindeer { height: 510px; width: 350px; position: ab ...

Ensuring Consistent HTML5 Page Layout Across Various Browsers Using CSS

Hey everyone, I recently created an HTML5 page and utilized the <section> tag. However, I'm encountering issues with the CSS file I'm using. It seems to work perfectly on Firefox, but the layout is all over the place when viewed on Chrome a ...

Correct CSS essential for achieving flawless alignment of text and hyperlinks

Just starting out with CSS and I recently made some modifications to existing code: .feedback_h1 { width: 100%; float: left; margin: 0px; font-family: Arial; font-size: 12px; font-width: 400; color: #000000; margin-bottom: 15px; } <di ...

Javascript is experiencing a decrease in the variability of its content

I currently have multiple pages structured like this: <body> <table><tr><td align="center" width="100%"> --PAGE HTML-- </td></tr></table> </body> For a temporary period, I need to change the str ...