How to personalize cell and border spacing within the <table> HTML element

Looking to create a design similar to this

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

I attempted using border-spacing, but it applies between all cells. Is there a way to eliminate the space only between label and text cells? Or would using divs for the entire structure be better? What is the recommended approach to achieve this layout?

EDIT: Appreciate all the helpful suggestions, you are amazing! I will remember to use tables for tabular data only. For now, I am going to test out GCyrillus's solution.

Answer №1

Are you a fan of flexbox? If so, make sure to adjust the container paddings and child margins for optimal layout.

* {
  box-sizing:border-box;
}
main {
  display:flex;
  flex-wrap:wrap;
  margin:2em;
  padding:1em 2em;/* replicate your border-spacing */
  border:solid;
  justify-content:space-between;
}
header {
  border:solid;
  width:100%;
  padding:2em;/* adjust based on content , value used for demonstration */
}
div {
  margin-top:2em;/* replicate your border-spacing */
  width:43%;/* adjust the width based on your desired middle spacing */
  display:flex;
}
div p {
  margin:0;/* reset */
  flex:1;
  border:solid;
  padding:2em;/* adjust based on content , value used for demonstration */
}
<main>
  <header>header</header>      
  <div>
    <p> Example text</p>
    <p>Example text</p>
  </div>
  <div>
    <p> Example text</p>
    <p>Example text</p>
  </div>
  <div>
    <p> Example text</p>
    <p>Example text</p>
  </div>
  <div>
    <p> Example text</p>
    <p>Example text</p>
  </div>
</main>

Answer №2

My thoughts and ideas are all gathered in the Snippet, which takes inspiration from GCyrillus' solution but incorporates a unique twist with the use of display:table-* properties. It is a bit intricate and delicate; any unnecessary additions could lead to significant changes. However, as it stands, it functions exceptionally well.

/* Flex/Display-Table Layout */
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
|| This layout utilizes flexbox for element positioning
|| and employs some display: table-* properties to ensure they
|| behave like "table" components. The fundamental idea is:
|| If an element has children*, turn it into a flex container†.
|| If an element lacks children or the children don't need control,
|| transform it into a table component using suitable display:
|| table-*‡ property.
*/
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|| main.table
|| Designed to contain its children - header.caption, section.row -
|| in a structured layout where the children adhere to their relative positions
|| while being flexible due to their percentage-based dimensions.
*/
// Vertical alignment with evenly spaced children
// horizontally centered
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|| header.caption
|| Since header.caption has no children (ignoring text node),
|| it is assigned display: table-row.
*/
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|| section.row
|| As section row contains children like label.cell
|| that require specific positioning, it's set as a flex container.
*/
// Horizontal alignment producing two separate columns
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|| label.cell
|| label.cell is deemed a flex-container because it houses children
|| and requires minimal assistance for proper alignment.
*/
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|| input.text and div
|| The request was to have 'label' and 'text' elements adjacent
|| without any spacing between them. Even though 'label' is not
|| a direct sibling of 'text', I styled their borders separately to create
|| the illusion of adjacency. The 'div' element was used to position
|| the text 'LABEL'; there might be a better approach, but none comes to mind.
*//*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//*Children
|| Direct descendants within the parent element, excluding grandchildren.
//†Flex Container
|| A parent element with flex properties controlling their childrens' positioning.
//‡display: table-* Proerties
|| Specific values within the CSS property display: 
||   table, 
||   inline-table, 
||   table-row, 
||   table-cell
|| Other related table values exist, but the mentioned ones are most relevant.
*/
// http://www.sketchingwithcss.com/samplechapter/cheatsheet.html
// http://colintoh.com/blog/display-table-anti-hero
.table {
  display: flex;
  flex-flow: column nowrap;
  align-items: space-around;
  align-content: space-between;
  justify-content: center;
  width: 75vw;
  height: 75vh;
  padding: 0 1%;
  border: 4px solid black;
}
.caption {
  display: table-row;
  width: 100%;
  height: 27%;
  border: 2px solid blue;
  text-align: center;
  font-size: 3em;
}
.row {
  display: flex;
  justify-content: space-between;
  width: 100%;
  height: 27%;
  padding: 1% 0;
  border: 0 none transparent;
}
.cell {
  display: inline-block;
  position: relative;
  width: 48%;
  height: 100%;
  border: 2px solid red;
  border-bottom: 3px solid red;
  font-size: 1.5em;
}
.cell div {
  display: table-cell;
  position: absolute;
  top: calc(50% - .5em);
  left: 5px;
  line-height: 100%;
}
.text {
  display: table-cell;
  float: right;
  height: 100%;
  width: 50%;
  padding: 0 5px;
  border: 0 none transparent;
  border-left: 1px solid green;
  border-bottom: 1px solid red;
  font-size: inherit;
}
<main class="table">
  <header class="caption">HEADER</header>
  <section class="row">
    <label class="cell">
      <div>LABEL</div>
      <input class="text" value="TEXT">
    </label>
    <label class="cell">
      <div>LABEL</div>
      <input class="text" value="TEXT">
    </label>
  </section>
  <section class="row">
    <label class="cell">
      <div>LABEL</div>
      <input class="text" value="TEXT">
    </label>
    <label class="cell">
      <div>LABEL</div>
      <input class="text" value="TEXT">
    </label>
  </section>
</main>

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 best way to make my text scroll horizontally if the container is not wide enough?

Instead of overwhelming you with code, I'll just share a link to the types of animations I've come across so far. Although these options are close to what I'm looking for, none of them are exactly right. The one that comes closest to my vis ...

``There seems to be an issue with implementing the SlideDown feature in JavaScript

I am having an issue with a code where the expected behavior is to slide down a div when a person hovers over text, but it's not working. Can someone please review and identify the cause of this problem? <script type="text/javascript" src="/js/jqu ...

Utilizing jQuery to dynamically pass parameters to the YouTube API

When making a request to the YouTube API, I am structuring it as follows: $.get("https://www.googleapis.com/youtube/v3/channels", { part: "contentDetails", id: "somestring", key: "MY-API-KEY" } /*, ...*/ ) A hidden field contains the value id ...

Responsive design for iPad and larger screens - adjusting to different screen sizes

Does anyone know of a CSS code that can effectively target both iPad and desktop devices with a max-width of 768? I attempted the following code, however it seems to only work for desktops: @media only screen and (max-width: 768px) Due to this limitatio ...

Show two choices in a select box next to each other

I've been attempting to display a select box with two options side by side in a list format. Struggling to find any libraries that can achieve this functionality. Here is an example: <select> <option x><option y> <optio ...

Guide to periodically updating and displaying a <b-img> element in VueJS

Recently delving into the world of JS and Vue. Within my Bootstrap-Vue project, there's an image element that looks like this: <b-img src="/api/camera" fluid alt="camera"></b-img> Whenever the page is refreshed, the br ...

HTML Email Clickable Box

I am very new to this platform and I feel like I am overlooking something obvious. What I want is an HTML button that will open the user's native email client with the To: field already filled in. <form> <button class="button-21" ...

Positioning div at the top of the body section

Presently, I have this specific designhttps://i.sstatic.net/U4IT4.png The issue I am facing is with the alignment of the jumbotron (boostrap v5). It is currently centered, but I would like it to be positioned at the very top of the body. I have experiment ...

Tips for verifying that one of the two input fields is filled in Bootstrap 5 validation

I have implemented Bootstrap validation for the other input fields in this form by using the 'required' attribute. However, for these two specific fields, if at least one is not empty, then the form should be submitted. <form class="needs ...

Obtaining Text within <span>Tag</span> with Selenium in C#

I am encountering an issue when trying to retrieve the Subject title of an email from Unread emails using Selenium Webdriver with C#. Below is the HTML code provided : <div class="ae4 UI UJ" gh="tl"> <div class="Cp"> <div> <ta ...

Is there a way to save individual rows in my MySQL Database for each new line entered in a text area form?

I've been struggling to create a text area form where users can input multiple links, each separated by line breaks. My goal is to have each link populate its own row in the link table of my database, but I just can't seem to crack the code. I&ap ...

Files are nowhere to be found when setting up an angular project

After creating an Angular project, I noticed that some key files were missing in the initial setup, such as app.modules.ts and app-routing.modules.ts The project was generated using the command ng new name Here is a screenshot displaying all the files th ...

Steps to create a hover effect that alters the appearance of another chosen "element"

For instance, I have two classes named A and B. I want to be able to change the appearance of B when I hover over it, such as its color or size. I am unsure if this can be achieved using CSS and the onmouseover event. I am including a snippet of code that ...

React app (storybook) experiencing loading issues with @font-face

I am struggling to load custom fonts from a local directory. Can someone provide assistance? Below is the code I am currently using: @font-face { font-family: 'My Custom Font'; src: url('./fonts/MyCustomFont.eot'); src: url(&apo ...

Troubleshooting Problems with Owl Carousel Loading

Having trouble with Owl Carousel loading issue. I've set up my carousel using the Owl Carousel jQuery plugin as guided, but it's showing me an "owl-carousel loading" class added to the DOM (owl-loading). I've tried adding custom styles and J ...

Jquery script to update the background of the parent element when Bootstrap Tabs

Currently, I am exploring ways to come up with a more sophisticated solution. However, my proficiency in jquery is somewhat limited at the moment. I believe there must be a more efficient method to achieve the desired function outlined below. In essence, ...

The event.target.x attribute on the <i /> tag element

In my code, there is an <i name="documentId" onClick={this.openDocument}/> element with the onClick method defined as follows: openDocument(event) { const { name } = event.target; console.log('name', name); console.log('target&a ...

Issue with PrimeFaces radiobutton styles not updating after being clicked programmatically

My setup includes a p:selectOneRadio constructed like this: <p:selectOneRadio id="positionRadio" value="#{employeeBean.empPosition}" converter="#{empPositionConverter}" layout="custom" required="true" requiredMessage="Please select ...

jsoup - locate element and delete it along with the element preceding it

In my Android app, I am working on extracting data from a stock market historical prices table. However, there are certain rows in the table that need to be removed for clarity. Specifically, I am trying to remove a row in the third tr. I have managed to r ...

What is the method for implementing two-way binding on a checkbox in Angular?

Within my accordion, I have a series of options in the form of checkboxes. Users are able to select these checkboxes, but I am seeking a way to pre-select certain checkboxes based on specific conditions. The challenge arises when these conditions are deter ...