keep link from expanding across entire page - html

I'd like a generalized solution that will always limit the clickable link area to the text of my h2 text. Note that the issue is that when you hover over or click on the space to the right of the text you are still on a clickable area.
Here is an example:
markup:
<a href="#p1">
<h2 class="page services">xxxxxxxxxxxxxxxxxxxxxxxxxxxxx</h2>
</a>
css:
h2.services {
font-size: 16px;
}
Here is a demo:
http://jsfiddle.net/j7n3k/
ps - no js or jquery please. Only css and responsive solutions only if possible. Thanks!

You should put the <a> tag inside <h2>. By default, headings have display set to block, which means they will automatically take up all the horizontal space available if the width is not set explicitly. The link contains the heading, so the browser assumes the whole area is a link. If you insert <a> into <h2>, then it wraps only the text and not the entire heading.
<h2 class="page services">xxxxxxxxxxxxxxxxxxxxxxxxxxxxx</h2>

this will stop it from expanding:
h2.services {
font-size: 16px;
display: inline-block;
}
http://jsfiddle.net/vimes1984/j7n3k/4/
Read up on the display property and the position property.
DISPLAY
https://developer.mozilla.org/en-US/docs/Web/CSS/display
POSITION
https://developer.mozilla.org/en-US/docs/Web/CSS/position
To style:
you can use
.services{
/*this will style any element with a class of services*/
}
.services a{
/*this will style any a inside of a element with a class of services*/
}

Related

Why is my link clickable outside the linked text

https://i.imgur.com/T1hiXMO.png
Here is what it looks like right now. Clicking anywhere inside the black border links to the URL. I only want the text "RANKINGS" to be linked.
HTML:
<div id="div1">
RANKINGS</h4>
</div>
CSS:
#title {
margin-top: -10px;
font-size: 20px;
color: #e846ff;
text-align: center;
font-family: 'Trebuchet MS';
border: 1px solid black;
}
You are adding a Heading element (h4) inside an anchor (a) element.
Even though Anchors are inline elements, meaning they don't take the full width of the screen, you added a Heading element inside that Anchor.
Heading elements are block elements and they do take up the full with of the screen.
It would be better to reverse the html as seen in this codepen:
<h4 id="title">
<a href="https://example.com" target="_blank" rel='noopener noreferrer'>RANKINGS</a>
</h4>
This way you get you wanted.
As h4 is a block level element it takes the entire width of the window. use span tag instead of h4. Also h4 is not valid inside a tag. Still if you want to keep it you need to apply
#title{
display:inline;
}
Because, probably, it's element with value block in property display. Inspect element and check it in such cases. Block elements have 100% width. If you need, change display: block; to display: inline; (in headings width 100% set as default) or another value for auto width. Also, you should check display of link, in this case.
Another way to solve this issue is to give your a element the width: fit-content; property. This will make the link hug the text and not extend beyond it:
<h4 id="title">
<a href="https://example.com" target="_blank" rel='noopener noreferrer'>RANKINGS</a>
</h4>
(CSS:)
h4 a{
width: fit-content;
}

Why does this result in bold letters?

First of all I want to achieve a black background:
Instead of a black background I get bold letters.
HTML CODE
<span id="border"><h3>Title</h3>
<p>Lorem Ipsum</p>
</span>
CSS CODE
#border {
background-color: #000000;
}
Jsfiddle
http://jsfiddle.net/tYaCK/
This bold is actually coming from your h3 tag as a default style rather than your border css.
As for why your border isn't appearing... well that is because the span is an inline element and will not expand to contain the block level h3 tag.
You can see this working by adding display: inline-block; as a style to the h3 tag.
FYI You should not have a h3 within a span as that is not valid html. I would recommend a div tag as an alternative to the span.
Two issues:
The bold is the default styling of the <h3> tag.
Your black background isn't showing up because it's on a <span> element.
The span defaults to display:inline, which means it us not valid for it to contain block elements. The h3 is a block element.
To fix this, either use a div instead of a span, or set the span to display:block.
You could have seen the background color if the situation had been slightly different.
Consider the following HTML:
<span id="border">
Some opening text...
<h3>Title</h3>
<p>Lorem Ipsum</p>
and some closing text.
</span>
I just added some text to the span element.
And for the CSS, just add a color so you can see the text:
#border {
color: green;
background-color: #000000;
}
Demo at: http://jsfiddle.net/audetwebdesign/M8Exf/
What is happening here is that the CSS engine opens an inline block (span) and applies the format from #border.
However, upon finding the block level h3, the CSS engine closes off the span element (internally) and begins a new block level box, and similarly for the p element.
Upon finding the remaining text from the span, the CSS engine starts a new anonymous inline box and applies the same styling from #border.
This procedure is part of the CSS box and visual formatting model.

Link background is line per line instead of a solid background, fix?

Like the title says I can't get the background of the a element to be one solid color. Instead my code make each line the color. I have even tried wrapping the php element in a new div and setting that background color but it does the same thing. Any advice is useful.
Site: http://www.whatsatyourcore.com, bottom widget titles
The html:
<a class="teaser-title-top" title="Social customer service and your marketing strategy" href="http://whatsatyourcore.com/?p=78"> Social customer service and your marketing strategy</a>
CSS:
.teaser-title-top { background: black;}
a.teaser-title-top {
color: #fff;}
The above "code boxes" are an example. The first one is what the background is doing on my titles. The second one is what I want it to be.
if you wrap it in a div and set that divs background-color that should work. The element you are setting the background-color for needs to be a block-level element (a div is) you can set an element to display as a block level element with display: block; else it will just color the background of the inline content.
you are styling the anchor which is only going to stlye what's in it. Try something like:
<div id="title-box">
<div class="teaser-title-top">
Social customer service and your marketing strategy
</div>
</div>
And then style the .teaser-title-top
As with the other answers, you can style a wrapper element. However if you want to style just the anchor text directly, try this...
The issue is the line-height of your text. AS you can see, it is spaced out. If you. The height of the line, compared to the size of the text, is bigger.
As an example, set #bottom a { line-height: 100%; } (which is what teh font-size is set to) and you'll see the gap go away. You'll also see the spacing go away. You can "bring back" the spacing with top/bottom padding, as well as changing the line-height.
Try this hope it works
#bottom .teaser-title-top {
color: white;
}
In your css file

Removing link uderline in nested div

I began to learn html'n'css, but I've encountered one thing that I cannot explain. I have a html file, that has a div which acts like a link (in the application I am setting the div size and want for the whole box to act like a link). I cannot remove the text underline decoration for the text in the div though (Link1 in the Example is always underlined). The selector should be "any div within a link element", and because the link is red, I think it is correct.
I managed to do this by introducing a special class for removing the underline explicitly (Link2 in the Example is ok), but I would like to have all the menu styles in one place.
The question is, whether can someone explain why the removing deco like this (Link1) does not work. Moreover, I would like to ask if the organization of the menu is a good style, or if I should reorganize the code, e.g: having this for example:
<div>Blabla</div>
and the style:
a.menuitem {...}
a.menuitem div {width:...;}
Here is the minimal (non-)working Example:
<html>
<head>
<style>
a div.menuitem {
text-decoration: none;
color: red;
}
.remove-under {
text-decoration: none;
}
</style>
</head>
<body>
<a href="./index.html">
<div class="menuitem">Link1</div>
</a>
<a href="./index.html" class="remove-under">
<div class="menuitem">Link2</div>
</a>
</body>
</html>
Thanks a lot!
Semantically speaking a <div> should not go inside an <a>. div tags are block elements where anchor tags are inline elements - and block elements should never go inside inline elements. Instead use <span> if you need to stylize something different inline but in your case, additionally, you can add a class to the <a> which would work better.
Here is your new code:
<a href="./index.html" class="menuitem">
Link1
</a>
<a href="./index.html" class="remove-under menuitem">
Link2
</a>
You can have multiple classes to an element by putting a space, so Link2 has the class "remove-under" and "menuitem"
Update your CSS to remove the underline:
.remove-under {
text-decoration:none;
}
In order to get your whole a tag to be a link (not just the text) add the follow css for your menuitem class:
.menuitem {
display:block;
width: 100px;
height: 50px; /* or whatever your desired width and height */
background: red; /* to show that the whole anchor will be link, not just text */
}
This is not the ideal solution. You really should not be putting block level elements inside inline elements.
However, if you absolutely must get it working, you can add display: inline-block; to the div.
a div.menuitem {
text-decoration: none;
color: red;
display: inline-block;
width:100%;
}
.remove-under {
text-decoration: none;
}
You have 2 problems here:
You can't do something like this
<div></div>
because a is an inline element. What you do here is an invalid HTML code. DO it like this:
<div></div>
You try to apply text-decoration:none on the div element and you should apply it to the a element.
a {text-decoration:none;}

how to set anchor tag text width using css?

I have html and css as below -
.title {
display: block; background-color: red;
}
<a href="#">
<span class="title">Text</span>
</a>
I could see that the SPAN spans to the 100% of the available width (because of display: block). Like below
|----------------------------------------------------|
| Text |
|----------------------------------------------------|
In Firefox, I can click anywhere in the above box, and it takes me to the linked page. However, In IE (IE 7) I get the cursor as hand only when I hover over "Text" text only.
What hack I'll have to do to make it work (same as it does in FF) in IE as well?
I tried placing the anchor tag itself (not just the text) in span but it won't work.
Thanks.
Style the anchor and remove the span.
(The problem is due to how some browsers handle elements that are display: block inside elements that are display: inline. You can work around it by styling both the anchor and the span, but the span appears redundant in this example)
for your <a> tag, make the style "display: block; width:100%;"
Definitely, you need to remove the span and apply that class to the anchor tag. I don't think you need to set the width to 100% explicitly, but I could be wrong.
Remove the extra span and place that title class on the link itself. Then add width:100%; to the css.
Less markup is most often better, thats why you should remove the extra span.
you can also using margin or padding
Give a style to anchor of display:block and max-width:30px !important; max-width size can be any
li a {
display: block;
height: 30px;
max-width: 30px !important;
}