Adjustable-width div contained within a static-width div

Context: (summarized)

<div style="width:300px;" >
    <img src="blabla..." />
    <div style="oveflow:hidden; etc..." >
        description
    </div>
</div>

Issue:
The challenge arises when the description is too long and gets cut off due to "overflow:hidden;" property. To resolve this, a CSS animation triggers on hover, shifting the description by "margin-left:-200px;", enabling users to view the complete text gradually.
However, adjusting the margin left by -200px is not always sufficient or can even be excessive depending on the length of the description.

Possible Solutions:
1)

Javascript/jQuery

My website operates without JavaScript, hence I prefer a JS-free approach. While there are various JS solutions available online, I aim to tackle this issue purely with CSS.

2)

<marquee>description</marquee>

This method is outdated.

Alternative Approaches:
One idea involves implementing PHP code to calculate the length of the description dynamically and set the margin-left accordingly. However, this may lead to a convoluted PHP file bypassing the simplicity of using CSS stylesheets. Are there any CSS3 techniques that address this issue? Despite encountering similar challenges among designers, no definitive solution has been found through CSS3 features. Is this limitation within the scope of CSS language?
Thank you.

PS: The description should strictly occupy 1 line. No line breaks, scrollbars, or "show more" buttons permitted.

Answer №1

  1. One option is to utilize the PHP echo function with the <br/> tag to create line breaks at specific intervals.
  2. Alternatively, you can employ CSS and specify a maximum width using the max-width property to constrain the content within a certain limit.

Answer №3

Here is my best attempt at the solution.

I do have a slight concern that the slide may end with the description hidden, but I believe it's a minor issue compared to the overall criteria being met.

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.wrap {
  width: 300px;
  margin: 1em auto;
  border:1px solid green;
  display: flex;
  flex-direction: column;
}

.wrap img {
  max-height:100%;
  width: auto;
}

.desc {
  width: 300px;
  height: 15px;
  white-space:nowrap;
  overflow: hidden;
  font-size:13px;
  line-height: 15px;
  position: relative;
}
.wrap .desc p{
  position: absolute;
  transform: translateX(0);
  transition: transform 5s ease;
}

.wrap:hover .desc p{
  transform: translateX(-100%);
}
<div class="wrap" >
  <div class="image">
    <img src="http://lorempixel.com/output/abstract-q-c-300-300-10.jpg" alt="" />
  </div>
    <div class="desc" >
        <p>Tri-tip shank turkey chuck, porchetta drumstick andouille beef ribs prosciutto salami beef. Ham hock pork belly pig pastrami. </p>
    </div>
</div>

Check out the Codepen Demo here

Answer №4

One solution I recommend is setting fixed dimensions for the description container and implementing the CSS attribute overflow-y:auto.

The standard scrollbar can vary in appearance across different web browsers, so you might consider using a JavaScript plugin. However, if you prefer not to introduce additional libraries for a simple task, sticking with CSS may be the way to go.:)

Answer №5

Give this a try:

div.solution {
        white-space: nowrap; 
        width: 12em; 
        overflow: hidden; 
        height:30px;   
    }
    div.solution:hover {
        text-overflow: inherit;
        overflow: visible;
    }

    <p>Hover over the two divisions below to reveal the complete text.</p>

    <div class="solution" style="text-overflow:ellipsis;">This is a lengthy piece of text that won't fit in the container. This is a long text snippet that will not fully display within the boundaries.This is a long text snippet that will not fully display within the boundaries.This is a long text snippet that will not fully display within the boundaries.This is a long text snippet that will not fully display within the boundaries</div>
    <br>
    <div class="solution" style="text-overflow:clip;">This is some lengthy content that exceeds the box size.</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

Fixed position does not adjust based on the screen resolution

If I were to create a web page, I would include a main menu that always remains on the screen, similar to this design: https://i.sstatic.net/I57Xk.png There are no issues with screen resolution exceeding 1300px, as seen in the image above. However, when ...

Creating three select tags using PHP and Ajax

Today is my first experience with AJAX, and it's actually easier than I had imagined. I created a select tag that opens another select tag and it works perfectly. Here is the code... test.php <?php require'models/category.php'; $object= ...

Using jQuery .animate() leading to erratic input movements

I am currently utilizing jQuery's .animate() feature to create a smooth animation effect on the width of a <div> element when a child <input> is in focus. Nevertheless, I'm encountering an issue where the input field jumps up and down ...

Guide to utilizing JavaScript for a basic gaming experience

Looking to incorporate multiple divs that will vanish upon being clicked, while also increasing the score by 1 through javascript. Any suggestions on how to accomplish this? ...

What could be causing the vertical alignment issue of this logo in its parent container?

I am having an issue with the vertical centering of the logo element within the <header> container on this page. It seems to be more pronounced on mobile devices compared to desktop. However, the second element (#forum-link) is aligning correctly. W ...

Gather numerical data from an iframe and calculate the total of all the numbers

When dealing with the expression 1 - 0, the goal is to capture these two numbers, add them together, and present the resulting value on the page. in this instance, you would sum 1 + 0 to get the result of 1 and dynamically update this total so that it chan ...

The website is experiencing crashes in IE11 whenever the developer tools are closed

I'm experiencing an issue with my website where it crashes internet explorer if the developer tools are not opened. I have checked for console.log calls as a possible cause, but that doesn't seem to be the problem here. The error is not just di ...

What is preventing me from clicking on the left side of the link on this CSS flip card while using Chrome browser?

When attempting to click the left side of the link on the back of this flip card in Chrome, it does not respond. However, hovering over the right side allows interaction with the link: /* Vendor prefixed by https://autoprefixer.github.io */ .card { ...

Align an element to the right side of the webpage using CSS

I've been struggling to align elements to the right side of the page using various methods like float and flex, but nothing seems to work. To make it easier to understand my issue, I've attached a picture that illustrates what's going on. I& ...

Having trouble retrieving JSON data from the open weather API

I'm working on a small website to display the weather of a specific city using an API. However, I am facing an issue where I can't seem to display the temperature from the response. Below are snippets of my JavaScript and HTML files: var ap ...

Accessing an HTML DOM element and assigning it to the value of an input tag

One of the elements on my webpage has the unique identifier last_name. I am trying to extract its value and pass it to an input tag. My attempt at achieving this is shown below, but it does not work as intended. <input type="hidden" name="lastName" va ...

Javascript code fails to execute properly on IE8

I have a scenario where I am working with two drop-down menus. When the user selects an option from one menu, it should dynamically change the options in the other menu. However, I'm facing an issue with Internet Explorer where the second drop-down me ...

What is the best way to make a DIV box span the entire webpage without overflowing to the left?

I am currently working on a client's website and running into some issues with the layout of the booking page. The address is supposed to be on the left side, the contact form on the right, and both should be contained within a white box that fills th ...

The issue of the back button not functioning in the React Multi-level push menu SCSS has

I have been developing a mobile-friendly Multi-level push navigation menu for my website using dynamically generated links based on projects from my GitHub account. I found a push menu on CodePen here and am in the process of integrating it into React inst ...

Steps for sending data to a modal window

Consider this scenario: I have a function that retrieves some ids, and I utilize the following code to delete the corresponding posts: onClick={() => { Delete(idvalue[i]) }} Nevertheless, I am in need of a modal confirmation before proceeding with the ...

When the user clicks, automatically direct them to a different webpage

I tried to create a changing background color effect in the following order: Black, White, Red, Green, Blue. <body style="background-color:black"> <script type="text/javascript"> function changeColor() { var currentBg ...

Adjust `theme-color` as the class and theme of the page are switched

My website has a JavaScript toggle that changes the color/theme of the page by adding/removing classes on the html element. The default theme is white, the second theme is black (.dark-mode), and the third theme is beige (.retro). The JavaScript code for ...

How can I access the value of a textbox located in the footer row of a gridview control using JavaScript?

Below is the code snippet for a gridview in an aspx page, where the gridview is placed inside a form tag. <Grdview:GridViewExtended ID="GridView1" runat="server" AutoGenerateColumns="False" CssClass="Grid" onpageindexchanging="GridView1_Pa ...

Unable to resolve the problem related to <ul> and <li>

I am currently working on integrating a chat snippet into an angular project. The challenge I am facing is that I cannot use jquery components, so I only need the HTML and CSS parts. You can find the original snippet here: After copying it to Codepen, I n ...

The desired outcome is not showing up as expected in the HTML coding

<!DOCTYPE html> <head> <link href="https://fonts.google.com/specimen/Poppins?preview.size=20" rel="stylesheet"> </head> <body> <header class="header"> <div class="container"> <div class="row"> ...