How do I make an tag the size of it's parent
  • tag for larger clickable region?
  • 前端 未结 7 1392
    我在风中等你
    我在风中等你 2020-12-08 04:11

    I would like to do this so that the clickable region on the tag is size of the LI.

    My html looks like:

  • 相关标签:
    7条回答
    • 2020-12-08 04:47

      If you currently have this same question you can simply add padding to the right place:

      li {
        //remove any padding or margin attributes from here
      }
      
      li a {
        display: block;
        padding: 20px; //or however big you want the clickable area to be
      }
      

      Anchor tags are by default inline elements, so you have to explicitly change them to display as block elements before you can mess with the padding or the margins.

      Hope this helps!

      0 讨论(0)
    • 2020-12-08 04:48

      In CSS:

      li a {
           display: block;
      }
      

      Of course, you'll want to make your selector more specific than that.

      <ul>
          <li class="myClass">
              <a href="#">Link</a>
          </li>
      </ul>
      
      li.myClass a {
          display: block;
          background-color: #fdd; /* Demo only */
      }
      

      http://jsfiddle.net/userdude/jmj2k/

      0 讨论(0)
    • 2020-12-08 04:51
      li a{     
      display: inline-table;
      height:95%;    
      width: 95%;
      }
      

      the 95 to anticipate any li margin or padding

      0 讨论(0)
    • 2020-12-08 04:57

      Just another option I used is create a transparent png image in photoshop and put it inside the anchor tag, make its position absolute and increase its dimensions to fit that parent div you want and you could have a large clickable area.

      <a href="test.html" />
       <img id="cover_img" src="cover.png" />
      </a>
      
      #cover_img {
      display: block;
      height: 200px;
      width: 193px;
      position: absolute;
      }
      

      Might be useful in certain circumstances.

      0 讨论(0)
    • 2020-12-08 04:59

      As others have said

      li a { display: block; }
      

      should achieve what you're after. But you must also remove any padding from the <li> and set it on the <a> instead. For example:

      li { padding: 0; }
        li a { display: block; padding: 1em; }
      
      0 讨论(0)
    • 2020-12-08 05:02

      Try this css

      li{
          border:1px solid black;
          height:40px;
      }
      
      li a{
          border:1px solid red;
          display:block;
          height:100%;
      }
      

      0 讨论(0)
    提交回复
    热议问题