CSS :hover selector not working on button - html

I'm working on an internet layout using CSS grid, and one of my grid areas is a navbar. I have tried to make buttons inside the navbar using divs. I have tried to use the :hover selector on the div to change the mouse to pointer, change the bg color, etc, but it isn't working.
When I force hover in chrome using dev tools it acts as if there were no code for hover on the button at all. I have tried to use the hover on the navbar itself, and it works but the strange thing is that it causes the hover to function correctly on the button as well. So, if I apply the same hover rules to the navbar, I can hover over the navbar or over the button and they both work.
For my assignment I have to use SCSS, so the code below is the compiled CSS code. Can the SCSS cause problems like this?
.myGrid {
display: -ms-grid;
display: grid;
grid-template-areas: "nav nav nav nav nav" background-color: white;
width: 85%;
margin: auto;
margin-top: 0px;
}
.myGrid section {
text-align: center;
-webkit-box-shadow: 0px 15px 20px black;
box-shadow: 0px 15px 20px black;
overflow: hidden;
}
#myNavMenu {
background-color: brown;
-ms-grid-row: 3;
-ms-grid-column: 1;
-ms-grid-column-span: 5;
grid-area: nav;
height: 100px;
margin-top: 20px;
border-top-left-radius: 10px;
border-top-right-radius: 10px;
-webkit-box-shadow: 0px 0px 20px black;
box-shadow: 0px 0px 20px black;
}
.myButton {
background-color: white;
width: 100px;
height: 30px;
padding: 5px;
margin: auto;
border-radius: 8px;
z-index: 2;
}
.myButton :hover {
cursor: pointer;
}
<main class="myGrid">
<section class="navMenu" id="myNavMenu">
<h3>Nav</h3>
<div class="myButton" id="button1"> MyButton </div>
</section>
</main>

the space between .myButton and :hover causes all the mess.
.myButton :hover {
cursor: pointer;
}
Here is a working version https://jsfiddle.net/m0sc9ugb/

Related

Why is the margin not fitting into the div?

My button margin isn't fitting into my div. Here's the relevant HTML and CSS.
.card {
justify-content: flex-start;
box-shadow: 0 4px 8px 0 rgb(0 0 0 / 20%);
margin: 8px;
}
.card {
margin-bottom: 16px;
padding: 0px 8px;
float: left;
}
.button {
border: none;
padding: 8px;
color: white;
background-color: black;
text-align: center;
cursor: pointer;
width: 100%;
}
.button:hover {
background-color: #555;
}
.title {
color: grey;
}
.container {
padding: 0px 16px;
}
.card > img {
width: 100%;
}
<div class="ccard">
<div class="card">
<img src="https://pbs.twimg.com/profile_images/1115280588322295808/N1mHLfHy_400x400.png" alt="Jane">
<div class="container">
<h2>Jane Doe</h2>
<p class="title">CEO & Founder</p>
<p>Jane has had 9 years of experience in the tech industry and knows it like the back of her hand. She's very warm and welcoming towards new customers.</p>
<p>janedoe#cabbagedude.com</p>
<p>
<button class="button">Contact</button>
</p>
</div>
</div>
</div>
It has always worked for me before, but it doesn't work now. I also haven't changed anything, except by adding the class ccard around card.
Here's what it looks like for me.
You need to add box-sizing: border-box; to all relevant elements in order to include borders and paddings (I suppose you mean those, since you don't have any margin on the button) in the given width, otherwise they are added to the width, which causes overflow if the width is 100%.

Search icon moving out of div, works fine in chrome but not in mozilla

I have a simple search bar which contain a div and inside that div there is -
input element for text and search button.
<div className="search-box">
<input
type="text"
placeholder={PLACEHOLDER.search}
value={this.state.search}
onChange={this.updateSearchBar}
onKeyUp={this.keyPress}
/>
<IconButton className="search-icon-button" onClick={this.handleSearchIcon}>
<SearchIcon />
</IconButton>
</div>
Css file -
.search-box {
border: 1px solid black;
width: 200px;
height: 30px;
display: flex;
min-width: 180px;
margin-right: 2%;
padding-left: 1px;}
.search-box input{
width: 100%;
color: black;
border: none;
outline: none;
}
.search-icon-button {
color: #FF7D50 !important;
padding: 3px 0px 0px 7px !important;
}
IconButton and SearchIcon are material ui component.
Below I am attaching the screen shot of chrome and mozilla.
Chrome -
Mozilla -
Could you please help I have used width: webkit-mozilla but I want this to work fine on both.
Give browser specific css.You can give padding according to set search icon.I hope it will helps you.
#-moz-document url-prefix(){
.search-icon-button {
color: #FF7D50 !important;
padding: 3px 0px 0px 4px !important;
}
}

Dropdown menu is dropping too low

Recently I fixed my dropdown menu which used to drop with all the other un-hovered buttons also changing their positions. The current issue I just noticed accidentally. When the page is all scrolled up, i.e. I am at starting position, dropdown works as expected. But if I scroll a little bit down, and then hover over button, the dropdown appears where it was before but I am expecting it to appear below the button. This is the snap of code I am using:
.tut_navi_buttons .drop_down_navi_content {
display: none;
right: 0;
position: unset;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
height: 0px;
transition: height 1s ease;
}
.tut_navi_buttons:hover .drop_down_navi_content {
position: fixed;
display: block;
height: auto;
/*height: auto;*/
/*box-shadow: 0px 2px 1px grey;*/
}
#tut_navi {
overflow: hidden;
padding-top: 10px;
float: left;
padding-left: 5px;
/*display: inline-block;*/
}
.tut_navi_buttons .drop_down_navi {
overflow: hidden;
border: none;
outline: none;
color: black;
background-color: inherit;
}
.tut_navi_buttons {
width: auto;
height: 20px;
margin: 10px;
/*margin-bottom: 20px;*/
/*padding: 5px;*/
position: relative;
display: inline-block;
}
<div id='tut_navi'>
<div class="tut_navi_buttons">
<button class="drop_down_navi">Python Basics <i class="fa fa-angle-down"></i></button>
<div class="drop_down_navi_content">
Beginner
Pre-intermediate
Intermediate
</div>
</div>
Please refer to the images I am attaching as they will make it clear.
After scrolling a bit
While at top of page
Remove position:unset; replace with position:relative;
Separate the first two classes in the style sheet because it seems to be clashing

Button centered in flex container is too narrow

My project uses a 20 row by 20 column css grid layout (5% of screen for each cell). One of the pages has a button. Originally the page was contained within grid columns 5-8 and grid rows 6-9, and there was no problem with the button itself, but I need to center it within the grid area, so I used flexbox for centering within the css grid.
The button text is "Select file to upload" but problem is that the button is now only the width of a single word; I want the button to be as wide as all four words. Before I added a flex container for centering the button was as wide as all four words but now with the flex container it's not.
The flexbox arrangement works perfectly for all other elements on the page and two other pages, but this button has that problem (on Firefox 64). I may need to use a unique flex-item class for it, different from the flex-item class shown below.
Here are the css classes that apply to this button:
.center_text_grid {
display: grid;
grid-column: 5 / 12;
grid-row: 6 / 19;
display: block;
overflow: auto;
align-items: center;
justify-items: center;
}
.flex-item {
display: flex;
width: 70%;
flex-direction: row;
align-items: center;
justify-content: center;
}
.upload-btn-wrapper {
display: inline-block;
text-align:center;
border-radius: 15px;
}
.btn {
border: 1px solid rgb(117,163,126);
background-color: black;
width: 75%;
padding: 8px 20px;
border-radius: 15px;
font-size: 20px;
font-weight: bold;
font-family: CamphorW01-Thin, sans-serif;
font-size: 13pt;
color: rgb(117,163,126);
cursor: pointer;
}
Here is the html for this button:
<div class="center_text_grid flex-item">
<div class="upload-btn-wrapper">
<button class="btn" style="width: 100%">Select file to upload</button>
<input type="file" name="fileToUpload" />
</div></div><br><br>
I know this is not a complete reproducible example but as this is specific to button styling, I hoped it could be answered with the code above.
Thanks for any help.
Here's how I solved this:
<div class="upload-btn-wrapper center_text_grid flex-item">
<button class="btn">Select file to translate</button>
<input type="file" name="fileToUpload" />
</div><br><br>
As posted in my original question, this html code wrapped the button in two divs. I changed to a single div and added the upload-btn-wrapper class to the single div. I also changed width in the .btn class to 65%.
That leaves only one problem to resolve: the btn class has a hover selector, but the button text is not highlighted on hover:
.btn:hover{
-webkit-border-radius: 15px;
-moz-border-radius: 15px;
border-radius: 15px;
-webkit-box-shadow: 0px 0px 10px 0px rgba(0, 255, 0, 0.67);
-moz-box-shadow: 0px 0px 10px 0px rgba(0, 255, 0, 0.67);
box-shadow: 0px 0px 10px 0px rgba(0, 255, 0, 0.67);
color: rgb(175,222,162);
}
This is a file selection button so it's handled differently from other buttons, but I don't know how to do it yet. I'll post the answer to that one when I have it. If anyone else knows, please let us know.
Thanks to those who answered.
I copied your code to a jsfiddle and have it below. It works fine on Stack Overflow and jsfiddle. It could your browser not supporting a line of code, causing the code to not work properly. I suggest updating your browser, or switching to a different browser.
Hope this helps!
.center_text_grid {
display: grid;
grid-column: 5 / 12;
grid-row: 6 / 19;
display: block;
overflow: auto;
align-items: center;
justify-items: center;
}
.flex-item {
display: flex;
width: 70%;
flex-direction: row;
align-items: center;
justify-content: center;
}
.upload-btn-wrapper {
display: inline-block;
text-align: center;
border-radius: 15px;
}
.btn {
border: 1px solid rgb(117, 163, 126);
background-color: black;
width: 75%;
padding: 8px 20px;
border-radius: 15px;
font-size: 20px;
font-weight: bold;
font-family: CamphorW01-Thin, sans-serif;
font-size: 13pt;
color: rgb(117, 163, 126);
cursor: pointer;
}
<div class="center_text_grid flex-item">
<div class="upload-btn-wrapper">
<button class="btn" style="width: 100%">Select file to upload</button>
<input type="file" name="fileToUpload" />
</div>
</div><br><br>
Remove styles for both button and input
wrap them in a div.
Add the button css styles to the div.
Example button CSS -
.divButton {
background-color: cyan;
border: none;
color: white;
padding: 12px 30px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
}

dropdown menu is visible even if you don't have your mouse on the dropdown menu

I just created a button with a dropdown menu, you can view the demo here.
In the demo I added a black background to shopping-cart-wrapper element so you can see where the problem lies.
The problem is when you hover over the button you can keep your mouse on the black background and the dropdown menu is still visible.
I only want the dropdown menu to be visible when you hover over the button or keep your mouse on the dropdown menu.
Here is the code I have:
HTML:
<div class="shopping-cart-wrapper">
<a class="shopping-cart" href="#" alt="my-shopping-cart">My Shopping Cart (0)</a>
<div class="shopping-cart-dropdown">
<div class="empty-cart"><span>Your shopping cart is empty</span></div>
</div>
</div>
CSS:
.shopping-cart-wrapper:hover .shopping-cart-dropdown {
opacity: 1;
display: block;
}
.shopping-cart-wrapper {
display: inline-block;
background: #000;
margin-top: 5px;
margin-left: 15px;
}
.shopping-cart {
border: 1px solid #d3d3d3;
color: #656565;
padding-right: 10px;
padding-top: 8px; padding-bottom: 8px;
padding-left: 40px;
font-weight: bold;
display: inline-block;
font-size: 13px;
text-align: right;
text-decoration: none;
box-shadow: 0px 1px 0px #f2f2f2;
background: #f8f8f8 url("http://placehold.it/32x32") no-repeat 0 0 ;
position: relative;
}
.shopping-cart:hover {
background: #fff url("images/cart-sprite.png") no-repeat 0 -29px ;
color: #202020;
border: 1px solid #c6c6c6;
box-shadow: 0px 1px 0px #e5e5e5;
}
.shopping-cart-dropdown {
opacity: 0;
display: none;
border: 1px solid #d3d3d3;
padding-bottom: 80px;
position: relative;
right: 49px;
width: 247px;
background: #f6f6f6;
font-size: 12px;
font-weight: bold;
}
.empty-cart{
background: #202020;
padding: 10px;
color: #fff;
text-align: center;
position: relative;
}
What's Going On
The problem here really isn't a problem, because everything is working as it is supposed to. When you hover over the container, the child is visible. Then the child is visible, the parent becomes larger to encompass it.
Current Selector:
To fix this, you have a couple options. The easiest would be to use a sibling selector instead of a parent. Select the a inside .shopping-cart-wrapper instead of .shopping-cart-wrapper itself, and use the + sibling selector.
We've got to be careful though, because we want the child to stay visible when the mouse is hovering over itself. When using the parent as a selector, this is automatic. With a sibling, we have to manually do this. We'll use both the sibling and the child itself as selectors.
Code
Working Example
Current:
.shopping-cart-wrapper:hover .shopping-cart-dropdown {
opacity: 1;
display: block;
}
Working:
.shopping-cart-wrapper a:hover + .shopping-cart-dropdown,
.shopping-cart-dropdown:hover {
opacity: 1;
display: block;
}
Further Information
http://reference.sitepoint.com/css/adjacentsiblingselector