Encapsulate spans within a cell of a table

Currently, I am facing an issue with my table where the green span elements (a,b,c,d,e,f) are not wrapping properly to have equal column widths. The first column is taking its width from the spans and all are in one line, which is not the desired outcome. I have tried adjusting the display and word-wrap options but nothing seems to be working.

body {
  background-color: #444;
}

table {
  border-collapse: collapse;
}

table,
th,
td {
  border: 1px solid black;
  font-size: 30px;
  height: 40px;
  text-align: center;
}

td {
  width: 100px;
}
.goods td:nth-child(1){
   color:green;
}
.goods span{
  border: 1px solid green;
  border-radius:15px;
  padding-right: 5px;
  width:13px;
  cursor:pointer;
}
<table id="main">
  <tr>
    <td>K</td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
  </tr>
  <tr>
    <td>*</td>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
    <td>6</td>
    <td>*</td>
  </tr>
  <tr class="goods">
    <td><span id="good1">a</span><span id="good2">b</span><span id="good3">c</span><span id="good4">d</span><span id="good5">e</span><span id="good6">f</span></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
  </tr>
  <tr>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td><span id="ship">+</span></td>
  </tr>
</table>

Answer №1

span is typically an inline element, so it may be necessary to adjust the display property to inline-block, for instance.

body {
  background-color: #444;
}

table {
  border-collapse: collapse;
}

table,
th,
td {
  border: 1px solid black;
  font-size: 30px;
  height: 40px;
  text-align: center;
}

td {
  width: 100px;
}
.goods td:nth-child(1){
   color:green;
}
.goods span{
  border: 1px solid green;
  border-radius:15px;
  padding-right: 5px;
  width:13px;
  cursor:pointer;
  display: inline-block;
}
<table id="main">
  <tr>
    <td>K</td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
  </tr>
  <tr>
    <td>*</td>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
    <td>6</td>
    <td>*</td>
  </tr>
  <tr class="goods">
    <td><span id="good1">a</span><span id="good2">b</span><span id="good3">c</span><span id="good4">d</span><span id="good5">e</span><span id="good6">f</span></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
  </tr>
  <tr>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td><span id="ship">+</span></td>
  </tr>
</table>

Answer №2

To make a span display as inline block, simply add display: inline-block; in the CSS

body {
  background-color: #444;
}

table {
  border-collapse: collapse;
}

table,
th,
td {
  border: 1px solid black;
  font-size: 30px;
  height: 40px;
  text-align: center;
}

td {
  width: 100px;
}
.goods td:nth-child(1){
   color:green;
}
.goods span{
  border: 1px solid green;
  border-radius:15px;
  padding-right: 5px;
  width:13px;
  cursor:pointer;
  display: inline-block;
}
<table id="main">
  <tr>
    <td>K</td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
  </tr>
  <tr>
    <td>*</td>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
    <td>6</td>
    <td>*</td>
  </tr>
  <tr class="goods">
    <td><span id="good1">a</span><span id="good2">b</span><span id="good3">c</span><span id="good4">d</span><span id="good5">e</span><span id="good6">f</span></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
  </tr>
  <tr>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td><span id="ship">+</span></td>
  </tr>
</table>

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

What is the most effective method for resetting the scroll position of a browser when refreshing a page?

Portfolio Query I am in the process of setting up a basic portfolio website. My navigation bar includes various links, one of which is labeled "About Me". Upon clicking this link, the page immediately jumps to a specific section with an element ID of #abo ...

Capturing variable data without retaining the information entered

I recently received some code that seems to be malfunctioning, and I'm struggling to pinpoint the issue. The code in question involves a hyperlink (.control_button) that triggers the opening of a form (#convert-form) within a lightbox. Within this for ...

Can CSS selectors be grouped together under the same root?

Is it possible to cluster these selectors together in normal CSS like below? .container { .header {...} .content p {...} .button-wrapper .button {...} .footer {...} } Or do I need to use Sass in order to achieve this grouping? ...

Removing Borders from HTML Tables

I am struggling to eliminate the border of a table within a gridview. The table is located inside the itemtemplate, and I can't figure out how to remove the border. Can someone please assist me with this? Thank you. Shown below is the code I am worki ...

What's the best way to display a component once a function has been executed?

My service controls the visibility of components using *ngIf! Currently, when I click a button, the service sets to true and the components appear instantly! I want the components to only show after a specific function has finished executing. This means I ...

Issues with jquery gui elements in Chrome extension being unresponsive

Using chrome extensions and jquery to create a customized GUI, but running into issues with displaying the jquery elements on the HTML page. Can anyone spot what might be the problem? manifest.json: { "name": "my extension", "description": "my first ...

What would be more efficient: inputting all items in a form at once or adding them one by one consecutively

My mind is preoccupied with a dilemma: is it better to have all possible items already on the form, or should items only be added when the user requests them? Let me elaborate - Imagine I have a form with 4 input fields and one textarea. Alongside that, t ...

Unable to retrieve the unprocessed value (including periods) from an HTML number input

Whenever I press a key on the <input type="number" id="n" /> and then type a ., it appears in the input but does not show up in $('#n').val(). For instance, if I type 123., the result from $('#n').val() is just 123. Is there a w ...

Implementing a striking image background for your Bootstrap website header

I am trying to add a background image to a main header that contains a navbar, but I am having trouble getting the image to display correctly. Here is the jsfiddle link for reference: http://jsfiddle.net/Mwanitete/fgkq759n/8/ Here is the HTML code snippe ...

Updating an SVG after dynamically adding a group with javascript: a step-by-step guide

Currently, I am working on a circuit builder project using SVG for the components. In my HTML structure, I have an empty SVG tag that is scaled to full width and height. When I drag components from the toolbar into the canvas (screen), my JavaScript functi ...

The size of the table and the ability to scroll cannot be adjusted

<div id="answer3" class="tab-pane"> <div class="col-md-12" style="background-color:rgba(68, 70, 79,0.4);padding-left:50px;padding-top:5px;border-radius:5px;height:100%;"> <h3>KEYBOARD SHORTCUT INFO</h3>< ...

"Enhance User Interactions with Angular-ui Tooltips Featuring

I recently integrated bootstrap tooltips into my application. While the "normal" tooltips are working fine, I encountered an issue with tooltip-html-unsafe. It seems to display an empty tooltip. Here is my tooltip code: <a><span tooltip-placeme ...

Noticed a peculiar outcome when working with hexadecimal calculations in LESS. What could be causing this phenomenon?

I recently dove into the world of LESS and have found it to be quite interesting. However, I encountered a unique situation where a background color was assigned the code #03A9F4 - 100. Based on my limited knowledge of hexadecimal numbers from some IT clas ...

Unlocking the potential of resizable bootstrap table columns in React

Currently utilizing a bootstrap table <div class="table-responsive"> <table class="table table-bordered"> <thead> <tr> <th>#</th> <th>Table heading</th> </tr> < ...

Tips for creating a hyperlink to a particular tab

Please click here for the demo I am attempting to generate links like this page1.html#section1, page2.html#section2, but these links are not functioning correctly. Relevant code snippet function showSection( sectionID ) { $('div.section'). ...

Ways to create an API for customizing CSS, styles, and branding for a self-contained AngularJS directive (reusable widget/component)

As I delve into the world of directives and explore resources like , I ponder the idea of a reusable component or widget model. One question that arises is: How can we best provide an API for a hosting application to customize the CSS of a encapsulated wi ...

Automatically populate fields when the selection changes

Currently, I am facing an issue with a mysql table named clients that I utilize to populate the options of a select in one of the rows. The goal is to automatically fill out 2 input fields with client information when a specific option is selected. To acc ...

Deactivating the submit button after a single click without compromising the form's functionality

In the past, I have posed this question without receiving a satisfactory solution. On my quiz website, I have four radio buttons for answer options. Upon clicking the submit button, a PHP script determines if the answer is accurate. My goal is to disable t ...

The "else" statement is never being executed

Trying to create a form where users enter their first name, last name, and city. If any input is empty or contains numbers, a message should appear requesting to fill out all boxes without numbers. Otherwise, display a personalized quote using the input in ...

What is the best method to extract the value of a TextField from an HTML file and transfer it

I am a newcomer to Nativescript and I'm interested in storing user data on my mobile device using Couchbase database. Currently, I have set up TextField elements for users to enter their first name and last name, with a save button available. My quest ...