So, what I want to do is position 2 divs side by side. When I hover over the left div, I want it to expand its width to the full page, while covering the right div. When I hover over the right div, I want to expand the width -100% so it covers the left div. I got them to both sit side by side as well as for the left div to transition to the right 100% and the right div to expand to the left -100%. The main problem I am having is that it will show 100% of the div on either side that it expands to. Here is my code.
As of now I am only using CSS properties, but let me know if some javascript will work better.
HTML code
<div id="parent">
<div id="wide">Wide (rest of width)</div>
<div id="narrow">Narrow (200px)</div>
</div>
CSS Code
#narrow {
margin-top: 100px;
padding-top: 20px;
float: right;
width: calc(100% - 50%);
height: 400px;
background: lightblue;
transition: width 2s;
z-index: -1000;
}
#narrow:hover {
width: 100%;
}
#wide {
margin-top: 100px;
padding-top: 20px;
float: left;
width: calc(100% - 50%);
background: lightgreen;
height: 400px;
transition: width 2s;
position: absolute;
z-index: 1000;
}
#wide:hover {
width: -100%;
}
Here we go! position: fixed and z-index!
#narrow {
padding-top: 20px;
float: right;
width: 50%;
height: 400px;
background: lightblue;
transition: width 2s;
z-index: 0;
position:fixed;
right:0;
}
#narrow:hover {
width: 100%;
z-index: 1;
}
#wide {
padding-top: 20px;
width: 50%;
background: lightgreen;
height: 400px;
transition: width 2s;
position:fixed;
left:0;
z-index: 0;
}
#wide:hover {
width: 100%;
z-index: 1;
}
<div id="parent">
<div id="wide">Wide (rest of width)</div>
<div id="narrow">Narrow (200px)</div>
</div>
Another solution: position both divs absolute, both with a 0 z-index. with one of the having a right: 0; Then, set the width and z-index on hover.
<div id="parent">
<div id="wide">Wide (rest of width)</div>
<div id="narrow">Narrow (200px)</div>
</div>
#narrow {
margin-top: 100px;
padding-top: 20px;
Float: right;
width: 50%;
height: 400px;
background: lightblue;
transition: width 2s;
Z-index: 0;
Position: absolute; right: 0;
}
#narrow:hover {
width: 100%;
Z-index: 1;
}
#wide {
margin-top: 100px;
padding-top: 20px;
float: left;
width: 50%;
background: lightgreen;
height: 400px;
transition: width 2s;
position: absolute;
Z-index: 0;
}
#wide:hover {
width: 100%;
Z-index: 1;
}
I added some of the common elements to a panel class. Setting your parent as relative gives the child panels a common starting point. Setting the panels as absolute will allow you to position them on the left and right of the window. Adding a z-index of 2 to the panel that is being hovered over brings it over the non-hovered panel.
<div id="parent">
<div class="panel" id="wide">Wide (rest of width)</div>
<div class="panel" id="narrow">Narrow (200px)</div>
</div>
#parent {
position:relative;
width:100%;
height:400px;
overflow:hidden;
}
.panel {
margin-top:100px;
padding-top:20px;
height:100%;
width:50%;
z-index:1;
}
#narrow {
position:absolute;
right:0;
top:0;
background: lightblue;
transition: width 2s;
}
#narrow:hover {
width: 100%;
z-index:2;
transition: width 2s;
}
#wide {
position:absolute;
left:0;
top:0;
background: lightgreen;
transition: width 2s;
}
#wide:hover {
width:100%;
z-index:2;
transition: width 2s;
}
Related
When my mobile menu opens, I would love the rest of the visible background (other than the menu itself) to 'dim.' (Both my pages and menu background are very white in general).
There is a plugin that offers this functionality but in trying to keep the website light, am trying to see if this is possible with just some lines of code?
Googling for quite a while came up with nothing other than the app which is a surprise... maybe I searched the wrong keywords?
Any ideas?
Here is my full code (not my original code, can link various parts to their respective Authors).
/*Change hamburger menu colour*/
span.mobile_menu_bar:before{
color:#D7AF39;
}
/*Remove shading of top menu to match sub menu*/
.et_mobile_menu .menu-item-has-children a {
background-color:#FFFFFF;
}
/** Divi Space slide in mobile edits**/
#mobile_menu { display: block !important; min-height: 100vh; top: 0; border-top: none; padding-top: 80px; z-index: 9998; }
.mobile_nav.closed #mobile_menu {
transform: rotateY(90deg); -webkit-transform: rotateY(90deg);
transform-origin: right; -webkit-transform-origin: right;
background: #fff; transition: .8s ease-in-out !important; }
.mobile_nav.opened #mobile_menu {
transform: rotateY(0deg); -webkit-transform: rotateY(0deg);
transform-origin: right; -webkit-transform-origin: right;
background: #fff; transition: .8s ease-in-out; }
.mobile_nav.opened .mobile_menu_bar:before {
content: "\4d"; color: #D7AF39; }
.et_mobile_menu li a, .et_mobile_menu .menu-item-has-children>a {
font-weight: 600;
font-family: open sans;
font-size: large;
}
#media(max-width: 980px) {
.et_header_style_split .mobile_menu_bar, .et_header_style_left .mobile_menu_bar { z-index: 9999; }
#main-header .container.clearfix.et_menu_container { width: 100%; }
.logo_container { padding-left: 30px; }
#et-top-navigation { padding-right: 30px; }
}
#media(min-width: 341px) {
#mobile_menu { width: 340px; margin-left: calc(100% - 340px); }
}
One way of doing this is to assert a blanket div over the entire page, beginning just below the menu bar, then setting that div's opacity to the desired level of dimming.
I have thrown together a very simple proof of concept. Hover the dummy Menu button to observe the effect. Take it onwards from there.
body {
--menu-height: 50px;
}
#page {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: yellow;
}
#menu_bar {
position: absolute;
width: 100%;
height: var( --menu-height);
background-color: blue;
}
#menu_item {
position: absolute;
top: 10px;
left: 10px;
width: 50px;
height: 30px;
background-color: white;
line-height: 30px;
text-align: center;
}
#menu_item:hover:after {
content: '';
position: fixed;
top: var( --menu-height);
left: 0;
height: 100vh;
width: 100vw;
background-color: black;
opacity: 0.5;
/* Ensure z-index is higher than page's content/data items */
z-index: 2
}
#data {
position: absolute;
top: 25%;
left: 25%;
width: 50%;
height: 50%;
background-color: white;
border: 2px solid black;
padding: 10px;
z-index: 1;
}
#text {
display: inline-block;
width: 100%;
text-align: center;
font-weight: bold;
}
<div id="page">
<div id="menu_bar">
<div id="menu_item">Menu</div>
</div>
<div id="data">
<span id="text">Hover the "Menu" button...</span><br><br> Lorem ipsum dolor etc
</div>
</div>
Currently, I am building a style guide and I have a question about the transition of an element. Imagine you have a container with two elements besides each other. Both have 50% width. The left element should always be visible, but the right element slides from the right into its 50% width. How can I achieve something like this? I am a bit overwhelmed with the top, bottom, left, right, position:absolute properties.
The html would look like this:
<div class="module-container">
<div class="first-element">
<div class="second-element">
</div>
and the css like this:
.module-container {
display: flex;
}
.first-element {
width: 50%;
}
.second-element {
width: 50%;
}
which properties does the second Element need in the first place? And which should I add via JavaScript after pressing, for instance, a button?
try using jQuery and transitions
$('#btn').click(function() {
$('.secondElement').toggleClass("slide");
});
.moduleContainer {
display: flex;
height: 100px;
overflow: hidden;
}
.firstElement {
position: relative;
width: 50%;
border: 1px solid #000;
}
.secondElement {
position: relative;
width: 50%;
border: 1px solid #000;
left: 100%;
transition: left 1s;
}
.secondElement.slide {
left: 0;
}
#btn {
display: block;
margin-top: 20px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="moduleContainer">
<div class="firstElement"></div>
<div class="secondElement"></div>
</div>
<button id="btn">Click Here</button>
.moduleContainer {
display: flex;
height:100px;
}
.moduleContainer > * {
border:1px solid red;
box-sizing:border-box;
overflow:hidden;
}
.firstElement {
height: 100%;
width: 50%;
}
.secondElement {
height: 100%;
width: 0%;
transition:width 0.3s ease;
}
.moduleContainer:hover .secondElement {
width:50%;
}
<div class="moduleContainer">
<div class="firstElement"></div>
<div class="secondElement"></div>
</div>
I have achieved it with pure CSS.I think it's good to have a bar, so the user can hover it and expand.I hope it will help you.
.moduleContainer{ display: flex;flex-flow: row nowrap; }
.firstElement{ background-color:blueviolet;flex:1;height:100px;position:relative;max-width: 50%; }
.secondElement{ background-color:aqua;height:100px;flex:1;max-width:1%;position:relative;transition:1s ease;left:48%; }
.secondElement:hover{ background-color: chartreuse;left:0px;max-width:50%; }
<div class="moduleContainer">
<div class="firstElement">First Element</div>
<div class="secondElement"></div>
</div>
You can use a negative value for margin-left of the .second-element.
.module-container {
display: flex;
height: 50px;
overflow: hidden;
}
.module-container:hover .second-element {
right: 0;
}
.first-element {
flex: 0 0 50%;
background: #f90;
}
.second-element {
flex: 0 0 50%;
background: #0f9;
right: -50%;
transition: all .6s ease;
position: relative;
}
<div class="module-container">
<div class="first-element"></div>
<div class="second-element"></div>
</div>
Or you can use position: absolute and animate the left property
.module-container {
position: relative;
height: 50px;
overflow: hidden;
}
.module-container:hover .second-element {
right: 0;
}
.first-element {
width: 50%;
background: #f90;
position: absolute;
left: 0;
top: 0;
bottom: 0;
}
.second-element {
background: #0f9;
width: 50%;
position: absolute;
right: -50%;
top: 0;
bottom: 0;
transition: all .6s ease;
}
<div class="module-container">
<div class="first-element"></div>
<div class="second-element"></div>
</div>
So, I have a box of content that has a title and a description, which are positioned at the bottom of the div. Initially, the description is hidden. What I'm trying to do is when you hover over the div, the title should move up and reveal the description, which has a dynamic height.
Here's what I have now: https://codepen.io/tayanderson/pen/qJrmXE
The problem is that it wouldn't display correctly if the description was 1 line or 3 lines. The title div should move up depending on the size of the description div.
Here's an example of what I'm trying to do
HTML
<div class="grid-item" style="background-image: url(https://source.unsplash.com/WLUHO9A_xik/1600x900);">
<div class="title">Title</div>
<div class="desc">Lorem ipsum dolor sit amet, consectetur</div>
</div>
CSS
.grid-item {
height:300px;
background-size: cover;
width:300px;
position: relative;
overflow: hidden;
color: #fff;
.title {
position: absolute;
left: 0;
bottom: 0;
width: 100%;
padding: 0 20px;
}
.desc {
position: absolute;
bottom: 0;
transform: translateY(100%);
padding: 5px 20px;
}
&:hover .title {
bottom: 30%;
}
&:hover .desc {
transform: translateY(0%);
}
}
It this what you meant?
.inner, .inner:hover .grid-item.inner {
-webkit-transition:all linear 0.2s;
transition:all linear 0.2s;
}
.inner {
background: #afa;
width: 300px;
overflow: hidden;
position: absolute;
}
.grid-item:hover .inner{
margin-top: -100px;
}
<a class="grid-item" href="{{ .Permalink }}" style="background-image: url(images/recipes/{{.Params.image}})">
<div class="inner"><h3 class="title is-3">{{.Title}}</h3></div>
<div class="content"><p class="grid-item-blurb">{{.Description}}</p></div>
</a>
Note that I reversed the order of the inner elements.
.body {
background: #aaf;
height: 100px;
width: 300px;
overflow: hidden;
}
.inner, .content {
transition: all linear 0.2s;
}
.content {
height: 100%;
}
.inner {
background: #afa;
transform: translateY(100%);
top: 100%;
}
.body:hover .inner,
.body:hover .content {
transform: translateY(-100%);
}
<div class="body">
<div class="content">
Blue is a viewport (<body>, visible part of a page), which content should be compressed upon green slide-in
</div>
<div class="inner">Green is variable-height text which slides in on viewport hover</div>
</div>
Kindly check this fiddle and tell me how I can keep the second <div> at the same position even when the height of first <div> is increased. If the first <div> overlaps the second it's fine. I just don't want the second div to move at all.
https://jsfiddle.net/7v9dud8u/
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<!-- page content -->
<div id="main-div" style="background-color: #ae2477; width:300px; height: 100px;" onclick="expandHeight();">
Main
</div>
<br/>
<div id="sub-div" style="background-color: #FF0000; width:300px; height: 100px;" onclick="reduceHeight();">
Sub
</div>
<script>
function expandHeight(){
$("#main-div").animate({height:"200px"},400);
}
function reduceHeight(){
$("#main-div").animate({height:"100px"},400);
}
</script>
Thanks.
Using CSS, you can use position: absolute;
<style>
#main-div {
position: absolute;
}
</style>
Doing this will allow the two divs to move freely around the browser window without affecting one another.
If all you want is to keep the second div from moving, then some simple css will do. Just add this to the second div's style attribute:
position:absolute; top:130px;
Try similar answer:
http://fiddle.jshell.net/MvzFC/24/
CSS:
.userWrap {
position: relative;
display: inline-block;
width: 300px;
height: 50px;
overflow: visible;
z-index: 1;
}
.userWrap:hover {
z-index: 2;
}
.user {
position: absolute;
display: inline-block;
width: 300px;
height: 50px;
margin-bottom: 5px;
background: #fff;
transition: width 0.3s, height 0.3s;
}
.user:hover {
width: 350px;
height: 200px;
background: #eee;
transition: width 0.3s ease 0.5s, height 0.3s ease 0.5s;
}
.user img {
float: left;
}
.user .name, .skills {
margin-left: 5px;
}
.user .name {
font-size: 21px;
font-weight: bold;
}
I know this is a frequently asked question and I have looked at many related questions/answers, still I am not able to get it to work. I want to dim the entire screen except for certain portions. in one case the portion that should not be dimmed is the region between two concentric squares. I want a solution that uses only javascript and css. It should not use any 3rd party library like jquery. I am not worried about old browsers. As long as it works on latest browsers I am okay with whatever css3 it needs. Some other restrictions I have - I am using absolute positioning in my app. All elements are absolutely positioned.
EDIT: thanks for helping me with you answers. I think I should have mentioned I want to dim the screen in response to an event like user clicking on a button. That event dims the screen except for certain areas that I want to highlight. When user clicks on the button again, the screen is restored. When the screen is dimmed user should not be able to do any interaction with DOM elements in the dimmed regions.
This is my js fiddle: http://jsfiddle.net/z1Lj7h90/
HTML:
<div id="main">
<div id="outer"></div>
<div id="inner"></div>
<button id="mybutton">Highlight</button>
<div id="darkness"></div>
</div>
CSS:
#mybutton
{
position: absolute; top: 450px; left:100px; z-index:10;
}
#main {
position: relative;
z-index:10;
width:500px;
height:500px;
overflow:hidden;
}
#darkness
{
z-index:5;
width:100%;
height:100%;
position:absolute;
left:0;
top:0;
-webkit-transition: all 1s ease;
-moz-transition: all 1s ease;
-ms-transition: all 1s ease;
-o-transition: all 1s ease;
transition: all 1s ease;
}
.fadein
{
background:rgba(0,0,0,0.75);
}
#outer
{
position:absolute;
top:10px;
left:10px;
width:400px;
height:400px;
border:2px solid red;
}
#inner
{
position:absolute;
top:40px;
left:40px;
width:340px;
height:340px;
border:2px solid blue;
}
JS:
var button = document.getElementById("mybutton");
button.addEventListener("click", toggle);
var ff = false;
function toggle()
{
var darkness = document.getElementById("darkness");
darkness.classList.toggle("fadein");
if (ff)
{
dice.style.zIndex = originalZIndex;
button.innerText = "Highlight";
}
else
{
dice.style.zIndex = 10;
button.innerText = "Restore";
}
ff = !ff
}
You can do it with CSS.
html, body {
height: 100%;
}
body {
background-color: #999999;
}
#outer{
position: absolute;
width: 200px;
height: 200px;
top: 50%;
left: 50%;
margin-left: -50px;
margin-top: -50px;
background-color: #44c;
}
#inner{
position: relative;
width: 100px;
height: 100px;
top: 50%;
left: 50%;
margin-left: -50px;
margin-top: -50px;
background-color: #ffffff;
}
<body>
<div id="outer">
<div id="inner"></div>
</div>
</body>
a solution in css whit five div..
I assume that the internal square is #q5 and you have a spacebetween of 20px; all other measures can be calculated.
<div id="q1" class="obscure"> </div>
<div id="q2" class="obscure"> </div>
<div id="q3" class="obscure"> </div>
<div id="q4" class="obscure"> </div>
<div id="q5" class="obscure"> </div>
now, q1 is at north, and q3 is at south; q2 and q4 lateral
#q5 {
width: 100px;
height: 100px;
left: 50px;
top: 120px;
}
#q4{
left:0px;
width: 30px; /* #q5.left - spacebetween; ex: spacebetween=20 */
top: 0px; bottom: 0px;
}
#q2{
left: 170px; /* #q5.width + 2*spacebetween + q5.width; */
right: 0px;
top: 0px; bottom: 0px;
}
#q1{
left: 30px; /* width of q4*/
top: 0px;
height: 100px; /*q5.top - spacebettween*/
width: 140px; /* q5.width + 2*spacebetween */
}
#q3{
left: 30px; /* width of q4*/
width: 140px; /* q5.width + 2*spacebetween*/
bottom: 0px;
top: 240px; /*q5.height + q5.top + spacebettween */
}
see http://jsfiddle.net/alemarch/czpksfdd/ for detail.
A more generic solution is simple implementing in js the formulas in my css comment