change property of div on hover of an image - html

#myUserMenu {
position: absolute;
background-color: white;
top: 20px;
width: 116px;
border: 1px solid #CCC;
}
#myAvatar:hover #myUserMenu {
background-color: red;
}
.menuItem {
cursor: pointer;
border-bottom: 1px solid #EEE;
}
<img id='myAvatar'>some text here...
<div id="myUserMenu">
<div class='menuItem'>Status online</div>
<div class='menuItem'>Status offline</div>
</div>
So when I hover the myAvatar, myUserMenu background should change to red
#myAvatar:hover #myUserMenu
And nothing happens ! Any idea why ?

Enclose the text inside a span and use + operator to affect the next element's style.
#myUserMenu {
position: absolute;
background-color: white;
top: 20px;
width: 116px;
border: 1px solid #CCC;
}
#myAvatar:hover + #myUserMenu {
background-color: red;
}
.menuItem {
cursor: pointer;
border-bottom: 1px solid #EEE;
}
<span id="myAvatar">some text here...</span>
<div id="myUserMenu">
<div class='menuItem'>Status online</div>
<div class='menuItem'>Status offline</div>
</div>

#myAvatar:hover #myUserMenu {
background-color: red;
}
This selector is looking for #myUserMenu inside #myAvatar. Obviously that won't work because it's outside #myUserMenu.
What you could do is look for #myUserMenu immediately after #myAvatar, like so:
#myAvatar:hover + #myUserMenu {
background-color: red;
}
This is the Adjacent Sibling Combinator. See this article for more details.
Or you could rearrange your HTML to put #myUserMenu inside #myAvatar.

Your div #myUserMenu is not a child of your image, so you need to target the div with a brother selector :
#myAvatar:hover ~ #myUserMenu {
background-color:red;
}
JsFiddle: http://jsfiddle.net/ghorg12110/o0c2hppv/
The general sibling combinator selector ~ allow you to select the element which can appear anywhere after it.

You have to apply parent child relationship in order to apply hover.
Like this
#myUserMenu {
position: absolute;
background-color: white;
top: 20px;
width: 116px;
border: 1px solid #CCC;
}
#myAvatar:hover #myUserMenu {
background-color:red;
}
.menuItem {
cursor:pointer;
border-bottom:1px solid #EEE;
}
<div id="myAvatar">
<img id=''> some text here...
<div id="myUserMenu">
<div class='menuItem'>Status online</div>
<div class='menuItem'>Status offline</div>
</div>
</div>

Related

Add two lines below text

I want to add two lines below my p tag, But I want the lines to take 100% width of the container.
How can I achieve this? I can't use hr tags, it should be done with CSS pseudo classes I guess.
CSS AND HTML CODE:
.test {
max-width: 320px;
width: 100%;
border: 1px solid black;
}
<div class="test">
<p>Test</p>
</div>
Add text-align:center to center your text.
Use border-bottom instead of border to make your border appear only below.
Then use a :after element to generate the second border.
To make your element take 100% of its container, just remove the max-width. As its a div (which is a block-level element) it will automatically take 100% of its parent space if you dont tell it otherwise.
.test {
width: 100%;
border-bottom: 1px solid black;
text-align: center;
}
.test:after {
content: '';
display: block;
border-bottom: 1px solid black;
margin-bottom: 4px;
}
<div class="test">
<p>Test</p>
</div>
.test {
max-width: 320px;
width: 100%;
border-bottom: 4px double black;
}
<div class="test">
<p>Test</p>
</div>
Simply change property border to border-bottom and set the border style to double
.test {
max-width: 320px;
width: 100%;
border-bottom: 6px double #444;
text-align: center;
}
<div class="test">
<p>Test</p>
</div>
This is my approach. I hope is helpful to you. You have the flexibility to adjust the space between lines with margin-bottom: 2px;
.test {
max-width: 320px;
width: 100%;
border-bottom: 1px solid black;
text-align:center;
}
.test p {
border-bottom: 1px solid black;
margin-bottom: 2px;
}
<div class="test">
<p>Test</p>
</div>
the "border-bottom" style property can help to draw lines below an element. You can use below code :
.test {
width: 100%;
border-bottom: double;
text-align: center;
}
<div class="test">
<p>Test</p>
</div>
You need to use ::before and ::after to achieve this style.
.test {
max-width: 320px;
width: 100%;
text-align: center;
position: relative;
}
.text {
position: relative;
width: 100%;
}
.text::before,
.text::after {
content: '';
position: absolute;
bottom: 0;
left: 0;
height: 1px;
width: 100%;
background: #000;
}
.text::after {
bottom: -5px;
}
<div class="test">
<p class="text">Test</p>
</div>

Add CSS Class OnHover in CSS

When I hover over an HTML element I would like that element to inherit the properties of a pre-written CSS class.
Is there a way to accomplish this task without Javascript?
Write a hover class for an item, which does the exact same thing that you are describing.
.box{
height: 60px;
width: 60px;
border: 1px solid black;
margin: 20px;
}
.box1:hover, .box2{
background-color: red;
}
<div class="box box1"></div>
<div class="box box2"></div>
More simply:
.box{
height: 60px;
width: 60px;
border: 1px solid black;
margin: 20px;
}
.box:hover{
background-color: red;
}
<div class="box"></div>
In your CSS file, write a hover selector for the element you are targeting:
a {
text-decoration: none;
}
a:hover {
text-decoration: underline;
color: red;
}
Creating the :hover selector will inherit the properties associated when the mouse hovers over the selected element.
<div>
Example 1
</div>
Hope that answers your question!
<div mouseover="this.className='myClass'" onmouseout="this.className=''">
Hover Me
</div>
That would add / remove the class name with javascript, otherwise do this in CSS:
div:hover {
background-color: red
}

Image border hover won't work

I have an image centered on the screen that I would like a border around, which when hovered over changes color. I am trying to do this as you can see in the code below, but the problem is that the image just keeps being a link but no border, what is wrong?
html code:
<div id="container">
<div id="content">
<div class="10Img">
<img src="10Pimg.png" alt="10img" style="width:900px; height:200px">
</div>
</div>
</div>
css code:
#content{
padding-bottom: 200px;
position: absolute;
float: left;
left: 50%;
margin-left: -450px;
top: 200px;
}
#container{
height:100%;
}
.10Img{
border: 2px solid grey;
}
.10Img a:hover{
outline: 2px solid black;
}
The main issue is you are starting your class name with a numerical character change 10Img and start it with an alphabetic character.
Ex. i change it from 10Img to aImg
Then you can use
.aImg img {
border: 2px solid grey;
}
or only
.aImg {
border: 2px solid grey;
}
Try this: Demo
a img {
border: 2px solid grey;
}
a img:hover {
border: 2px solid black;
}
See This Demo
.Img{border: 2px solid grey;}
.Img a:hover{
outline: 2px solid black;}
Note: Class Name can not start with integer.
Refer This for Rules regarding naming.
Your css class 10Img doesn't work, because css class names must not begin with a number, see:
Which characters are valid in CSS class names/selectors?
So if you call your class Img10 instead of 10Img it should work.
<div id="container">
<div id="content">
<div class="Img10">
<img src="http://dummyimage.com/900x200/000/fff" alt="10img" style="width:900px; height:200px" />
</div>
</div>
</div>
Also you may want to have the :hover border on the div instead on the a:
#content{
padding-bottom: 200px;
position: absolute;
float: left;
left: 50%;
margin-left: -450px;
top: 200px;
}
#container{
height:100%;
}
.Img10{
border: 2px solid grey;
}
.Img10:hover{
outline: 2px solid black;
}
Here is a working fiddle:
http://jsfiddle.net/k2Ld7yfe/

How to constrain CSS to when a contained input has focus?

Assume I have a form input structured as follows:
<div class="wrapper">
<span class="icon icon-search"></span>
<input type="text"/>
</div>
is there a way, with CSS3, to apply a red border around the .wrapper div on a focus state on the input element?
.wrapper input:focus {
border solid thin red;
}
puts the border on the input field but I want it on the containing div.
You're looking for a css parent selector. Unfortunately that isn't currently available. The exact functionality you're looking for would need JavaScript or alternative HTML.
It looks like you want the border to surround the icon and the field but currently it is only surrounding the field? My suggestion would be to use the sibling selector like so: (From my example below i've moved the icon after the input)
* {
box-sizing: border-box; /* Helps with sizing calculations */
}
.wrapper {
position: relative;
}
.icon {
width: 20px;
height: 20px;
position: absolute;
background-color: blue;
left: 200px;
top: 0;
border: solid 1px transparent;
border-left: none;
}
input {
width: 200px;
height: 20px;
border: solid 1px blue;
border-right: none;
}
input:focus {
border-color: red;
outline: 0;
}
input:focus + .icon {
border-color: red;
}
<div class="wrapper">
<input type="text"/>
<span class="icon icon-search"></span>
</div>

CSS :: child set to change color on parent hover, but changes also when hovered itself

I have an <a> with a <span> children. I have written some CSS which changes the border-color of the children when the parent is hovered, but it also changes the border-color when I hover the children, which it shouldn't.
a {
padding: 50px;
border: 1px solid black;
}
a span {
position: absolute;
top: 200px;
padding: 30px;
border: 10px solid green;
}
a:hover span {
border: 10px solid red;
}
<a>
Parent text
<span>Child text</span>
</a>
Update
The below made sense for 2013. However, now, I would use the :not() selector as described below.
CSS can be overwritten.
DEMO: http://jsfiddle.net/persianturtle/J4SUb/
Use this:
.parent {
padding: 50px;
border: 1px solid black;
}
.parent span {
position: absolute;
top: 200px;
padding: 30px;
border: 10px solid green;
}
.parent:hover span {
border: 10px solid red;
}
.parent span:hover {
border: 10px solid green;
}
<a class="parent">
Parent text
<span>Child text</span>
</a>
If you don't care about supporting old browsers, you can use :not() to exclude that element:
.parent:hover span:not(:hover) {
border: 10px solid red;
}
Demo: http://jsfiddle.net/vz9A9/1/
If you do want to support them, the I guess you'll have to either use JavaScript or override the CSS properties again:
.parent span:hover {
border: 10px solid green;
}