The text should be wrapped to overlap with an image positioned above it

Currently, I am in the process of enhancing a webpage that contains a significant amount of old legacy code. One issue I have encountered pertains to text wrapping within a fixed-height and width container, as illustrated in the image below:

https://i.sstatic.net/FiMPW.png

The first image shows the default behavior when the text is short enough to fit on one line.

The second image showcases the desired outcome: If the text needs to wrap, the container should expand in size accordingly without overlapping, similar to what is shown in the third image.

Removing the fixed-width or fixed-height constraints is not an option as it results in unpleasant inconsistencies. This was the initial problem that I aim to address.

I have attempted to resolve this by using z-index and adjusting positioning, but so far, I have had no success.


An example of a line that wraps due to its length.

.element {
  margin: 15px 7px;
  display: inline-block;
  overflow: hidden;
  position: relative;
  color: #222;
  max-height: 523px;
  max-width: 300px;
}
.element img {
  vertical-align: top;
}
.element h2 {
  text-align: center;
  background: #333333;
  padding: 12px 0;
  font: 400 16px/22px 'Roboto Slab',serif;
  margin: 0;
  color: #fff;
  border-top: 1px solid #333333;
  text-transform: uppercase;
}
.element h2 a {
  color: #fff;
  text-decoration: none;
  font: 400 16px/22px 'Roboto Slab',serif;
}
.element span.view {
  display: block;
  text-align: center;
  background: #fff;
  padding: 12px 0;
  font: 400 16px/22px 'Roboto Slab',serif;
  margin: 0;
}
.element span.view a {
  text-decoration: none;
  color: #333333;
}
body {
  background: #111;
}
<div class="element" sort-id="1">
  <a href="index.cfm?sendung_id=518">
    <img src="http://startv.ch/demandit/files/M_75AB1CC4DCE0B2C5F31/dms//Image/CC_Talk_Auslaenderkriminalitaet.jpg" 
         alt="CC_Talk_Auslaenderkriminalitaet.jpg" border="0"> 
  </a>

  <span class="lower-part-of-element">
    <h2>
      <a href="index.cfm?sendung_id=518">Adrian Amstutz &amp; Esther Egger-Wyss</a>
    </h2>

    <span class="view">
      <a href="index.cfm?sendung_id=518">Ausländerkriminalität</a>
    </span> 
  </span>
</div>

You can also view this issue on Codepen.io.

Answer №1

If you're looking to maintain the original HTML structure, one way to tackle this is by using absolute positioning for .lower-part-of-element at the bottom of the .element container. Make sure to include sufficient padding-bottom on the latter to accommodate the expected amount of text below the image.

.element {
  position: relative;
  margin: 15px 7px;
  padding-bottom: 5.75em;
  display: inline-block;
  overflow: hidden;
  position: relative;
  color: #222;
  max-height: 523px;
  max-width: 300px;
}
.element .lower-part-of-element {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
}
.element img {
  vertical-align: top;
}
.element h2 {
  text-align: center;
  background: #333333;
  padding: 12px 0;
  font: 400 16px/22px 'Roboto Slab',serif;
  margin: 0;
  color: #fff;
  border-top: 1px solid #333333;
  text-transform: uppercase;
}
.element h2 a {
  color: #fff;
  text-decoration: none;
  font: 400 16px/22px 'Roboto Slab',serif;
}
.element span.view {
  display: block;
  text-align: center;
  background: #fff;
  padding: 12px 0;
  font: 400 16px/22px 'Roboto Slab',serif;
  margin: 0;
}
.element span.view a {
  text-decoration: none;
  color: #333333;
}
body {
  background: #111;
}
<div class="element" sort-id="1">
  <a href="index.cfm?sendung_id=518">
    <img src="http://startv.ch/demandit/files/M_75AB1CC4DCE0B2C5F31/dms//Image/CC_Talk_Auslaenderkriminalitaet.jpg" 
         alt="CC_Talk_Auslaenderkriminalitaet.jpg" border="0"> 
  </a>

  <span class="lower-part-of-element">
    <h2>
      <a href="index.cfm?sendung_id=518">Adrian Amstutz &amp; Esther Egger-Wyss</a>
    </h2>

    <span class="view">
      <a href="index.cfm?sendung_id=518">blah blah blubb</a>
    </span> 
  </span>
</div>

<div class="element" sort-id="1">
  <a href="index.cfm?sendung_id=518">
    <img src="http://startv.ch/demandit/files/M_75AB1CC4DCE0B2C5F31/dms//Image/CC_Talk_Auslaenderkriminalitaet.jpg" 
         alt="CC_Talk_Auslaenderkriminalitaet.jpg" border="0"> 
  </a>

  <span class="lower-part-of-element">
    <h2>
      <a href="index.cfm?sendung_id=518">Foo Bar</a>
    </h2>

    <span class="view">
      <a href="index.cfm?sendung_id=518">blah blah blubb</a>
    </span> 
  </span>
</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

Mastering the art of creating an authentic carbon fiber CSS background

Authentic carbon fiber consists of a series of interconnected grey fibers that resemble woven sheets in grey color. Does anyone have knowledge on how to replicate this pattern using CSS? The examples created by Lea Verou do not accurately depict true carb ...

Is there a way to navigate to the next or previous image in a lightbox by using ev.target.nextElementSibling or ev.target.prevElementSibling?

I am new to the world of web development Currently, I'm facing an issue with my lightbox feature. I want to navigate between images by using ev.target.nextElementSibling and ev.target.prevElementSibling when clicking on the next/previous arrow.png ic ...

Charting Add-Ons for jQuery

Looking for suggestions on user-friendly graph drawing plugins compatible with jQuery. Currently developing a web application that will retrieve data from a remote database and present it through visual graphs... Explored jgcharts (the jQuery Google Chart ...

What is the process by which photoswipe applies styles to blur an image upon clicking the share button?

If you want to see an example, check out this link: http://codepen.io/dimsemenov/pen/gbadPv By clicking on the Share button, you'll notice that it blurs the image (and everything else). Despite inspecting it closely, I still can't seem to figu ...

Exploring the World of jQuery Caching and Navigating Through Objects

I'm interested in learning more about jQuery caching and how it can enhance performance. Can you explain how to properly utilize this technique? From what I understand, when using a jQuery selector, you're essentially searching the DOM to create ...

CSS vertical text not functional as expected

Trying to make some text vertical, here is the CSS I'm using: h2.verticle{ color:#1a6455; border:0px solid red; writing-mode:tb-rl; filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=3); -webkit-transform:rotate(90deg); -moz-transform:rota ...

Turn off HTML5 Audio

There are 4 <audio> elements on a webpage. The client has asked for them to be available sequentially. Meaning, the first audio should play, then the rest should remain disabled until the first one finishes, and so on. In addition, they want the au ...

Utilizing local storage, store and access user's preferred layout options through a click function

Exploring the realm of localstorage for the first time, I am considering its use to store a specific ID or CLASS from my css file. This stored information would then be utilized to render a selected layout (grid/list) in the user's browser upon their ...

Exploring HTML Parsing in Python with Regular Expressions

Currently, I am navigating through the HTML code of a website in search of the maximum enrollment for a particular class. Initially, I attempted to identify a specific substring within each line of the HTML file, but this method proved to be ineffective ...

What is the best method to pass a JavaScript file array to a Node.js server?

Is it possible to transfer data from a Javascript file linked to an HTML page to a Node.js file on a post route without displaying it on the HTML page? If so, what is the best way to accomplish this? ...

The HTML date input is displaying the date incorrectly, appearing one month off when set via the value attribute

i have tried this in two different browsers 1.Chromium Version 76.0.3809.87 2.Mozilla firefox 68.0.1 every time i enter a date in the input field in the format y-m-d, the month seems to be reduced by one without any explanation this is my code <i ...

Are there any available tools or scripting methods for accommodating various vendor prefixes?

My preference is to use standard CSS rules for different styles that include various vendor prefixes. To test, I would start with the following: border-radius: 5px; box-shadow: 0px 0px 4px 0px #fff; transform: rotate(90deg); For the final version, I woul ...

Tips for ensuring that divs resize to match the container while preserving their original proportions:

#container { height: 500px; width: 500px; background-color: blue; } #container>div { background-color: rgb(36, 209, 13); height: 100px; } #one { width: 1000px; } #two { width: 800px; } <div id="container"> <div id="one">&l ...

Create an element that spans the entire width of the page

Having trouble adjusting the width of the .navbar? It seems to only grow to one side and setting it to 100% width doesn't expand it to fill the whole page. Can anyone provide some guidance or assistance? Here's the code: return ( <div cla ...

Do you know of a solution to fix the Stylelint error regarding semicolons when using CSS variables?

React.js Css in JS(Emotion) The components above are part of this setup. Here is the configuration for Stylelint: module.exports = { extends: [ "stylelint-config-standard", "./node_modules/prettier-stylelint/config.js", ], ...

Tips on how to align several divs within another div

I've been attempting to center multiple div blocks within another div or HTML document, but I haven't had any success with the methods I've found on StOv. Here's an image of what I'm aiming for: https://i.stack.imgur.com/DB7uQ.jp ...

My goal is to populate all of these elements with Javascript

My goal is to have all the boxes filled with "excellent": https://i.sstatic.net/oHW6W.png let a = document.querySelectorAll('.select2-choice'); a.forEach((e) => {console.log(e)}) a.forEach((e) => {e.innerHTML = ` <span ...

Modifying the HTML <select> element with JavaScript

[resolved] I'm encountering an issue with the code below that is supposed to dynamically change the options in a second drop-down menu based on the selection made in the first menu. I've tried to troubleshoot the problem but haven't been suc ...

Issue with positioning content

Hey everyone, I'm currently developing a website and encountered an issue with placing content inside the body tag within a div element. I attempted to place the div inside the body tag, but the outcome was unexpected: The content seems to be appear ...

What could be causing the background color not to change on the HTML page with Bootstrap?

Learning html, bootstrap, and css styling can be a confusing endeavor. Sometimes things don't work as expected, like changing the background color. Despite following what seems like a simple solution after googling, the background color remains unchan ...