Enclosing hyperlinks within a container with a set width limit

I've set up a fixed width div with several links inside, each with text containing non-breaking spaces between the words.

My goal is to ensure that when a link reaches the edge of the div, the entire link moves down to the next line.

While testing in JSFiddle, I initially had a single unformatted piece of html and noticed the final link did not move to a new line as desired.

Upon reformatting the code with tags on separate lines, the final tag now appropriately moves to a new line when it hits the width limit of the div.

The HTML is being dynamically generated from a script, producing a concatenated string for rendering. Here is the HTML used along with a working fiddle demonstrating both approaches:

http://jsfiddle.net/8gdez8ur/2/

<div style="width: 350px; overflow: hidden;"><a href="#" action="zoom" style="font-size:11px;padding-right:2px;">Zoom</a><a action="streetview" href="#" style="font-size:11px;padding-right:2px;"> Street'nbsp;View</a><a action="addplacemarker" href="#" style="font-size:11px;padding-right:2px;">Add'nbsp;Placemarker</a><a action="route" href="#" style="font-size:11px;padding-right:2px;">ETA'nbsp;To</a><a action="reportproblem" href="#" style="font-size:11px;padding-right:2px;">Incorrect'nbsp;Location'nbsp;Details</a><a action="sendmessage" href="#" style="font-size:11px;">Send'nbsp;Message</a></div>
<hr>
<div style="width: 350px; overflow: hidden;">
<a href="#" action="zoom" style="font-size:11px; padding-right:2px;">Zoom</a>
<a action="streetview" href="#" style="font-size:11px; padding-right:2px;">Street'nbsp;View</a>
<a action="addplacemarker" href="#" style="font-size:11px; padding-right:2px;">Add'nbsp;Placemarker</a>
<a action="route" href="#" style="font-size:11px; padding-right:2px;">ETA'nbsp;To</a>
<a action="reportproblem" href="#" style="font-size:11px; padding-right:2px;">Incorrect'nbsp;Location'nbsp;Details</a>
<a action="sendmessage" href="#" style="font-size:11px; padding-right:2px;">Send'nbsp;Message</a>
</div>

What could be causing this discrepancy in behavior? Any insights are appreciated!

Answer №1

The issue appears to be stemming from the absence of sufficient white space between your inline elements. This is causing them to display as one continuous string. To address this, consider changing your anchor tags to display as inline-block elements.

a{
    display: inline-block;
}

Check out the updated fiddle here

Answer №2

It appears to be connected to a discussion on creating new lines between anchors in HTML source code, causing empty space in the browser.

To address this issue and achieve the desired outcome, I suggest adding whitespace after each anchor, such as by inserting &nbsp; where applicable.

Answer №3

When you include line breaks in your code (like in the second example), it will result in generating whitespace that will display the content as desired. In contrast, the first example without line breaks would all be on one line, creating a leading space issue with your hyperlink.

To fix this problem, simply add

div a { display: inline-block; }

to your first example code to achieve the desired effect.

Answer №4

While utilizing `inline-block` can solve the issue, the root of the problem in your original post lies within this code snippet:

<a action="streetview" href="#" style="font-size:11px;padding-right:2px;"> Street&nbsp;View</a>

If you observe closely, there is a single space between ">" and "Street," which causes the line break after the first `a` element.

By removing the space, the line remains on a single line but gets clipped due to `overflow: hidden` styling.

div {
    width: 350px;
    overflow: hidden;
    border: 1px dotted blue;   
}
div a {
    font-size: 11px;
    padding-right: 2px;
}
<div><a href="#" action="zoom" >Zoom</a><a action="streetview" href="#" > Street&nbsp;View</a><a action="addplacemarker" href="#" >Add&nbsp;Placemarker</a><a action="route" href="#" >ETA&nbsp;To</a><a action="reportproblem" href="#" >Incorrect&nbsp;Location&nbsp;Details</a><a action="sendmessage" href="#">Send&nbsp;Message</a></div>
<hr>
<div><a href="#" action="zoom" >Zoom</a><a action="streetview" href="#" >Street&nbsp;View</a><a action="addplacemarker" href="#" >Add&nbsp;Placemarker</a><a action="route" href="#" >ETA&nbsp;To</a><a action="reportproblem" href="#" >Incorrect&nbsp;Location&nbsp;Details</a><a action="sendmessage" href="#">Send&nbsp;Message</a></div>
<hr>
<div>
<a href="#" action="zoom" >Zoom</a>
<a action="streetview" href="#" >Street&nbsp;View</a>
<a action="addplacemarker" href="#" >Add&nbsp;Placemarker</a>
<a action="route" href="#" >ETA&nbsp;To</a>
<a action="reportproblem" href="#" >Incorrect&nbsp;Location&nbsp;Details</a>
<a action="sendmessage" href="#" >Send&nbsp;Message</a>
</div>

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

Tips for smoothly transitioning elements to a visible state with a Fadein effect

I have been using a code snippet I found to fade in elements with the class "hideme" as I scroll to them. However, the elements only fade in when they are fully visible. Is there a way to modify the code so that they fade in as soon as they appear in the b ...

Adding vertical separation between Bootstrap Cards

Currently, I am utilizing bootstrap cards, and this is the current appearance of my setup: https://i.sstatic.net/LstAX.png My goal is to create more vertical space between the cards so that Card 1 and Card 3 are not directly next to each other, as well as ...

How can I retrieve XML through a URL using HTML5 and JavaScript?

Currently, I am looking to access an XML file through a URL link. My main goal is to retrieve the file automatically and extract its contents for further processing. However, I am facing difficulties in finding the appropriate method to obtain and read t ...

Employing plain Javascript (without the use of jQuery) to concatenate information from two JSON strings - encountering an error due to the absence of

Having a JSON stringified form data like this: {"name":"jack miller", "address":"123 main st"} I aimed to append more records but encountered an error saying "append is not a function." The desired outcome is to have both sets of records in this format: ...

Utilizing PHP variables within HTML code that is stored within another PHP variable

I've written a .php script that contains some added html in a variable. However, I need to extract text from another variable and include it in the html. Can someone guide me on how to achieve this? Below is the variable containing the html code: $a ...

The div element is finally loading properly after multiple clicks

I need some assistance with loading dynamic divs in Angular. I have created a button that adds new divs each time it is clicked in a specific area. However, once these new divs are added, they appear incorrectly: https://i.sstatic.net/sAE6q.png After add ...

What is the process for downloading a blob using an input field?

Can you help me figure out how to download a blob into an input field? Here is the HTML code I have: <img id="image" src="${pageContext.request.contextPath}/image/accaunt/user?${nowDate.time}"/> <label id="text-add-photo" for="img-input">repl ...

The vertical size of the "input" element is larger in Firefox Android when using "white-space:pre-wrap" compared to Chrome on Android

Check out this simplified example of a bug: .wrap { white-space: pre-wrap; } <div> <input type="date" class="wrap"> </div> If you view this code on Firefox Android (latest version), you'll notice that the height appears larger ...

Using AJAX for initiating Liquid code upon page load

I was trying to create an onClick function that would add liquid code to the div when a certain link is clicked. However, instead of adding both the liquid code and its content, it was only adding the code itself. Here's the code snippet I used: Inde ...

The <a> tag styled as a button with the btn-primary class is malfunctioning specifically on MacOS Safari

Trying to use an anchor tag with type button and btn-primary class on MacOS safari is causing issues, while it works perfectly on Chrome and Edge browsers <a class="btn btn-primary " style="border-radius: 25px;" type="button&qu ...

PHP For Loop Generating HTML Table

I need assistance troubleshooting my code in an attempt to display it within an HTML table using a while loop. Despite my efforts, the table is not appearing. Perhaps there's an issue with how I am attempting to echo out the table? <?php #---- ...

Incorporate a CSS file into your Kohana template

Looking to include a CSS file in my template. I've placed the template.php within the directory: kohana-v3.3.5\application\views. Inside this directory, I also added a folder named "css" where I have my styles.css file containing some code. ...

Is it possible to modify an HTML document on the server side using Node.js?

I am currently working on a website project. As part of this project, there is a Python script that is activated by the user through the UI and runs on the server to generate HTML files. These HTML files are then read by the node server and sent in full to ...

The poker table designed with CSS does not resize effectively when displayed in smaller dimensions

I have put together a CSS poker table design that I am quite happy with at the moment: https://jsfiddle.net/78fcs01m/3/ CSS: .poker-container { display: grid; width: 100vw; height: 100vh; .poker-table { justify-self: cent ...

Deleting Data with HTML Using the HTTP DELETE Method

I've encountered an issue with making HTTP DELETE requests in webapp2. Currently, I've resorted to using a different URI with GET method as a temporary workaround. However, I'd prefer to implement a more RESTful approach. Upon inspecting th ...

Ways to declare a function within the events onMouseOver and onMouseOut

<div align="right" style="border:1 #FF0 solid; background-color:#999" onMouseOver="javascript: function(){this.style.backgroundColor = '#DDD';}" onMouseOut="javascript: function(){this.style.backgroundColor = '#999';}"> Register & ...

Removing Firefox's button outline when the mouse is pressed, not clicked, hovered over, or focused

Is there a way to remove the default outline that appears on my Bootstrap page button when it is pressed and not released? I have already tried targeting mouse focus and active states, but haven't been successful. Default Button https://i.sstatic.ne ...

Showing the Datepicker upon clicking the button

I am trying to implement a datepicker that shows up when a button is clicked. Unfortunately, my attempts have been unsuccessful so far. I am working with Materialize Css and have experimented with using an input type of text, but it's not quite what I ...

Generating interactive HTML content using a JSON file with AngularJS

Currently, I am working on a project where I have a nested JSON object within AngularJS. In order to create a menu from this JSON using the Bootstrap navbar, I need assistance with parsing the highly nested structure and generating dynamic HTML code to d ...

Issues with overlapping floating labels in Bootstrap 5

Is there a way to display longer text without it being cut off or truncated, especially in input fields where the full content is necessary for clarity? It can be frustrating when only part of the text is visible. This issue arises from the Bootstrap 5 e ...