Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent div with display:table-cell from stretching

Tags:

html

css

I have the following HTML

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
    <title>Table-cell issue</title>
    <style type="text/css">
        html, body { height: 100%; }
        .table
        {
            display: table;
            height: 100%;
        }
        .row
        {
            display: table-row;
        }
        .aside
        {
            display: table-cell;        
        }
        .center
        {
            background-color: red;
            display: table-cell;
            width: 100%;
        }
        .wide
        {
            background-color: green;
            width: 16000px;
        }
    </style>
</head>
<body>
    <div class="table">
        <div class="row">
        <div class="aside">
            Left column
        </div>
        <div class="center">
            <div class="wide">&nbsp;</div>
        </div>
        <div class="aside">
            Right column
        </div>
        </div>
    </div>
</body>
</html>

The problem is that the div.center stretches to fit its content, while the div.table is meant to occupy the whole viewport, and the rest of the div.center's content should be invisible. Neither overflow:hidden nor explicit width setting in pixels (900px, for instance) helps.

I would be very grateful for any idea on how to fix the issue.

like image 604
Alexey Avatar asked Feb 13 '11 00:02

Alexey


People also ask

How do I stop my DIV from stretching?

To prevent a div container from streching, when defining the width and height, don't use %, because this means that the width and height of the div is changed based on the width and height of the browser window. Use px instead, and you should not have such issues.

How do you stop a cell from expanding in a table?

To prevent cells (rows) from expanding vertically, you have to set a fixed height for table rows. Select the relevant rows and, on the Table Tools Layout tab, click Properties. On the Row tab, select "Specify height" and then choose "Exactly" for "Row height is." Specify the desired amount.

How do I stop image stretching in CSS?

1.1.When you added max-width: 100%; to any image, the width will be adjusted automatically based on the outer element width and you know what height: auto; does as it's self-explanatory. This is the fix we have been using for years to prevent image stretching in our sites.


1 Answers

Use table-layout:fixed on the table div. This disables the automatic cell resizing and will make the center only as wide as you allow it to be.

like image 159
SpliFF Avatar answered Sep 30 '22 16:09

SpliFF