Search code examples
htmlcssbackground-imagehref

Turning Background Image within Div into a Link


Though there are a number of questions addressing turning a background image into a link, or creating a link within a div, I am having difficulty find a solution to linking a background image in a div. Specifically: https://jsfiddle.net/pmcgswf4/5/. Ideally, when the user selects either the image OR the caption, it should take them to "personal_project_pages/crazyhearts.html".

Thank you!

html:

    <li>
        <div id="crazyhearts">
            <div class="projects">
                <h2><a href=personal_project_pages/crazyhearts.html class = "alpha"> Crazy Hearts Cutting Board </a></h2>
            </div>
        </div>
    </li>

CSS:

/* Projects */
.projects {border-style: solid;
    width: 99.5%;
    height: 100%;
    /**  min-width: 630px;   **/
    min-height: 200px;
}

#crazyhearts {
    background:url("https://c1.staticflickr.com/5/4321/36112755175_f7f9ba8b84_b.jpg") no-repeat 0% 30%;
    background-size: cover;
    width: 100%;
    position: relative;
}

a {
  color: #214E34;
  font-family: 'Raleway', sans-serif;
  font-size: 13px;
  font-weight: 900;
  text-align: left;
  text-transform: uppercase;
  text-decoration: none;
  letter-spacing: 2px;
}

h2 {
  color: whitesmoke;
  font-family: 'Orbitron', sans-serif;
  font-size: 28px;
  font-weight: 500;
  text-align: left;
  text-transform: uppercase;
  background-color: rgba(0, 0, 0, 0.9);
}

Solution

  • <li>
      <div id="crazyhearts">
        <a href="personal_project_pages/crazyhearts.html" class = "alpha">
            <div class="projects">
                <h2> Crazy Hearts Cutting Board </h2>
            </div>
        </a>
      </div>
    </li>
    

    You're missing " " around the href attribute and HTML5 allows you to wrap a tag