add different styles when text wraps to next line - css

I have a div with a small heading - just two words.
When I resize da browser the text wraps and the last word goes to the next line.
I want to display different styles to the last word only when it goes to the next line. For instance: font-weight:normal instead of being bold.
Is that possible only with css?

You could use the::first-line pseudo element for this.
From MDN:
The ::first-line CSS pseudo-element applies styles to the first line
of a block-level element. Note that the length of the first line
depends on many factors, including the width of the element, the width
of the document, and the font size of the text.
section {
width: 10%;
border: 2px solid tomato;
}
h2 {
font-weight: normal;
}
h2:first-line {
font-weight: bold;
color: green;
}
<section>
<h2>Some heading</h2>
</section>
Codepen demo (resize to see the effect)

Since your content is more dynamic, you may need to use Javascript to accomplish this. An event listener on page resize that checks the height of your heading div and adjusts a span containing the second word should do the trick.
window.onresize = function(){
document.getElementById('last_word').style.font-weight = 'normal';
};
If your page content is fairly static, you could probably get away with using a css media query. I don't know of any way to have css grab the second word in a div, but if you put the second word in a span you could do something like this:
#media (max-width: 700px) {
#header_id>span {
font-weight: normal;
}
}
Of course, replacing the 700px with whatever width you find triggers the content to word wrap.

Related

CSS - removing one of the divs if their content cant fit into one line

I have filename div and filesize div in one line, I would like to remove the filesize div if the
filename is too long (e.g in case of mobile screen because of not enough space)
[aaaaaa.jpg|51Kb] -> [aaaaaaaaaaa.jpg]
Is there any way to do this in CSS or CSS and JS ?
Thanks,
Csaba
UPDATE
I have to remove completely the filesize part, to have more space for the filename.
There is at least part of the way to do it by using the CSS ::first-line pseudo element.
This snippet sets the font-size of the container to 0 and then sets it back to something for the first-line.
The filename and filesize divs are displayed inline, so the CSS considers them as part of the first line (and the filesize to be not in the first line if the line has been broken at the space).
Note: this gives the right appearance, but it does not remove the filesize div from the DOM, it just makes it have 0 as dimensions.
.container {
width: 100vw;
font-size: 0;
overflow-x: auto;
/* this is not essential - put here so you can check */
}
.container::first-line {
font-size: 1rem;
}
.container>* {
display: inline;
}
<div class="container">
<div class="filename">https://aratherlongurlaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</div>
<div class="filesize">123MB</div>
</div>

Can I hide a :before or :after element if the original div has no content?

I have a div that says "Tags: Example 1, Example 2"
The css looks like this:
.tag {
color: #bbb9b9;
font-size: 8pt;
font-weight: 300;
font-family: 'Roboto', sans-serif;
}
.tag:before {
content: 'Tags: ';
}
.tag:empty {
display:none;
}
Now, if no tags show up at the Wordpress post, I also want to hide the text "Tags", but it is in a :before tag.
Is there a way to hide the :before element somehow? Since the :before element is never actually empty, I'm finding it hard to fix.
The :empty pseudo selector will select elements that contain either nothing or only an HTML comment.
It will only work if the div is completely empty or has comments.
<div></div>
<div><!-- Comment --></div>
If the div has space or tabulations, you will have to use the pseudo element :blank
<div> </div>
<!-- Notice that inside this div there is a blank space -->
<div>
<!-- Comment-->
</div>
I think maybe this is what might happen. Although there is no content, the div may have some space or tabulation. If this doesn't help, it would be nice to have an example of the html code when it's theoretically empty.

Target certain words with CSS?

This is kind of a theory question but I wonder whether it is possible or not.
So if you have a div with some content like...
<div>£100.00 - BUY</div>
Can you then target "£100.00" without targeting the rest?
So you can increase the font size of £100.00?
No, that can't be done without wrapping the desired text in another element, typically a <span>.
You could target it by wrapping it in a span element:
jsFiddle example
<div><span class="price">$100.00</span> - BUY</div
You can use this
<div><span id="price">£100.00</span> - BUY</div>
CSS
#price {
font-size: 2em; // double of the current font
}
div {
font-size: inherit; // get the font-size from the browser or the document.
}
This will give the div a default font-size but the span with id price will have twice the font-size as the div.

how to remove the gap between the inline-block elements

.item-list {
letter-spacing: -0.3em;
}
.item-list a {
letter-spacing: 0;
display: inline-block;
}
<div class="item-list">
a
a
a
a
</div>
only in win ie6,the gap between a is still exit ,the style letter-spacing:-0.3em will make effective when delete the style of a { letter-spacing:0 }
why? can i figure out this problem?
wow this one stumped me for a while...believe it or not here is your answer:
font-size:0; must be added to parent element
In the case of your example, I would define the font-size of the a tags separately, and add "font-size:0;" to the parent div element
In other words:
css:
.item-list{letter-spacing:-0.3em; font-size:0;}
.item-list a{letter-spacing:0;display:inline-block; font-size:SOMETHING HIGHER;}
(also your DOCTYPE declaration must be correct or display inline-block can have problems working in IE, at least I had trouble with it with IE7)
This should end any extra margin frustration you're experiencing from display:inline-block;
It has to do with how you're typing your HTML. Because you're formatting it nicely in your IDE, a la, with spaces and new lines, those spaces and newlines show up when displayed on the page. So instead of
<div class="item-list">
a
a
a
a
</div>
type it out as one line and they will go away:
<div class="item-list">aaaa</div>
You can add this CSS
a{float:left}
Gap will Remove
I always use:
line-height: 2.2; //or whatever value you want
I took from facebook layout and works amazing for me

How to align an indented line in a span that wraps into multiple lines?

Does anyone have an idea how to align the second line?
span.info {
margin-left: 10px;
color: #b1b1b1;
font-size: 11px;
font-style: italic;
font-weight: bold;
}
<span class="info"></span>
display:block;
then you've got a block element and the margin is added to all lines.
While it's true that a span is semantically not a block element, there are cases where you don't have control of the pages DOM. This answer is inteded for those.
<span> elements are inline elements, as such layout properties such as width or margin don't work. You can fix that by either changing the <span> to a block element (such as <div>), or by using padding instead.
Note that making a span element a block element by adding display: block; is redundant, as a span is by definition a otherwise style-less inline element whereas div is an otherwise style-less block element. So the correct solution is to use a div instead of a block-span.
span is a inline element which means if you use <br/> it'll b considered as one line anyway.
Change span to a block element or add display:block to your class.
http://www.jsfiddle.net/tZtpr/1/
<!DOCTYPE html>
<html>
<body>
<span style="white-space:pre-wrap;">
Line no one
Line no two
And many more line.
This is Manik
End of Line
</span>
</body>
</html>
try to add display: block; (or replace the <span> by a <div>) (note that this could cause other problems becuase a <span> is inline by default - but you havn't posted the rest of your html)
Also you can try to use
display:inline-block;
if you would like the span element to align horizontally.
Incase you would like to align span elements vertically, just use
display:block;
You want multiple lines of text indented on the left. Try the following:
CSS:
div.info {
margin-left: 10px;
}
span.info {
color: #b1b1b1;
font-size: 11px;
font-style: italic;
font-weight:bold;
}
HTML:
<div class="info"><span class="info">blah blah <br/> blah blah</span></div>

Resources