utilizing the float property in CSS without specifying a width

Below is the structure of my HTML:

<div class="content">
    <div class="icon"></div>
    <div class="text">I have a long message that wraps into two lines.</div>
    <div style="clear:both"></div>
</div>

Accompanied by the following CSS:

.content {width:300px; margin:30px auto; border:1px solid #000}
.icon {width:40px; height:40px; background-color:maroon}
.icon, .text {float:left}

I am seeking a way to align the icon and text components on the same line without the use of any relative or absolute width values.

You can view an example in this fiddle - http://jsfiddle.net/7kNSs/

Answer №1

Get rid of the CSS float declaration in the .text class.

Check out this Fiddle link

HTML code :

 <div class="content">
    <div class="icon"></div>
    <div class="text">Some lengthy message text that wraps to two lines.</div>
    <div style="clear:both"></div>
</div>

CSS code :

   .content 
        {
      width:300px; margin:30px auto; border:1px solid #000;
        }
   .icon 
        {
        width:40px; height:40px; background-color:maroon; float:left;
        }

Answer №2

To remove the extra .text div, simply follow these steps:

<div class="content">
    <div class="icon"></div>
    Some lengthy message text wrapped over two lines.
    Some lengthy message text wrapped over two lines.
    Some lengthy message text wrapped over two lines.
    Some lengthy message text wrapped over two lines.
    <div style="clear:both"></div>
</div>

.content {width:300px; margin:30px auto; border:1px solid #000}
.icon {width:40px; height:40px; background-color:maroon}
.icon, .text {float:left}

View the updated fiddle here: http://jsfiddle.net/Milanzor/tfrH6/

Answer №3

update text class by removing float property

html

<div class="content">
    <div class="icon"></div>
    <div class="text">A lengthy message split over two lines.</div>
    <div style="clear:both"></div>
</div>

css

  .content {width:300px; margin:30px auto; border:1px solid #000}
.icon {width:40px; height:40px; background-color:maroon;}

Answer №4

Here is an alternative line of code:

#content { overflow: hidden;width:300px; margin:30px auto; border:1px solid #000}
Try replacing the original line with this one

#content { overflow: hidden;width:300px; margin:30px auto; border:1px solid #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

Guide to ensuring child elements inherit parent's width in Bootstrap 4

Want to ensure that a child element inherits the parent's width in Bootstrap 4? Trying to adjust the width of the download_resume block. Currently using two key Javascript scripts: 1. The first function changes the image with a hover effect 2. The ...

Error: The specified element to insert before is not a direct descendant of this parent node

As I work on converting a jquery dragable script to plain javascript, I encountered an issue at the insertBefore point. The error message that pops up is: NotFoundError: Node.insertBefore: Child to insert before is not a child of this node. This particula ...

Is there a way to display a loader when an item is selected from a dropdown menu? For example, while data is being fetched from the backend, can we

This is the HTML form division I have created: <div class="col-lg-2 "> <select name="limitCount" id="limitCount" ng-model="limitCount" class="form-control col-md-2 panel-refresh" ...

SVG Star Rating system designed to accommodate ratings with decimal points

I am struggling with creating a star rating using SVG for the first time. I need the rating to be out of 5 and display decimal ratings like 4.5, 3.2, 1.1 along with whole stars. The rated stars should be yellow while the remaining stars should be grey. How ...

``There seems to be a problem regarding the alignment of elements in

Having some trouble with centering the grid on my simple website. As a beginner in Bootstrap, I'm seeking guidance. Will be grateful for any help you can offer! Please bear with me as I learn. <html> <head> <meta charset="utf8"/& ...

updating the content of a webpage using innerHTML on Squarespace platform

Could anyone help me with changing the status from "sold out" to "coming soon" on this specific page? Below is the code I have tried, but unfortunately, it is not working as expected. window.onload = function() { document.getElementsByClassName("produ ...

Is it possible to modify the color of a bootstrap navbar dropdown upon clicking?

I am having trouble changing the color of a bootstrap navbar dropdown link when it is clicked. Currently, the color changes to teal for a moment but then reverts back to the default gray. Here is my current CSS: li.dropdown:active { background- ...

Customizing the direction of Ext.menu.Menu involves modifying the menu's orientation to

I am looking for a solution to make Ext.menu.Menu display horizontally on the first level, with the submenu (second level) displaying vertically, and then the subsubmenu (third level) back to horizontal... My goal is to have odd-level menus display horizo ...

Customizing Bootstrap Styles in Angular: How to Override Specific Classes

Looking to customize an HTML element <div [style.color]="themeService.backgroundColor" class="col-md-10 col-md-offset-1 col-sm-10 col-sm-offset-1 col-lg-10 col-lg-offset-1"> <ul class="pagination" id="pag-bar"> <l ...

Content from ajax request is invalid

Currently, I am facing an issue with loading a comment list and replacing the existing list on my HTML page with the new one. Previously, I used to load a simple XML list, iterate over its elements, and generate content. Although this method worked fine, i ...

Leverage Javascript to interact with a complex Select HTML element through automation

Having some trouble navigating a Kelley Blue Book page on Chrome using JavaScript in the Console, specifically with the "make" dropdown. My ultimate goal is to choose a make and model before proceeding by clicking the next button. Here's a snippet of ...

What are some effective ways to utilize localstorage efficiently?

I've been working on creating a variable that stores its value across multiple pages using local storage. However, I've encountered an issue where the variable is not being saved, and there are no error messages to help diagnose the problem. Her ...

Dynamic presentation created with Serif software

As a newcomer to web coding and one of the older generation, I recently created a basic .swf slideshow using Serif's graphic software. However, I realized that it does not display on an Apple iPad. You can experience this issue quickly here. My questi ...

What is the proper way to specify the background image path in CSS?

My CSS file is located at: Project/Web/Support/Styles/file.css The image I want to use is found here: Project/Web/images/image.png I am attempting to include this image in my CSS file. I have tried the following methods: 1) background-image: url(/images ...

Implementing a validation step prior to executing a redirect in an Ajax request

I am facing an issue with my ajax script where after logging in, it redirects to the dashboard.php file regardless of whether the login fails (for example, using incorrect username and password). How can I implement a check for this? Is there a way to redi ...

Is it possible to use a variable for the value attribute in a select option?

In a mongodb database, I have a collection that stores "username" and "email". Now, I am trying to create a webpage on a localhost server where I can display the username and email of a specific user based on a selection from a drop down menu. I have succe ...

Creating responsive images with Bootstrap 5

1 I am currently using Bootstrap 5 to develop a website and encountering an issue with getting images to move above text in the feature and mission sections on mobile or tablet screens. Previously, I was able to achieve this effect using the following cod ...

Display a snippet of each employee's biography along with a "Read More" button that will expand the content using Bootstrap, allowing users to learn more about each team

I have successfully developed a Google App Script that links to a Google Sheet serving as a database. The application iterates through each row, and I showcase each column as part of an employee bio page entry. ex. Picture | Name | Title | Phone | Email ...

Preserving the HTML format of a string stored in MongoDB: Best practices

Currently, I am utilizing AngularJS for my frontend and mongodb for backend database management. For post editing, I rely on textAngular. When creating a new post, such as a service agreement, textAngular automatically adds formatting using HTML tags. &l ...

Strange outcomes observed with array created from HTML code

html : <input type="text" name="ps[part1['test']]" value="testing multiple array"> Current result : [ps] => Array ( [part1['test'] => testing multiple array ) Expecting result : [ps] => Array ( ...