Making the parent div dynamically adjust to the size of its children with CSS

I'm attempting to include an input field with an icon inside a div container. I want the width of the div to match exactly the size of the input.

Below is the code I have:

<div>
    <input type="text">
    <i class='glyphicon glyphicon-search'></i>
</div>

This is my CSS:

div {
    background: blue;
    display: inline-block;
}
i {
    left: -30px;
    color: #fff;
}
input {
    position: relative;
    width: 100px;
    background: transparent;
}

Here's a working example on JSFiddle: http://jsfiddle.net/dwfh16me/3/

The issue I'm facing is that the div expands to accommodate the width of both the input and the icon, even though the icon is absolutely positioned. How can I ensure it doesn't consider the width of the icon?

Thank you in advance

Answer №1

example - http://jsfiddle.net/sarah_123/dwfh16me/6/

include position:absolute for i and make sure the parent has position:relative

i.icon {
   position:absolute;
   right:10px;
   top:5px;
   color: #000;
}

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

What is the best way to display a page within a div when clicking in Yii?

I'm trying to use the jQuery function .load() to load a page into a specific div. Here's my code: <a href="" onclick="return false;" id="generalinfo"> <div class="row alert alert-danger"> <h4 class="text-center">Gen ...

visibility:hidden causing issue with hiding empty option in IE dropdown

I am currently working on an Angular 11 application. On one of the pages, there are 2 radio buttons and a dropdown. The desired behavior is that whenever the radio button selection is changed, the dropdown value should be reset to empty. The code provided ...

What methods do you utilize to safeguard your commercial HTML, CSS, and JavaScript codes during demos?

What strategies can be implemented to safeguard the integrity of commercial HTML/CSS and JavaScript files in market demos? Please refrain from flagging this question as irrelevant, as I am aware that methods such as: Disabling right-click functionality. ...

Achieve a full-width span for a single item in a CSS grid without any alterations to the HTML code

How can I make the first item in this grid span 100% without changing the HTML structure? The first item is assigned an extra class ".sub" Is it possible to achieve this? Click here for more information <div class="fd-col fd-5col"> <div class= ...

The slideToggle() function appears to be malfunctioning when sliding up or down, as it

Currently, I am in the process of creating a dropdown menu. However, I am facing an issue where the menu does not slide down or up smoothly when clicked. The animations seem to be delayed or non-existent. $('.menu-vertical .nav-menu > li'). ...

Guide to inserting a glyphicon within a bootstrap badge

I've been working on a unique way to display hierarchical data using bootstrap badges and AngularJS. My goal is to create a user-friendly experience where people can easily navigate through categories. There are 3 main types of categories: parent: T ...

Ways to utilize a single HTML page for various URLs while changing one variable value based on the queried URL

My current HTML page structure looks like this: <body ng-controller="DashboardDisplay" onload="submit()"> <div class="container-fluid" > {{scope.arr}} </div> </body> <script> var myApp = angular.module(&apos ...

The android web browser and its interactive virtual keyboard

I am currently developing an HTML5 application using PhoneGap version 2.5.0. One issue I have encountered is related to the virtual keyboard behavior on different parts of the app. On the search page, the keyboard appears without obstructing the view, all ...

Safari has a unique feature where margins are not factored into the scroll width calculation

I am facing an issue with a container that scrolls horizontally. Inside this container, there are multiple items and I have added a margin to the last item on the right. However, in all browsers except for Safari, the margin is included in the scroll width ...

Attempting to create a JavaScript class within an HTML document

Having an issue with instantiating a JavaScript class in a separate HTML file. The JavaScript class looks like this: class Puzzle { constructor(fenStart, pgnEnd) { this.fenStart = fenStart; this.pgnEnd = pgnEnd; } } module.exports = Puzzle; ...

I continually run into the error message "Uncaught ReferenceError: dropMenu is not defined" and "Uncaught TypeError: Cannot read property 'style' of null."

I'm attempting to create a navigation bar with submenus, but I keep encountering errors such as "dropMenu is not defined" and "Uncaught TypeError: Cannot read property 'style' of null" when hovering over the Paris links. Any thoughts on what ...

My attempts to troubleshoot the JavaScript function have all been unsuccessful

I am completely new to JavaScript and feeling a bit embarrassed that I'm struggling with this. My goal is to build a website that takes first and last names as input and generates email addresses based on that information. Despite my attempts, moving ...

Utilizing a JavaScript variable within an HTML style attribute value

I have added some divs to a webpage. I am looking to adjust the width of these divs based on the browser or other settings. While I can manually set the width to 200px using inline styles, I need the flexibility to change it to 220px, 230px, or 240px depen ...

Eliminate the need for a scrollbar

As I design my website, I have a vision of incorporating fixed-type links at the bottom. These links would allow visitors to navigate through the page by moving one picture down with each click. Additionally, I want the ability for users to scroll seamle ...

Executing a JavaScript/jQuery function on the following page

I'm currently working on developing an internal jobs management workflow and I'd like to enhance the user experience by triggering a JavaScript function when redirecting them to a new page after submitting a form. At the moment, I am adding the ...

Using Jquery to Highlight the Background Image in a Circle

I currently have 3 images named back_about_1.jpg, back_about_2.jpg, and back_about_3.jpg. There is a div with the ID "about" - <div id="about"></div> The default background image of the div is set to back_about_1.jpg using CSS - background: u ...

"Unusual" symbols encountered during PDF file saving

I have a PDF file titled "SA Pias - Margaça Branco.pdf" that I need to access. Currently, I am using this link: href="SA Pias - Marga%E7a Branco.pdf" Although the file opens fine in the browser, when users try to save it, they see strange characters ins ...

Issue with input cursor and text not displaying in input fields with Bootstrap 3.1 version

Recently, I made some changes to my portfolio website at www.jaredmc.com and integrated the Twitter Bootstrap framework. However, I encountered a problem in Firefox (latest version) where the cursor and text do not appear in the first two form fields/input ...

The CSS file was not applied because the mime type did not match (Error code: SEC711

I am currently working on a JSP-Servlet application. I have successfully deployed the application using Eclipse with the default Tomcat 7 server. However, upon deployment, I noticed that the UI renders properly in Chrome and Firefox but encounters issues ...

`CSS animation for vanishing line effect`

I want to create an animated logo that gives the illusion of being pulled up by a rope, represented by a vertical black line, from the bottom of the page to the top. As the logo moves upwards, I would like the rope to disappear behind it, but I'm uns ...