I am a beginner in html and I have problem with css button styles.
I can understand .button code
please discribe me to above code that I rounded in red colour border.
Some help from anyone know about html and css
So what you're looking at are different kinds of selectors for CSS.
They're a way of targeting different parts of your HTML.
First example
.button span {
...
}
This is possibly the most common usage of selectors, called "decedents".
.button span "means" HTML with the "button" class and has a descendant span element
So it would look like this:
.button span {
color: red
}
<button class="button">
Button Text
</button>
<button class="button">
Some text
<br />
<span>I'm Red</span>
</button>
<button>
Some text
<span>I'm not Red</span>
</button>
Second example
.button span:after uses the aforementioned descendent selector but also combines it with the "and" selector... that's the span:after part!
Any selectors written with a : before them are called "pseudo-elements". They can be from mouse events (such as :hover), or they can things like the :before or :after pseudo-elements.
.button span:hover {
color: red;
}
<button class="button">
Button Text
</button>
<button class="button">
<span>I'm Red when you hover on me</span>
<br/>
But I will never be red when you hover
</button>
<button>
<span>I'm not Red when you hover</span>
</button>
<div class="button">
<span>I will be red when you hover on me</span>
</div>
CSS can be tricky, its a big world out there!
Be sure to check out MDN Documentation or W3Schools as they have some really good interactive examples.
Related
I'm working on a div that contains a span button such that the div is only visible when hovered. That works fine already, but when the span button is clicked to open up its associated menu, the button is no longer visible in the background of the menu. If I click outside of the menu around where the span button should be (basically within the parent div), the button is immediately visible again. From my understanding, clicking the span button makes it so that the div is no longer in a hover state, so the styling for it is no longer applied. I've tried including the :focus and :focus-within selectors to this style too, but it doesn't seem like clicking the span button is triggering it. Is there any way to keep this parent div visible when its child button has been clicked, preferably with CSS?
<div class="menu-button-container" style="visibility:hidden">
<span class="menu-button">
<i class="btn btn-menu"></i>
</span>
</div>
&:hover, &:focus, &:focus-within {
.menu-button-container {
visibility: visible
}
}
Is this what you are trying to achieve??
.menu-button-container{
display:none
}
button:focus + .menu-button-container {
display:block
}
<span class="menu-button">
<button class="btn btn-menu">Hello</button>
<div class="menu-button-container">
hello world
</div>
</span>
When button with class menu-button is clicked, div with class popover-menu is added to the DOM. When this happens, the popover-menu element becomes active.
Is there a way using CSS to add an additional class to the button whenever popover-menu is within DOM?
<div>
<button class="menu-button">
<span>Clickme</span>
</button>
<div class="popover-menu">
Menu
</div>
</div>
While there is no ability to mutate an element's classes within the context of a stylesheet, in your case the only-child pseudo-class may suffice for what you want to do here.
<style>
.menu-button:only-child {
color: pink;
}
</style>
<div>
<button class="menu-button">
<span>Clickme</span>
</button>
<div class="popover-menu">
Menu
</div>
</div>
https://codepen.io/hamzatayeb/pen/dzKyja
I want to change read more button style by adding hover.
I tried some CSS like:
a:hover{
background-color: #077fad;
color: #ffffff;
}
or
div.blmore.rmbutton-center.rmbutton-medium:hover{
background-color: #077fad;
color: #ffffff;
}
But the first approach changes also <a> inside the <h2> paragraph and the other one adds hover to the whole read more button section, except the read more button.
My question is how to select the <a> inside <div class="blmore rmbutton-center rmbutton-medium"> paragraph and how to apply hover effect only to that section?
<div id="bloglist_title">
<h2 class="entry-title">
Title
</h2>
</div>
<div class="entry-content">
<div class="myleft">
<a class="entry-post-thumbnail left" style etc.....></a>
<p>
Short text
</p>
<div id="bloglist_morebutton">
<div class="blmore rmbutton-center rmbutton-medium">
<a style="background-color: #fdc550; border-radius: 5px;"href="my.website.link.com">Read more</a>
</div>
</div>
</div>
/* gets applied to any hovered <a> on the page */
.a:hover {}
/* gets applied to an hovered <a> on the page
within the given class combination */
.blmore.rmbutton-center.rmbutton-medium a:hover {}
/* gets applied to any <a> within an hovered
element of the given class combination */
.blmore.rmbutton-center.rmbutton-medium:hover a {}
The key is the whitespace between two classes or elements, which allows you to represent the nesting of the markup.
It's as simple as it looks like. I want to display:none; the first button. I have two of them with same parent class. For some reason I can't figure out why I don't achieve the result I want.
.main-content .cbToolBar:first-child button{
display:none;
}
<div class="main-content">
<span class="cbToolBar">
<button class="cbButton"></button>
</span>
<span class="cbToolBar">
<button class="cbButton"></button>
</span>
</div>
There is something wrong with my selection but I can't figure out what.
Thanks.
...there are other tags before but at the same level as 'cbToolBar' span, but I thought it would select the first child called 'cbToolBar'.
CSS's :first-child pseudo-class selector selects specifically the first child, regardless of it's class. The documentation on :first-child states:
Same as :nth-child(1). The :first-child pseudo-class represents an element that is the first child of some other element.
There are several workarounds. The one I'd suggest is that if your .cbToolBar elements are the only span elements within your .main-content parent, you can instead use the :first-of-type pseudo-class selector:
Same as :nth-of-type(1). The :first-of-type pseudo-class represents an element that is the first sibling of its type in the list of children of its parent element.
.main-content .cbToolBar:first-of-type button{
display:none;
}
<div class="main-content">
<p>Hello, world!</p>
<span class="cbToolBar">
<button class="cbButton">Button 1</button>
</span>
<span class="cbToolBar">
<button class="cbButton">Button 2</button>
</span>
</div>
Or, if you know the exact position of your element you want to hide, you can always just use :nth-child(n). In this example, the element we want to hide is the second one, so we use :nth-child(2):
.main-content .cbToolBar:nth-child(2) button{
display:none;
}
<div class="main-content">
<p>Hello, world!</p>
<span class="cbToolBar">
<button class="cbButton">Button 1</button>
</span>
<span class="cbToolBar">
<button class="cbButton">Button 2</button>
</span>
</div>
You can use the general sibling combinator in this case.
Set a rule for all elements with a certain class
Use the combinator to select following siblings and unset the rule
.cbToolBar button {
display: none;
}
.cbToolBar ~ .cbToolBar button {
display: inline-block;
}
<div class="main-content">
<span>span</span>
<span class="cbToolBar">
<button class="cbButton">button</button>
</span>
<span>span</span>
<span class="cbToolBar">
<button class="cbButton">button</button>
</span>
<span>span</span>
</div>
How can I choose only the first button in this code?
It's even more nested in my case, but this code is also a problem for me.
<div class="container">
<div class="some_class">
<span>abc</span>
<button class="btn">...</button>
</div>
<div class="some_class">
<span>abc</span>
<button class="btn">...</button>
</div>
<div class="some_class">
<span>abc</span>
<button class="btn">...</button>
</div>
</div>
You would use the :first-child pseudo class.
EXAMPLE HERE
.container .some_class:first-child button {
background:black;
}
Alternatively, assuming that the markup can be different, you might need to use something like this to ensure that the first button is selected even if .some_class isn't the first element. (example)
.container :first-child button {
background:black;
}
This will work
.container .some_class:first-child button {
/* rules here */
}
http://jsfiddle.net/cUu82/1/
you could just use .some_class:first-child button as well if these are the only ones on the page
The first-child (https://developer.mozilla.org/en/docs/Web/CSS/:first-child) will select the first some_class div which was probably your only issue