Search code examples
csslayout

Item with `position: absolute; top: 0; left: 0;` has a gap with Its parent on Chrome for Android


Item with position: absolute; top: 0; left: 0; has a gap with its parent on Chrome for Android.

Chrome for Android version: 91.0.4472.120

I write a minimal reproducible code:

html, body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  font-size: 16px;
}

body {
  background-color: #333;
  color: #ccc;
}

ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
}

li {
  position: relative;
  box-sizing: border-box;
  overflow: hidden;
  margin: 1rem;
  height: 4rem;
  line-height: 4rem;
  text-align: center;
  border: solid 1px #ccc;
}

li::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  z-index: 1;
  border-style: solid;
  border-width: 1rem;
  border-color: #ccc transparent transparent #ccc;
}
<ul>
  <li role="button">Cool button A</li>
  <li role="button">Cool button B</li>
  <li role="button">Cool button C</li>
</ul>

https://codepen.io/arzyu/pen/WNjRLXv

Unexpected(chrome on android):

Unexpected

Expected(ios):

expected-on-ios


Solution

  • Do it differently using background only:

    html, body {
      height: 100%;
      margin: 0;
      overflow: hidden;
      font-size: 16px;
    }
    
    body {
      background-color: #333;
      color: #ccc;
    }
    
    ul {
      list-style-type: none;
      margin: 0;
      padding: 0;
    }
    
    li {
      position: relative;
      box-sizing: border-box;
      overflow: hidden;
      margin: 1rem;
      height: 4rem;
      line-height: 4rem;
      text-align: center;
      border: solid 1px #ccc;
      background:linear-gradient(to bottom right,#ccc 50%,#0000 50.5%) 0 0/2rem 2rem no-repeat;
    }
    <ul>
      <li role="button">Cool button A</li>
      <li role="button">Cool button B</li>
      <li role="button">Cool button C</li>
    </ul>