I am having trouble with the combination of the CSS selector :nth-child(...) and the box-shadow effect. The desired effect is as follows:
Even-numbered div elements in a container are given an alternating background color.
When the user hovers over one of the div elements, a box shadow is applied, giving the appearance of the "hovered" div "hovering" above the following div.
However, I am running into a problem. While the box shadow is applied to the "hovered" element, the effect is different for even-numbered div elements as opposed to odd-numbered ones. Essentially, the shadow of each even div overlaps the following odd div, while the shadow of each odd div is rendered behind the following even div.
This pen demonstrates the issue better: http://codepen.io/jtlovetteiii/pen/cEaLK
Here is the HTML snippet:
<div class="rows">
<div class="row"></div>
<div class="row"></div>
...
</div>
Here is the CSS:
.rows
{
background-color: #AAAAAA;
}
.rows .row:nth-child(even)
{
background-color: #E2E2E2;
}
.row
{
height: 20px;
cursor: pointer;
}
.row:hover
{
box-shadow: 0px 10px 10px #888888;
}
What am I missing?
The reason this is happening is because only your nth-child(even) divs have a background color. While it appears that the hover shadow is overlapping the other div, it really isn’t – it’s overlapping the parent’s background color.
You can fix the issue with a combination of position: relative and z-index:
.rows {
position: relative;
}
.row
{
position: relative;
height: 20px;
cursor: pointer;
background-color: #CCCCCC;
}
.row:nth-child(even)
{
background-color: #E2E2E2;
}
.row:hover
{
box-shadow: 0px 10px 10px #888888;
z-index: 100;
}
CodePen demo
Interesting. Not sure why that is happening, but I found a workaround. By adding a position: relative to the :hover elements, the hover effect is more consistent:
http://codepen.io/anon/pen/hsKEf
.rows
{
background-color: #AAAAAA;
}
.rows .row:nth-child(even)
{
background-color: #E2E2E2;
}
.row
{
height: 20px;
cursor: pointer;
}
.row:hover
{
box-shadow: 0px 10px 10px #888888;
position: relative;
}
It still doesn't look quite right, but maybe a margin offset would cause it to look a bit better.
JSFiddle
.row
{
height: 20px;
cursor: pointer;
position:relative;
z-index:1;
}
.row:hover
{
box-shadow: 0px 10px 10px #888888;
z-index:2;
}
Related
I can't find a similar problem to this one.
I want to have two CSS :hover buttons horizontally aligned in a centered parent div (orange div), but it isn't working.
Centering:
The cursor-activated area (purple border) expands far beyond the buttons, covering much of the page. How can I format the cursor-activated area to match the size of the buttons' source content:url() without destroying the centering of the parent div? Using something like position:absolute doesn't seem to be the right solution.
Horizontal alignment:
display: inline-block; works only if I change the div id #alpha a{ to #alpha{ and #beta a{ to #beta {.
This way the buttons horizontally align but then I lose their functionality.
See/edit the example here:
http://dabblet.com/gist/0ec177e3a1191051cc3555ca958a6d20
A possible solution:
Certain styling is probably needed for :hover or something like a:hover so that the unexpectedly large cursor-activated area can be reduced to the same size as the button without effecting the centering from the parent div. I've had no luck with position:absolute.
Any insights?
body {
background-color: rgb(0,0,0);
margin: 0px;
border: 0px black;
padding: 0px;
}
#parent {
background-color: rgb(200,80,0) !important;
width: 50vw;
font-size: 0;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
text-align: center;
}
a{
border: 1px solid red !important;
}
:hover{
border: 1px solid purple !important;
}
#alpha a{
border: 1px solid black !important;
max-width:7%;
content: url("https://cdnimages.opentip.com/thumbs/VLL/VLL-LET-A_130_130.jpg");
display: inline-block;
}
#alpha:hover a{
border: 1px solid yellow !important;
max-width:7%;
content: url("https://cdnimages.opentip.com/thumbs/VLL/VLL-LET-D_130_130.jpg");
display: inline-block;
}
#beta a{
border: 1px solid black;
max-width:7%;
content: url("https://cdnimages.opentip.com/thumbs/VLL/VLL-LET-B_130_130.jpg");
display: inline-block;
}
#beta:hover a{
border: 1px solid yellow !important;
max-width:7%;
content: url("https://cdnimages.opentip.com/thumbs/VLL/VLL-LET-D_130_130.jpg");
display: inline-block;
}
<div id=parent>
<div id="alpha">
</div>
<div id="beta">
</div>
</div>
Add this to #parent
display: flex;
align-items:center;
justify-content: center;
I'm new to flexbox and trying to make a menu using it.
I want links to have a nice border on the bottom when user hovers on them. But even if I set box-sizing: border-box; flex keeps recalculating text position and element 'jumps' instead of predicted behaviour.
I have an example with the problem. I don't want content inside my element to jump when I hover.
Is there any simple solution/edition to my code to make it work as expected? I know other ways of achieving what I want: set baseline, use relative/absolute positioning...
.item {
display: flex;
width: 200px;
height: 100px;
background: #123;
color: #fff;
text-align: center;
justify-content: center;
flex-direction: column;
}
.item:hover {
border-bottom: 10px solid lightgreen;
box-sizing: border-box;
}
<div class="item">
Content
</div>
By adding a 10px border on hover, you are changing the size of the box on hover. That will reposition surrounding elements... on hover.
One solution is to reserve the space for the border at all times. In other words, have the 10px border factored into the element in a normal state.
This border gets the element's background color (or transparent) so it is not visible. On hover, you only change the color.
.item {
display: flex;
width: 200px;
height: 100px;
background: #123;
color: #fff;
text-align: center;
justify-content: center;
flex-direction: column;
position: relative;
}
.item::after {
content: "";
position: absolute;
bottom: 0;
left: 0;
width: 100%;
border-bottom: 10px solid #123;
}
.item:hover::after {
border-bottom: 10px solid lightgreen;
}
<div class="item">Content</div>
I would use an inset box-shadow for this feature.
I managed to recreate the effect by changing the :hover css to:
.item:hover {
box-shadow: inset 0 -10px lightgreen;
}
example here
What if you just set a bottom border by default using the same colour which is on its background? Once you hover over your item, you will need just to change the colour.
I created a navigation with a subnavigation, see JSFiddle with subnavigation. I have the issue that between the top navigation with yellow background and the subnavigation with red-colored background I want to have a distance of 1px solid white to separate both areas. At the moment this CSS definition is used
html, body {
background-color: green;
}
I tried to put another div around <nav class="top-bar" data-topbar> and set the background-color: white; but without success.
The goal is to have always a 1px solid line below the .top-bar area. So also when there is no subnavigation displayed, see JSFiddle without subnavigation, there should be this separator. I tried it to achieve it there with
.top-bar {
background: yellow;
border-bottom: 10px solid white; /*10px only to see that the border is inside the box*/
}
but the border is not outside the yellow top-bar box, it is inside, which I do not want to have. Also it would be great to have a combined so that the 1px white space between top and sb navigation is always there.
Working demo
Your border solution was almost correct, just change the box-sizing property so the border isn't placed inside the div:
.top-bar {
background: yellow; border-bottom: 10px solid white;
box-sizing: content-box;
}
This is the default value but you included Foundation that override this value with box-sizing: border-box;.
Add outer Box Shadow to your top-bar. like This:
.top-bar{
background: yellow;
box-shadow:0 0 1px #fff;
}
You can use a white border-bottom with 1px extra height on the nav:
.top-bar { height:71px; border-bottom:1px solid #fff; }
add Z-INDEX on .menu-center .active > a:before, .menu-center .active > a:after and
ul.sub-menu {
background-color: red;
display: block;
left: 0;
border-top: 1px solid #FFF;
position: absolute;
right: 0;
text-align: center;
top: 100%;
}
http://jsfiddle.net/aytaxykf/12/
After it did work at first, I (apparently) changed something in my CSS. After hours of research I still can't figure out why the content of my <span> now won't be displayed on hover anymore...
Can anyone help me, please? Thank you!
This is a snippet of my CSS:
td.matchbool {
text-align: center;
box-shadow: inset 0px 0px 0px 10px #fff;
}
td.matchbool:hover{
background:#e2e236;
width:400px;
}
td.matchbool span{
display: none;
}
td.matchbool:hover span{
z-index: 9999;
width: 300px;
border-left: 5px solid #e2e236;
display: block;
padding: 1px;
}
And the important HTML part:
<table><tr>
<td class='matchbool'>
<span>
Some Content
</span>
</td>
</tr></table>
Note that on hover the background of the <td> does change.
You are hiding the only element in the single row, single column table.
This makes the HTML collapse to save space for other controls in the page.
That does not mean that hover function is gone. It just means that the space taken up by the span will not be "reserved" as if it is invisible.
To make it more simple: if you want the cell to take the same space as it should when hovering over it, then I suggest you re-write your CSS to preserve the size of the cell, even if its content is not visible.
One way to do so is to make the font color white, but that is a lousy way.
I have included a snippet code to convince you that the cell is there, and hover works but you need to know where to point your mouse pointer.
For that, I have added extra padding to the td and made it with a border so you know where to put the cursor. once your mouse is inside the border, the hover functionality will kick in and you will see the span
td.matchbool {
text-align: center;
box-shadow: inset 0px 0px 0px 10px #fff;
padding: 10px;
border: 1px dashed grey;
}
td.matchbool:hover {
background: #e2e236;
width: 400px;
}
td.matchbool span {
display: none;
}
td.matchbool:hover span {
z-index: 9999;
width: 300px;
border-left: 5px solid #e2e236;
display: block;
padding: 1px;
}
<table>
<tr>
<td class='matchbool'>
<span>Some Content</span>
</td>
</tr>
</table>
Your span is on display: none; this means the td has no propper size to hover over it.
Try: visibility: hidden; or opacity: 0; instead of display: none;.
visibility: visible; and opacity: 1 to make it visible.
td.matchbool {
text-align: center;
box-shadow: inset 0px 0px 0px 10px #fff;
}
td.matchbool:hover{
background:#e2e236;
width:400px;
}
td.matchbool span{
visibility: hidden;
}
td.matchbool:hover span{
z-index: 9999;
width: 300px;
border-left: 5px solid #e2e236;
visibility: visible;
padding: 1px;
}
For td.matchbool span and td.matchbool:hover span, use the visibility property instead of display. An invisible element will leave its space for the mouse to interact whereas non-displayed elements cannot be interacted with the mouse at all.
td.matchbool span
{
display: block;
}
remove display:none from it and you will see the content
Can you make round cap underlines (as in the above image) with CSS? How?
Is there a way to do this with border-bottom? border-radius produces this stylish effect instead:
EDIT: I missunderstood what hpique wated, but this should work:
#test {
font-size: 50px;
background: transparent;
border-radius: 10px;
height: 10px;
width: 255px;
box-shadow: 0 55px 0 0 #000;
font-family: sans-serif;
}
<div id="test">Hello world</div>
Basically I'm putting the text on a div, and the box shadow will be of the same size as the set height and width for that div, just play with the height/width and you should get what you want...
JSBin Demo
Screenshot from the Demo:
Yes, it’s possible. Add a block element using :after with no content and give it desired width/height like so:
h1:after {
content:"";
float:left;
background:green;
width:100%;
height:6px;
border-radius: 3px;
}
Fiddle here: https://jsfiddle.net/toqL0agq/1/
I tried doing this same thing with the accepted answer, but found I was still getting the undesired result shown in the question. You can achieve this with a psuedo class:
HTML:
<span class="kicker">Hello World</span>
CSS:
.kicker {
font-size: 1rem;
position: relative;
&:after {
content: '';
display: block;
width: 100%;
height: 6px;
border-radius: 6px;
background: #000;
position: absolute;
bottom: 0;
left: 0;
}
}
One of the tricks i just learned is instead of working with div borders try adding an :after selector to the heading like :
h1:after{
content: " ";
display: block;
width: 1.5em;
height: .2em;
background-color: #f0860c;
border-radius: 10px;
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>test</h1>
</body>
</html>
No. If you want to do this purely with HTML+CSS you would need a secondary element to sit beneath the text, and then apply curvature and background colour to that. Alternatively, and cringe-worthy, in my opinion, you could use an image.
Like youtag's answer, my solution uses pseudo-elements—but my underline only runs the length of the text and can wrap onto multiple lines (with an underline running beneath each line of text).
Basically, I manually cap the ends of the element's border with pseudo-element circles before and after the element:
h1 a {
text-decoration: none;
position: relative;
border-bottom: 15px solid;
padding-bottom:3px;
}
h1 a:hover, h1 a:focus {
border-bottom: 15px solid #eb6d32;
}
h1 a:before, h1 a:after {
content: '';
height: 15px;
width: 15px;
background-color: currentColor;
border-radius: 15px;
position: relative;
display: inline-block;
vertical-align: text-bottom;
margin-bottom: -18px;
}
h1 a:before {
left: .2ex;
margin-left: -.4ex;
}
h1 a:after {
margin-right: -.4ex;
right: .2ex;
}
I use left and right on the pseudo-elements so the ends don't stick out too far past the text.
See my codepen.
you can do that by using a div beneath the text and setting its border-radius to 2000px. i think that will be simpler
HTML:
<div class="wrapper">
<span>Hell World</span>
<div class="underline"></div>
</div>
CSS:
.underline{
height:0px;border: 3px solid black;
border-radius: 2000px;
}
.wrapper{
display:inline-block;
}
JQUERY SNIPPET:
var arbitrarynumber = 5
$('.underline').width($('.underline').parent().width()-arbitrarynumber)