Display a <button> element as a text hyperlink within a paragraph, ensuring proper wrapping and flow

I want to incorporate a link-styled <button> within a block of text while ensuring that it flows smoothly with the surrounding content.

Although styling the button is straightforward and setting display: inline allows it to blend into the text flow, it becomes problematic if the button spans multiple lines as it then displays on a separate line.

Essentially, I am looking for what MDN refers to as a "line-split inline element."

(While converting the <button> into an <a> or a <span> would be easier, it's not ideal for accessibility purposes. Modern HTML and CSS should offer enough customization options without resorting to such alternatives.)

Below are my attempts so far, along with a demonstration illustrating the issue:

.inline-button {
  background: none;
  border: none;
  color: blue;
  text-decoration: underline;
  cursor: pointer;
  font: inherit;
  padding: 0;
  display: inline;
  line-height: inherit;
}

#narrow {
  width: 5em;
}
<p>
  This is a paragraph with <button class="inline-button">inline button</button> that wraps nicely with the text.
</p>

<div id="narrow">
  <p>
    This is a paragraph with <button class="inline-button">inline button</button> that wraps nicely with the text.
  </p>
</div>

Answer №1

To address this issue, one potential solution is to utilize the display: contents property, however, it's important to consider any potential accessibility concerns that may arise.

.inline-button {
  background: none;
  border: none;
  color: blue;
  text-decoration: underline; /* this won't work (and other properties as well) */
  cursor: pointer;
  font: inherit;
  padding: 0;
  display: contents;
  line-height: inherit;
}

#narrow {
  width: 5em;
}
<p>
  This is a paragraph with <button class="inline-button">inline button</button> that wraps nicely with the text.
</p>

<div id="narrow">
  <p>
    This is a paragraph with <button class="inline-button">inline button</button> that wraps nicely with the text.
  </p>
</div>

Answer №2

Given the current state of CSS and HTML, I decided to address this minor issue by following @Paulie_D's simple solution: using a <span> element with the appropriate CSS styling and HTML attributes (tabindex=0 role=button, as well as handling Enter / Space key events) to mimic a link.

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

Is there a way to deactivate or dim a specific hour in an HTML time form?

I am currently facing a challenge with my booking form. I need to find a way to grey-out or disable the hours/times that have already been booked by previous customers. This will help ensure that the next person can select a different time slot. However, I ...

Can you identify the specific name of the IE CSS bug affecting double vertical padding?

(Just when I thought I've encountered all the strange IE CSS bugs, here comes another one. Is there a name for this double-vertical-padding bug?) After creating a simple test case that worked perfectly in browsers like FF and Opera, I was surprised t ...

The CSS background-image fade transition is optimized for Chrome browsers

HTML: <a class="navbar-brand page-scroll" href="#intro"></a> CSS: .navbar-custom .nav li a.navbar-brand { width: 70px; height: 62px; background: url(myimg.png) no-repeat; background-size: 70px 62px; display: block; po ...

Is the imported style file not properly scoped?

Whenever I attempt to import a CSS file using this method, the styling is not properly scoped. Is it necessary to write the styles within a style tag for them to work correctly? I have been experimenting with this in Vue-cli. <style scoped> @im ...

Tips for Building Common Controller Logic in CakePHP

I am in the process of developing a website where I have set up dynamic content for the footer, which is retrieved from a database. I am looking to create a single controller logic code that can be used across all pages on the site. Any suggestions or gu ...

Steps for properly inserting a hyperlink:

I'm in need of assistance. I have a grid containing several images that I want to make clickable by adding anchor tags, but when I do this, the images seem to disappear completely. I suspect there's an issue with them being inside the container d ...

Make the checkbox font-weight bold when it is checked

Attempting to apply custom CSS to enhance the appearance of checkboxes using input and label elements. The goal is to make the font-weight bold when a user clicks on the checkbox, and then revert it back to regular if clicked again. Currently stuck on this ...

Creating a dynamic form in Angular based on user input

I am in the process of creating a dynamic web form where the user's input will determine the subsequent form inputs that are generated. For example: <mat-form-field> <mat-select placeholder="Type" [(ngModel)]="row.Type" (change)="Typ ...

Dynamic HTML Generation using ASP.NET

Below is the code snippet that needs to be optimized: <asp:TemplateField HeaderText="Item Data"> <ItemTemplate> <%# DataBinder.Eval(Container.DataItem, "ItemData1") %> <br />&nbsp;&nbsp; <%# D ...

Displaying Data in a Table

In my current setup, I have a table that receives data from a form through JavaScript. The code is as follows: <div class="row"> <div class="span*"> <table id="production" class="table table-bordered" style="margin:10px auto;bor ...

Images are not being shown on the desktop carousel display

I am encountering an issue with my carousel. When I attempt to decrease the height of the carousel, the entire image is not being displayed properly (on desktop), although it works fine on mobile devices. My goal is for the carousel not to take up the ent ...

When the navbar is expanded, a right-aligned image is visible; however, it becomes invisible when the

My website has a navigation bar with two logos - one in the left corner (the coat of arms) and one in the right corner (the scout lily), along with text links in between. The coat of arms uses the "navbar-brand" class. To see how it looks, click here: htt ...

Altering the hover functionality for dynamically created class elements

I have a vision to create a unique e-commerce storefront with tile design, featuring an item thumbnail that reveals the item name upon hovering. I found inspiration from this example, but I want the item name to slide up smoothly on hover, rather than simp ...

Reset functionality for input selector is malfunctioning

My HTML code looks like this: <input type="number"> <br> <input type="number"> <br> <input type="number"> <br> <button>+</button> In my JS script, I have the following: $('input').on('c ...

Overcoming Challenges with Ajax Success in Bootstrap DataTable Modal

Recently, I created a webpage featuring a top fixed navbar and a CRUD datatable with pagination supported by buttons that trigger modals. However, I'm encountering an issue where upon clicking the delete button in the modal to remove a record, the mod ...

Opening a Text File via an ASPX Web Page

Currently, I am immersed in a Web Pages project. The goal is to retrieve text from a Text File and display it within a div element. To achieve this, I utilized the ajax xmlhttprequest method, following a tutorial available at http://www.w3schools.com/ajax/ ...

How can you make a dropdown button interactive and display a list simultaneously?

I'm facing an issue with lines 45-47 in the second link. If that code is present, the button animates when clicked (which is great) BUT the dropdown items ("About," "Archive," "Contact") do not appear. If I remove lines 45-47, the dropdown items appea ...

Develop a responsive component on the right side

I am encountering an issue with creating a responsive div that matches the height of the image on the left side. I want to include text in this div, however, when I do so, the paragraph expands in height along with the div element when I attempt to change ...

The onClick functionality of a button may not work properly when nested within a DIV element in an ASP.NET project

Here is a snippet of code that includes a <div> element: <div id="specific"> <table cellpadding="2" border="0"> <tr><td>Choose category</td><td><select id="list_categories" runat="server"></select>< ...

Can someone help with resolving the error message "TypeError: object.__init__() takes no arguments" that occurs when attempting to create a Button using Kivy?

Previously, I had no trouble passing **kwargs to object.__init__() when using a button. However, after adding a Button to my grid, an error started appearing. [INFO ] [Logger ] Record log in C:\Users\trist\.kivy\logs\kivy_20 ...