What is the best way to stop text from wrapping around an icon that has been floated to the left in CSS?

I'm facing an issue where the text in my message boxes wraps around the bottom of a smaller icon when I use a left floated span with MDI Icons via class ::before selection.

Interestingly, using a larger (50px) icon doesn't cause this problem (although I have no idea why). Here's a screen recording to show the behavior: https://i.sstatic.net/sokJH.gif

The desired outcome is to keep the text aligned on the right side of the icon without wrapping underneath it. I hope that clarifies things.

I suspect that switching from left floating the icon might solve the issue, but I'm unsure how to achieve what I need without using float. Below is the HTML code:

<!DOCTYPE html>
<html class="no-js" lang="">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>UI Playground</title>
    <link rel="stylesheet" href="css/normalize.css">
    <link rel="stylesheet" href="css/admonitions.css">
    <link rel="stylesheet" href="css/icons.css">
</head>

<body>
    <div class="admonition info">
        <span class="admonition-icon icon-35 mdi-information-outline"></span>
        <span class="admonition-title">This is an informational message with an Icon</span>
        <span class="admonition-text">
            Ut mollis ligula vulputate lectus eleifend, et commodo nibh faucibus.
        </span>
    </div>

    <div class="admonition-alt">
        <span class="admonition-icon icon-55 mdi-ubuntu"></span>
        <span class="admonition-title">This is the title</span>
        <span class="admonition-text">
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras ullamcorper turpis ex, eget fringilla ligula gravida nec. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Nulla vulputate lacus sed massa aliquet suscipit. Suspendisse potenti. Maecenas iaculis dolor non mollis ornare.
        </span>
    </div>

    <div class="admonition-alt">
        <span class="admonition-icon icon-35 mdi-ubuntu"></span>
        <span class="admonition-title">This is the title</span>
        <span class="admonition-text">
            Lorem ipsum dolor sit amet, consectetur adipiscing elit.
        </span>
    </div>

</body>
</html>

And here is the CSS code:

:root {
    --ui-font-stack-sans: "Helvetica Now Text", "Helvetica LT Pro", "Helvetica", "SF Pro Text", "Arial", sans-serif;
}

body {
    font-size: 15px;
    color: #555555;
    line-height: 1.5em;
    font-family: var(--ui-font-stack-sans);
}

/* Global Admonition Defaults */

div.admonition,
div.admonition-alt {
    border-radius: 5px;
    overflow: visible;
    page-break-inside: avoid;
    display: flow-root;
    padding: 1em 0 1em 0em;
    margin-bottom: 1.5em;
    font-family: var(--ui-font-stack-sans);
}

div.admonition>.admonition-title,
div.admonition-alt>.admonition-title {
    display: flow-root;
    font-size: 1.1rem;
    color: #535464;
    padding-left: .96em;
    font-weight: 400;
}
div.admonition>.admonition-text,
div.admonition-alt>.admonition-title {
    color: rgb(118 116 116);
    font-size: .94em;
    font-weight: 400;
    display: flow-root;
    line-height: 1.5em;
    padding-left: 1.2em;
    margin-top: 2px;
}

/* Blue Info Admonition Colors */
/* Gray Admonition Defaults */

... //CSS continued

.admonition-icon {
    float: left;
}

.icon-35 {
    font: normal normal normal 35px/1 "Material Design Icons";
    display: inline-block;
    text-rendering: auto;
    position: relative;
    top: 8px;
    margin-left: 14px;

}

.icon-55 {
    font: normal normal normal 55px/1 "Material Design Icons";
    display: inline-block;
    text-rendering: auto;
    position: relative;
    top: 3px;
    margin-left: 16px;
}

If anyone has any suggestions, advice, or solutions to offer, I would greatly appreciate it!

Answer №1

If you're unsure about what to do with the text, there are a few options available.

For hiding the text, consider adding overflow:hidden; and white-space:nowrap; to your class styles:

div.highlight.notice > .highlight-title {
    color: #2e72d8;
    font-weight: 400; 
    overflow: hidden;
    white-space: nowrap;
}

div.highlight.notice > .highlight-text {
    color: #7182b1;    
    overflow: hidden;
    white-space: nowrap;
}

If making the text smaller is your goal, use media queries and an additional class style:

@media screen and (max-width: 600px) { 
  div.highlight.notice > .highlight-title {
    font-size: 12px;
  }
  
  div.highlight.notice > .highlight-text {
    font-size: 10px;
  }
}

You can customize the max-width and font sizes as needed and apply them where necessary.

Answer №2

Utilizing the grid system can help achieve the desired layout

<div class="container">
<div class="admonition-alt">
        <span class="admonition-icon icon-55 mdi-ubuntu"></span>
        <span class="admonition-title">This is the title</span>
        <span class="admonition-text">
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras ullamcorper turpis ex, eget fringilla ligula gravida nec. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Nulla vulputate lacus sed massa aliquet suscipit. Suspendisse potenti. Maecenas iaculis dolor non mollis ornare.
        </span>
    </div></div>

To enhance the structure, a new container has been added with relevant CSS styles below:

.container{
   display:grid;
   grid-template-rows: auto auto;
   grid-template-columns: auto auto;
 }
.admonition-icon{grid-area:1/1/3/2}
.admonition-title{grid-area:1/2/2/3}
.admonition-text{grid-area:2/2/3/3}

Feel free to give it a try and for more guidance, visit https://www.geeksforgeeks.org/css-grid-layout-module/

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

Ensure that divs remain in a static position within the dialog box

Within the jQueryUI dialog, I have gray boxes at the top and bottom of the visible area represented by the .nav elements. I want these gray boxes to stay fixed in their position even when scrolling within the dialog. Despite setting position: absolute, the ...

Auto Height Background Colour in Dual Columns

There seems to be a simple issue that I'm missing here. I have two columns that maintain the same height based on the content inside them, but the background color doesn't fully extend in the shorter column. When the display shrinks, the columns ...

What is the best way to expand the subcategories within a list?

I have successfully implemented a script to open my hamburger menu. However, I am facing an issue with subitems as some list items contain them. How can I modify the script to ensure that the subitems expand when clicked instead of just hovering? functi ...

Looking to add a bulleted list to an HTML document with PHP?

I have integrated a PHP script within an HTML list element that enables users to edit the HTML element directly from the webpage if they enter the correct password via the variable ?pw = "password". Now, I am looking to allow users to add new list items ...

Text misalignment in div elements across various browsers

Having an issue with the alignment of text in different browsers. I'm attempting to make the comments line and the "like" heart line line up properly, similar to the layout in the lower right corner shown in this image taken from Firefox: However, wh ...

Customizing Material UI: Grouping Tab components within a div container

How can I wrap the children of a Material UI Tabs component in divs? <Tabs value={value} indicatorColor="primary" textColor="primary" onChange={handleChange} > <div> <Tab label="x" /> ...

The jQuery preventDefault() method is preventing the blur event handler from functioning

I am currently developing an interactive web application located at (user: [email protected], password: demo). This application allows users to drag items with images and text from the sidebar onto the workspace on the right, resize them, and make ed ...

Issues with creating modal images using only JavaScript

I am facing an issue with modal popup images. I have a collection of images and attempted to implement this code (with some modifications): https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_modal_img However, it seems to only work for the initi ...

Naming axes in Chart.js is a simple process that can easily be implemented

Greetings to all, I'm currently working on creating bar charts using chartjs...everything is going smoothly except for one thing - I am struggling to find a clean way to name my axes without resorting to CSS tricks like absolute positioning. Take a ...

Issues with CSS styling are affecting the display of a form

Hey there! I was working on creating an order form for my website, and everything was going smoothly until I hit a snag while styling the webpage. The problem is that the legends of the form are extending all the way to the right side of the page, and the ...

Transforming a Java applet into an <object> tag

After updating my Java to the latest version, I encountered a problem with an applet I regularly use. Despite adjusting the security settings to the lowest level through the Control Panel, the applet still fails to run correctly. Here is how the applet was ...

My Testimonial Slider seems stuck in its current position and won't budge to the left

As an aspiring web designer, I took a template from CodePen and crafted a testimonial slider for my website. To seamlessly merge it with the background, I've been trying to shift the entire testimonial to the left. Despite attempting the float-left p ...

Illuminate the <select> within the form at the bottom

I have experimented with various combinations of Bootstrap 4's flexbox align utilities, as outlined in their documentation, but to no avail. Is there a way to ensure that the second <select> in the form below is aligned at the bottom of the row ...

Combining two forms into one PHP page, but intending to submit only a single form

I'm facing an issue on my page where I have both a sign-in and a sign-up form. Whenever I click on either the sign-in or sign-up button, it always ends up submitting the sign-up form. What am I doing wrong? Here's the PHP code snippet: if($_SE ...

Adjusting the Style of a Parent Element Depending on the Content of its Child Element

I'm currently working on changing the visibility of the second div that has the class .form-group. I'm using jQuery to get the selected option value and then adjust the visibility based on that value. If the value is 0, then the second div .form ...

Struggling to retrieve the tagName attribute when clicking in JavaScript

I am trying to extract the tagName of any element within an iframe when it is clicked. Unfortunately, my JavaScript code is not working as expected. As a beginner in JavaScript, I would appreciate some guidance on where I might be going wrong. <div cl ...

The CSS division is perplexingly dimensionless

As I work on finalizing a client's webpage, I encountered an issue with applying vertical padding to one of the div elements. Upon inspecting the element using Chrome's dev tools, I found that it had "NaN x NaN" dimensions. This prevented me from ...

Show the extracted data on the following web page once it has been cleaned

I am looking to connect a python file with an HTML page using Flask application. On the first page (upload.html), the user is required to select the job field and job title from two dropdown menus. Afterwards, the user needs to upload a file containing a l ...

Tips on utilizing the Google Maps autocomplete feature exclusively for place suggestions, without displaying the map itself

I have implemented an input form where users can see suggestions of places related to the word they type. I came across a solution using Google Maps autocomplete, but I do not need the map itself. All I require is the functionality that provides suggestion ...

Ways to hide the value from being shown

I have integrated jscolor (from jscolor) to allow users to pick a color with an input field. However, I am facing issues in styling it as I'm unable to remove the hex value of the selected color and couldn't find any relevant documentation on av ...