:hover command won't work with a general sibling selector - html

I am trying to have a display that changes when hovering a div class. The idea is to have one div disappear when hovering another. I have tried using general sibling selectors to make the display change from inline to none. The CSS is as follows:
#Inicio {width: 100%;
height: 100%;
background-color: yellow;
position: absolute;
display: inline;
}
.buttons:hover ~ #Inicio {display: none;}
.buttons {width: 80%;
margin-left: auto;
margin-right: auto;
position: static;
margin-left: 10%;
font-size: 22px;
border-top: 1px solid white;
padding-top: 20px;
padding-bottom: 20px; }
.buttons:hover {font-size: 24px;
transition: all .5s ;}
And the HTML:
<div id="wrapper">
<div id="menubar">
<div id="menu">
<h1>Menu</h1>
</div>
<div class="buttons">
Inicio
</div>
<div class="buttons">
Productos
</div>
<div class="buttons">
Localizacion
</div>
<div class="buttons">
El equipo
</div>
<div class="buttons">
Ideas
</div>
<div class="buttons">
La pagina
</div>
</div>
<div id="content">
<div id="inicio"></div>
</div>
</div>

First of all, your id names doesn't match, its case sensitive, you #inicio and #Inicio are completely two different things..
And as I commented, the issue is that you cannot pop out of the element using CSS means you cannot select the parent element and than go ahead and select the parents sibling element, so you need to change your DOM, you are trying to select an element which is adjacent to the buttons parent element and not the button itself, so the best you can do is this
.buttons:hover ~ #content > #inicio {
display: none;
}
Demo
Altered DOM, you need to bring the elements on the same level, if #inicio is nested, it's fine, but to select it's parent, bring the elements adjacent to each other on the same level so that all are direct child to an element having an id of #wrapper
<div id="wrapper">
<div id="menu">
<h1>Menu</h1>
</div>
<div class="buttons">
Inicio
</div>
<div class="buttons">
Productos
</div>
<div id="content">
<div id="inicio">Disappear this</div>
</div>
</div>
As #enguerranws commented, I thought to put a compatibility table as well,
Credits - Support Table

Maybe because it's #inicio, not #Inicio ?
Then you need to change your DOM. You have to put #inicio in .buttons div. Or :
.buttons:hover ~ #Inicio
Won't work.
<div id="wrapper">
<div id="menubar">
<div id="menu">
<h1>Menu</h1>
</div>
<div class="buttons">
<span>Inicio</span>
<div id="inicio"></div>
</div>
</div>
<div id="content">
</div>
</div>
You should use that structure. Btw, I added a span to wrap your text, as it's not valid to put text directly in block element (here: div).

Related

Style same class in another element with + selector differently

There's a website that has code like I've put on the following jsfiddle:
https://jsfiddle.net/roa8k7js/
The site has very complex css styling, over 10,000 lines. In switching this site over to WordPress, the styling was all maintained and everything was put into a theme.
As such, every <section> is now inside a wrapper. Because of this, the styling does not function correctly.
The <section> padding is determined by a number of rules, including at least one major rule that uses the + selector.
The rule looks like this:
#layout1 .option-b:not(.custom-bg-image) + .option-b:not(.custom-bg-img)
{
padding-top: 0;
}
Since the wrapper was added, the + selector won't correctly identify the pattern:
<div class="wrapper1">
<section class="option-b">This is some text</section>
</div>
<div class="wrapper1">
<section class="option-b">This is some more text - there shouldn't be any padding on top of this one</section>
</div>
section {
border: 1px solid black;
}
#layout1 .container1>section {
padding-top: 2em;
padding-bottom: 2em;
}
#layout1 .option-b:not(.custom-bg-image)+.option-b:not(.custom-bg-img) {
padding-top: 0;
}
#layout1 .container1>.wrapper1>section {
padding-top: 2em;
padding-bottom: 2em;
}
<div id="layout1">
<div class="container1">
<section class="option-b">This is some text</section>
<section class="option-b">This is some more text - notice how there isn't a padding top on this one</section>
</div>
</div>
<div id="layout1">
<div class="container1">
<div class="wrapper1">
<section class="option-b">This is some text</section>
</div>
<div class="wrapper1">
<section class="option-b">This is some more text - there shouldn't be any padding on top of this one</section>
</div>
</div>
</div>
I am trying to determine if there is an easy way to adjust the CSS so that it can correctly identify a <section> in the next wrapper to set a value for top-padding
Also note, there is no way to remove the wrappers, and all the sections are already styled correctly, without them.
The sibling selector can not be used with elements that don't share the same hierarchy. See here for info:
The adjacent sibling combinator (+) separates two selectors and matches the second element only if it immediately follows the first element, and both are children of the same parent element.
Here are a few workarounds (assuming there are only 2 <section> per wrapper as your example depicts):
Give it another class that overrides the padding (by far the easiest)
Target only the nth element
Javascript
1 ) Just add a "no padding" class to any element you don't want padding added to:
section {
border: 1px solid black;
}
.wrapper1 > section {
padding-top: 2em;
padding-bottom: 2em;
}
.wrapper1 > section.pt-0 {
padding-top: 0;
}
<div id="layout1">
<div class="container1">
<div class="wrapper1">
<section class="option-b">This is some text</section>
</div>
<div class="wrapper1">
<section class="option-b pt-0">This is some more text - there shouldn't be any padding on top of this one</section>
</div>
<div class="wrapper1">
<section class="option-b pt-0">This is some more text - there shouldn't be any padding on top of this one</section>
</div>
<div class="wrapper1">
<section class="option-b">This is some text</section>
</div>
</div>
</div>
2 ) Target the nth element
section {
border: 1px solid black;
}
.wrapper1 > section {
padding-top: 2em;
padding-bottom: 2em;
}
.wrapper1:not(:first-child) > section {
padding-top: 0;
}
<div id="layout1">
<div class="container1">
<div class="wrapper1">
<section class="option-b">This is some text</section>
</div>
<div class="wrapper1">
<section class="option-b">This is some more text - there shouldn't be any padding on top of this one</section>
</div>
</div>
</div>
3) of course javascript:
const elems = document.querySelectorAll('#layout1 .wrapper1 > .option-b:not(.custom-bg-image)');
if (elems.length > 1) elems[1].style.paddingTop = 0;
section {
border: 1px solid black;
}
.wrapper1 > section {
padding-top: 2em;
padding-bottom: 2em;
}
<div id="layout1">
<div class="container1">
<div class="wrapper1">
<section class="option-b">This is some text</section>
</div>
<div class="wrapper1">
<section class="option-b">This is some more text - there shouldn't be any padding on top of this one</section>
</div>
</div>
</div>

Is it possible to select a specific <div> when another <div> which is not a parent is :hover in CSS3 only?

Is it possible to select a specific <div> when another <div> which is not a parent is :hover?
All that in HTML5/CSS3 only, without JS.
<section>
<div id=first></div>
</section>
<section>
<div class=second></div>
</section>
As an example, i want <div class=second> to show when <div id=first> is :hover.
This is possible, but only if the two elements have the same parent.
Using the element1 ~ element2 selector. For example:
HTML:
<div class="first">
<!-- content -->
</div>
<span class="example-element"></span>
<div class="second">
<!-- content -->
</div>
CSS:
.first:hover ~ .second {
/* styles */
}
If you need to select an element that does not have the same parent, you need to use javascript.
this is two ways to achive that, with click adding an a tag or with hover that its a little tricky
.second{
display:none;
}
#second:target {
display:block;
}
#first a{
text-decoration:none;
color:black;
}
.disp1:hover + .disp2{
display:block;
}
.disp2{
display:none;
}
<section>
<div id="first"><a href="#second" >div one</a></div>
</section>
<section>
<div id="second" class="second">div two</div>
</section>
<div class="disp1">first div</div>
<div class="disp1 disp2">second div</div>

Apply css if element exists using CSS selector

I have header in such a way that I would like to hide my image and span's in my pheader div if "banner" exists. Is there a quick way to do this using CSS selectors? Below is the HTML code.
<header>
<div id="banner">
<!-- main content -->
</div>
<div class="pheader">
<div class="user-panel">
<div id="hey-user" class="d2c-user-panel">
<img src="../images/defaultHeadshot_lg.png" class="userimg">
<!-- Hide This -->
<span class="caret" id="down-arrow"></span>
<span class="hey-user d2c-header-username"><b>Hello</span>
</div>
</header>
Yes.
#banner + .pheader img,
#banner + .pheader span {
display:none;
}
This selector only applies if .pheader is directly after #banner.
You might find this Tutsplus article useful:
The 30 CSS Selectors You Must Memorize
.pheader {
padding: 1em;
background: lightblue;
}
#banner + .pheader img,
#banner + .pheader span {
display: none;
}
<header>
<div id="banner">
<!-- main content -->
</div>
<div class="pheader">
<div class="user-panel">
<div id="hey-user" class="d2c-user-panel">
<img src="http://lorempixel.com/output/animals-q-c-100-100-3.jpg" class="userimg" />
<span class="caret" id="down-arrow">Arrow</span>
<span class="hey-user d2c-header-username"><b>Hello</b></span>
</div>
</div>
</div>
</header>
It's probably worth noting that I've had to close a few divs in your original HTML structure which were missing from your code.
Try this. Link https://jsfiddle.net/7bv53xqd/
#banner + .pheader img {
display: none;
}
#banner + .pheader span {
display: none;
}

Force text over 2 lines with CSS

I'd like to have all surnames on the second line AND maintain the exact same width for test div. What is the best way of achieving this with CSS?
HTML:
<div class="test">
<h1>Mike S</h1>
</div>
<div class="test">
<h1>Mike Smith</h1>
</div>
<div class="test">
<h1>Mike Smiths</h1>
</div>
CSS:
.test {width:25%;float:left;background:red;margin-right:20px}
h1 {text-align:center}
http://jsfiddle.net/zcg9k5xh/
Update your code with this:
.test {width:25%;float:left;background:red;margin-right:20px}
h1 {text-align:center}
h1 span{display: block;}
<div class="test">
<h1>Mike <span>S</span></h1>
</div>
<div class="test">
<h1>Mike <span>Smith</span></h1>
</div>
<div class="test">
<h1>Mike <span>Smiths</span></h1>
</div>
You can also do this by using css, update above css
h1 span{display: list-item;list-style:none;}
jsfiddle with this
http://jsfiddle.net/zcg9k5xh/2/
Given that it seems you are willing to change your HTML, I would recommend you simply add <br> after the first name, instead of wrapping the last name in any other tags. This would be deemed best practice.
The HTML <br> Element (or HTML Line Break Element) produces a line
break in text
This will give more semantic HTML- without the need to adjust native element styling, or clutter your DOM with uneccessary nodes.
.test {
width: 25%;
float: left;
background: red;
margin-right: 20px
}
h1 {
text-align: center
}
<div class="test">
<h1>Mike<br>S</h1>
</div>
<div class="test">
<h1>Mike<br>Smith</h1>
</div>
<div class="test">
<h1>Mike<br>Smiths</h1>
</div>
Use the word-spacing attribute to the child tag:
.test {
width: 25%;
float: left;
background: red;
margin-right: 20px
}
h1 {
background-color: blue;
word-spacing: 100px;
}
<div class="test">
<h1>Mike S</h1>
</div>
<div class="test">
<h1>Mike Smith</h1>
</div>
<div class="test">
<h1>Mike Smiths</h1>
</div>
I don't see what you are asking, it seems like the jsfiddle is what you are asking here.
But you can always set width to 100% so it cover for the text, if you want all that text in the same div then put it all under one Div tag.
Is this what you want?
.test {width:25%;float:left;background:red;margin-right:20px}
h1 {text-align:center}
<div class="test">
<h1>Mike</h1>
<h1>S</h1>
</div>
<div class="test">
<h1>Mike</h1>
<h1>Smith</h1>
</div>
<div class="test">
<h1>Mike</h1>
<h1>Smiths</h1>
</div>

Horizontal scrollbar if the content of a <div> won't fit the width

I am using an absolute div so I can overlap one div from another. The div that overlaps is the absolute one (.content). However, if the overlapped div (.left) doesn't fit the screen, a horizontal scroll bar doesn't appear of course. How can I make the horizontal scroll bar automatically appear if its contents doesn't fit the given width? Here is the css:
.left {
width: 70%;
height:100%;
float:left;
overflow-x:auto;
}
.content {
position: absolute;
width: 70%;
height: 100%;
margin-top: 0px;
margin-bottom: 0px;
margin-right: 0px;
margin-left: 130px;
background-color: #2b3e50;
border-left-width:5px;
border-left-style:solid;
border-left-color:#153450;
padding-left: 20px;
}
Please help me figure this out.
EDIT
Here is the div structure:
<div class="topbar">
<div class="fill">
<div class="container">
Home
<ul class="nav">
<li> One </li>
<li> Two </li>
<li> Three </li>
<li> Four </li>
</ul>
<p align="right">Log-out </p>
</div>
</div>
</div>
<div id="loader" class="left" style="border-right-width:15px;">
</div>
<div class="container">
<div class="content">
<div class="row">
<div class="span14">
#content
</div>
</div>
</div>
</div>
<script>
$("#loader").html('<object data="#routes.Tags.map(false)" />');
</script>
EDIT
I surrounded the left div with a parent div.
<div class="parent">
<div id="loader" class="left" style="border-right-width:15px;">
</div>
</div>
<div class="container">
<div class="content">
<div class="row">
<div class="span14">
#content
</div>
</div>
</div>
</div>
With the following css for parent.
.parent {
width: 100%;
position: relative;
}
But still doesn't show a scrollbar.
In your html left is not child of content you can't make it scroll.
If you have parent > child then just use position: relative on parent block.
Here is example http://jsfiddle.net/4swN9/