How can I ensure that the <a> href tag is clickable only when the user clicks on the actual

I want to create a header with a fixed title that is centered both horizontally and vertically. I also want to include a home icon that serves as a link.

The issue I'm facing is that the link is clickable on everything left of the Home icon.

Here's an example of the problem in this codepen;

body {
  margin: 0;
  background-color: #2d2d2d;
}

#header {
  margin: 0;
  background-color: rgb(171, 228, 250);
  height: 10vh;
  display: flex;
}

#homeIcon {
  position: absolute;
  padding-top: 2px;
  padding-left: 30vw;
  height: 10vh;
}

#headerTitle {
  margin: auto;
  font-size: 5vh;
  color: #2d2d2d;
}
<div id="header">
  <a href="">
    <img id="homeIcon" src="https://image.flaticon.com/icons/svg/846/846551.svg" alt="homeicon" />
  </a>
  <h1 id="headerTitle"> title </h1>

</div>

Is it possible to restrict the link functionality to only the content of the img tag?

Answer №1

The CSS code you've written sets the homeIcon with a padding-left of 30vw. If you decide to change it to margin-left, be aware that it will lose its clickability. The reason for this is that padding affects the inside of your element, while margin impacts the outside. For more information on this concept, check out https://www.w3schools.com/css/css_boxmodel.asp.

Answer №2

The issue lies within the CSS styling, specifically with the padding-left: 30vw; property. To resolve this, consider changing it to margin-left: 30vw;

For further insights, you might find this article helpful: Understanding the distinction between margin and padding

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

How to position ion-title at the center using position:absolute

I am a newcomer to the world of Ionic Framework. Recently, I created a page that features a swipe-back-button located on the left side of the navigation bar. This positioning causes the title to shift slightly to the right, as demonstrated by the addition ...

Utilize the dropdown menu across all table cells in a table

My p-table has a unique feature - the last column contains three dots that trigger a dropdown menu when clicked. The only issue is that the fixed position of the dropdown menu does not align properly with the td element in each row. Check out the HTML cod ...

The border shifts slightly by 1 pixel

Following the initial page load, I've observed that the borders of the 2nd and 3rd items are shifting by 1px to the left. In an attempt to replicate the issue in the provided snippet below, everything appears to be working fine (indicating another fa ...

Tips for making radio button selection mandatory in an HTML5 form

How can I require a user to select one radio button in an HTML 5 form before submitting it? I attempted to use the required attribute, but since there is no radio button group, how can I apply this requirement? <form action="/Home/Sendtitle" method=" ...

What is the best way to resize images to fit in a 200 pixel square box using CSS?

I have a collection of images that all fit within a 400px × 400px box, with one dimension being 400px and the other smaller. I want to resize these images using CSS or jQuery/JavaScript if necessary, to fit into a 200px by 200px box. The goal is to have t ...

Looking to extract and upload a thumbnail image from an HTML5 video element

I am trying to extract and upload a thumbnail from an HTML5 video tag, Below is my code snippet: var w = 135;//video.videoWidth * scaleFactor; var h = 101;//video.videoHeight * scaleFactor; var canvas = document.createElement('canvas&apo ...

Tips for effectively extracting information from a website using R

Looking for advice on scraping this web page with ASP and passing parameters? Take a look at the URL: I've made an attempt using the code below, but it's not as efficient as I'd like it to be (the results are not accurate, especially with m ...

What is the method for absolutely positioning an element with separate targets for the `left` and `right` properties?

Seeking a complex CSS result that may seem impossible. Consider the following scenario: <div class="div1"> <div class="div2"> <div class="div3"> <div class="div4"></div> ...

CSS, The text is spilling over outside the boundaries of the table cell

I am currently working on a Virtual Machine and unfortunately cannot copy the code. However, I will provide screenshots instead. The problem seems trivial, but I am struggling to solve it. Can someone please offer some suggestions? Here is the table code: ...

What is preventing me from binding ng-click to my element?

I've encountered an issue where the ng-click event is not activating on my element. Despite using the in-line controller script as shown below, I am unable to trigger my alert. Are there any integration issues that I should be mindful of? What could p ...

Difficulty encountered with text truncation provided by Bootstrap 4 in a table cell

Many questions on SE address this particular issue in Bootstrap 3 and have found solutions by adding a custom class. In Bootstrap 4, a text-truncate class was introduced to restrict the display of text within an element, and we have successfully implemente ...

"Enforce a specific height on a div that is exclusively showing a background

I have been trying to use a div element to showcase a specific color, where the background color acts as the content. However, no matter what height or width settings I input, I am unable to achieve the desired size for the div. Even after experimenting w ...

WHMCS content covering Bootstrap 3 Nav Menu

I am currently in the process of redesigning a website that utilizes WHMCS for certain content. While the main site operates on Bootstrap 4, I have had to adapt and recode elements using Bootstrap 3 to align with WHMCS's templates. Everything seems t ...

Having trouble locating precise text within meta tags when using Selenium and Beautifulsoup

<div class="product-grid-item "> <meta itemprop="url" content="https://undefeated.com/products/air-humara- 17-qs-silver-carotene-bluespark-black"> My quest is to locate the code snippet above, specifically looking for the word &apo ...

Utilize Boostrap to stylishly incorporate an image within a DIV using CSS

I have a project with numerous HTML pages all containing the same elements. To make updating easier, I decided to call all images from the CSS files so that if an image needs to be changed, I only need to update the CSS file rather than each individual HTM ...

Ways to conceal a badge using JavaScript

Can anyone help me figure out how to edit the data-count of a badge and hide the badge using JavaScript in a Bootstrap 5 navbar with a stacked Font Awesome icon? I have tried the following: document.getElementsByClassName("p1 fa-stack fa-2x has-badge ...

What are effective strategies to stop the generic * { … } style from overriding its parent tag?

How can I prevent my general * { ... } style from overriding the parent tag? In the given scenario, I want the text "sssssssss" to have the style text-huge. .text-huge { font-size: 28px !important; color: green !important; } * { font-size: 12px; ...

Challenging z-index problem arising between mobile menu and dynamic animations

I'm facing a unique challenge on my website related to the z-index. The content's z-index is lower but somehow appears above the menus (z-index: 4;) on mobile or resized browsers. To understand this issue better, let's delve into the CSS beh ...

Trouble with Bootstrap dropdown button functionality when clicked

When attempting to construct a navbar with buttons using bootstrap, I encountered an issue where the dropdowns failed to open upon clicking. The necessary CDN links for bootstrap, ajax popper, and jquery have been correctly included in the code. No externa ...

What is the most effective way to showcase news content using HTML?

I am in the process of developing a news website that will present news articles as images with accompanying text. When users click on these image-text combinations, a new page displaying the full news article will open. I am currently considering differen ...