Trying to apply CSS
.content>span:first-child{
color: red;
font-size: 12px;
font-weight: bold;
}
<span class="content">
<svg xmlns="http://www.w3.org/2000/svg">
<use xlink:href="assets/icons.svg#back-button"></use>
</svg>
<span> Lorem ipsom </span>
<!-- only this span font color should be red -->
<span>
<span> </span>
</span>
</span>
for only the first span. I have tried but it is not working. If anyone knows please help to find the solution.
Demo: https://stackblitz.com/edit/angular-svg-use-gvmekn?file=src%2Fapp%2Fapp.component.html
use the :first-of-type pseudo-class
:first-of-type
The :first-of-type CSS pseudo-class represents the first element of its type among a group of sibling elements.
https://developer.mozilla.org/en-US/docs/Web/CSS/:first-of-type
span.content > span:first-of-type {
color: red;
}
<span class="content">
<svg viewBox="0 0 10 2" xmlns="http://www.w3.org/2000/svg">
<line x1="0" y1="0" x2="10" y2="0" stroke="black" />
</svg>
<span> Lorem ipsom </span>
<!-- only this span font color should be red -->
<span>
Child
<span> Inner child </span>
</span>
</span>
use adjacent sibling combinator
+
The adjacent sibling combinator (+) separates two selectors and matches the second element only if it immediately follows the first element, and both are children of the same parent element.
https://developer.mozilla.org/en-US/docs/Web/CSS/Adjacent_sibling_combinator
svg+span {
color: red;
}
<span class="content">
<svg viewBox="0 0 10 2" xmlns="http://www.w3.org/2000/svg">
<line x1="0" y1="0" x2="10" y2="0" stroke="black" />
</svg>
<span> Lorem ipsom </span>
<!-- only this span font color should be red -->
<span>
Child
<span> Inner child </span>
</span>
</span>
You can use first-of-type so something like:
.content > span:first-of-type {
color:red;
}
The problem is that the :first-child selector only applies if the span you're after is the first child of .content.
In your case the first child is an svg tag, hence why it's not working.
There are 3 ways of getting around this:
Make the selector .content > span:nth-child(2). This'll work in the above but will again break if you ever put additional elements between the span in question and your svg.
.content > span:first-of-type. This seems to be what you're after, i.e. the first span that's the child of .content.
span.some-meaningful-classname - This gives you the flexibility to choose exactly which spans to apply the class to by adding classname="some-meaningful-classname" to your span.
Up to you which to go with, depending on your requirements.
Also, there's a good reference to CSS selectors here: https://www.w3schools.com/cssref/css_selectors.php
You can do it by just giving the span a class like the following
Old:
<span> Lorem ipsom </span>
New:
<span class="firstspan"> Lorem ipsom </span>
(Edit: Sorry forgot to mark the as code so it did not display correctly)
next you can add the class name to the css file like this:
.firstspan {
/* this is a example of code that will make the text red*/
color: red;
font-size: 12px;
font-weight: bold;
}
I hope this helps.
Because the .container is also a span, the declarations overlap each other.
To do this, you just need to target the nth child, like span:nth-child(2) or whichever span you want by nth-child('child No.'). Here is the solution:
HTML:
<span class="content">
<svg xmlns="http://www.w3.org/2000/svg">
<use xlink:href="assets/icons.svg#back-button"></use>
</svg>
<span> Lorem ipsom </span>
<!-- only this span font color should be red -->
<span>
<span> </span>
</span>
</span>
CSS:
span:nth-child(2) {
color: red;
font-size: 12px;
font-weight: bold;
}
Related
I have a short text that is followed by an SVG in a limited-width container.
The expected behaviour is that the text breaks if it's longer than the container width BUT I would like it NOT to break right between the text and the svg:
Current result:
Expected result:
Adding a <nobr> or a <span>tag in the middle of the text (before blue) and closing it after the SVG is not an option as the text comes from an external database and cannot be edited.
<span class="text">
Jack Wolfskin Jacke Colorado Flex - Midnight Blue
</span>
<span class="svg">
<svg>
....
</svg>
</span>
add display-block to svg container:
.svg {
display: inline-block;
}
The only solution I found required a nasty change in the origin HTML.
To make sure the icon is never alone in the new line I wrapped the last word and the icon in a new element with white-space: no-wrap;, plus if we want it to still split if the line cannot accommodate last word with the icon we can make this new container inline flex and flex-wrappable.
<div>
Lorem ipsum dolor sit
<span class="last_word">
very_long_last_word
<svg>...</svg>
</span>
</div>
.last_word {
/* Stick icon to last word */
white-space: no-wrap;
/* Make sure last word and icon will break ultimately */
display: inline-flex;
flex-wrap: wrap;
}
Live example: https://jsfiddle.net/uerzo6sa/
You can prevent the line breaking with this markup. It doesn't need to include the last word, so you can use it even with a generated content.
JSX
<div>
{children}
<span className="tail">
{'\u00a0'}
<svg></svg>
</span>
</div>
HTML
<div>
Lorem ipsum dolor sit<span class="tail"> <svg></svg></span>
</div>
CSS
.tail {
white-space: no-wrap;
}
https://jsfiddle.net/radarfox/65h40jt7/
You can add padding to the text and a negative margin:
<span class="text" style="padding-right: 15px;">
Jack Wolfskin Jacke Colorado Flex - Midnight Blue
</span>
<span class="svg" style="margin-left: -15px;">
<svg>
....
</svg>
</span>
That way, if there isn't room for the padding, the last word will get pushed to the next line also.
(This is based on this answer: https://stackoverflow.com/a/25857961/5899236)
You can define position: absolute on the SVG, with auto for top, right, etc.
https://codepen.io/jsit/pen/xxOQoVW
The only side-effect is this will allow the SVG to appear outside of the containing box; this is in a sense the reason it works at all.
I am learning Selectors and not.
What I am trying is to PUT the text of the span in color red BUT NOT the text of the link, combining both. It is just to learn.
My HTML code
<div>1
<p>2
<span>Here red
<a>Here NOT red
</a>
</span>
<div>3
</div>
</p>
</div>
What I am trying to do with CSS
div p span:not(:nth-child(0)) {
color: red;
}
/* Or */
div p span:not(a) {
color: red;
}
Anyone can help me? I do not want to set another rule for A. It is just to learn as I said.
Thanks!
There were a couple of issues with your page. One is that you had an extra div closing tag. Second, the a tag defines a hyperlink, so it should have an href attribute. Your a tag had no attributes.
Take a look at this snippet
span:not(a) {
color: red;
}
<div>1
<p>2
<span>Here red
Here NOT red
</span>
</div>
</p>
</div>
Alternatively, you could just close the span tag before the a tag, and then just select the span element.
I have used the code below to attempt to select this line of code:
body >div first-child + div + div > img {
}
<body>
<div id="container">
<div class="profilebartr">
<div class="login">
<a href="google.com">
<img src="images/login.png" width="40" height="40" onmousedown="return false;" alt="Login" />
</a>
</div>
</div>
I'm still learning something in CSS, sorry.
And my preview box no longer shows the previews for anything after running code snippets, sorry.
http://jsfiddle.net/tmbwk27c/
The > operator only refers to the immediate children and, in this case, you img is not direct child of the div. This is the reason it is not working. Once go through: http://www.w3schools.com/cssref/css_selectors.asp
body > div:first-child > div > div img {
background-color:black;
padding:10px;
border: 1px solid black;
}
I have an anchor link which has an image and two spans of text, a title and a tagline with different colors, and i want them to change differently when hovering the link.
<style>
span.title {color: #666;}
span.tagline {color: #aaa;}
</style>
<a class="button" href="http://www.link.com" target="_blank">
<div style="display:block">
<img src="images/button.png">
<span class="title">TITLE</span><br>
<span class="tagline">tagline</span>
</div>
</a>
I wonder if it's possible to use something like:
<style>
a.button:hover span.title {color: #000;}
a.button:hover span.tagline {color: #2ae;}
</style>
Yes thats possible. Psuedo class :hover doesn't have to be for the last element in the selector.
Live Demo: http://jsfiddle.net/H35rf/
For future reference its easier/quicker to try this out for yourself in jsFiddle before asking questions.
I'm trying to a language switching system in this website. The idea is to have a SPAN which upon hover displays a dropdown box (UL based) that contains a list of available languages. Here's a snapshot of the effect I'm trying to achieve.
alt text http://img337.imageshack.us/img337/3474/dropboxfinal.png
The dropdown box is actually an unordered list that is hidden by default. Upon hovering on the span, I'm making the UL visible. Here's the HTML and the CSS.
HTML
<span id="langswitch">Language↓
<ul id="langlist">
<li class="en">
<a title="Current language: English" href="http://domain/en">
<img width="16" height="13" alt="English Language" src="flag-en.gif" />
English
</a>
</li>
<li class="th">
<a title="Switch to Thai language" href="http://domain/th">
<img width="16" height="13" alt="Thai Language" src="flag-th.gif" />
Thai
</a>
</li>
<li class="zh">
<a title="Switch to Chinese language" href="http://domain/zh">
<img width="16" height="13" alt="Chinese Language" src="flag-zh.gif" />
Chinese
</a>
<li>
</ul>
</span>
CSS
ul#langlist {
border:1px solid #3399CC;
color:#006699;
background:#fff;
padding:0 !important;
width:100px;
list-style:none;
position:absolute;
top:62px;
right:0;
z-index:100;
display:none;
}
span#langswitch:hover ul#langlist { display:block; }
But instead of the dropdown appearing aligned with my span, it's appearing at the extreme right end of the browser. Here's the screenshot.
alt text http://img84.imageshack.us/img84/1687/dropbox.png
Can any of the CSS gurus here recommend a fix for this?
Thanks,
m^e
just set the position of the "span" to "relative", set a fixed width to the "span" and play a bit with the values "top" and "left" (applied to #langlist) to manage the vertical and horizontal alignment of the list!
EDIT (IN RESPONSE TO YOUR QUESTION): By default, when you place in a web page an element with an absolute position, its position (defined by the attributes "top" and "left") is calculated relatively to the html page ... so, the "left" and "top" values are the "x" and "y" coordinates of an apparent coordinate system whose origin is the top-left corner.
For elements in the head of the webpage there is no problem (in the most of cases), but the elements absolutely positioned in the content of the page are constantly locked on their position so they don't move with the content of the page if the user scrolls or resizes the web page!
If you set to "relative" the position of the element (in this case the span) that contains the absolutely positioned tag, the values "top" and "left" are calculated relatively to this container, so the problem it's fixed!
ABOUT YOUR CODE: Technically you can't apply the "hover" pseudo-class to non link elements if your Doctype is not HTML5, so my suggestion is to consider to use a jQuery function to apply "display: block" to the list when occurs an hover event on the #label. However, you can correct your code as follow ... manage the distance of the list from the #label modifying the "margin-top" value of #langlist:
<style type="text/css">
#container{
position: relative;
width:100px;
}
#langswitch{
padding: 0px;
margin: 0px;
list-style:none;
position:absolute;
}
#langlist {
border:1px solid #39C;
color:#069;
background:#fff;
padding:0 !important;
margin-top: 2px;
width:100px;
top:20px;
left:0px;
display: none;
}
#container:hover #langlist{
display:block;
}
img{
background-color: #CCC;
border: 1px solid black;
}
</style>
<div id="container">
<ul id="langswitch">
<li>
<a id="label" href="#">Language↓</a>
<ul id="langlist">
<li class="en">
<a title="Current language: English" href="http://domain/en">
<img width="16" height="13" alt="English Language" src="flag-en.gif" />
English
</a>
</li>
<li class="th">
<a title="Switch to Thai language" href="http://domain/th">
<img width="16" height="13" alt="Thai Language" src="flag-th.gif" />
Thai
</a>
</li>
<li class="zh">
<a title="Switch to Chinese language" href="http://domain/zh">
<img width="16" height="13" alt="Chinese Language" src="flag-zh.gif" />
Chinese
</a>
</li>
</ul>
</li>
</ul>
</div>
Notice that this solution doesn't work on IE6!