Html div 上的 CSS 光标指针不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36092334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 13:01:50  来源:igfitidea点击:

CSS cursor pointer on div not working

htmlcss

提问by Robert Rocha

I want to changer the cursor type on a div but it is not working.

我想更改 div 上的光标类型,但它不起作用。

HTML:

HTML:

<div class='upload-wrapper'>
  <label for='file-input'>
    <img src='http://i.imgur.com/MIY6LmH.png' alt='Upload File' title='Upload File' width='250' height='250'>
  </label>
  <input type='file' name='photo' id='file-input'>
</div>

CSS:

CSS:

.upload-wrapper {
    width: 250px;
    height: 250px;
    margin: 0 auto;
    cursor: pointer;
}

.upload-wrapper img {
    width: 250px
    height: 280px;
}

.upload-wrapper input {
    display: none;
}

Result:https://jsfiddle.net/m7tctnvh/

结果:https : //jsfiddle.net/m7tctnvh/

Thanks in advance for solutions but I would also want to know why the CSS is not working as expected.

提前感谢您的解决方案,但我也想知道为什么 CSS 没有按预期工作。

回答by Blackbam

The image (or label tag) is resetting the cursor. The following works:

图像(或标签标签)正在重置光标。以下工作:

.upload-wrapper img {
    width: 250px
    height: 280px;
  cursor:pointer;
}

Edit: Inherited from label (user agent stylesheet):

编辑:继承自标签(用户代理样式表):

label {
    cursor: default;
}

So this is a browser default. Maybe not in any browser. However you have to be specific.

所以这是浏览器的默认设置。也许不是在任何浏览器中。但是,您必须具体。

Tipp: You may use reset CSS in your projects (http://meyerweb.com/eric/tools/css/reset/). This causes much less pain with browser defaults.

Tipp:你可以在你的项目中使用重置 CSS ( http://meyerweb.com/eric/tools/css/reset/)。这对浏览器默认值造成的痛苦要小得多。

And by-the-way

顺便说一下

label {
cursor:inherit;
}

also solves the problem and is maybe more what you want.

也解决了问题,也许更多的是你想要的。

回答by Vincent G

See this fiddle

看到这个小提琴

.upload-wrapper img {
    width: 250px;
    cursor:pointer; 
}

You have to put it on the img and you forget to add ";" after widthproperty

你必须把它放在img上,你忘记加“;” width财产后

回答by Brahim Chougrani

you forget to put ;in .upload-wrapper imgand you need to put the cursor when hovering over the image so your code should look like this:

您忘记;输入.upload-wrapper img并且需要在将鼠标悬停在图像上时放置光标,因此您的代码应如下所示:

.upload-wrapper {
    width: 250px;   
    height: 250px;
    margin: 0 auto;
}

.upload-wrapper img {
    width: 250px;
    height: 280px;
    cursor: pointer;
}

.upload-wrapper input {
    display: none;
}

https://jsfiddle.net/84rgb45o/

https://jsfiddle.net/84rgb45o/