/    Sign up×
Community /Pin to ProfileBookmark

Misbehaving CSS display:table;

I’m trying to eliminate HTML tables in my pages in favor of the CSS equivalents, and I’m running into a snag. I’ve put together a simple demo to show the problem. I’m simulating three columns of mixed text and graphics. So there is a div styled as a table, followed by a div styled as a table-row, and then three divs styled as table-cell. Each is filled with demo text.

But at the top of the middle table cell, I’ve placed a div set to the width of the cell, a height of 200px, and a red background. Its the one with the “testImg” class (My original problem was noticed with an <IMG>, but the problem is the same with the simple DIV.)

So as expected, the text in the middle column gets pushed down below the 200pX taken up by the height of the DIV . What is NOT expected is that the text in the first and third columns also gets pushed down! That is crazy, and NOT the way HTML tables work.

Now notice that I have a commented out <br> at the top of the second cell, right above the red styled DIV. If you put that <br> back (by UN-commenting it) the problem goes away. So at the expense of the single extra unwanted space added to the top of the second column, the text in the first and third columns go back to the normal expected behavior, starting at the top of the column!

So is this a BUG in the the way CSS simulates tables? It happens in every browser I own. Obviously I COULD just code a special case, adding a <br> with a font-size of near zero above any images or blocks appearing at the top of a “table-cell” DIV. But I’m hoping I’m just missing something obvious that will solve the problem, without resorting to a kludge.

[code=html]
<!DOCTYPE HTML >
<html>
<head>

<title>Column Problem Demo</title>

<style>
.table {display:table; width:100%;}
.trow {display:table-row;}
.tcell {display:table-cell; padding:5px; border: 1px solid black; }
.testImg {width:100%; height:200px; background-color:red;}
</style>

</head>
<body>

<h1>Column Problem Demo</h1>

<div class=”table”>
<div class=”trow”>

<div class=”tcell”><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In nec viverra mi, ac ultricies erat. Suspendisse at lacus euismod, facilisis leo ac, posuere nisl.</p></div>

<div class=”tcell “>
<!– <br> uncomment to elimiate problem –>
<div class=”testImg”></div> <!– or, could be an image –>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In nec viverra mi, ac ultricies erat. Suspendisse at lacus euismod, facilisis leo ac, posuere nisl.</p></div>

<div class=”tcell “><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In nec viverra mi, ac ultricies erat. Suspendisse at lacus euismod, facilisis leo ac, posuere nisl.</p></div>

</div></div>

</body>
</html>

[/code]

EDIT: OK… I may have solved the problem. If I add “vertical-align:top;” to my table-cell DIV styling class, the problem seems to vanish. I’d like to say “EUREKA”, but it still seems crazy though that the inclusion of a graphic or other object in one column should affect the others! I wonder what other “gotcha’s” I’ll discover as i attempt to convert all my old TABLE based pages to the CSS display:table methods!

to post a comment
CSS

2 Comments(s)

Copy linkTweet thisAlerts:
@ashokapackersJan 21.2019 — Packers And Movers Ameerpet, Hyderabad In India

Ashoka Packers and Movers in Ameerpet, Hyderabad, give best moving arrangement the nation over and abroad with our expert help group and well-moving hardware. We have moved numerous family unit merchandise, mechanical products, workplaces, vehicle and bicycle from Ameerpet to around the globe sheltered and secure, we employed by our profitable clients over and over in Hyderabad and give the most extreme consideration each migration. We take all the security and harm at claim hazard, manage our group advance pressing and moving the procedure to meet the global component.

Contact Us: 9000809809

Get more details to visit us: http://bit.ly/2CacfTE
Copy linkTweet thisAlerts:
@rootJan 21.2019 — It all depends on what you are displaying.

If you are displaying tabulated data then you do need to be using a table, it is after all what they were designed to be used for.

If you are thinking of using divs as the containers, that means a lot of CSS to control positioning and so on.

×

Success!

Help @PeterPan_321 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...