I have a scenario like the below to show a spacer(line) before and after icons(Cross symbols) and not to show spacer(line) before and after buttons(with Cancel text). How can I achieve this...
My Css file is
.Container > *:first-child::before,
.Container > *::after
{
display: inline-block;
content: url('../Content/Images/Line.png');
}
All my icons, buttons(with Cancel text) are inside container div
Can we restrict showing lines before and after buttons(with Cancel text)?
I tried the below code which did not work.
.Container > *:not(input[type="button"]):first-child::before,
.Container > *:not(input[type="button"])::after
{
display: inline-block;
content: url('../Content/Images/Line.png');
}
Edit:
Assuming demo markup like this:
<div class="container">
<span>x</span>
<span>x</span>
<span>x</span>
<input type="button" value="Cancel" />
<input type="button" value="Cancel" />
<span>x</span>
<span>x</span>
<span>x</span>
</div>
.. you could use the following CSS to acheive what you need:
CSS
.container > *:not([type="button"]):first-child::before,
.container > *:not([type="button"])::after
{
/*content: url('../Content/Images/Line.png');*/
content: ''; /* if line image is used, this is not necessary */
background: #555; /* if line image is used, this is not necessary */
display: inline-block;
width: 1px;
height: 100%;
vertical-align: middle;
margin: 0 8px;
}
FIDDLE
Side note: Instead of using the * selector - you could target the specific child elements, or -even better - add a class name to the child elements
So why didn't your original css - as posted in the question - work?
The :not() pseudo class can only accept a simple selector.
From the spec:
A simple selector is either a type selector, universal selector,
attribute selector, class selector, ID selector, or pseudo-class.
So although the not pseudo class can accept an attribute selector like: :not([type="button"]), in your code you have combined it with an element selector - ie. input ---- :not(input[type="button"]) - which is why the code doesn't work.
So this will work:
.Container > *:not([type="button"])::after
{
display: inline-block;
content: url('../Content/Images/Line.png');
}
..but this won't:
.Container > *:not(input[type="button"])::after
{
display: inline-block;
content: url('../Content/Images/Line.png');
}
Here is a demo to illustrate this.
If you only want the line before and after the icons, instead of using wildcard * and then trying to deselect buttons, simply target the icons alone. Assuming the icons has class .class
.Container > .icon:first-child::before,
.Container > .icon::after
{
display: inline-block;
content: url('../Content/Images/Line.png');
}
or if the icons are <img>, corresponding css would be
.Container > img:first-child::before,
.Container > img::after
{
display: inline-block;
content: url('../Content/Images/Line.png');
}
problem solved (can't be more specific since you haven't provided much information).
I think the key to solving your problem is using an adjacent sibling selector. You can select elements by their preceding sibling as follows:
.sibling#one + .sibling#two {
/* style every .sibling#two that is preceded by a .sibling#one */
}
I've made a quick example here, using borders instead of the images with lines and div's as buttons. I hope this will help, good luck!
Related
Hi please see the following two html codes:
<div class="price">
2
</div>
And:
<div class="price">
<div class="symbol">£</div> 2
</div>
So in the first one I want to add £ to the price so I am using this CSS
.price::before {
content: "£";
}
And it is working . But I don't want this if .symbol class is already present in .price class. Is there is any way to do this in CSS?
If you are applying £ symbol as default and .symbol for the different symbols as #BoltClock mentioned in the comments
You can use pseudo classes with position:absolute combination...
You have to apply position:absolute to both pseudo element and symbol class at the same position. If there is no symbol class, the pseudo element :before will be visible and if symbol class is there, it will be visible above pseudo element :before
Here the tricky part is the setting background-color:white of the .symbol class
Stack Snippet
.price::before {
content: "£";
position: absolute;
left: 0;
}
.price {
display: inline-block;
position: relative;
padding-left: 20px;
}
.symbol {
position: absolute;
left: 0;
background: #fff;
}
<div class="price">2</div>
<br>
<div class="price">
<div class="symbol">$</div> 4
</div>
You can achieve this in pure CSS:
.price::before {
content: "£";
}
/* Do not show the symbol if contained into a price element */
.price > .symbol {
display:none;
}
Why
So, CSS selectors work in a way for which a selector can only query an element's parents or siblings. It is not possible to create a selector which act by querying the children of an element. You have to reverse the order. That is why, you work on the .symbol class and show it or hide it.
You can check this class via jquery it is good aproch.
if( $('.price').hasClass('symbol') ) {
// ...
}
You can not achieve this using CSS only. You need a bit of jquery here.
What my jQuery code is doing is it is adding class .symbol-present to .price which has .symbol element and my CSS code is then adding symbol accordingly.
$(".price").addClass(function(){
return $(this).find(".symbol-present").length > 0 ? "symbol":"";
})
.price:not(.symbol-present)::before {
content: "£";
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="price">
2
</div>
<div class="price">
<div class="symbol">£</div> 2
</div>
Here is my code.
<div class="start">start</div>
<div>middle-1</div>
<div>middle-2</div>
<div>middle-3</div>
...................
...................
<div>middle-n</div>
<div class="end">end</div>
I want to apply css to all div's when mouse hover the first div with class start.
With the current HTML structure you can use couple of sibling selectors for this.
.start:hover ~ div {
color: red; /* styles you want to apply */
}
/* reset styles back for all other divs after .end */
.start:hover ~ .end ~ div {
color: inherit;
}
Demo: http://jsfiddle.net/3c6V6/1/
However I would recommend to change HTML structure if you can. For example:
<div class="start">start</div>
<div class="middles">
<div>middle-1</div>
<div>middle-2</div>
<div>middle-3</div>
<div>middle-n</div>
<div class="end">end</div>
</div>
<div>after-1</div>
<div>after-2</div>
and CSS:
.start:hover + .middles > div {
color: red;
}
You would just have much more flexibility.
Demo: http://jsfiddle.net/3c6V6/2/
Could it be as simple as putting a parent container around it, and putting the hover on that, or do you wish to single out some of the siblings directly?
In this case, try putting :hover on the parent container like this:
.parent:hover div {/*style*/}
This is for your second version found in the comments: JSFiddle DEMO
div.start:hover~div.middles div:not(.end) {
font-weight: bold;
}
(This is for your original question):
div.start:hover~div:not(.end) {
font-weight: bold;
}
JSFiddle DEMO
This is where I found the information to do it. Didn't know there were so many CSS selectors.
If .text is hovered, how do I also haver .imageis?
All My Clipboards
<a class="imageis sprite_image_base" href="#"></a>
I tried doing something like this in my CSS:
.text:hover, .imageis:hover + .text {
background-position: -107px -311px !important;
height: 16px;
margin-right: 8px;
vertical-align: middle;
width: 23px;
}
Note: I want to do this with pure css without jquery.
I don't know why others are taking about Javascript, this is easily done with CSS.
You use the adjacent sibling combinator or general sibling combinator.
First, declare that you want .text to be hovered for something to happen, like this: .text:hover, then add a sibling combinator: ~ or +, and finally select the sibling you want to affect, in this case: .imageis.
Put it together and you get:
.text:hover + .imageis {
/* css-stuff */
}
You could also select both elements' parent and do a regular descendant selector, like this:
.parent:hover .imageis {
/* css-stuff */
}
I want the btn next to the string. I can't figure it out even using CSS inline
<span class="subscribe_button"> <h3>Books</h3> <%= render 'follow_form' %></span>
CSS:
.subscribe_button {
display: inline;
}
You have some invalid HTML here.
A block level element cannot be within an inline one, this is basic HTML knowledge.
What I suggest you do is wrap both elements in a div and use float: left;
<div class="wrap">
<h3>Books</h3>
<span class="subscribe_button"> unsubscribe</span>
</div>
CSS:
.wrap
{
width: 300px;
}
.wrap h3,
.wrap span
{
float: left;
}
.wrap span
{
margin-left: 10px/*your value*/;
}
I also suggest you go read up on HTML rules, what is allowed where and why they are or are not allowed.
http://jsfiddle.net/Kyle_Sevenoaks/zJUZs/
The Books part is (also) a block (due to <h1>), so you need to set it to inline as well (as shown in the comment of limelights), otherwise your button will still be pushed to the next line.
Try adding this to your CSS
.subscribe_button h3 {
float: left;
}
If you float an element it means other elements after it will wrap onto the same line as it (as long as theyre width does not make them too wide).
Span is inline element and h3 is block element. Inline elements should be inside block elements. Have you tried to validate your html code? http://validator.w3.org/
try:
display: inline-block;
Try following code
.subscribe_button h3{
display: inline;
}
use float:left for both h3 and button
I think you can do this with this code:
.subscribe_button > * {
display: inline;
}
'>' is a child selector and * matches to all element.
Yo can read more about CSS2 selectors: CSS2 Selectors
I'm having some difficulty with this example:
<img src="image1/<?php echo $file; ?>.jpg" style="width:500px" />
<p id="caption"><?php echo $caption; ?></p>
I'm trying to get the caption with CSS when hovering the image.
I tried to use a img{hover;} and p {hover;}.
Is there a way for me to get the caption when hovering the image? The example is in PHP and if it was in CSS or Javascript maybe I could search for it, but so far I can't find a solution for this.
I appreciate any explanation & examples.
/* by default, hide caption: */
#caption { display: none; }
/* if caption is next to a hovered img, show: */
img:hover + #caption { display: block; }
jsFiddle Demo
+ is the Adjacent Sibling Selector, supported from IE8.
:hover pseudoclass is used to style elements the mouse goes over
Note that if you want to use more than one caption in your document, you should use a class instead of an id. Ids must be unique in the document.
If you need something that works in IE7, consider HTML like this:
<div class="image-with-caption">
<img src="whatever.png" style="width:200px" />
<p class="caption">caption</p>
</div>
And the CSS would be:
.caption { display: none; }
.image-with-caption:hover .caption { display: block; }
jsFiddle Demo
To affect the style of the p while hovering over the img:
img:hover + #caption {
display: block; /* or whatever...*/
}
Or:
img:hover ~ #caption {
display: block; /* or whatever... */
}
It's worth noting that these examples assume that you have only one p element with an id of 'caption,' if you have multiple p elements with that id, then you need to use a class instead, as an id must be unique within the document.
The + is the CSS adjacent-sibling combinator, and selects the #caption that immediately follows the img over which the user is hovering.
The ~ is the CSS general-sibling combinator, and selects any sibling #caption element that is a later-sibling of the img which is hovered; regardless of any elements that might appear in between (though they do have to be siblings within the same parent element).
Reference:
CSS selectors, at the W3.org.
You want img:hover {/* css */}
Actually, you probably want to do something like this:
<div class="hoverme">
<img src="image1/<?php echo $file; ?>.jpg" style="width:500px" />
<span><?php echo $caption; ?></span>
</div>
and then in your CSS:
div.hoverme span{
display: none;
}
div.hoverme:hover span{
display: block;
}
Try using JavaScript events onMouseOver and onMouseOut to show/hide the caption, something like this:
<img src="image.jpg" style="width:500px"
onMouseOver="document.getElementById('caption').style.display='block'"
onMouseOut="document.getElementById('caption').style.display='none'" />
<p id="caption" style="display:none">Caption here</p>