Display the button immediately following the text within the cell without extending beyond its boundaries

Check out the code snippet below:

<td>
  <div>
    <span class ="name">Can be long, can be short</span>
    <button>Test</button>
  </div>
</td>

td{
  white-space: no-wrap;
  width:175px;
  position: relative
}
button{
  position:absolute;
  right: 15px;
  bottom: 5px;
}

When I implement this code, I observe:

https://i.sstatic.net/S5UqP.png

https://i.sstatic.net/EceXs.png

I want the name to display in a single line even if it extends beyond the cell, while the button should always remain inside the cell on the same line as the name. If the name is short, the button should appear next to it, but for longer names stick to the right side of the cell.

Although I used absolute positioning, the button consistently aligns with the right side of the cell which is not desired for short names.

Therefore, I aim for the result shown in the picture for long names, while for short names, I prefer the yellow button to be displayed beside the name rather than adhering to the right side.

Here is the working jsfiddle link: https://jsfiddle.net/8kchkucv/

My question is, can this be achieved solely using CSS?

Answer №1

Andrew, achieving what you're asking for with just one CSS for both buttons is not feasible. You may need to consider something like the example provided in this JSFiddle:

https://jsfiddle.net/rohts76/8kchkucv/1

.but
{
  cursor: pointer;
  padding: 2px 5px;
  margin: 5px 0px 0px 5px;
  border-radius: 5px;
  font-family: 'Pacifico', cursive;
  font-size: 10px;
  color: #FFF;
  text-decoration: none;
  background-color: #F2CF66;
  border-bottom: 1px solid #D1B358;
  text-shadow: 0px -2px #D1B358;
  position:absolute;
  //right: 15px;
  bottom: 5px;
}
.cell{
  white-space: nowrap;
  width:175px;
  position:relative;
}
.cell div{
    margin: 0;
    padding: .35em;
    float: left;
    width: 100%;
    height: 100%;
}
tr{
    background-color: #8db4e3;
    background-size: 100% 100%;
}

This code snippet will help you achieve the desired outcome.

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

Creating a pull-up toolbar with just HTML and CSS for an Android browser

I need to create a draggable toolbar on my mobile webapp that can reveal content beneath it. I want to achieve this using just HTML/CSS, without relying on touch/scroll events or libraries. To do this, I'm overlaying a scrolling element on top of the ...

Scrolling background for a nested Bootstrap modal

I am facing an issue with a modal that has a lengthy content and a button to open another modal. Once the inner modal is closed, the outer modal becomes unresponsive. It fails to scroll and instead, the background starts to scroll. While I have come acros ...

Utilizing preg_match in PHP to validate a textarea for alphanumeric characters, numeric values, and spaces

I've almost got everything working, but I'm stuck on how to utilize preg_match. I checked the manual here http://php.net/manual/en/function.preg-match.php, but it doesn't clearly explain the syntax for creating my own validation. For example ...

Tips for designing a customizable color scheme for your website

Are you looking to implement a changeable color scheme for your website? Getting started can be daunting, especially if you're unfamiliar with sass. Would appreciate it if anyone could share some helpful tutorials or links? For example: example ...

Display a progress bar that shows completion based on the maximum value supplied

I successfully created a progress bar using HTML, CSS, and Javascript. It functions perfectly up to a provided value of 100. However, if a value higher than 100 is given, the progress completes but the value continues to change until it reaches the maximum ...

How to extract a one-of-a-kind identification number from the browser using just JavaScript in a basic HTML file?

Is there a way to obtain a distinct identification number from a browser without resorting to techniques such as generating and storing a unique number in cookies or local storage, and without utilizing a server-side language? ...

Opt for utilizing local data table files instead of relying on a content delivery network (CD

I'm in the process of minifying the CSS in my project and am looking to replace CDN files with offline files. <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/r/bs-3.3.5/jq-2.1.4,dt-1.10.8/datatables.min.css"/> <link h ...

Sending information through AJAX in Elgg

Is there a way to transfer information between two PHP files using AJAX with Elgg? data_from_here.php <?php $entity = elgg_extract('entity', $vars, FALSE); $guid = $entity -> guid; require_js('javascript_file.js'); ...

Tips for integrating elements within React Components to create a Container-like structure

I am looking to create a component similar to the following: class MyContainer extends React.Component { render(){ return <div width="1000">{xxx }</div> } } and it should be used in this way: <MyContainer><xxx>&l ...

Updating form validation by altering input value

I'm currently utilizing JavaScript regular expressions for form validation in my project. After successfully completing the validation without any errors, upon clicking the submit button, I want the form to be submitted and change the submit value to ...

Unable to open links specified in 'href' with Bootstrap 5 Navbar

After finishing developing my first page, I encountered an issue while working on the second page. I am using a bootstrap 5 navigation bar. The way I have set up the navbar is that once it reaches a breaking point, I disable the view to allow the full bar ...

Keep the bootstrap label text fixed below the input field

I'm working on creating a file upload style using bootstrap. The code below is functional, but I'm facing an issue where the label Choose file appears below the input field rather than in the middle. How can I adjust the positioning of Choose fil ...

What are the steps to remove scrollbars in Webmethods 8?

After transitioning from Webmethods 7 to Webmethods 8, I noticed that my portlets now have unwanted horizontal scroll bars at the bottom. Is there a way to remove them? Would CSS be able to solve this issue? Any advice would be greatly appreciated! ...

Problem with keyframe animations in Internet Explorer 10

My CSS animation works flawlessly in all modern browsers, except for IE10 which is causing some trouble. It appears that IE is having difficulty properly rotating 360deg. I am still searching for a workaround that won't compromise the functionality in ...

Surround an embedded YouTube video with a styled DIV

I'm having an issue with embedding three YouTube videos on a page. I wrapped them in a DIV and gave them a CLASS of "videoplayer". However, the DIV and CLASS don't seem to be displaying on the page. To view the problem, check out: Below is the ...

Issues with content overlapping within Bootstrap can arise when elements are

Struggling with my band website development using Bootstrap. Inexperienced in web design, I'm facing difficulties getting elements to align properly without slipping under each other. Looking for insights and suggestions on how to avoid manual CSS adj ...

What is the significance of the A tag when parsing a URL?

So, I encountered a problem that involved parsing a URL to extract the hostname and pathname, then comparing the extracted pathname with a string. If you need help with this issue, check out this post: How do I parse a URL into hostname and path in javasc ...

What is the method for fetching a WebElement with a specific CSS attribute using JavascriptExecutor?

Currently, I am faced with a situation in which I need to locate a WebElement based on its CSS property, specifically the background-color. I successfully crafted the JQuery script below to pinpoint the element using the firefox console: $('.search-b ...

Arrange elements both at the beginning and the end of a line using FlexLayout in Angular

Using the Angular flexlayout library in angular 7/8, I am trying to align 4 buttons on the left side (flex-start) and two checkboxes on the right side (flex-end) at the same level. I would prefer to achieve this layout using the flexlayout Angular library. ...

Tips for sending the output of a Python function to the webpage associated with the clicked action button in Python's Bottle framework

Here is a method I have: @post('/home', method='post') def myFunction(): if(len(request.forms.get('Matrix_dimension').strip()) != 0): length = int(request.forms.get('Matrix_dimension').strip()) tableRow = ...