The width of the absolute div shrinks to such an extent that the text starts wrapping around itself

My goal is to create a button that, when hovered over, displays a div. However, I am encountering challenges with the width and position of this div.

The issue is illustrated in the images below and in the accompanying jsfiddle. When I set the positioning of the div to absolute relative to the button, it seems that its max-width is limited by the width of the button itself. This results in a small hover div when the button is small. How can I ensure that the hover div's width adjusts dynamically without being restricted by the button size?

http://jsfiddle.net/ghpc9fwk/

Answer №1

To ensure the text stays on one line without wrapping, simply include the white-space: nowrap; attribute in your CSS code.

For a practical demonstration, check out this example: http://jsfiddle.net/9zr67d3h/

I've also made adjustments to the bottom property for better alignment.

Answer №2

If you're looking for a solution, consider utilizing the ::before pseudo element to create the desired layout.

.custom-box{    
    background-color:green;    
    display:inline-block;
    margin:0;
    padding:0;
    position:relative;    
    margin-bottom:30px;
}

.custom-box::before{
    content:"";
    display:block;
    height:15px;
    width:15px;
    background:yellow;
    position:absolute;
    left:0px;
    top:-15px;    
}

Check out the JSFIDDLE here

Answer №3

Is this what you had in mind?

<div class="button">
    <div class="alt">Click here now!</div>
</div>

.button{
    width:10px;
    height:10px;
    background-color:red;
}
.alt{
    margin-top: 10px;
    display: none;
    background-color:blue;
    width: auto;
    white-space: nowrap;
}

.button:hover .alt{
    display: inline-block;
}

Check out the updated Fiddle link

Answer №4

To enhance your button and alternative container, you can enclose them in another div with the style attribute position: relative;. See this interactive demonstration

Here is the HTML markup:

<div class="container">
    <div class="button"></div>
    <div class="alternative">Click here!</div>
</div>

And the corresponding CSS styling:

.container {
    position: relative;
}

.button{
    width:15px;
    height:15px;
    background-color:green;
    position:relative;
}

.alternative{
    position:absolute;
    background-color:yellow;
    display: none;
}

.button:hover + .alternative {
    display: block;
}

Answer №5

To solve this issue, a simple solution would be to include the CSS property white-space:nowrap in your alt/caption div so that it extends until you add a line break with <br>

In addition

I suggest avoiding the use of

bottom: -[something]px

and instead opt for

top: 100%.

This way, the button can have varying heights.

I've made adjustments to your JSFiddle here: http://jsfiddle.net/ghpc9fwk/3/

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

Incorporate an icon into an Angular Material input field

I want to enhance my input search bar by adding a search icon from Angular Material : <aside class="main-sidebar"> <section class="sidebar control-sidebar-dark" id="leftMenu"> <div> <md-tabs md-center-tabs id=" ...

Tips on how to properly position the navigation bar within a wrapper element

I'm having trouble fixing the navigation bar at the top of the wrapper when scrolling down. However, when I try using position: fixed in the #nav ul, the navigation bar extends beyond the wrapper. Below is my HTML code, where the nav bar should be con ...

What steps can be taken to resolve the issue with the background's placement?

https://i.sstatic.net/d3GEw.jpg There seems to be an issue with the border being hidden. I've tried adjusting the background-position variables, but without success. Here is a code example: * { margin: 0; padding: 0; } .container { ...

The drop-down menu selection is non-functional on mobile and iPad devices when using Bootstrap

<div class="panel panel-default"> <select> <option value="B3">B3</option> <option value="B4">B4</option> <option value="B5">B5</option> & ...

Static Sidebar integrated with Twitter Bootstrap version 2.3.5

Currently, I am in the process of learning how to incorporate a fixed sidebar using Twitter Bootstrap 2.3.5. Below is a snippet of the code I have been working on: <div class="container"> <div class="row"> <div class="span4" id="sid ...

Tips for changing the appearance of blockquote lines in bootstrap 5

I'm trying to see if it's possible to customize the blockquote-footer line in bootstrap 5. Can I change its color and width? Here's the code snippet from bootstrap 5: <figure> <blockquote className="bloc ...

Monitoring WooCommerce order submissions through AJAX

I'm exploring the world of Ajax for the first time. My goal is to submit an order tracking form and show the results using AJAX. I attempted to use Ajax to display the results within div track_result. The issue I'm facing is that the following ...

Wordpress isn't loading CSS as expected (I believe...)

Recently, a wordpress based website I am working on suddenly stopped pulling the CSS files...or at least that's what it seems like. I can't post a screenshot of the wordpress admin dashboard because I don't own the site and my boss might no ...

Using Twitter Bootstrap to set a minimum number of lines for thumbnail captions

I currently have a carousel on my website created using Bootstrap that displays 4 columns of thumbnails. You can view the carousel here. When navigating to the third page of the carousel, you will notice that the container increases in height to accommodat ...

When pressing the next or previous button on the slider, an error message pops up saying "$curr[action] is not a

I found this interesting Js fiddle that I am currently following: http://jsfiddle.net/ryt3nu1v/10/ This is my current result: https://i.sstatic.net/VygSy.png My project involves creating a slider to display different ages from an array, such as 15, 25, ...

The calendar on the datetime picker is malfunctioning and not displaying any dates

The date and time picker feature appears to be malfunctioning on my website. I suspect it may be due to a conflict between the full calendar and gull admin theme that I am using. I have attempted various solutions but nothing seems to be working. How can ...

Guide on how to prevent Paste (Ctrl+V) and Copy (Ctrl+C) functionalities in a TextField within MaterialUI

Is there a way to restrict the Paste (Ctrl+V) and Copy (Ctrl+C) functions in a specific TextField within MaterialUI? I am currently working with ReactJs ...

Choose a JavaScript function by clicking on the HTML text

As a beginner in the world of coding, I have been diving into JavaScript, HTML, and CSS. Inspired by fictional supercomputers like the Batcomputer and Jarvis, I've challenged myself to create my own personal assistant to manage tasks, games, programs, ...

The CSS counter fails to increment

In this unique example: h3 { font-size: .9em; counter-reset: section; } h4 { font-size: .7em; counter-reset: subsection; } h3:before { counter-increment: section; content: counter(section)" "; } h4:before { counter-increment: subsection 2; ...

HTML needs to be wrapped around every set of three items using C# ForEach loop

Currently, I am faced with the task of constructing an HTML code structure within C# code behind. Specifically, I need to insert an HTML element that spans 3 columns in a row of 12 columns (utilizing Zurb Foundation). To achieve this, I am iterating throu ...

Clicking does not trigger scrollIntoView to work properly

I'm facing an issue with a button that is supposed to scroll the page down to a specific div when clicked. I implemented a scrollIntoView function in JavaScript and attached it to the button using onClick. Although the onClick event is functioning as ...

Storing values from a content script into textboxes using a button press: a simple guide

I am new to creating chrome extensions and currently utilizing a content script to fetch values. However, I am facing difficulty in loading these values into the popup.html. Here is the code snippet: popup.html <head> <script src ...

Strange occurrences with the IMG tag

There seems to be an issue with my IMG element on the webpage. Even though I have set the height and width to 100%, there is some extra space at the end of the image. You can view the problem here: I have also included a screenshot from the developer too ...

Incorporate this form created externally onto my website

I have been working on my website and I am looking to incorporate a form that opens up once the login button is clicked. This form was generated using an online form builder which provides an embed code for seamless integration onto websites. Below, you wi ...

Angular conditional operator: running a series of statements

Let's break down the logic: When the enter key is pressed, we want to execute the following conditional statement: if (!elementB.isOpen) { elementB.open(); } else { elementC.open(); elementC.focus(); elementB.close(); } I attempted ...