The gaps separating rows

I'm struggling with getting my divs to look like a table, especially when it comes to highlighting a selected row. I've tried adjusting padding and margins without success. Does anyone have any suggestions on how I can achieve this effect?

.table {
  display: table;
}
.header {
  display: table-header-group;
  font-weight: bold;
}
.row {
  display: table-row-group;
}
.cell {
  display: table-cell;
  border: 1px solid black;
  padding: 5px;
}
.selected {
  background: red;
  margin: 20px;
  border: 1px solid red;
  border-radius: 10px;
}
<div class="table">
  <div class="header">
    <div class="cell">Header 1</div>
    <div class="cell">Header 2</div>
  </div>
  <div class="row selected">
    <div class="cell">Row 1 Cell 1</div>
    <div class="cell">Row 1 Cell 2</div>
  </div>
  <div class="row">
    <div class="cell">Row 2 Cell 1</div>
    <div class="cell">Row 2 Cell 2</div>
  </div>
  <div class="row">
    <div class="cell">Row 3 Cell 1</div>
    <div class="cell">Row 3 Cell 2</div>
  </div>
</div>

Answer №1

Does this meet your requirements?

$('.list-item').on('click', function () {
    $('.table-content div').removeClass('active');
    $(this).addClass('active');
});

Fiddle: http://jsfiddle.net/9mthwzq1/

Note: It seems like you're more interested in the CSS design aspect rather than the functionality. In that case, one suggestion could be to use a absolutely positioned div on the selected item.

Answer №2

Perhaps this solution can assist you:

.table {
    display: table;
    border: 1px solid black;
    border-bottom: 0;
}

.row {
    display: table-row-group;
}

.header .{
    display: table-header-group;
    font-weight: bold;
    
}

.cell {
    display: table-cell;
    padding: 5px;
    border-bottom: 1px solid black;
  
}

.cell:first-child{
    border-right: 1px solid black;
}


.selected + .row .cell{
    border-top: 1px solid black;
}

.selected .cell{
    background: red;
 
}

.selected .cell:first-child{
    border-right: 0;
    border-radius: 10px 0 0 10px;
    border-bottom: 0px;
}
  
.selected .cell:nth-child(2){
    border-radius: 0 10px 10px 0;
    border-bottom: 0px;
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
  
<div class="table">
<div class="row header">
    <div class="cell">Header 1</div>
    <div class="cell">Header 2</div>
</div>
<div class="row selected">
    <div class="cell">Row 1 Cell 1</div>
    <div class="cell">Row 1 Cell 2</div>
</div>
<div class="row">
    <div class="cell">Row 2 Cell 1</div>
    <div class="cell">Row 2 Cell 2</div>
</div>
<div class="row footer">
    <div class="cell">Row 3 Cell 1</div>
    <div class="cell">Row 3 Cell 2</div>
</div>
  
</body>
</html>

The issue lies in the fact that the padding property does not apply to elements with display: table-row-group, table-header-group, table-footer-group, table-row, table-column-group, and table-column.

For more information, you can refer to: https://developer.mozilla.org/en-US/docs/Web/CSS/padding

Check out an example here: http://jsbin.com/tofokoriri/edit?html,css,output

Answer №3

Although not a direct solution, this hint might steer you in the right direction. Consider experimenting with the outline property.

.table {
  display: table;
}
.header {
  display: table-header-group;
  font-weight: bold;
}
.row {
  display: table-row-group;
}
.cell {
  display: table-cell;
  border: 1px solid black;
  padding: 5px;
}
.selected {
  background: red;
  margin: 20px;
  border-radius: 10px;
}
.selected { outline: 2px solid blue;}
.selected .cell:first-child {border-right:1px solid red;}
.selected .cell:last-child {border-left:1px solid red;}
<div class="table">
  <div class="header">
    <div class="cell">Header 1</div>
    <div class="cell">Header 2</div>
  </div>
  <div class="row selected">
    <div class="cell">Row 1 Cell 1</div>
    <div class="cell">Row 1 Cell 2</div>
  </div>
  <div class="row">
    <div class="cell">Row 2 Cell 1</div>
    <div class="cell">Row 2 Cell 2</div>
  </div>
  <div class="row">
    <div class="cell">Row 3 Cell 1</div>
    <div class="cell">Row 3 Cell 2</div>
  </div>
</div>

Answer №4

Is this something you were looking for? I included an additional div layer to achieve the desired padding effect. Hopefully, this solution meets your requirements.

$('.row').on('click', function () {    
    $('.table div').removeClass('selected');
    $(this).addClass('selected');
});
.table {
    display: table;
}
.header {
    display: table-header-group;
    font-weight: bold;
}
.row {
    display: table-row-group;
    cursor: pointer;
}

.cell {
    display: table-cell;
    border: 1px solid black;
    padding: 10px;
}
.selected .cell {
    padding: 10px;
}
.selected .cell .content{
    padding-top: 10px;
    padding-bottom: 10px;
}
.selected .cell:nth-child(1) .content{
    background-color: red;
    border-bottom-left-radius: 10px;
    border-top-left-radius: 10px;
    padding-left: 5px;
    margin-left: -5px;
    margin-right: -11px;
    
}
.selected .cell:nth-child(2) .content{
    background-color: red;
    border-bottom-right-radius: 10px;
    border-top-right-radius: 10px;
    padding-right: 5px;
    margin-right: -5px;
    padding-left: 11px;
    margin-left: -11px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="table">
    <div class="header">
        <div class="cell">Header 1</div>
        <div class="cell">Header 2</div>
    </div>
    <div class="row">
        <div class="cell"><div class="content">Row 1 Cell 1</div></div>
        <div class="cell"><div class="content">Row 1 Cell 2</div></div>
    </div>
    <div class="row selected">
        <div class="cell"><div class="content">Row 2 Cell 1</div></div>
        <div class="cell"><div class="content">Row 2 Cell 2</div></div>
    </div>
    <div class="row">
        <div class="cell"><div class="content">Row 3 Cell 1</div></div>
        <div class="cell"><div class="content">Row 3 Cell 2</div></div>
    </div>
</div>

Answer №5

It appears that you are comfortable toggling the class selected with jQuery, so if you need to maintain the existing markup and use display: table, here is a potential solution:

.table {
        display: table;
        border: 1px solid black;
    }

    .header {
        display: table-header-group;
        font-weight: bold;
    }

    .row {
        display: table-row-group;
    }

    .cell {
        display: table-cell;
        padding: 5px;
        border-bottom: 1px solid black;
    }

    .cell:first-child{
        border-right: 1px solid #000;
    }

    .row:last-child > .cell {
        border-bottom: none;
        border-top: 1px solid black;
    }

    .selected {}

    .selected > div:first-child {
        background: red;
        border-radius: 10px 0 0 10px;
        border: 4px solid white;
        border-right: none;
    }

    .selected > div:last-child {
        background: red;
        border-radius: 0 10px 10px 0;
        border: 4px solid white;
        border-left: none;
    }

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

The contenteditable attribute does not function properly in a foreignobject element on Firefox browsers

<?xml version="1.0" standalone="yes" ?> <svg xmlns="http://www.w3.org/2000/svg"> <foreignObject width="100" height="100" x="20" y="65"> <body xmlns="http://www.w3.org/1999/xhtml"> <p contenteditable="true">text&l ...

What is the best way to implement pushstate degradation – using backbone.history or history.js?

I am interested in implementing pushstate functionality, which involves making an ajax request, changing the URL, and adjusting the browser history stack. However, since pushstate is not universally supported by all browsers, I need to find a workaround f ...

Having trouble interacting with an xpath-selected element in Selenium using Python?

I've been attempting to click on an element that I've selected using Xpath, but it appears that I am unable to locate the element. Specifically, I'm trying to click on the "Terms of Use" button on the page. The code snippet I've written ...

What is the process for transforming a String into an HTML element within a Next JS Application?

I stored the product description as HTML elements in a Database, but when I try to render this data into a div, it displays as a String. I am looking to showcase all the data as HTML elements in my Next JS application. I attempted using JSON.parse, but unf ...

Leveraging Ajax XHR2 for uploading files and accessing values in PHP

I recently followed a fantastic tutorial online on how to use AJAX to upload a file in an HTML form. Here is the code snippet I used: HTML <input type="file" id="myFile" name="myFile" /> <input type="button" id="upload" value="upload" /> < ...

How can I align text to the center and set the text color at the same

Looking to place text in the center of a colored box? Although attempting to center the text, you may find that it remains towards the top rather than in the middle of the box. How can you shift the text down so it aligns with the middle of the backgroun ...

Utilizing a media query to execute a <script> exclusively on desktop devices

I am attempting to only run <script type="text/javascript" src="#" data-cfasync="false" async="async"></script> on desktop devices. I experimented with <style> @media screen and (max-width: 600px) { ...

JavaScript taking over the HTML footer content

I'm attempting to update the content of my footer without modifying the HTML by including a class or an id. HTML: <footer> <div> Lorem ipsum dolor sit amet, consectetur adipisicing elit. </div> </footer> Java ...

How can I alter the background color while performing a transformation in JS/CSS?

I'm currently working on achieving a flipboard effect for some divs, where one side is white and the other is black. Here's what I have so far: setInterval(function () { document.getElementById('test').classList.toggle('flipped& ...

Leveraging the ReactJS Hook useState for detecting Key press events

As I am in the process of constructing a piano, I want it to showcase certain CSS styles when the user "presses" specific keyboard buttons (via keydown event) - even enabling the simultaneous clicking of multiple different buttons. Once the user releases t ...

Leveraging reframe.js to enhance the functionality of HTML5 video playback

I'm struggling with using the reframe.js plugin on a page that includes HTML5 embedded video. When I try to use my own video file from the same folder, it doesn't work as expected. While everything seems to be working in terms of the page loadin ...

Troubleshooting problems with jqplot pie charts

Currently, I am utilizing jqplot to create pie charts. Despite following the example code provided in the jqplot documentation, I am encountering errors such as "e.jqplot is undefined." Below is a snippet of my code: <script src="jquery-2.0.3.js"> ...

Troubleshooting issue: Dropdown menu malfunctioning after using replaceWith() and appendTo()

I could really use some assistance. Admittedly, my knowledge of php, js, jquery, and similar languages is limited. I am currently working on a small web application where users fill out a form with specific requests, the data is then stored in a database, ...

Align audio and video elements in HTML5 using JavaScript

I am facing a situation where I have two files - one is a video file without volume and the other is an audio file. I am trying to play both of these files using <audio> and <video> tags. My goal is to make sure that both files are ready to pla ...

I am experiencing an issue where my JavaScript code does not redirect users to the next page even

I'm experiencing an issue with this code where it opens another webpage while leaving the login screen active. I've tried multiple ways to redirect it, such as window.location.href and window.location.replace, but nothing seems to be working. Ide ...

Ensure that when adjusting the height of a div, the content is always pushed down without affecting the overall layout of the page

My webpage contains a div element positioned in the middle of the content, with its height being adjustable through JavaScript code. I am seeking a way to manage the scrolling behavior when the height of the div changes. Specifically, I want the content t ...

Display a loading message using jQuery Dialog when the page is loading

I have a button that triggers a jQuery Dialog box to open. $( "#dialog" ).dialog({ autoOpen: false, title: 'Contract', height: 450, width:1100, modal:true, resizable: true }); $( ".btnSend" ).click(function() { var id=$(this).a ...

Fade out and slide close a div using jQuery

I am creating a new website and incorporating jQuery for animation and simplified JavaScript implementation. My goal is to have a button that, when clicked, will close a div with a fading out and slide up effect. Can this be achieved? Thank you. ...

Issue with Internet Explorer 7 causing table to display with added height

Currently investigating why there is additional space being added by IE. The only fixed data is the width of the image (171 px). Using Jquery, I checked the height of the div in Firefox, Chrome, and Opera which came out to 164px, but in IE 7 it's 172 ...

What is the best way to address a row of hyperlink text located at the top of a webpage?

I have encountered an issue where three rows of text links near the top of a page shift up to the very top when a link is pressed, causing them to obscure a line of text that was already at the top. How can I keep the position of these three rows anchored? ...