How do I style part of the pseudo element content: "..." - html

I am inserting a FontAwesome icon along with some text from the data attribute in a div pseudo element:
<div data-date="Mar 01, 2016"></div>
div:before {
content: "\f073" attr(data-date);
font-family: "FontAwesome", sans-serif;
}
https://jsfiddle.net/cn0vhns9/
Now what I want is to increase the size of the icon and put the text right under it with some margin. While putting the text content under the icon seems achievable (if I fix the width of the pseudo element), I can't think of the way to increase the size of the icon without increasing the size of the text content as well. Any ideas?

It is possible even in your case. Please, see the updated fiddle at https://jsfiddle.net/cn0vhns9/3/. You will have to style the icon as a pseudo-element (::first-letter, or alternatively :first-letter with just a single semicolon for compatibility reasons).
My answer only demonstartes the problem with different font-size.
CSS
div:before {
content: "\f073" attr(data-date);
font-family: "FontAwesome", sans-serif;
padding: 20px;
color: black;
font-size: 20px;
}
div::first-letter {
font-size: 40px;
font-family: "FontAwesome", sans-serif;
}
HTML
<div data-date="Mar 01, 2016">
</div>
However, I consider this solution to be a bit hacky. Most likely I would separate the icon and the date and place each of them into a separate HTML tag if possible.

You can use another pseudo element:
div:before {
content: "\f073" ;
font-family: "FontAwesome", sans-serif;
padding: 20px;
color: black;
font-size:2em;
}
div:after
{
content:attr(data-date);
display:block;
border-top:1px solid black;
}

Related

Is there a way to overwrite text with different text using HTML, CSS etc?

Not sure if this is a stupid question or not.
Is there any way to overwrite text in HTML/CSS with a different text?
An example of what I mean
I want to make it obvious it's a terrible reskin of a previous website.
I know I could do it with an image, but I'd like to have it as regular text. Is it something you'd have to do with custom fonts? Or is it even possible?
This is a very basic example to achieve this.
The content that you really want to display is in the :after of the span that surrounds the year.
body {
background: #000;
}
h1 {
color: #fff;
font-family: sans-serif;
}
.year {
position: relative;
}
.year:after {
position: absolute;
top: .1rem;
left: 0;
display: block;
content: '2021';
transform: rotate(-10deg);
color: red;
font-family: 'Comic Sans MS';
}
<h1>Happy <span class="year">2020</span> anniversary!</h1>

Convert img tag to display FontAwesome icon

I have a third party product which outputs an icon via an <img> tag with a background-image set via css.
I could probably write some javascript to change the html tag itself to something different but I would rather change the css to display a FontAwesome icon using the :before and content="{FontAwesomeText}".
However, I cant get this to work...does someone know if this is possible to do this?
Please see
.x-tree-icon-leaf:before {
content: "";
font-size: 0.6em;
position: relative;
top: -3px;
}
.x-tree-icon-leaf {
font-family: FontAwesome;
width: 20px;
height: 20px;
display: inline-block;
color: #5fa2dd;
}
https://jsfiddle.net/uz9q6m33/
Your jsFiddle did not properly import the FontAwesome library.
You can't just copy paste the character and place it inside content, you need the proper unicode value of that specific icon, which is: \f06c
img cannot have pseudo elements, use a classed sibling or parent for the icon and hide the image with display: none.
Working code:
.x-tree-icon-leaf, .x-tree-icon-text::before {
font: normal normal normal 14px/1 "FontAwesome";
display: inline-block;
color: #5fa2dd;
}
.x-tree-icon-leaf::before, .x-tree-icon-text::before {
content: "\f06c\00a0";
font-size: 1em;
position: relative;
top: -1px;
}
img.x-tree-icon-leaf { display: none; }
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet"/>
<!-- working with normal span -->
<div> <span class="x-tree-icon-leaf"></span> <span>Hello</span> </div>
<!-- hiding image then applying icon to text-span -->
<div> <img class="x-tree-icon-leaf"> <span class="x-tree-icon-text">Hello</span> </div>
jsFiddle fork: https://jsfiddle.net/azizn/1y3jwnsg/
Note: Add \00a0 at the end of content:'' string to force a space for consistency.
The icon is applied to x-tree-icon-leaf and x-tree-icon-text at the same time. When x-tree-icon-leaf is an img tag, it will be hidden.

How to remove the extra space between two span elements?

I want to remove the extra space between these two elements. I tried but couldn't do it.
Is this a problem of margin collapsing?
How can this be solved? How can I remove that extra space?
Here is my HTML and CSS:
body {
width: 250px;
height: 100px;
background: #F2F2F2;
}
#output {
font-family: roboto light;
color: #A4C639;
font-size: 30px;
}
#grade {
font-size: 25px;
color: black;
}
#max {
color: black;
}
#percentage {
background: #A4C639;
color: #FFFFFF;
padding: 15px;
border-radius: 15px;
}
<div id="output">
<i>
<span id="grade">Your grade :</span>
<span id="total">524</span>
<span id="max">/725</span>
<center><h1><span id="percentage">72.28%</span></h1></center>
</i>
</div>
Whitespace characters between HTML elements create a new text block, which is displayed as a space between the elements.
Remove all the whitespacing between the elements to get rid of it:
<span id="total"></span><span id="max"></span>
Alternatively, you can fill the whitespaces with a comment block:
<span id="total"></span><!--
--><span id="max"></span>
Put the <span> tags on the same line without any space between them.
It looks as if you have the wrong title - your h1 is what is causing the space between the text and the percentage box. To remove try this:
#output h1 {margin-top:0; padding-top:0;}
If it actually the spans you are talking about then you need to remove any white space that is between them - See the other answers for this
I know this has been answered, but I would have done this differently - the original HTML is combining display and semantic elements together ( with the italic, H1 and center tags).
I would do the HTML like this:
<div id="output">
<span class="grade">
Your grade :
<span class="total">123</span>/<span class="max">777</span>
<div class="percentage">23.45%</div>
</span>
</div>
And the CSS like so:
#output {
font-style:italic;
text-align: center;
font-family : roboto light;
color : #A4C639;
font-size : 30px;
width: 250px;
}
.grade {
font-size : 25px;
color: black;
}
.total {
color : #A4C639;
}
.max {
margin-left:0;
}
.percentage {
text-align: center;
font-size: 200%;
background : #A4C639;
color : #FFFFFF;
padding : 15px;
border-radius : 15px;
}
This gives you what you were after but the style and layout was done totally in the CSS markup.
If you want to see it in action try this jsfiddle: http://jsfiddle.net/justin_thomas/P6wmJ/19/
It is usually much better to separate your style from your content and semantics. It will make things easier if you ever need to change the layout and so on.
That's some weird behavior of span elements in HTML.
If the first span has style text-decoration: underline; then one extra space will be underlined also.
I solved it by changing span to div and applying display: inline-block to divs.
try i{font-size:0}#output span{font-size:30px;} in your css

Getting the psuedo element property of css work.. to change the property of first line

I am trying to change the css property of first line of a big sentence, I am experimenting with it by using color property, I have an html element
<span class="tripname_heaing">Where Hummus All Began: Jasdasdasddasdasdasdasdadasdasdasdasdsadsadasdasdordan & Issdassasdsadsadsadrael</span>
and CSS property
.span.tripname_heaing:first-line {
color: red
}
span.tripname_heaing{
color: blue
}
span.tripname_heaing {
font-size: 24pt;
font-weight: normal;
line-height: 24pt;
margin: 0;
padding: 0;
}
But the psuedo element property is not working with the above syntax. I am adding a fiddle to show the demo. What could be wrong with this?
http://jsfiddle.net/X33pY/126/
And edit made to fiddle to show the feature
http://jsfiddle.net/X33pY/126/
The ::first-line pseudo element doesn't apply to inline-level elements; from Selectors Level 3:
In CSS, the ::first-line pseudo-element can only have an effect when
attached to a block-like container such as a block box, inline-block,
table-caption, or table-cell.
You can instead use a <p> or change the display value of the <span> - http://jsfiddle.net/X33pY/127/
You had a lot of problems like color: red, it should be color: red;
You also had to add display: block; to the main container since it is a span tag, these are not block elements.
.tripnameHeaing {
font-size: 24pt;
font-weight: normal;
line-height: 24pt;
margin: 0;
padding: 0;
color: blue;
display: block;
}
.tripnameHeaing:first-line {
color: red;
}
jsfiddle with answer

How to change content on hover

I've been playing around with this, and I thought it would be pretty simple. What I'm trying to do is hover over the 'NEW' label. Once in its hover state, change the content from 'NEW' to 'ADD' using only CSS.
body{
font-family: Arial, Helvetica, sans-serif;
}
.item{
width: 30px;
}
a{
text-decoration:none;
}
.label {
padding: 1px 3px 2px;
font-size: 9.75px;
font-weight: bold;
color: #ffffff;
text-transform: uppercase;
white-space: nowrap;
background-color: #bfbfbf;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px;
text-decoration: none;
}
.label.success {
background-color: #46a546;
}
.item a p.new-label span{
position: relative;
content: 'NEW'
}
.item:hover a p.new-label span{
display: none;
}
.item:hover a p.new-label{
content: 'ADD';
}
<div class="item">
<a href="">
<p class="label success new-label"><span class="align">New</span></p>
</a>
</div>
Here's a JSFiddle to show you what I'm working with.
The CSS content property along with ::after and ::before pseudo-elements have been introduced for this.
.item:hover a p.new-label:after{
content: 'ADD';
}
JSFiddle Demo
This exact example is present on mozilla developers page:
::after
As you can see it even allows you to create tooltips! :) Also, instead of embedding the actual text in your CSS, you may use content: attr(data-descr);, and store it in data-descr="ADD" attribute of your HTML tag (which is nice because you can e.g translate it)
CSS content can only be usef with :after and :before pseudo-elements, so you can try to proceed with something like this:
.item a p.new-label span:after{
position: relative;
content: 'NEW'
}
.item:hover a p.new-label span:after {
content: 'ADD';
}
The CSS :after pseudo-element matches a virtual last child of the
selected element. Typically used to add cosmetic content to an
element, by using the content CSS property. This element is inline by
default.
.label:after{
content:'ADD';
}
.label:hover:after{
content:'NEW';
}
<span class="label"></span>
This little and simple trick I just learnt may help someone trying to avoid :before or :after pseudo elements altogether (for whatever reason) in changing text on hover. You can add both texts in the HTML, but vary the CSS 'display' property based on hover. Assuming the second text 'Add' has a class named 'add-label'; here is a little modification:
span.add-label{
display:none;
}
.item:hover span.align{
display:none;
}
.item:hover span.add-label{
display:block;
}
Here is a demonstration on codepen: https://codepen.io/ifekt/pen/zBaEVJ