Tips for keeping a span element from breaking onto a new line in responsive web design

Here is a simple list with h4 headings, each ending with a span element.

<ul class="items">
    <li>
        <h4>Avoid Line Break Of Plus <span class="goto">o</span></h4>
    </li>
    <li>
        <h4>Digital Signage <span class="goto">o</span></h4>
        … 

</ul>

Screenshot of the page's source:


The CSS for the span is as follows:

.items .goto {
   font-family: 'QuaySans-Icons';
   font-size: 1.6em;
   position: relative;
   float: right;
}

The final result looks like this:

One issue I encountered is that when resizing the browser window (working on a responsive web design), the span icon breaks into a new line.

If you have any creative solutions or ideas on how to prevent this, it would be greatly appreciated!

Thanks in advance, Matt

Answer №1

If you wish for the icon to remain inline with the last word of your text line, you can achieve this by:

<ul class="items">
    <li>
         <h4>Prevent LineBreakOfPlus<span class="goto">o</span></h4>
    </li>
    <li>
         <h4>Digital Signage<span class="goto">o</span></h4>
    </li>
</ul>

and then style it using CSS:

.items {
    list-style: none;
    margin: 0;
    padding: 0;
}
.items li {
    border-bottom: 1px solid gray;
    margin: 0;
    padding: 0;
}
.items h4 {
    margin: 0;
}
.items .goto {
    background-color: gray;
    font-size: 1.6em;
    margin-left: 10px; /* optional */
}

If there is no white space between your text and the span, the icon will stick closely to the word even if the li element wraps onto a second line.

You can adjust margin-left for spacing or add a &nbsp entity before the span. There are multiple ways to achieve this effect. The choice depends on the visual outcome you aim for.

Fiddle: http://jsfiddle.net/audetwebdesign/VsBet/ (demonstrates two methods)

Aligning Icon to Right Justified

One technique to position the icon to the right of the h4 element is shown below:

.ex2.items h4 {
    position: relative;
    line-height: 1.5;
    outline: 1px dotted blue;
    padding-right: 2.00em;
}
.ex2.items .goto {
    background-color: wheat;
    line-height: 1.00;
    font-size: 1.6em;
    position: absolute;
    right: 0;
    bottom: 0.0em;
    height: 1.00em;
    width: 1.00em;
    outline: 1px dotted red;
}

Utilize absolute positioning for the span to keep it at the right and bottom of the h4. If the h4 spans multiple lines, the icon will drop down accordingly. Adjust positioning based on the icon's size. To prevent overlapping text as the window size changes, include some padding-right in h4.

Note: Specifying accurate line-height values helps address uncertainty regarding the icon's height. Adjust these values as needed to vertically align the icon.

Answer №2

To maintain consistency in responsive designs, consider reducing the font size when space is limited, such as in media with a max-width of 480px. Lowering the font size can be an effective solution to ensure the design remains cohesive across various screen sizes.

Answer №3

I've created a basic layout on the demo, but it still needs some refining.

.items {
    padding:0;
    margin:0;
    /*width:180px;*/
}
.items li {
    border: 1px solid red;
    list-style-type: none;
    position: relative;
}
.items h4 {
    margin:0; padding:0; font-size:16px; padding-right:10px;
}

.items .goto {
    margin-top: -10px;
    position: absolute;
    right: 0;
    top: 50%;
}

View DEMO

Take a look at the link below and try resizing your browser window to see how the layout reacts.

See RESULT

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

JavaScript error leading to ERR_ABORTED message showing up in the console

When I try to load my HTML page, my JavaScript code keeps throwing an error in the console. I am attempting to import some JavaScript code into VSCode using an app module for future reuse. The code is being run through a local server. Error Message: GET ...

The curly brackets in Vue.js do not function properly within a v-for loop when there is an object nested inside an array

I am a novice in vuejs and I am attempting to utilize mustache {{}} to render elements of an array. It works fine for simple arrays, but when I try it with an array containing objects, it doesn't seem to work. Is there an issue with my code, or is it ...

"Troubleshooting a CSS Bug on a PHP Dynamic Web

I have a PHP web page that is dynamic. localhost/ctd/index.php Everything is working fine, except when I add a forward slash like this: localhost/ctd/index.php/ The CSS does not load. Is this a CSS bug or PHP issue? Here is the full code of index.php: ...

Encountering a problem in vue.js: "The instance is referencing 'step1_category' which is not defined as a property or method during render"

I encountered an error that says, "Property or method 'step1_category' is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializin ...

An error that cannot be recovered from occurred: Trying to access a method called viewConfessionLikes() that is

Greetings! I am currently facing an issue on my website where the page displaying the most liked post is showing an error message: Fatal error: Call to undefined method Confession::viewConfessionLikes() on line 55 Unfortunately, this page is not showing a ...

Unable to locate a specific div element using Selenium in Python, encountering a NoSuchElementException

Hello, I'm new to Python and still learning the ropes. Currently, I am attempting to locate something on a particular website. Here is what I'm looking for: <div class="price">$ 1.67<span class="discount">-19.71%&l ...

Transparent CSS Table Styling

Can you create a CSS effect where all content inside a table becomes transparent on hover? For example: <table><tr><td> AN IMAGE </td> <td> SOME TEXT </td></tr></table> So if either the image or the text ar ...

What is the best way to design a Bootstrap grid layout that includes a responsive left column with a minimum width and a scrollable right column?

I'm so close to getting the desired look and behavior, but I'm facing a problem with the scrolling in the right column. The scrollbar is visible but it's not scrolling the content on the right side. It should look like this: . Currently, i ...

retrieve information from a div using an AJAX request

I don't have much experience with ajax and javascript, but I need some assistance, so I will do my best to explain clearly! Below is the JavaScript associated with a button: $('#button-confirm').on('click', function() { $.ajax({ ...

The constricted styles are causing the whole page to bounce (scroll) up and down

On my SPA frontend, I have a parent div with a height of 580 containing 9 smaller divs (about 190px in height each). The parent div has an overflow set to hidden so that only 3 elements are visible at one time. Every 5 seconds, I change the styles by addin ...

Cascading Style Sheets variable and Sassy CSS mixin

I'm facing a challenge involving the use of CSS variables in conjunction with my VueJs app. The goal is to make a hover effect (changing background-color) customizable by the application, while having a default value set in a nested SCSS map using the ...

Is it possible to modify the button icon on hover by utilizing chakra-ui and vanilla-extract?

My stack includes nextjs13, chakra-ui, and vanilla-extract Seeking guidance on how to update both icon and text within a button upon hover, utilizing chakra-ui and vanilla-extract. The current setup of my button is as follows: <Button > <svg wi ...

`Custom transitions between sections using fullpage.js`

Has anyone used the fullpage.js extension to achieve an animation similar to this one, where sections are destroyed on scroll? ...

What is the best way to apply styling to a kendo-grid-column?

Utilizing the kendo-grid in my project serves multiple purposes. Within one of the cells, I aim to incorporate an input TextBox like so: <kendo-grid-column field="value" title="{{l('Value')}}" width="200"></kendo-grid-column> It is ...

The alignment of the text seem to be malfunctioning

The text-align:center property isn't working as expected. Can someone please assist me in figuring out why it's not functioning correctly? Here is the HTML code snippet: <!DOCTYPE html> <html> <head> <title>Wheat and ...

What could be causing the discrepancy in sizes of the columns in my multi-column layout?

https://jsfiddle.net/drqjmssy/ Seeking assistance from the linked jsfiddle, I aim to align "div class='main_content'" vertically with "div class='sidebar'". What would be the best approach to achieve this alignment? In case the fiddle ...

Clone the children of an li element using jQuery, modify the text within a child tag, and then add it to

I have some awesome CSS that I want to recycle within a <ul>. My plan is to duplicate an existing <li> (to leverage the CSS), modify a <p> element, and then add it at the end of the <ul>. I believe I can achieve this by locating... ...

Stylish Sudoku Grid Design with CSS-Grid Borders

I have designed a sudoku puzzle and I am seeking assistance with CSS to style it. My goal for styling using CSS is illustrated here... https://i.stack.imgur.com/nrA47.png What I currently have appears like this... https://i.stack.imgur.com/zpNp6.png T ...

Unexpected behavior from Internet Explorer - Span contents remain unchanged despite valid input

I have a simple question because I'm feeling a bit lost. Check out this JSFiddle link It seems that in Internet Explorer, the contents of my span won't update even though the input is valid. However, in other browsers, the span content changes ...

Unable to align only one link on the right as flex-end is not functioning correctly

I need help rearranging my row of items displayed at the top of the page. I want one item to appear on the far right while the rest are aligned to the left. I attempted using align-self: flex-end but it didn't work. Here's the current code snippe ...