Solved

Breaking td to next row using responsive design

Posted on 2013-12-06
11
1,044 Views
Last Modified: 2014-01-15
I've got a table like
<table><tr>
  <td>A</td>
  <td>B</td>
  <td>C</td>
  <td>D</td>
</tr></table>

that of course displays like:
  A  B  C  D

Now I'd like, in certain circumstances (responsive) make this display like
  A  B
  C  D

Thus, I'd like a "line break" between <td> B and C.
I'd like to handle this by setting a css class on cell C (or B) but not set any particular classes on the other cells.

Is there any smart css to use that forces a "line break" between two <td> cells?

Thanks
/Stefan
0
Comment
Question by:stefanlennerbrant
  • 6
  • 2
  • 2
  • +1
11 Comments
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 39700604
Mate... this is wrong from the core :)

Making layouts with tables is already not good. Making a responsive layout with tables becomes insane :)

Rework your layout using div's.
Have a look at some responsive layouts around and see how they do it.

Have a good look at Bootstrap for instance.
This example illustrates perfectly the way you should be doing it: http://getbootstrap.com/examples/offcanvas/
0
 

Author Comment

by:stefanlennerbrant
ID: 39700645
Sure, all this is no problem at all doing it that way.
However, now there is an existing table-based layout and I'd like to modify it as little as possible.
0
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 39700732
Ok... if you insist :)

The best way I see here without too much impact is to have 2 tables side-by-side instead of 1 but the implementation depends on how your page looks like.

Can you create a sample code with your current table layout?
A mean width wise... do you have a fixed width or you're using width 100% and want to break if the page width is lower than x?
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 39701169
However, now there is an existing table-based layout and I'd like to modify it as little as possible.

Then you will never have anything but a hacked version of a 20th century layout.

Without a link or at least a sample of the content, I don't know if you are trying to do layout management with the table or if you need options for tabular data presentation.

If you are just looking for a quick fix with the least amount of work, then I am not the one to help you.  If you want to do it right then post a link and we can explore the options; but you will need to do some work if you want good presentation across multiple resolutions.

Cd&
0
 

Accepted Solution

by:
stefanlennerbrant earned 0 total points
ID: 39721531
Well, the following works quite ok.
Of course it's not how it's supposed to be done, but it doesn't feel to "hacky", does it?

<!DOCTYPE html>
<html>
<style>
.adjusting { display: block }
</style>
</head>
<body>
<table border=1 class=adjusting><tr>
  <td class=adjusting>
    <table border=1 ><tr>
      <td>A</td>
      <td>B</td>
    </tr></table>
  </td>
  <td class=adjusting>
    <table border=1 ><tr>
      <td>C</td>
      <td>D</td>
    </tr></table>
  </td>
</tr></table>
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:stefanlennerbrant
ID: 39723070
I've requested that this question be closed as follows:

Accepted answer: 0 points for stefanlennerbrant's comment #a39721531

for the following reason:

This seems to solve the problem quite ok
0
 

Author Comment

by:stefanlennerbrant
ID: 39721542
Well, I originally stated that I wanted to "not set any particular classes on the other cells"
My solution doesn't really fulfill this, but as I only need to set class on two td's, not needing to tweek the td's containing actuall text/data, this is good enough for me.
0
 
LVL 42

Expert Comment

by:David S.
ID: 39723071
It's best to avoid using nested tables.

I would recommend looking into whether inline-blocks would suit your requirements. It would be great to be able to use Flexbox for this, but Firefox only supports single-line Flexbox currently.
<!DOCTYPE html>
<html lang="en"><head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">

table {
	margin: 1em auto;
	background: #eee;
}
td {
	width: 140px;
}

table.responsive2Col {
	max-width: 100%;
}

@media screen and (max-width:640px) {
	table.responsive2Col,
	table.responsive2Col tbody,
	table.responsive2Col tr {
		display: block;
		w\ord-spacing: -.43em;
	}
	:root table.responsive2Col {
		letter-spacing: -.31em;
	}
	table.responsive2Col th,
	table.responsive2Col td {
		display: inline-block;
		vertical-align: top;
		width: 50%;
		-webkit-box-sizing: border-box;
		-moz-box-sizing: border-box;
		box-sizing: border-box;
		word-spacing: normal;
		letter-spacing: normal;
	}
}

</style>
</head>
<body>

<table class="responsive2Col"><tr>
	<td>A</td>
	<td>B</td>
	<td>C</td>
	<td>D</td>
</tr></table>

</body>
</html>

Open in new window

0
 

Author Comment

by:stefanlennerbrant
ID: 39725899
That look very interesting, Kravimir, but I don't understand the magic with word-spacing

The tr word-spacing seems to be the thing that avoids breaking on all cells, instead breaking only between cell B and C.
Is this a hack or is there a "good" explanation? (I tested only with Chrome now, and there it all works quite OK)
0
 
LVL 42

Assisted Solution

by:David S.
David S. earned 500 total points
ID: 39727935
Good question. Removing the word-spacing and letter-spacing declarations makes each cell drop to its own line because they are set at 50% width, such that when there is any space between them at all, they won't fit on the same line. Inline-blocks are treated in some ways like words by the browser's rendering engine. As such they get the normal word-spacing around them. So therefore, when you want to have full control of the space between them, you have to reduce the word-spacing. Unfortunately in some browsers that does not work and thus letter-spacing needs to be used instead.

The backslash in the first "word-spacing" declaration and the ":root" pseudo-class are used to hide those declarations from older browsers which would not display the desired effect. (The worst case is that IE5 does not allow resetting at least one of those two properties on descendent elements, so it causes text to be unreadable.) Since those rules are in a CSS3 media query, those two hacks/filters aren't needed, but I left them in the code in case it was moved outside of the media query. Both of those hacks/filters are valid CSS3.

There are other techniques for removing the space between inline-blocks, as the following article explains, but I do not recommend them.
http://css-tricks.com/fighting-the-space-between-inline-block-elements/
0
 

Author Closing Comment

by:stefanlennerbrant
ID: 39781790
Interesting solution.
My own code does solve the actual problem, though :-)
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
In this tutorial viewers will learn how to style different bullet points for unordered lists in CSS. Begin with a normal unordered list; the default bullet point is a solid circle: In the CSS, create a defined class of unordered list by typing "ul.S…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now