Sometimes links don't work on first click - html

I knew this answer at one time, but I can't for the life of me remember it. I've searched all over for the answer but I just can't find it. I have a webpage with some anchors that are as shown below:
<a onclick="make_ajax_request('list')">
<div class="viewbar ">
<div class="icon list">
<img src="/icons/list.png"/>
</div>
<p>List</p>
</div>
</a>
They basically are links with picture icons and text. They work most of the time, but every now and then you have to click on them twice to trigger the function. I remember that it had something to do with not having an "href" attribute or the anchors themselves being inline-blocks. But I've tried everything to no avail. Does anyone have any idea of what I am referring to?

You shouldn't be wrapping block level elements such as with inline elements like <a>. Actually this is fine now, my information was outdated. That could very well be causing the trouble. Some (all?) browsers will try to fix that when it loads, and you could effectively end up with something like this:
<a onclick="make_ajax_request('list')"></a>
<div class="viewbar ">
<div class="icon list">
<a><img src="#" /></a>
</div>
<p><a>List</a></p>
</div>
<a></a>
In any case, you might be better off wrapping the img in the tag, and then using javasript to interpret a click on the div as a click on the inner link. Or in your case where it's not actually a link, just do:
<div class="viewbar" onclick="make_ajax_request('list')">
<div class="icon list">
<img src="/icons/list.png"/>
</div>
<p>List</p>
</div>
and in your css
.viewbar { cursor: pointer; }

Related

Html. I've a problem with my carousel code, when I click on the chervon my screen page down?

Helle, I am making a website. This is a project for my studies. Since I haven't studied java script yet, I wanted to make a carousel in html en css only. This one works well except that when I press the right/left chevrons, the page scrolls down. I couldn't find where the problem was in my code.
My teacher asked me to come and ask you the question.
I therefore cometo ask you for help in order to find the solution.
Many thanks in advance.
Have a good day.
<body>
<div id="conteItemsCarrusel">
<div class="itemCarousel"id="itemCarousel-1">
<div class="carousel"id="acarrusel-1">
<img src="./image/cuisinier.jpeg" alt="itemCarousel-1">
</div>
<div class="fleche">
<a href="#itemCarousel-3">
<div class="gauche">
«
</div>
</a>
<a href="#itemCarousel-2">
<div class="droite">
»
</div>
</a>
</div>
</div>
<div class="itemCarousel"id="itemCarousel-2">
<div class="carousel"id="acarrusel-2">
<img src="./image/photojpgd.jpg" alt="itemCarousel-2">
</div>
<div class="fleche">
<a href="#itemCarousel-1">
<div class="gauche">
«
</div>
</a>
<a href="#itemCarousel-3">
<div class="droite">
»
</div>
</a>
</div>
</div>
<div class="itemCarousel"id="itemCarousel-3">
<div class="carousel"id="acarrusel-3">
<img src="./image/serveur.jpeg" alt="itemCarousel-2">
</div>
<div class="fleche">
<a href="#itemCarousel-2">
<div class="gauche">
«
</div>
</a>
<a href="#itemCarousel-1">
<div class="droite">
»
</div>
</a>
</div>
</div>
</div>
</body>
WhyMy page scrolls when I click on the chevrons and I don't know where is the problem in my code
The point of href="#foo" is to link to an element on the page and scroll directly do it.
You are presumably (you didn't include your CSS in your question) using :target to style that element. :target is designed so you can add additional styling to the element to draw more attention to it that it would get from just being at the top of the screen.
You appear to be trying to get the styling of the element linked to element without triggering the primary effect of linking to it.
You can't do that.
If you want a carousel, then use JavaScript. It's the right tool for the job.

How can I add a clickable link to my collapsable sidebar menu without breaking my code?

Currently trying to make a sidebar on HTML/CSS however I am struggling to finalise it by adding clickable links for navigating around my site.
I believe it is a simple CSS issue or a simple HTML problem but it's driving me crazy!
<li class="c-menu__item has-submenu" title="Account">
<div class="c-menu__item__inner"><i class="fa fa-cog"></i>
<div class="c-menu-item__title">
<a href="accountdetails.php"
style="text-decoration: none"><span>Account Settings</span></div></a>
</div>
Is what I have at the moment but there are two problems, I cannot click the button when the sidebar is collapsed, nor can I click the div the text/icon are present in, I must only click the text to get it to work.
Preferably I would like to click the box the icon, text are held in for easier usage.
How can I achieve this instead of just text being hyperlinked?
Photo showing the area I would like clickable on sidebar opposed to just the text
This is because your anchor tag is wrapping only the text "Account settings".
You should place your anchor tag in a way that wraps the elements that you want them to work like a link.
Also be aware that in the code you shared, you closed a div before closing the anchor tag, and you are not closing the list item tag.
Try like this:
<li class="c-menu__item has-submenu" title="Account" >
<a href="accountdetails.php" style="text-decoration: none">
<div class="c-menu__item__inner">
<i class="fa fa-cog"></i>
<div class="c-menu-item__title">
<span>Account Settings</span>
</div>
</div>
</a>
</li>
I see you closed </div> before a tag, But your a tag start just before your span tag.
Hence you need to close a tag before </div> like these:-
<li class="c-menu__item has-submenu" title="Account">
<div class="c-menu__item__inner"><i class="fa fa-cog"></i>
<div class="c-menu-item__title">
<a href="accountdetails.php" style="text-decoration: none">
<span>Account Settings</span>
</a>
</div>
</div>
</li>
Now, you don't want to show that your text is hyperlinked, in that case, you can use text-decoration: none; to solve your issue.

When wrapping a DIV in an anchor tag the layout gets messed up?

So I have been given the task of mocking up a new bootstrap version of the building floor map on the web and while trying to make a DIV area clickable and linking to another page, the anchor tag used to make the link makes the div layout messed up?
Here is my code and an image
before I add the anchor tag around the div
after I add the anchor tag around the div
<div class="container">
<div class="row">
<a href="floor0.html">
<div class="item col-lg-4">
<p> floor zero </p>
<br>
<br>
<br>
<br>
</div>
</a>
$(".item").click(function() {
window.location = $(this).data("location");
return false;
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="item col-lg-4" data-location="floor0.html">
<p> floor zero </p>
</div>
You can do similar like this to avoid this css conflict with anchor tag and also to make a div clickable and redirect to destination link. If you run this code you will find 404 error code as there is no file here. Thank you. If this helps you then please make it accepted.
Your mark up needs to be updated as you have some closing div tags missing, not sure if this just the case in the code in your question. The markup should be:
<div class="container">
<div class="row">
<div class="item col-lg-4">
<a href="floor0.html">
<p> floor zero </p>
</a>
</div>
</div>
</div>
You need to give the <a> a style property of display: block; like this:
.item a {
display: block;
}
The anchor will now cover the whole div and work the way you expect it to. It's also a good idea to note; if using a framework like Bootstrap the order of divs is important to make sure the grid behaves as expected, it should always be:
container > row > col
Managed to solve this issue by removing the div with the column classes and then adding the classes from the removed div to the anchor tag
<div class="container">
<div class="row">
<a class="item col-lg-4" href="#">
<p> floor zero </p>
<br>
<br>
<br>
<br>
</a>
Try reading this: Is putting a div inside an anchor ever correct?.
If your goal is to open a link when the div is clicked you can try putting an onClick=(window.location.href='link here') event on the div tag or use jQuery event in script $('div').click(function(){window.location.href='link here'});

nth-child, change style of every other div with class name

I have got some elements on my page, they should all be styled the same except for every other one, where I just want to change some styling.
Here is the CSS which I was hoping would select the div inside the stack of different elements:
.stagger_reviews[class=inner]:nth-child(2n+2) {
background-color:#003;
}
Here is the HTML:
<div class="stagger_reviews">
<!-- Later use PHP to load reviews, CSS should switch the images from left to right -->
<article class="container box style1">
<a style="background-image:url(images/blank-user.jpg); " href="#" class="image fit"></a>
<div class="inner">
<header>
<h2>Martyn Ball</h2>
</header>
<p>
I found this service on a Google Search, didn't expect it to be so great!
</p>
</div>
</article>
<article class="container box style1">
<a style="background-image:url(images/blank-user.jpg); " href="#" class="image fit"></a>
<div class="inner">
<header>
<h2>Martyn Ball</h2>
</header>
<p>
I found this service on a Google Search, didn't expect it to be so great!
</p>
</div>
</article>
</div>
As you can see I just want to adjust the one div inside each article which has the class name inner. And maybe some other elements as well but once I have this working I can do that.
The style isn't being applied to the second inner div, I have made about 4 copies of the article and none are being changed.
Here is the solution, I put the nth-child in the wrong place.
.stagger_reviews > article:nth-child(2n+2) div[class=inner]

When I wrap contents in an <a> tag, the link doesn't work in IE, help?

I know this is probably frowned upon, but a client wants live text wrapped in ONE tag....
So, here is my dilemma:
I have some code like this:
<a href="google.com">
<img src="img.jpg" style="float:left;">
<div style="float:right;">
<h3>Title</h3>
<p>Description</p>
</div>
<div style="clear:both;"></div>
</a>
I styled the to be display:block.
This method works great in FF5, IE = not all content is clickable, in this example only the image is clickable. Chrome = works. Safari = works.
If you have a different method you might suggest please offer it.
Thanks in advance!
Try setting the link style like this:
<a style="display: block; position: relative;" ><div/></a>
One suggestion is not to use block level elements in an inline element. It won't always render properly (beyond being semantically incorrect). Try removing the floats and using spans to style the part that you want to be an H3 and p.
<a href="google.com">
<img src="img.jpg" style="float:left;">
<span class='title'>Title</span>
<span class='desc'>Description</span>
</a>
This is completely against the html specification since <a> elements can only contain inline elements and <div> is a block element. So, it's no surprise that it doesn't work in Internet Explorer. In fact, it probably doesn't work in a number of other browsers, and even in those where it does work, there's no guarantee that it will continue to work.
The only workaround I can think of involves JavaScript, like this:
<div onClick="javascript:window.open('google.com')">
<img src="img.jpg" style="float:left;">
<div style="float:right;">
<h3>Title</h3>
<p>Description</p>
</div>
<div style="clear:both;"></div>
</div>
However, that will break support for users that don't have JavaScript, or have it disabled.
In HTML 4, block-level elements are not allowed in <a> tags so I would not expect it to work cross-browser.
A better solution may be to use a Javascript onclick event to send the user to the intended destination.
<div id="clickable_div" onclick="document.location.href='http://google.com'">
<img src="img.jpg" style="float:left;">
<div style="float:right;">
<h3>Title</h3>
<p>Description</p>
</div>
<div style="clear:both;"></div>
</div>
You could try making the <a> into a <div> and use javascript & css to make it look & work like a link
<div onclick="document.location.href='http://www.google.com'" style="cursor:pointer">
<img src="img.jpg" style="float:left;">
<div style="float:right;">
<h3>Title</h3>
<p>Description</p>
</div>
<div style="clear:both;"></div>
</div>