2

So I am working on a form. And part of how I want the form to function requires a select box and an input(text) box in the same tr. The other items in that tr are just text boxes. I am having a problem getting the text box that shares a tr with the select to go to where the solo text box goes. Therefore, the right edges do not line up.

What I am looking for...

1) I want the text boxes that are solo to fill the box, ideally without adding width. I am fine with a min width of 550px, but not necessary.

2) I want the text boxes that are with the select to fill the rest of the box, meeting the same edge as the solo boxes. Satisfying OCD...

CSS for the table:

table, th, td {
    border: 1px solid var(--main-color);
    padding: 6px 8px;
    white-space: nowrap;
}

table {
    border-collapse: collapse;
    table-layout: fixed;
}

thead {
    background-color: #333;
    border-bottom: 3px solid var(--main-color);
}

thead th, thead a, thead a:visited {
    color: white;
}

th.active {
    background: var(--main-color);
}

table > :not(thead) tr th {
    background-color: #333;
    border-right: 3px solid var(--main-color);
    color: white;
    text-align: right;
}

tr {
    height: auto;
}

td {
    color: black;
}

table input {
        width:100%; /* simply scale inputs to table cell size */
    }
        td.input-group input {
            width:auto; /* but not for radios or checks */ 
        }

HTML (shortened, just one solo text box and one that is sharing with the select):

<table>
    <tr>
        <td>Serial Number: </td>
        <td><input type="text" name="device-serial-number" maxlength="8" required></td>
    </tr>

    <tr>
        <td>MAC Address NIC: </td>
        <td>
            <select name="media-nic">
                <option value="">Media</option>
                <option value="eth">Ethernet</option>
                <option value="inf">Infiniband</option>
                <option value="fib">Fiber</option>
            </select>

            <input type="text" name="mac-nic" maxlength="17">
        </td>
    </tr>
</table>

Fiddle with more CSS and such: https://jsfiddle.net/2o0sn4ep/

  • you want to match edges in cell ? – Pranay Rana Nov 29 '17 at 07:56
  • has the one standing alone always maxlength="8"? Or rather, do the ones standing alone have something in common which differs from the other one? – Kathara Nov 29 '17 at 08:08
  • @PranayRana I want the gap between the box and the right side to be the same as the gap on the left. And if I do width at 100%, it goes over the table border. – Dylan Rhodes Nov 29 '17 at 08:21
  • @Kathara There are many different fields and I am checking/limiting what is entered to limit errors in entry. I have an IP address field, MAC address, name, serial number, and some other things. – Dylan Rhodes Nov 29 '17 at 08:22
  • Why not give the textboxes which are alone a class and style the class with "width: 100%" and "box-sizing: border-box"? (works in your js-fiddle) – Kathara Nov 29 '17 at 09:07
  • @DylanRhodes - let me know if you want DIV base design ....because i dont prefer table base design as it took loger time for browser to interpret table – Pranay Rana Nov 29 '17 at 09:10
  • @Kathara I think Alvaro had the same thing in mind and did so in his suggestion. Is that what you meant? – Dylan Rhodes Nov 29 '17 at 09:15
  • @PranayRana I guess I am not 100% against a div base design, I just need it to be as generic as possible as I am using the same CSS and format for a couple different sites. – Dylan Rhodes Nov 29 '17 at 09:16
  • I suggest you make use of BootStrap that will free you from writing CSS and make your desing responsive... – Pranay Rana Nov 29 '17 at 09:19
  • @DylanRhodes yes his solution is almost the same. The thing that differs is that you can give the class to the elements you want to adapt the style to which solves your problem with the search-bar. I've created a pen for you: https://codepen.io/Kathara/pen/aVRRjK It's just another option, you can try or leave it :) – Kathara Nov 29 '17 at 09:21

1 Answers1

1

You could use the trick marked as correct answer in this question

Using this nice "trick" I modified a bit your css and html (I added a div container to wrap your second input).

It's responsive as well, your inputs will always fill whatever width remaining in the container

:root {
  --main-color: rgb(46, 58, 150);
}

* {
  box-sizing: border-box;
}

table,
th,
td {
  border: 1px solid var(--main-color);
  padding: 6px 8px;
  white-space: nowrap;
}

table {
  border-collapse: collapse;
  table-layout: fixed;
}

thead {
  background-color: #333;
  border-bottom: 3px solid var(--main-color);
}

thead th,
thead a,
thead a:visited {
  color: white;
}

th.active {
  background: var(--main-color);
}

table >:not(thead) tr th {
  background-color: #333;
  border-right: 3px solid var(--main-color);
  color: white;
  text-align: right;
}

tr {
  height: auto;
}

td:first-child {
  text-align: right;
}

td {
  color: black;
}

input[type=text] {
  padding: 9px 10px;
  font-size: 16px;
  width: 100%;
}

select {
  padding: 9px 10px;
  font-size: 16px;
  height: 41px;
  float: left;
  margin-right: 5px;
}

option {
  padding-top: 2px;
  padding-bottom: 2px;
}

div {
  width: auto;
  overflow: hidden;
}
<table>
  <tr>
    <td>Serial Number: </td>
    <td>
      <input type="text" maxlength="8" required>
    </td>
  </tr>

  <tr>
    <td>MAC Address NIC: </td>
    <td>
      <select>
        <option value="">Media</option>
        <option value="eth">Ethernet</option>
        <option value="inf">Infiniband</option>
        <option value="fib">Fiber</option>
      </select>
      <div>
        <input type="text" maxlength="17">
      </div>



    </td>
  </tr>
</table>
Alvaro Menéndez
  • 8,766
  • 3
  • 37
  • 57
  • float: left; in select css .. i think that need to be class base no element base ...because if you use select element elsewhere in page than it will create problem – Pranay Rana Nov 29 '17 at 09:08
  • That fixes my problem with the table, but does raise one smaller problem. Which I would probably come across sooner or later, as I would most likely need an "exception" for this. But I don't know. I have a text box on my menu bar for a search. With the current rules it pushes the button down and makes it taller. https://jsfiddle.net/46c5w2e5/ – Dylan Rhodes Nov 29 '17 at 09:09
  • @PranayRana I will keep in mind...I do not foresee an issue with using select else where. I do not think I am going to use anywhere else. But If need be I will make the switch. Thank you. – Dylan Rhodes Nov 29 '17 at 09:12
  • Use the same trick in your search: https://jsfiddle.net/alvaromenendez/3gabnbfv/ Notice I wrapped again the input and change html buttom position – Alvaro Menéndez Nov 29 '17 at 09:13
  • So after all of the suggestions and fiddling...I think I got what I need. Thank you all very much! I, sadly, spent a lot of time trying to get this to work. CSS is not my strongest language...So thanks again! :) – Dylan Rhodes Nov 29 '17 at 10:04
  • @AlvaroMenéndez It may just be too small, but on my phone I get the text box going outside the table...but it is all fine on desktop. Suggestions? I am using (at)media screen and (max-width: 980px) for my first (and currently only) modifiers. But I do not have anything in there for inputs, divs, etc. – Dylan Rhodes Nov 29 '17 at 10:14
  • Can't help you with that. I know this trick works perfectly on the webs I have used it at phones. It may be some code behind messing with it or maybe phone model or screen resolution. Phone testing is a pain and often not easy. I would recommend you to always use *{box-sizing:border-box;} at the beggining of any project. It may solve future pains (not saying this will help in this case... but it may) – Alvaro Menéndez Nov 29 '17 at 10:19
  • btw. in the answer... remove the "min-width" of the select. This also may be the problem – Alvaro Menéndez Nov 29 '17 at 10:21