html - :hover on a div with a border radius -


i'm having issue hovering , div border radius.

when div has images inside , border radius, "hitbox" incorrect. hovering on of corners of div (where corners if didn't have border radius) causes hover style show. expect style show when mouse within circle.

if there nothing in div, div's "hitbox" correct, surpasses border when there elements within it.

i background image in div, i'd prefer not accessibility reasons.

#test-wrapper {    background-color: #eee;    border: 4px dashed #999;    display: inline-block;  }    #switcher {    border-radius: 50%;    overflow: hidden;    position: relative;  }    #switcher,  #switcher .first,  #switcher .second {    width: 100px;    height: 100px;  }    #switcher .first,  #switcher .second {    position: absolute;    top: 0;    left: 0;  }    #switcher:hover .first {    display: none;  }
  <!-- used show "hitbox" switcher , has no effect on switcher -->  <div id="test-wrapper">    <div id="switcher">      <!-- shown on hover -->      <img src="https://placeholdit.imgix.net/~text?txtsize=30&txt=second&w=100&h=100&txttrack=0" class="second">            <!-- shown -->      <img src="https://placeholdit.imgix.net/~text?txtsize=30&txt=first&w=100&h=100&txttrack=0" class="first">    </div>  </div>

the problem here child elements not inherit border-radius of parents. there 2 ways achieve want: can either set border-radius of child element(s) match or greater parent element's radius or set overflow property of parent element hidden.

here's quick snippet illustrating problem , both solutions:

*{box-sizing:border-box;color:#fff;font-family:arial;margin:0;padding:0;}  div{      background:#000;      border-radius:50%;      display:inline-block;      line-height:150px;      margin:10px;      text-align:center;      vertical-align:top;      width:150px;  }  p{      background:rgba(255,0,0,.25);  }  div:nth-of-type(2)>p{      border-radius:50%;  }  div:nth-of-type(3){      overflow:hidden;  }
<div><p>square hit area</p></div><div><p>round hit area 1</p></div><div><p>round hit area 2</p></div>


if child elements images you'll need added trick of using image map crop hit areas (credit: border-radius , :hover state area issue), so:

*{box-sizing:border-box;color:#fff;font-family:arial;margin:0;padding:0;}  div{      background:#000;      border-radius:50%;      display:inline-block;      margin:10px;      text-align:center;      vertical-align:top;      width:calc(33% - 20px);      max-width:600px;  }  img{      display:block;      cursor:pointer;      height:auto;      width:100%;  }  div:nth-of-type(2)>img{      border-radius:50%;  }  div:nth-of-type(3){      overflow:hidden;  }
<div><img alt="" height="600" src="http://lorempixel.com/600/600/nature/3/" width="600"></div><div><img alt="" height="600" src="http://lorempixel.com/600/600/nature/3/" width="600" usemap="circle"></div><div><img alt="" height="600" src="http://lorempixel.com/600/600/nature/3/" width="600" usemap="circle"></div>  <map name="circle"><area shape="circle" coords="0,100%,100%,100%"></map>


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

android - Go back to previous fragment -