Analysis and solution of the reason why overflow-y: visible; does not work in CSS

Analysis and solution of the reason why overflow-y: visible; does not work in CSS

Scenario

A recent requirement is an h5 page for mobile devices, which requires a row of selectable cards. The part that exceeds the container can be slid left and right, and each card must have a delete button in the upper left corner. As shown below:

I thought: So easy, just add max-width: 200px; white-space: nowrap; overflow-x: auto; to the parent container and it will be done. The demo is as follows:

<div class="container">
  <div class="son">
    <div class="delete_btn"></div>
  </div>
  <div class="son">
    <div class="delete_btn"></div>
  </div>
  <div class="son">
    <div class="delete_btn"></div>
  </div>
</div>
.container {
  max-width: 500px;
  overflow-x: auto;
  white-space: nowrap;
}
.son {
  display: inline-block;
  width: 200px;
  height: 200px;
  background-color: lightblue;
  position: relative;
  margin-right: 20px;
}
.delete_btn {
  width: 20px;
  height: 20px;
  position: absolute;
  top: 0;
  left: 0;
  background-color: red;
  transform: translateX(-50%) translateY(-50%);
}

I thought everything went well, but the result I got was as shown below:

Look at the red square in the upper left corner of the rectangle. A part of the original 20 * 20 red square is hidden. I thought it was due to overflow, so I tried to solve it by using overflow-y: visible;, but it didn't work. Careful friends should remember that the default value of overflow is visible. So what is the reason?

Why

After searching for a long time, I roughly understood the following reasons

The computed values ​​of 'overflow-x' and 'overflow-y' are the same as their specified values, except that some combinations with 'visible' are not possible: if one is specified as 'visible' and the other is 'scroll' or 'auto', then 'visible' is set to 'auto'. The computed value of 'overflow' is equal to the computed value of 'overflow-x' if 'overflow-y' is the same; otherwise it is the pair of computed values ​​of 'overflow-x' and 'overflow-y'.
This means that when overflow-x is scroll or auto, overflow-y is set to auto, and vice versa. This is very embarrassing. How do we solve this problem?

ps: The above paragraph is from the w3c document, but I couldn't find the original text after searching for a long time. If you find it, please leave a link~ [Manual dog head]

How

After all, I still want the red square in the upper left corner to be displayed completely, so what is the solution? Here I use the following style to add to the container

padding-top: 20px;
margin-top: -20px;

The principle is actually quite simple. After adding padding-top: 20px;, the absolutely positioned red square has space to display and will not exceed the container volume. Then, the position change is offset by margin-top: -20px;. As shown in the figure

ps: The covered part on the left side of the first red square is solved with the same idea, that is, by using padding-left and margin-left.

Summarize

The above is the analysis and solution of the reasons why overflow-y: visible; does not work in CSS introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

<<:  How to display the border when td is empty

>>:  Chrome monitors cookie changes and assigns values

Recommend

Detailed steps to start the Django project with nginx+uwsgi

When we develop a web project with Django, the te...

Box-shadow and drop-shadow to achieve irregular projection example code

When we want to add a shadow to a rectangle or ot...

Solution to the img tag problem below IE10

Find the problem I wrote a simple demo before, bu...

Build a Scala environment under Linux and write a simple Scala program

It is very simple to install Scala environment in...

HTML table mouse drag sorting function

Effect picture: 1. Import files <script src=&q...

Circular progress bar implemented with CSS

Achieve results Implementation Code html <div ...

JavaScript to achieve JD.com flash sale effect

This article shares the specific code of JavaScri...

Use js in html to get the local system time

Copy code The code is as follows: <div id=&quo...

A few things you need to know about responsive layout

1. Introduction Responsive Web design allows a we...

The whole process of node.js using express to automatically build the project

1. Install the express library and generator Open...

Nginx cache configuration example

When developing and debugging a web application, ...

11 Linux KDE applications you didn't know about

KDE Abbreviation for Kool Desktop Environment. A ...

Implementation of proxy_pass in nginx reverse proxy

The format is simple: proxy_pass URL; The URL inc...

jenkins+gitlab+nginx deployment of front-end application

Table of contents Related dependency installation...