How to ensure a div within an anchor tag occupies the full width in HTML and CSS?

In my code, I am working on creating multiple small boxes with images and centered text inside. The goal is to have these boxes clickable, where clicking the image will take you to a specific link. On desktop, I want a hover effect that darkens the image background when you hover over it, making the white text more visible. However, on mobile devices, the image background should always be darkened to enhance the visibility of the text.

The issue I am facing is that the "overlay" div within the anchor tag does not cover the entire width of the box (300px by 300px). This results in some space on both sides remaining undarkened when hovering over the anchor tag.

Below is a snippet of the code:

HTML for the projects section:

<!-- Projects Showcase Section -->
<div class="projects-section">

    <div class="wrapper">

        <a href="link-to-project" class="project">
            <div class="overlay">
                <h1>Project Name</h1>
            </div>
        </a>

        <a href="link-to-project" class="project">
            <div class="overlay">
                <h1>Project Name</h1>
            </div>
        </a>

        <a href="link-to-project" class="project">
            <div class="overlay">
                <h1>Project Name</h1>
            </div>
        </a>

    </div>
</div>

CSS:

.projects-section {
    width: 90%;
    margin: 0 auto;
    text-align: center;
}

.wrapper {
    margin-bottom: 50px;
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    text-align: center;
}

.project {
    /* Styles for project boxes */
}

a {
    /* Anchor tag styles */
}

.project .overlay {
    /* Overlay styles */
}

.overlay:hover {
    /* Hover effect styles */
}

Here's an image showcasing the problem:

There seems to be an issue where the overlay div does not fully expand to fit the anchor tag, leaving some space uncovered. Any help in resolving this would be greatly appreciated. Thank you!

Answer №1

In order to achieve the desired look, you can adjust the width of your .project elements to 320px and remove the left/right padding on the anchors. However, be mindful of any padding that may serve another purpose in your layout.

.project {
    width: 320px;
    height: 300px;
    background-image: url('http://via.placeholder.com/300x300');
    background-repeat: no-repeat;
    background-size: cover;
    border-radius: 20px;
    margin: 20px 20px 0 20px;
}

a {
    text-decoration: none;
    font-weight: bold;
    font-family: 'Roboto', sans-serif;
    color: white;
    font-size: 30px;
    padding: 0;
}

http://jsfiddle.net/dtwLjqx4/

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to increase the spacing between the titles of these progress bars?

Ensure your responsiveness by enabling the Toggle Device Toolbar on your browser I'm attempting to create a progress bar, but I'm facing some challenges. Firstly, I want to increase the height of the semi-transparent black background, but I can& ...

Gulp fails to generate a CSS file after compiling Sass to CSS

Recently, I've been having trouble compiling my sass file to css. Even though it was working fine before and I haven't made any changes, now my CSS file is empty. Below is the task I am trying to accomplish: gulp.task('sass', function ...

I'm having trouble resolving the issue in my React App after attempting to export a functional component. How can I troubleshoot and

Since the first week of this online course on Coursera.org, I've been struggling to get my React app to display. Even after watching videos and revising the code multiple times based on Google search results, I couldn't make it work. Despite seek ...

Recursively converting trees in JS/ES6

Currently, I am attempting to convert a tree structure provided in the following format: {"Parent": { "Child1": ["toy1"], "Child2": { "Nephew": ["toy2", "toy3"] } } } into a standardized tree form ...

Unusual Quirks with the Range Setting in a jQuery

I am facing an issue with jQuery UI Slider. The slider in my code is not functioning properly. Instead of rendering two handlers, it only renders one. Additionally, the slider can only slide until the value 70, which corresponds to the second value in my a ...

What is the best way to send this JavaScript array to my MVC 3 controller?

When I try to access the controller, I am encountering null values. I can't seem to figure out what's causing this issue. In my grid, there is a list of guests with names and emails, from which users can select guests using checkboxes. I then e ...

Tips for incorporating a CSS transition when closing a details tag:

After reviewing these two questions: How To Add CSS3 Transition With HTML5 details/summary tag reveal? How to make <'details'> drop down on mouse hover Here's a new question for you! Issue I am trying to create an animation when t ...

Harnessing the Power of Google Apps Scripts: Mastering the Art of Handling Comma-Separated Spreadsheet Values Transformed

I have a spreadsheet where column 1 contains source file IDs, with each cell holding only one ID. Column 2 has destination file IDs, where cells contain multiple IDs separated by commas. I utilize a script to retrieve these values and perform various opera ...

Is there a method to ensure that Text First and Image First layouts are consistently displayed in a uniform manner?

When creating a Shopify theme for a client, I encountered an issue with the layout of my liquid file. Specifically, when using the text-first layout, the image in the image-with-text div is partially hidden due to the content that follows it causing the he ...

typescript defining callback parameter type based on callback arguments

function funcOneCustom<T extends boolean = false>(isTrue: T) { type RETURN = T extends true ? string : number; return (isTrue ? "Nice" : 20) as RETURN; } function funcCbCustom<T>(cb: (isTrue: boolean) => T) { const getFirst = () => ...

how can a select dropdown be dynamically displayed based on the previous selection?

If the first dropdown is set to "Professor" I want to display a second dropdown, but if it is set to "Student" then I do not want to display the second dropdown. function checkPrivilege() { var privilege = document.getElementById("permisija5").value; ...

Do external JavaScript files exclusively serve as repositories for functions?

Hey there, I'm a beginner in Web Development and I have a question that might sound silly: If I decide to keep my scripts in an external .js file, would it essentially just serve as a container for functions? For instance, if I attempt to include cod ...

The functionality of .getJSON() in JSON/JQuery is not compatible with IE8 or IE9 browsers

It seems that this particular code is not functioning properly in IE8/IE9, and I am uncertain whether it is caused by the JSON or JQuery .getJSON() element. The main objective of this code is to retrieve data from foursquare and present it on a webpage. I ...

Unchecking and checking the radio button is necessary in order for it to function properly

Today, I'm puzzled by the odd behavior of my radio buttons in a pixel drawer project. In order for the radio button to function properly, I have to uncheck it and then recheck it. The pixel drawer allows me to change colors and sizes using these radio ...

Last item in Material UI Grid container not filling up entire space

I designed a Grid container with 2 Grid columns, each containing 3 elements. However, I am facing an issue where the last element of each column is not appearing at the bottom as intended. What could be causing this problem? For reference, you can view th ...

Having trouble identifying the data variable. Uncaught ReferenceError: edu_id has not been defined

How can I successfully pass the edu_id from an AJAX request to my Laravel controller? Utilizing anchor tags <a href="javascript:void(0);" onclick="showEditEducation(some_specific_id);" title=""><i class="la la-pencil"></i></a> Im ...

Problem with React Router: Uncaught Error - Invariant Violation: The element type is not valid, a string is expected for built-in components

I am encountering an issue with react-router and unable to render my app due to this error. Here is a screenshot of the error I have searched extensively for a solution but have not been able to find anything useful. Any help would be greatly appreciated ...

Unable to Update the Status Code

When it comes to setting the status code to 9999, I am utilizing the basic Response.StatusCode. In this case, the page is accessed via an AJAX call and the status code is checked at readyState = 4. On detecting 9999 as the status code, an alert pops up wit ...

Develop expandable objects containing a set size of items using HTML and CSS

I need help creating a flexible content area using only HTML/CSS. The width of this area should be able to vary from 0 to 50% within its container, which can itself vary from 0 to 100% of the window size. I have two items that need to be positioned next ...

Exploring ways to personalize the behavior and style of the material-ui Accordion component

There is a div container in gray with an accordion component containing only one AccordionSummary component. Below that is another div in blue. The initial view looks good (see pic #1). When the accordion is expanded: The AccordionDetails component is dis ...