As far as I know, p tags create paragraphs. But I often find HTML code where these tags are used for other aims. For example, I found it in a Codecademy exercise:
<div><p>Rex</p></div>
div {
position: relative;
display: inline-block;
height: 100px;
width: 100px;
border-radius: 100%;
border: 2px solid black;
margin-left: 5px;
margin-top: 5px;
text-align: center;
}
div p {
position: relative;
margin-top: 40px;
font-size: 12px;
}
It creates a circle with the name Rex in the center of this circle: http://jsfiddle.net/cvT6E. But the word Rex isn't a paragraph, it's just a word!
When I try to replace p tags by span (it seems more logical for me), it doesn't work: http://jsfiddle.net/cvT6E/2/. Why not?
Finally, I would like to know:
1) Is the use of p tags semantically correct in the example?
2) Why isn't the word centred when p tags are replaced by span tags?
At first, recall that <p> is a block-level element. Mozilla puts it nicely "Their most significant characteristic is that they typically are formatted with a line break before and after the element (thereby creating a stand-alone block of content). That is, they take up the width of the containers." This explains why there <p> centers and span doesn't.
Furthermore, <p> is meant for paragraphs, so it is more appropriate for representing textual content, hence the semantic is preserved, as you might recognize too. I would suggest using <span> for situations where you do not have a semantic markup available in HTML, and in your case, representing a word or block of text with <span> is not insisted.
Appreciate that you might approach this with a different mindset, although I wouldn't recommend complicating things unless necessary. Observe the markup as stated below.
<p><span>text</span></p>
This would essentially give you the same effect i.e. centering of the "Rex" word in the circle, because <p> is a block-level element, and it can act as a container for the <span> element. Hence, the <span> would effectively inherit the properties of <p>
.This is evident from the fact that the text is vertically centered.
The <span> is on the contrary an inline-element, and think of it as a grouper, tying up elements together. Meanwhile, a <div> is an ideal container. Personally, I use <span> when I can't find an appropriate markup element and when I wan't no special properties of <p> or others. I like <span> for it's pureness.
If you strictly want to use <span> and center "Rex" in that circle, you can do this. The last two properties essentially enable you to do just that and the output is shown in the image below.
div span {
position: relative;
margin-top: 40px;
font-size: 12px;
display:inline-block;
vertical-align:middle
}
p is often used for displaying paragraphs, but it can also be used for many other things due to its default properties. In this case, span will not work because span has default display:inline.
Question 1 is primarily opinion-based, and the answers have no practical implications.
Question 2 has a simple answer: a span element is by definition an inline element (with display: inline), and the text-align property thus does not apply to it (i.e., has no impact on its rendering). You can fix this e.g. by using div instead of span or by using span with the CSS declaration display: block.
Related
There are a lot of tooltip solutions on Stack Overflow, ie. ways of showing some text or HTML when the user hovers over some other text. However, I can't seem to find one that:
is pure HTML + CSS
allows the tooltip to be inline (eg. doesn't require a <div> or other block element)
allows HTML tooltip content (potentially with display:block)
These requirements come from wanting to provide definitions of terms inline, inside paragraphs of text, and I'd like for those definitions to be able to contain block-display HTML content (eg. paragraphs and images).
Most of the CSS-only solutions I find seem to work by nesting the "tooltip" definition inside the hover target's HTML. However, that's impossible if you want the term to be inline, but its definition to be block, because you'd have to nest block content inside an inline tag.
Is there any other approach I can use that lets me have words or phrases inside paragraphs as "hover targets", but then show HTML hover definitions when that happens ... without Javascript?
If it is allowable to change the markup of the actual text to replace inline elements (p is tprobably the most likely to need this) with divs + suitable CSS, you can insert tooltip content within divs which are inline-block.
This means any block content can be held in the tooltip.
Here's a trivial example:
.inline {
display:inline-block;
position: relative;
}
.inline .content {
display: none;
position: absolute;
top: 1em;
left: 0;
z-index: 99999;
border-style: solid;
background-color: white;
}
.inline:hover .content {
display: block;
}
<div>I am not hoverable.
<div class="inline">But I am hoverable.
<div class="content">I am tooltip content and I've got an image in me...
<img src="https://picsum.photos/200"/>
<div>...and another div</div>
</div>
</div>I am not hoverable.
</div>
Of course, there's the question of how to ensure the tooltip is visible on all types of devices and where to place it, but that's another question and something ordinary span type tooltips have to solve too.
So, I've looked around SO, and I've found the inverse of this question mostly everywhere. That makes me feel this is either a rarer occurrence, or something trivial that I just can't figure out.
https://jsfiddle.net/je5dpqrL/
The above jsFiddle shows that I have an <h2> element within which I've put an anchor tag with the pull-right class of Bootstrap. Since I want the anchor to display in a smaller font, I'm using font-weight and font-size. Now, since it's floating, the text is centered.
Is there any way to align the text so that the baseline of the Title and the <a> element is the same?
You can adjust the vertical position of the <a> with line-height (and use for example em to make it relative size):
.cl {
font-weight: normal;
font-size: 40%;
display: inline-block;
line-height:4em
}
This is what you need https://jsfiddle.net/p05bu4c2. Create a span inside the link
.cl {
font-weight: normal;
font-size: 40%;
display: inline-block;
}
.cl span {
line-height: 1;
vertical-align: bottom;
}
h2 {
border:1px solid #ff0000;
}
<h2>Title <a class='pull-right cl'><span>Stuff</span></a></h2>
So, The following code seems to work:
<h2>Text<a class="pull-right"><span class="text-right" style="display:inline-block">Test</span></a></h2>
Turns out that adding an .inside-block to an element inside the .pull-right class seems to fix it. No need to play around with line-heights
JSFiddle: https://jsfiddle.net/je5dpqrL/10/
EDIT: Thanks to Diamond for this suggestion of adding an element inside the <a> tag, although the CSS is completely different from the one suggested by him.
I have an <a> element nested within a <span> element. The <a> element occurs after some lines of text followed by two line breaks (<br>). So while the initial text within this <span> element needs to be text-align: left, I'd like to know if there is a way to change the formatting for the subsequent <a> element to be text-align: center.
I am using CSS to modify the formats and have succeeded in changing the color and text-decoration of the <a> element (independent of the former text), so I know that my code is pointing to the correct element, but when I try to alter the alignment it will not work for me...
Please hit me with potential solutions.... Thank you.
Use display: block; it will allow the a element to text-align
a {
display: block;
text-align: center;
}
jsFiddle Demo
I have a problem thats kinda driving me nuts. I have an article container and within are several paragraphs. The first paragraph contains a drop cap. This first paragraph does not use text-indent, however every following paragraph does.
When I begin a new paragraph following a h3-header, I don't want any text-indent. Fine, I can get this to work (blue text in example).
My problem is this, when I begin a new paragraph with a header (strong followed by a break), this line will use the text-indent of the paragraph, and I don't want it to. I must have the strong tags inside the paragraph (as one should), not outside.
I'm thinking of a way to select all paragraphs that start with a strong tag. I don't want to use any javascript to solve this. I want to change the text-indent of the paragraph, not the position of the strong text.
I've made a jsFiddle here. I have tried something like this:
p>strong {
color:#f0f;
text-indent: 0 !important;
}
You can add a negative margin to the strong tag, though I assume you'll want a specific class on it.
strong.subhead {
margin-left: -3em;
}
Working example at: http://jsfiddle.net/J5C86/2/
However, this is also assuming you don't want the paragraph associated with the strong tag indented. If you're looking for the paragraph under the subheading to be indented as well, you'll need another tag on the first word or letter after the br.
span.subhead-indent {
margin-left: 3em;
}
Example: http://jsfiddle.net/J5C86/4/
To expand on my comment on your question:
If there's a reason you can't use <h4> - which would be the more suitable tag here - you can simply add a negative margin to your <strong> element:
p > strong:first-child {
margin-left:-3em;
}
JSFiddle example.
Otherwise, use <h4> instead:
<h4>Strong sub header</h4>
<p>Aliquam semper placerat urna...</p>
h3+p, h4+p {
text-indent:0;
margin-top: 0;
padding-top: 0;
}
h3+p {
color:#00f;
}
JSFiddle example with <h4>.
It works for me. Use this:
p>strong {
text-indent: 0 !important;
color: #f0f;
display: block;
}
After doing this, Remove the br tag at the last of p>strong.
Demo
I saw your problem and found that you have not included your paragraph within the h3 tag, so define your css with your strong paragraph with a class for eg.
<p class="no-indent"><strong>Strong Sub Header</strong></p>
define your css this must work.
I see lot of times attempts to hide text with CSS, for instance:
<a class="back">back</a>
a.back { text-indent: -9999px; display:block; width: 100px; height: 50px; background: url(/images/back.png); }
I always wonder why not to use:
a.back { color: transparent; display:block; width: 100px; height: 50px; background: url(/images/back.png); }
It seems to me semantically correct, and in addition, when I tried the text-indent approach, it caused a bug in iPad display: The text was displayed 99999px left to the anchor tag and caused a strange unnecessary horizontal scroll.
Is there a common known problem with the second code or it's OK to use?
font-size: 0px; should do the trick.
If you want to make the button smaller than the text, you'll also need to add line-height: 1em; or something similar.
Using the display property allows you to edit the state of an element in C#.
Here are 4 main display elements people use:
p.ex1 {display: none;}
p.ex2 {display: inline;}
p.ex3 {display: block;}
p.ex4 {display: inline-block;}
display:none means that the tag in question will not appear on the page at all (although you can still interact with it through the dom). There will be no space allocated for it between the other tags. visibility:hidden means that unlike display:none, the tag is not visible, but space is allocated for it on the page
But if you want to learn more about them you can go to this site: https://www.w3schools.com/cssref/pr_class_display.asp
w3schools explains a lot of languages in a simple and understandable way.
Don't hide content and depend on a background image at all.
HTML provides a way to include images which have meaning (the <img> element) with text content for situations where the image can't be displayed (the alt attribute). There is no need to fake it with the stylesheet.
<a class="back"><img alt="back" src="/images/back.png"></a>
Icons are content and deserve to be treated as such. All efforts to use text and background images are ultimately hacks with limitations. The <img> element was designed for this use case.