Why display:table subtract 1 pixel from an image?

Artem Z

What I need is to align text vertically. I add display:table to images, but that makes images 1px shorter. In order to show what I mean I added display:table to the hover state so you can see what happens. Why is that happens?

.wrapper {
  margin: 0 auto;
  max-width: 940px;
  background: #EBEBEB;
}
.border {
  border-radius: 4px;
}
.ideas__gallery div {
  margin: 10px;
}
.ideas__gallery__h4 {
  text-decoration: none;
  font-weight: bold;
  color: #fff;
  font-size: 22px;
  line-height: 1.2em;
  padding: 0;
  margin: 0;
}
.one:hover .ideas__gallery__h4 {
  color: #ff5b5d;
  transition: all 0.2s ease-in;
}
.one:hover,
.two:hover,
.three:hover,
.four:hover,
.five:hover,
.six:hover {
  display: table;
}
.ideas__gallery__h3 {
  color: #333;
  font-weight: bold;
  font-size: 22px;
  text-align: center;
  margin-bottom: 34px;
}
.one {
  width: calc(33.3333333333333333% - 20px);
  height: 310px;
  background: url('http://carwallstar.com/wp-content/uploads/2014/11/ford-car-images2015-ford-mustang--2015-ford-mustang-29-----froggpondcom-w8lqchv6.jpg') 100% 100% no-repeat;
  background-size: cover;
  float: left;
  text-align: center;
}
.two {
  width: calc(33.3333333333333333% - 20px);
  height: 310px;
  background: url('http://www.jdpower.com/sites/default/files/legacy_files/pictures/j/jdpower/0981/d6be82ef8f0dfc684d7aed8755d13dcbx.jpg') 50% 100% no-repeat;
  background-size: cover;
  float: left;
  text-align: center;
}
.three {
  width: calc(33.3333333333333333% - 20px);
  height: 310px;
  background: url('http://cdn.corlate.co/2015/08/04/fordmustangsportscar-l-64469263d3a6c918.jpg') 50% 100% no-repeat;
  background-size: cover;
  float: left;
  text-align: center;
}
.four {
  width: calc(33.3333333333333333% - 20px);
  height: 310px;
  background: url('http://7-themes.com/data_images/out/75/7029170-ford-cars-wallpaper.jpg') 50% 100% no-repeat;
  background-size: cover;
  float: left;
  text-align: center;
}
.five {
  width: calc(66.6666666666666667% - 20px);
  height: 310px;
  background: url('http://img.otodriving.com/files/2015/10/Ford-www.otodriving.com-HD-33.jpg') 50% 100% no-repeat;
  background-size: cover;
  float: left;
  text-align: center;
}
.six {
  width: calc(66.6666666666666667% - 20px);
  height: 310px;
  background: url('http://resources.carsguide.com.au/styles/cg_hero_large/s3/ford-mustang-2015-v8-gt-(2).jpg') 50% 100% no-repeat;
  background-size: cover;
  float: left;
  text-align: center;
}
.seven {
  width: calc(33.3333333333333333% - 20px);
  height: 310px;
  background: url('http://carsformula.com/wp-content/uploads/2014/10/2015-Ford-Mustang-50-Year-Limited-Edition-Specs.jpg') 80% 100% no-repeat;
  background-size: cover;
  float: left;
  text-align: center;
}
<div class="wrapper">
  <div class="ideas__gallery">
    <h3 class="ideas__gallery__h3">Headline</h3>
    <a href="#">
      <div class="one border">
        <h4 class="ideas__gallery__h4">Headline Three Words</h4>
      </div>
    </a>
    <div class="two border"><a href="#" class="ideas__gallery__a">Headline Three Words</a>
    </div>
    <div class="three border"><a href="#" class="ideas__gallery__a">Headline Four Nice Words</a>
    </div>
    <div class="four border"><a href="#" class="ideas__gallery__a">One</a>
    </div>
    <div class="five border"><a href="#" class="ideas__gallery__a">Headline Three Words</a>
    </div>
    <div class="six border"><a href="#" class="ideas__gallery__a">One</a>
    </div>
    <div class="seven border"><a href="#" class="ideas__gallery__a">One</a>
    </div>
  </div>
</div>

Nelson

Your 1 pixel is coming from rounding.

If you load the developer console in Chrome, and check the width, setting display: table causes the width to round down to a whole number. Since it is up to the user agent to handle fractions of a pixel, even though the behavior is inconsistent, it is still valid.

Look at the following code and check out the dimension via the developer console. The display: table is clearly dropping the fractional pixels.

.wrapper {
  margin: 0 auto;
  max-width: 940px;
  background: #EBEBEB;
}
.border {
  border-radius: 4px;
}
.ideas__gallery div {
  margin: 10px;
}
.ideas__gallery__h4 {
  text-decoration: none;
  font-weight: bold;
  color: #fff;
  font-size: 22px;
  line-height: 1.2em;
  padding: 0;
  margin: 0;
}
.one:hover .ideas__gallery__h4 {
  color: #ff5b5d;
  transition: all 0.2s ease-in;
}
.table {
  display: table;
}
.ideas__gallery__h3 {
  color: #333;
  font-weight: bold;
  font-size: 22px;
  text-align: center;
  margin-bottom: 34px;
}
.one {
  width: calc(33.3333333333333333% - 20px);
  height: 310px;
  background: url('http://carwallstar.com/wp-content/uploads/2014/11/ford-car-images2015-ford-mustang--2015-ford-mustang-29-----froggpondcom-w8lqchv6.jpg') 100% 100% no-repeat;
  background-size: cover;
  float: left;
  text-align: center;
}
<div class="wrapper">
  <div class="ideas__gallery">
    <h3 class="ideas__gallery__h3">Discover holiday activity ideas</h3>
    <a href="#">
      <div class="one border">
        <h4 class="ideas__gallery__h4">Headline Three Words</h4>
      </div>
      <div class="one border table">
        <h4 class="ideas__gallery__h4">Headline Three Words</h4>
      </div>
    </a>
  </div>
</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to display only 1 pixel from loaded image and tile it

Why do I need to subtract 1 from label in LDR instruction?

why does gcc subtract 1 from an expression while assigning the expression?

Attempting to display image using pixel values in OpenCV. Only 1/3rd of Image shows

Subtract 2D array from each pixel of a 3D image and get a 4D array

Unable to display image from MySQL table

Subtract one table from another

Python: how to display an image pixel by pixel using random coordinates

Recreating image from pixel data

Get color of Pixel from an image

Unable to retrieve image data from table and display image

why mysql- Select data from table and display to php and when inserting it to another table in mysql it save only 1 value?

Display data from Table 1 after verifying column from table 1,table 2 and table3

How do I display a column from 1 table to another table

Why image has different pixel resolution in Hololens

Golang & Postgres Interaction - Subtract 1 From int64 Table Every Query

Table with image display

Cannot display 1 image from a Array from a API

Insertion sort python algorithm: Why do we subtract 1 from i?

Why is there a pixel space around my table?

How to display image url from firebase into html table?

Not able to display the blob image dynamicaly from mysql database in itext table

Why does "(`subtract`) 1 2" fail?

Why does Array.length- - not subtract 1?

OpenCv subtract same image but from different sources

Subtract vignetting template from image in OpenCV Python

1 pixel table border in JTextPane using HTML

Subtract Two Values from Different Table

Subtract scanner value from sql table value