Repairing the formatting of the content inside a dynamically growing div

Check out the code on this Fiddle link,

<!DOCTYPE html>

<body>
<div class="test">
    <h1>?</h1>
    <p>This paragraph gives more information that can be accessed by hovering over the question mark (Move your mouse away to shrink back.)</p>
</div>

<h1>Examples</h1>
<br />

<h3>My name is Shaun</h3>
<div class="test">
    <h1>?</h1>
    <p>Individuals with this name are often passionate, compassionate, intuitive, and have magnetic personalities. They tend to be humanitarian, broadminded, and generous, often choosing professions where they can serve humanity. They are affectionate and giving, may be easily hurt, and are sometimes quick-tempered.</p>
</div>
<h3>I am on a see food diet</h3>
<div class="test">
    <h1>?</h1>
    <p>When I see food, I want to eat it.</p>
    </div>

</body>

There are two issues I would like to address:

  1. When hovering over the question marks, you can see that as the div expands, the text constantly reformats to fit inside it. I want the text to already be at the correct size based on the fully expanded DIV.
  2. How can I dynamically choose the size of the DIV so that its maximum expanded size is based on the text within it? I tried using 100%, but that just sizes it to the webpage.

Answer №1

1) To prevent text from repositioning as the container expands, you need to ensure that the width of the paragraph element matches the width of the expanded box:

.test:hover, .test p {
    width: 750px;
}

Additionally, you had the p element set to display: inline;, which should be changed to display: block; (or inline-block could also be used) since you cannot explicitly set the width on inline elements.

demo: http://jsfiddle.net/UfhG2/6/

2) Achieving an element to shrink to the size of its content without scripting is a bit tricky. Without using auto for either height or width, transitioning from a specified height or width to auto is not straightforward. However, there are suggestions provided here:

As a simple compromise, you can set:

.test:hover {
    height: auto;
}

This way, the height will not transition, but you will still have a reasonably sized box, and the width will expand smoothly.

demo: http://jsfiddle.net/UfhG2/8/

3) Consider using pseudo-elements instead of repeating <h1>?</h1>:

.test:before {
    content:'?';
    margin: 0;
    color: white;
    font-size: 2em;
    font-weight: bold;
}
.test:hover:before {
    display: none;
}

demo: http://jsfiddle.net/UfhG2/7/

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

Discovering phrases and adjusting the hue

Is there a way to change the color of text output by the ECHO function using CSS and conditions? For instance: If I use the ECHO function to display text like this: echo ": The net value of the entire order" echo ": The gross value of the entire order" ...

Receiving an absence of string or a null value from a text box

My goal is to test the functionality of an <input> element by entering text into a textbox and then displaying that data in the console. However, I am encountering issues with retrieving and printing the content. Additionally, I am interested in test ...

Displaying a PHP variable within an HTML tag that has been previously output within a PHP block

I'm struggling with printing a variable that was already echoed in a php tag within an html tag. Specifically, I want to print the variable $A in an h1 tag. Here is my code: <?php while($pr = mysql_fetch_assoc($qAccess)) { $ ...

Incorporating dual CSS stylesheets into a single HTML document

I have been utilizing jsonform from https://github.com/joshfire/jsonform to create forms based on a json schema. However, jsonform requires its own css for the form, while I prefer to use a different css for my website's template. Is there a method t ...

I am looking to adjust the width of an element within an Iframe

In my single post, I have a social sidebar on the left side. Currently, the buttons in the sidebar are appearing in different sizes, but I want them all to be the same size as the "Twitter" button. To see the issue, please visit this link. I've tried ...

Reactjs error: Invariant Violation - Two different nodes with the matching `data-reactid` of .0.5

I recently encountered a problem while working with Reactjs and the "contentEditable" or "edit" mode of html5. <div contenteditable="true"> <p data-reactid=".0.5">Reactjs</p> </div> Whenever I press Enter or Shift Enter to create ...

Tips for updating multiple bundled javascript files with webpack

I am working on a straightforward app that requires users to provide specific pieces of information in the following format. Kindly input your domain. User: www.google.com Please provide your vast URL. User: www.vast.xx.com Select a position: a) Bottom ...

Deciphering HTML entities and decoding URLs within a continuous flow

Is it possible to find functions that accept streams and output streams for decoding operations, rather than loading the entire encoded document into memory like the HttpUtility.UrlDecode and HttpUtility.HtmlDecode methods do when taking strings as input ...

The radio button that disables other inputs only functions correctly for a single element when using Query Selector, but does not work with Query

I have attempted to develop a form section that is disabled when the user selects option A and enabled when they choose option B. document.getElementById('delivery').onclick = function() { var disabled = document.querySelectorAll(".dis ...

Ways to center text within a nested div in a parent div with table-cell styling

The issue I am facing involves a parent div with the CSS property display: table;. Nested inside is a child div with display: table-cell;. Despite applying text-align: center;, the text within the second div does not align to the center as expected. Here ...

Obtaining the HTML content of a div element that has been retrieved through an AJAX request to a PHP script

My challenge is to fetch a dropdown menu from a server and then manipulate it using jQuery after loading. Although everything loads correctly, I am unable to interact with the dropdown items because they were dynamically added post AJAX response instead of ...

Stop options from being hidden in a select dropdown using HTML

Can I keep the options visible when a user selects an item in the 'select' dropdown? I want to add more options to the 'select' when the user clicks on the 'op2' item, without closing the list of options. <select> <o ...

Colorbox scalePhotos function malfunctioning

The scalePhotos option in Colorbox does not seem to be working correctly for me. I have tried various methods to set it, including initializing Colorbox using the following code snippet right before the closing </body> tag in my HTML: jQuery('a ...

What is the reason behind the error message in Bokeh?

While following a Udemy tutorial, I encountered the following error message associated with Bokeh: Bokeh Error attempted to retrieve property value for property without value specification when running the code provided in the HTML. Can anyone shed some l ...

Troubleshooting: Issue encountered while adding jQuery Slideshow plugin to current website

I am currently facing some challenges with a specific feature on my website. I am trying to integrate Jon Raasch's "Simple jQuery Slideshow Plugin" into an existing site that was not originally built by me. However, when viewing the site in IE 9, two ...

Effortlessly Concealing Numerous Elements with a Single Click in Pure JavaScript

I have successfully created an HTML accordion, but I am facing an issue. I want to implement a functionality where clicking on one accordion button will expand its content while hiding all other accordion contents. For example, if I click on accordion one ...

In the absence of a value

In my code, I've implemented a functionality that saves the user's input into local storage and displays it in a specific ID. However, I want to make sure that if the input field is left empty, the user is prompted to enter their name. function ...

Enhance the functionality of Woocommerce email notifications by incorporating a customized VAT field

I have exhausted all options and tried various email hooks without success. I inherited an outdated PHP code that was developed by someone else, which I updated for new woocommerce hooks (since the code is 4 years old). Everything is functioning smoothly e ...

Color of Bootstrap tooltip arrow outline and fill

How can I customize the background color and border color of the tooltip arrow in Bootstrap 4? $(function() { $('[data-toggle="tooltip"]').tooltip() }) .tooltip-main { width: 15px; height: 15px; border-radius: 50%; font-weig ...

Creating multiple columns in a single row using Bootstrap5 for front-end development

Is there a way to make 10 columns responsive in the same line with Bootstrap 5? I noticed that after 5 or 6 columns, they automatically move to the next line. Using a class like "col-1" seems to disrupt the responsiveness and the columns don't fully ...