I have quite the puzzling enterprise here.
I'm building a mouse which serves as a 'torch / searchlight'. All text (inline elements, buttons, you get the point) gets inverted from the usual white to black if there is a hover happening on it, the normal background is a yellow-ish vibe.
I currently have the following setup:
const _$shadow = $('.b-cursor__shadow');
const _$front = $('.b-cursor__front');
const _$back = $('.b-cursor__back');
$(document).on('mousemove', (e) => {
_$back.css({
left: e.pageX,
top: e.pageY
});
_$front.css({
left: e.pageX,
top: e.pageY
});
_$shadow.css({
left: e.pageX,
top: e.pageY
});
});
html,
body {
padding: 0;
margin: 0;
cursor: none;
background: red;
}
.test {
background: darkblue;
}
p {
color: white;
font-family: sans-serif;
font-size: 20px;
max-width: 30rem;
padding: 1rem;
margin: 1rem;
border: 1px solid white;
}
p,
span,
a {
position: relative;
z-index: 105;
}
.b-cursor__back,
.b-cursor__front,
.b-cursor__shadow {
position: fixed;
width: 8rem;
height: 8rem;
margin-left: -4rem;
margin-top: -4rem;
border-radius: 50%;
}
.b-cursor__shadow {
box-shadow: 0px 0px 10px 10px rgba(231, 232, 192, 1);
}
/* background changes */
.b-cursor__back {
z-index: 104;
background: #18173e;
clip-path: circle(50% at 50% 50%);
}
.b-cursor__front {
z-index: 106;
background: white;
clip-path: circle(50% at 50% 50%);
mix-blend-mode: difference;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis pretium pharetra ipsum, at placerat ante maximus vitae. Duis lacus urna, posuere id dapibus in, semper vitae massa. Quisque at egestas nisl. In ex elit, imperdiet eu interdum a, auctor vitae ante. Pellentesque efficitur imperdiet elementum. Integer at nibh gravida nisl sodales ornare ut quis est. Suspendisse sem odio, congue vitae felis at, tincidunt interdum purus. Morbi vitae efficitur est, non congue ante. Proin vel odio et metus sodales lobortis quis ut justo. Phasellus rhoncus eu urna vitae tristique. Suspendisse potenti. Curabitur quis quam lobortis mi laoreet lacinia. Cras non ultrices eros. Nam sed leo et tortor vestibulum cursus nec eu massa. Suspendisse potenti.</p>
<section class="b-cursor">
<div class="b-cursor__shadow"></div>
<div class="b-cursor__back"></div>
<div class="b-cursor__front"></div>
</section>
<div class="test">
<p>ja uh misschien werkt dit wel niet
<p>
</div>
(codepen)
This nearly produces the desired result, except the border-radius: 50% doesn't handle semi-nice stacking divs correctly. Pixel drama! Image for clarification:
Question: How can I remove the black border created by stacking these two elements with identical size while preserving the current effect on text?
I would simply add another element above using pseudo element to hide that small border and simplify the code by moving the container instead of each element. Also no need for the clip-path
const _$cursor = $('.b-cursor');
$(document).on('mousemove', (e) => {
_$cursor.css({
left: e.pageX,
top: e.pageY
});
});
html,
body {
padding: 0;
margin: 0;
cursor: none;
background: red;
}
.test {
background: darkblue;
}
p {
color: white;
font-family: sans-serif;
font-size: 20px;
max-width: 30rem;
padding: 1rem;
margin: 1rem;
border: 1px solid white;
}
p,
span,
a {
position: relative;
z-index: 105;
}
.b-cursor { /*no z-index here !!!*/
position: absolute;
width: 8rem;
height: 8rem;
margin-left: -4rem;
margin-top: -4rem;
}
/*the magic element*/
.b-cursor:before {
content:"";
position:absolute;
top:-1px;
left:-1px;
right:-1px;
bottom:-1px;
border:2px solid rgba(231, 232, 192, 1);
border-radius:50%;
z-index:999;
}
/**/
.b-cursor__back,
.b-cursor__front,
.b-cursor__shadow {
position:absolute;
top:0;
left:0;
width:100%;
height:100%;
border-radius: 50%;
}
.b-cursor__shadow {
box-shadow: 0px 0px 10px 10px rgba(231, 232, 192, 1);
}
/* background changes */
.b-cursor__back {
z-index: 104;
background: #18173e;
}
.b-cursor__front {
z-index: 106;
background: white;
mix-blend-mode: difference;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis pretium pharetra ipsum, at placerat ante maximus vitae. Duis lacus urna, posuere id dapibus in, semper vitae massa. Quisque at egestas nisl. In ex elit, imperdiet eu interdum a, auctor vitae ante. Pellentesque efficitur imperdiet elementum. Integer at nibh gravida nisl sodales ornare ut quis est. Suspendisse sem odio, congue vitae felis at, tincidunt interdum purus. Morbi vitae efficitur est, non congue ante. Proin vel odio et metus sodales lobortis quis ut justo. Phasellus rhoncus eu urna vitae tristique. Suspendisse potenti. Curabitur quis quam lobortis mi laoreet lacinia. Cras non ultrices eros. Nam sed leo et tortor vestibulum cursus nec eu massa. Suspendisse potenti.</p>
<section class="b-cursor">
<div class="b-cursor__back"></div>
<div class="b-cursor__front"></div>
<div class="b-cursor__shadow"></div>
</section>
<div class="test">
<p>ja uh misschien werkt dit wel niet</p>
</div>
Here is anoher idea with less of code and pure JS without jQuery:
document.onmousemove = function(e) {
document.body.style.setProperty('--mx',(e.pageX)+'px');
document.body.style.setProperty('--my',(e.pageY)+'px');
document.body.style.setProperty('--x',(e.clientX)+'px');
document.body.style.setProperty('--y',(e.clientY)+'px');
}
html {
background:red;
}
body{
padding: 1px;
margin: 0;
min-height:100vh;
cursor: none;
background:
radial-gradient(circle at var(--x) var(--y),#18173e 4rem,transparent 4rem) fixed;
}
.test {
background:
radial-gradient(circle at var(--x) var(--y),#18173e 4rem,transparent 4rem) fixed,
darkblue;
}
p {
color: white;
font-family: sans-serif;
font-size: 20px;
max-width: 30rem;
padding: 1rem;
margin: 1rem;
border: 1px solid white;
}
.b-cursor { /*no z-index here !!!*/
position: absolute;
width: 8rem;
height: 8rem;
top:var(--my);
left:var(--mx);
margin-left: -4rem;
margin-top: -4rem;
}
/*the magic element*/
.b-cursor:before{
content:"";
position:absolute;
top:-1px;
left:-1px;
right:-1px;
bottom:-1px;
border:2px solid rgba(231, 232, 192, 1);
border-radius:50%;
z-index:999;
}
.b-cursor:after {
content:"";
position:absolute;
top:0;
left:0;
right:0;
bottom:0;
box-shadow: 0px 0px 10px 10px rgba(231, 232, 192, 1);
border-radius:50%;
z-index:998;
}
/**/
.b-cursor > div {
position:absolute;
top:0;
left:0;
width:100%;
height:100%;
border-radius: 50%;
z-index: 997;
background: white;
mix-blend-mode: difference;
}
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis pretium pharetra ipsum, at placerat ante maximus vitae. Duis lacus urna, posuere id dapibus in, semper vitae massa. Quisque at egestas nisl. In ex elit, imperdiet eu interdum a, auctor vitae ante. Pellentesque efficitur imperdiet elementum. Integer at nibh gravida nisl sodales ornare ut quis est. Suspendisse sem odio, congue vitae felis at, tincidunt interdum purus. Morbi vitae efficitur est, non congue ante. Proin vel odio et metus sodales lobortis quis ut justo. Phasellus rhoncus eu urna vitae tristique. Suspendisse potenti. Curabitur quis quam lobortis mi laoreet lacinia. Cras non ultrices eros. Nam sed leo et tortor vestibulum cursus nec eu massa. Suspendisse potenti.</p>
<section class="b-cursor">
<div></div>
</section>
<div class="test">
<p>ja uh misschien werkt dit wel niet</p>
</div>
You can still optimize more if we consider another gradient that will replace the shadow and the border that we used to fix the black one:
document.onmousemove = function(e) {
document.body.style.setProperty('--mx',(e.pageX)+'px');
document.body.style.setProperty('--my',(e.pageY)+'px');
document.body.style.setProperty('--x',(e.clientX)+'px');
document.body.style.setProperty('--y',(e.clientY)+'px');
}
html {
background:red;
}
body{
padding: 1px;
margin: 0;
min-height:100vh;
cursor: none;
background:
radial-gradient(circle at var(--x) var(--y),#18173e 4rem,transparent 4rem) fixed;
}
.test {
background:
radial-gradient(circle at var(--x) var(--y),#18173e 4rem,transparent 4rem) fixed,
darkblue;
}
p {
color: white;
font-family: sans-serif;
font-size: 20px;
max-width: 30rem;
padding: 1rem;
margin: 1rem;
border: 1px solid white;
}
.b-cursor { /*no z-index here !!!*/
position: absolute;
width: 8rem;
height: 8rem;
top:var(--my);
left:var(--mx);
margin-left: -4rem;
margin-top: -4rem;
}
.b-cursor:before{
content:"";
position:absolute;
top:-10px;
left:-10px;
right:-10px;
bottom:-10px;
background:
radial-gradient(farthest-side,
transparent calc(100% - 18px),rgba(231, 232, 192, 1) calc(100% - 15px),
rgba(231, 232, 192, 1) calc(100% - 10px),transparent 100%);
border-radius:50%;
z-index:999;
}
.b-cursor:after {
content:"";
position:absolute;
top:0;
left:0;
width:100%;
height:100%;
border-radius: 50%;
z-index: 998;
background: white;
mix-blend-mode: difference;
}
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis pretium pharetra ipsum, at placerat ante maximus vitae. Duis lacus urna, posuere id dapibus in, semper vitae massa. Quisque at egestas nisl. In ex elit, imperdiet eu interdum a, auctor vitae ante. Pellentesque efficitur imperdiet elementum. Integer at nibh gravida nisl sodales ornare ut quis est. Suspendisse sem odio, congue vitae felis at, tincidunt interdum purus. Morbi vitae efficitur est, non congue ante. Proin vel odio et metus sodales lobortis quis ut justo. Phasellus rhoncus eu urna vitae tristique. Suspendisse potenti. Curabitur quis quam lobortis mi laoreet lacinia. Cras non ultrices eros. Nam sed leo et tortor vestibulum cursus nec eu massa. Suspendisse potenti.</p>
<section class="b-cursor">
</section>
<div class="test">
<p>ja uh misschien werkt dit wel niet</p>
</div>
Safari doesn't support the gradient syntax with at as detailed in this question so here is another alternative:
document.onmousemove = function(e) {
document.body.style.setProperty('--mx',(e.pageX)+'px');
document.body.style.setProperty('--my',(e.pageY)+'px');
document.body.style.setProperty('--x',(e.clientX)+'px');
document.body.style.setProperty('--y',(e.clientY)+'px');
}
html {
background:red;
}
body{
padding: 1px;
margin: 0;
min-height:100vh;
cursor: none;
background:
radial-gradient(farthest-side ,#18173e 100%,transparent 100%)
calc(var(--x) - 4rem) calc(var(--y) - 4rem)/8rem 8rem fixed no-repeat;
}
.test {
background:
radial-gradient(farthest-side ,#18173e 100%,transparent 100%)
calc(var(--x) - 4rem) calc(var(--y) - 4rem)/8rem 8rem fixed no-repeat,
darkblue;
}
p {
color: white;
font-family: sans-serif;
font-size: 20px;
max-width: 30rem;
padding: 1rem;
margin: 1rem;
border: 1px solid white;
}
.b-cursor { /*no z-index here !!!*/
position: absolute;
width: 8rem;
height: 8rem;
top:var(--my);
left:var(--mx);
margin-left: -4rem;
margin-top: -4rem;
}
.b-cursor:before{
content:"";
position:absolute;
top:-10px;
left:-10px;
right:-10px;
bottom:-10px;
background:
radial-gradient(farthest-side,
transparent calc(100% - 18px),rgba(231, 232, 192, 1) calc(100% - 15px),
rgba(231, 232, 192, 1) calc(100% - 10px),transparent 100%);
border-radius:50%;
z-index:999;
}
.b-cursor:after {
content:"";
position:absolute;
top:0;
left:0;
width:100%;
height:100%;
border-radius: 50%;
z-index: 998;
background: white;
mix-blend-mode: difference;
}
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis pretium pharetra ipsum, at placerat ante maximus vitae. Duis lacus urna, posuere id dapibus in, semper vitae massa. Quisque at egestas nisl. In ex elit, imperdiet eu interdum a, auctor vitae ante. Pellentesque efficitur imperdiet elementum. Integer at nibh gravida nisl sodales ornare ut quis est. Suspendisse sem odio, congue vitae felis at, tincidunt interdum purus. Morbi vitae efficitur est, non congue ante. Proin vel odio et metus sodales lobortis quis ut justo. Phasellus rhoncus eu urna vitae tristique. Suspendisse potenti. Curabitur quis quam lobortis mi laoreet lacinia. Cras non ultrices eros. Nam sed leo et tortor vestibulum cursus nec eu massa. Suspendisse potenti.</p>
<section class="b-cursor">
</section>
<div class="test">
<p>ja uh misschien werkt dit wel niet</p>
</div>
This might work for your needs.
const _$shadow = $('.b-cursor__shadow');
const _$front = $('.b-cursor__front');
const _$back = $('.b-cursor__back');
$(document).on('mousemove', (e) => {
_$back.css({
left: e.pageX,
top: e.pageY
});
_$front.css({
left: e.pageX,
top: e.pageY
});
_$shadow.css({
left: e.pageX,
top: e.pageY
});
});
html, body {
padding: 0;
margin: 0;
cursor: none;
background: red;
}
.test {
background: darkblue;
}
p {
color: white;
font-family: sans-serif;
font-size: 20px;
max-width: 30rem;
padding: 1rem;
margin: 1rem;
border: 1px solid white;
}
p, span, a {
position: relative;
z-index: 105;
}
.b-cursor__shadow2, .b-cursor__back, .b-cursor__front, .b-cursor__shadow {
position: fixed;
width: 8rem;
height: 8rem;
margin-left: -4rem;
margin-top: -4rem;
border-radius: 50%;
}
.b-cursor__shadow {
box-shadow: 0px 0px 10px 20px rgba(231, 232, 192, 1);
z-index: 107;
height: 8rem;
width: 8rem;
}
.b-cursor__shadow2 {
background: radial-gradient(circle at center, #18173e 100%, #18173e 25%);
z-index: 109;
height: 8rem;
width: 8rem;
background-color: transparent;
}
/* background changes */
.b-cursor__back {
z-index: 104;
height: 8rem;
width: 8rem;
background: radial-gradient(circle at center, #18173e 100%, #18173e 25%);
background-size: 100% 100%;
background-position: 50% 50%;
}
.b-cursor__back:after {
width: 7rem;
height: 7rem;
content: '';
border-radius: 50%;
background: transparent;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
box-shadow: 0px 0px 0px 1rem #18173e;
transition: all 0.3s linear;
mix-blend-mode: normal;
}
.b-cursor__front {
z-index: 106;
height: 8rem;
width: 8rem;
background: white;
background: radial-gradient(circle at center, #ffffff 100%, #ffffff 25%);
background-position: 50% 50%;
mix-blend-mode: difference;
}
.b-cursor__front:after {
width: 7rem;
height: 7rem;
content: '';
border-radius: 50%;
background: transparent;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
box-shadow: 0px 0px 0px 1rem #ffffff;
transition: all 0.3s linear;
mix-blend-mode: normal;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis pretium pharetra ipsum, at placerat ante maximus vitae. Duis lacus urna, posuere id dapibus in, semper vitae massa. Quisque at egestas nisl. In ex elit, imperdiet eu interdum a, auctor vitae
ante. Pellentesque efficitur imperdiet elementum. Integer at nibh gravida nisl sodales ornare ut quis est. Suspendisse sem odio, congue vitae felis at, tincidunt interdum purus. Morbi vitae efficitur est, non congue ante. Proin vel odio et metus sodales
lobortis quis ut justo. Phasellus rhoncus eu urna vitae tristique. Suspendisse potenti. Curabitur quis quam lobortis mi laoreet lacinia. Cras non ultrices eros. Nam sed leo et tortor vestibulum cursus nec eu massa. Suspendisse potenti.</p>
<section class="b-cursor">
<div class="b-cursor__shadow"></div>
<div class="b-cursor__back"></div>
<div class="b-cursor__front"></div>
<div class="cursor_now"></div>
</section>
<div class="test">
<p>ja uh misschien werkt dit wel niet
<p>
</div>
try adding
filter:blur(1.4px); /* or anywhere between 0.7px to 1.9px */
to the outer circle or inner circles. in your CSS
See this codepen:
https://codepen.io/jossnaz/pen/BMwpjR
HTML
<br>
<div class="" style="
">
<span class="" style="">
Lorem ipsum nunc hendrerit imperdiet aliquet class massa suspendisse libero, enim condimentum himenaeos hendrerit torquent pellentesque per euismod, velit molestie eleifend per rhoncus feugiat fermentum proin.
Suspendisse porttitor diam egestas curabitur malesuada netus enim bibendum, lacinia integer suscipit sem taciti ut nisi nunc, vivamus neque tempor dictum pretium condimentum litora.
Litora primis justo commodo posuere id nullam lacus tempor sociosqu cubilia auctor, nulla maecenas cubilia lacus quisque malesuada risus viverra mattis senectus porttitor ligula a fringilla dui elit nulla hendrerit in condimentum tortor.
Sollicitudin cursus augue semper sodales vitae pel
</span>
</div>
CSS
div{
max-width: 500px;
width: 500px;
background-color: cornflowerblue;
}
blockquote, span{
padding-left: 50px;
background-color: salmon;
position: relative;
&:before {
content: '“';
position: absolute;
left: 0;
top: 6px;
font-size: 4.8rem;
font-style: italic;
line-height: 0.75;
text-align: left;
color: green;
font-weight: normal;
}
}
Result is overflowing element:
What do I want? that the salmon red inline element stops overflowing its parent. I want to keep using :before and I want to keep it an inline element.
You can try text-indent instead of padding and make the pseudo element relative to the parent container:
div {
max-width: 500px;
width: 500px;
background-color: cornflowerblue;
text-indent:50px;
position: relative;
}
span {
background-color: salmon;
}
span:before {
content: '“';
position:absolute;
left: 0;
top: 6px;
font-size: 4.8rem;
font-style: italic;
line-height: 0.75;
text-align: left;
color: green;
font-weight: normal;
text-indent:0;
}
<br>
<div>
<span>
Lorem ipsum nunc hendrerit imperdiet aliquet class massa suspendisse libero, enim condimentum himenaeos hendrerit torquent pellentesque per euismod, velit molestie eleifend per rhoncus feugiat fermentum proin.
Suspendisse porttitor diam egestas curabitur malesuada netus enim bibendum, lacinia integer suscipit sem taciti ut nisi nunc, vivamus neque tempor dictum pretium condimentum litora.
Litora primis justo commodo posuere id nullam lacus tempor sociosqu cubilia auctor, nulla maecenas cubilia lacus quisque malesuada risus viverra mattis senectus porttitor ligula a fringilla dui elit nulla hendrerit in condimentum tortor.
Sollicitudin cursus augue semper sodales vitae pel
</span>
</div>
Setting padding-left: 50px; on the <blockquote>/<span>-tag is causing the issue. If you can use the ::after pseudo-element, an option would be to create the 50px gab before the text with the ::before pseudo-element and then position the quotation mark with the ::after pseudo-element. For example:
blockquote,
span {
background-color: salmon;
position: relative;
&::before {
content: "";
display: block;
float: left;
width: 50px;
height: 1px;
}
&::after {
content: "“";
position: absolute;
left: -50px;
top: 6px;
font-size: 4.8rem;
font-style: italic;
line-height: 0.75;
text-align: left;
color: green;
font-weight: normal;
}
}
As I'm not sure about all your requirements another way would be using text-indent to offset the text.
I'd also advise to specifically set display: inline; for your <blockquote>/<span> selector styling as the default for a <blockquote> is display: block;.
If you set the span tag to display: block; you should be good.
blockquote, span{
display: block;
padding-left: 50px;
background-color: salmon;
position: relative;
...
}
When hovering a div, I want to display a full-page div with a background image and text on top. The hovered div should then stay on top of everything. This works so far, with the only exception that I can’t figure out how to make the overflow text scrollable.
Any ideas how to make this work?
JSFiddle
html:
<div class="container">
<div class="title">Show content</div>
<div class="content">
<div class="background"><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/d/d1/Mount_Everest_as_seen_from_Drukair2_PLW_edit.jpg/800px-Mount_Everest_as_seen_from_Drukair2_PLW_edit.jpg"></div>
<div class="infocontainer">
<div class="info">
Lorem Ipsum...
</div>
</div>
</div>
</div>
css:
.container {
position: fixed;
left: 50%;
top: 10%;
}
.content {
display: none;
}
.title:hover + .content {
display: block;
width: 100%;
height: 100%;
}
.title:hover {
position: relative;
z-index: 3;
}
.background {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 1;
}
.infocontainer {
position: fixed;
top: 0;
left: 0;
height: 100vh;
width: 100vw;
z-index: 2;
overflow: scroll;
}
.info {
height: 100%;
padding-right: 70%;
}
UPDATE: When I put .info on top with z-index, it becomes scrollable but I get a flickering effect because .title:hover gets over overridden. See: jsfiddle.net/dm41eo5z
You can't hover to make something appear over the whole page, because as soon as it is over the whole page you are no longer hovering the original thing (that's why your example above flickers) and it will disappear. If you use a JavaScript Event listener (such as onMouseOver) you could make it appear by adding a class with a "display: absolute" or "block" or whatever you want it to do. Then it would stay open until you use some other event listener (such as "Click" on an X) to close the element.
The .content div disappears as soon as you stop hovering the .title. To prevent that, continue showing .content while you hover it:
.title:hover + .content, .content:hover {
display: block;
width: 100%;
height: 100%;
}
Note: to hide .content, the user will have to move the cursor out of the screen.
Demo:
.container {
position: fixed;
left: 50%;
top: 10%;
}
.content {
display: none;
}
.title:hover + .content, .content:hover {
display: block;
width: 100%;
height: 100%;
}
.title:hover {
position: relative;
z-index: 3;
}
.background {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 1;
}
.infocontainer {
position: fixed;
top: 0;
left: 0;
height: 100vh;
width: 100vw;
z-index: 2;
overflow: scroll;
}
.info {
height: 100%;
padding-right: 70%;
}
<div class="container">
<div class="title">Show content</div>
<div class="content">
<div class="background"><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/d/d1/Mount_Everest_as_seen_from_Drukair2_PLW_edit.jpg/800px-Mount_Everest_as_seen_from_Drukair2_PLW_edit.jpg"></div>
<div class="infocontainer">
<div class="info">
Pellentesque venenatis tempor ultrices. Nunc maximus erat vel tellus vestibulum, id auctor justo tristique. Ut volutpat eu tellus ut vulputate. Cras id finibus massa. Quisque neque lacus, pretium sed luctus in, semper ut quam. Donec elementum volutpat elementum. Morbi nibh nunc, scelerisque congue turpis nec, lacinia venenatis tortor.
Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Morbi quis magna urna. Etiam tincidunt velit lacus, ac blandit ex pretium nec. Nulla semper erat ut tortor luctus, sit amet suscipit felis ullamcorper. Etiam laoreet, mauris ut volutpat pellentesque, velit neque euismod sem, in condimentum mauris orci nec nibh. Vivamus ac sem et turpis pellentesque volutpat ac ac ligula. Phasellus feugiat dapibus maximus. Donec eros felis, suscipit eu neque quis, sagittis faucibus ipsum. Nam auctor molestie quam nec tristique. Aliquam dolor velit, condimentum in vehicula ut, pretium sed leo. Proin sit amet quam nunc.
Integer eu orci quam. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam sed felis vitae justo faucibus blandit at a dolor. Curabitur id nisi nec elit aliquam convallis nec eu est. Aenean viverra id diam ac accumsan. Praesent tempor, magna eget molestie sodales, neque libero pretium magna, id euismod justo erat at est. Phasellus ultrices metus et massa varius, at rutrum augue pretium. Mauris ultrices felis et magna luctus sodales. Curabitur sodales pellentesque ante auctor molestie. Donec sed massa scelerisque elit auctor lacinia ac vel risus. In eleifend vulputate sapien a tristique. Nam eleifend rutrum metus.
</div>
</div>
</div>
</div>
Thank you for your replies so far but they don't exactly achieve what I'm looking for. Is there maybe a way to keep .title on top but specifically target .info for scrolling?
I have a page with a container holding a div and within that there are two more divs, side by side. As the view gets smaller, I want the div on the right to disappear and the div on the left to take up the entire parent div.
I've been having problems implementing this. When I tried an absolute position on the div it does expand to the fill width of the parent but it also pops out of the parent (sort of). When I tried a transition as was suggested elsewhere that just didn't do anything useful. Then I tried z-index and changing the margin to a - but that didn't seem to work either. A nudge in the right direction would be great.
.box {
background-color: #F28B20;
border-radius: 5px;
height: auto;
margin-bottom: 150px;
width: 80%;
display: inline-block;
box-shadow: 10px 10px 5px #888888;
background-image: url("../Images/logo_faded_background.png");
background-repeat: no-repeat;
background-position: center;
background-size: cover;
}
.sidebar {
float: right;
width: 29%;
margin-left: 0;
margin-right: 3%;
text-align: left;
color: #171581;
}
.whiteBoxHalf {
float: left;
width: 54%;
background: rgba(255, 255, 255, 0.6);
border-color: #000000;
border-width: 1px;
border-style: solid;
margin-left: 3%;
margin-right: 5%;
margin-bottom: 15px;
padding: 15px;
}
#media (max-width: 1019px) {
.header {
font-size: small;
}
.header li a {
padding: 20px 10px;
}
.sidebar {
display: none;
}
.whiteBoxHalf {
width: 95%;
}
}
<div class="box">
<div class="row">
<div class="whiteBoxHalf"></div>
<div class="sidebar"></div>
</div>
</div>
You could do something like this, using display:table-row and overflow:hidden. Fiddle
.box, .whiteBoxHalf {
min-width: 300px;
}
.box {
background-color: #F28B20;
border-radius: 5px;
margin-bottom: 150px;
width: 80%;
box-shadow: 10px 10px 5px #888888;
overflow: hidden;
}
.row {
display: table-row;
}
.sidebar {
display: table-cell;
margin-left: 0;
margin-right: 3%;
text-align: left;
color: #171581;
background: green;
}
.whiteBoxHalf {
display: table-cell;
background: rgba(255, 255, 255, 0.6);
border-color: #000000;
border-width: 1px;
border-style: solid;
margin-left: 3%;
margin-right: 5%;
margin-bottom: 15px;
padding: 15px;
width: 60%;
box-sizing: border-box;
}
<div class="box">
<div class="row">
<div class="whiteBoxHalf">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc scelerisque consequat porta. Praesent a odio lorem. Donec scelerisque mauris nibh, ut finibus ipsum semper eget. Praesent dapibus ante et diam condimentum tempus. Vivamus nisl nisl, finibus in neque non, maximus pretium est. In hac habitasse platea dictumst. Quisque dapibus faucibus nisl. Donec vel tincidunt est, blandit imperdiet eros. Integer blandit finibus vestibulum. Mauris vel ante quis neque congue ullamcorper in vitae quam. Morbi pretium elementum ante, eget suscipit enim elementum sit amet. Integer ornare, risus euismod tincidunt lobortis, arcu ligula lobortis lectus, in egestas est quam sed massa. Etiam porta, neque eget cursus gravida, sem nisl convallis erat, ut pharetra felis ex ac odio. Nunc aliquam enim quis nulla auctor suscipit. Maecenas id enim eu purus hendrerit interdum nec id est.
</p>
</div>
<div class="sidebar">
<p>
Praesent sem tortor, volutpat at volutpat lobortis, luctus eget tellus. Nulla facilisi. Vestibulum sodales mauris non sapien volutpat hendrerit. Curabitur quis vestibulum nulla. Pellentesque justo risus, vestibulum ac ligula eu, interdum lacinia lorem. Sed ornare libero nunc. Morbi vitae fringilla ipsum. Nulla tempor viverra odio eget ultrices. Etiam fermentum erat viverra ex rhoncus, eget lacinia arcu semper. Aliquam lobortis massa ac tincidunt vestibulum. Nam ex ligula, molestie vel luctus eu, pretium quis lacus. Cras tincidunt nisl nec viverra condimentum.
</p>
</div>
</div>
</div>
If this is not the desired behaviour, you will probably want to use media queries to target specific screen resolutions, and show/hide the sidebar accordingly. Example:
.sidebar {
display: none;
}
#media (min-width: 800px) {
.sidebar {
display: block;
}
}
I'm wondering if there is a way to fix an element inside a visible area of the div as I've described in the image below.
Here is an example of my problem (jsfiddle):
HTML:
<div id="header" ></div>
<div id="menu">
<a id="menu-collapse">collapse</a>
<div id="some-block"></div>
</div>
<div id="content"></div>
CSS:
#menu {
position: fixed;
top: 70px;
bottom: 0px;
left: 0;
width: 200px;
background-color: rgb(100, 134, 27);
overflow-x: auto;
}
#header {
position: fixed;
top: 0;
left: 0;
right: 0;
height: 70px;
background-color: rgb(27, 48, 0);
min-width: 540px;
}
#menu-collapse{
}
#content {
position: fixed;
top: 70px;
right: 0px;
bottom: 0px;
left: 200px;
background-color: rgb(27, 118, 0);
}
#some-block{
position:absolute;
display:inline;
background-color: yellow;
width: 200px;
height: 2500px;
}
I need to have #menu-collapse in the middle of the visible area of #menu, while scrolling #menu.
Use position: fixed; in your CSS class definition. This will position the div relative to the browser window instead of the document itself, allowing it to maintain its position as you scroll.
There is now way to fix the element inside a div area if you use position:fixed; property then it will automatically fix it relatively to the browser.
Its a default feature.
On #menu-collapse, you need to add position: fixed; to get it to stay in the same position. And, you need top: 50%; in order to have it in the middle of #menu, and z-index: 1; in order for it to display on top of #some-block.
JSFiddle
HTML:
<div id="header" ></div>
<div id="menu">
<a id="menu-collapse">collapse</a>
<div id="some-block">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque vitae tortor fermentum, pellentesque lorem vitae, ullamcorper urna. Sed in imperdiet nisl, porta porta sem. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum euismod sapien a imperdiet placerat. Ut a maximus felis. Mauris porta purus id mi feugiat ultrices. Etiam mattis leo id hendrerit sagittis. Vivamus dapibus blandit ligula varius semper.</p>
<p>Ut nisl neque, efficitur non risus vitae, luctus cursus dui. Pellentesque aliquam varius cursus. Fusce lobortis, sapien at dictum porta, erat eros rhoncus metus, sit amet finibus libero quam nec nisl. Phasellus tempus ante vitae rutrum ultricies. Fusce sit amet hendrerit quam, egestas gravida risus. Donec eget tristique enim, ac lacinia nunc. Nam pulvinar, nunc et scelerisque ornare, est augue mollis sem, vel vulputate arcu mi et eros. Proin varius arcu vitae nisi porttitor, sit amet finibus eros congue. In dapibus tincidunt tortor. Sed at sem id tortor molestie finibus eget ut sem. In semper feugiat elementum. Morbi enim mauris, venenatis non felis ac, interdum euismod sapien. Praesent semper ante vel mauris auctor, ut commodo ipsum malesuada. Morbi hendrerit tortor sapien. Nulla in hendrerit dui, eu accumsan est</p>
</div>
</div>
<div id="content">
</div>
CSS:
#menu {
position: fixed;
top: 70px;
bottom: 0px;
left: 0;
width: 200px;
background-color: rgb(100, 134, 27);
overflow-x: auto;
}
#header {
position: fixed;
top: 0;
left: 0;
right: 0;
height: 70px;
background-color: rgb(27, 48, 0);
min-width: 540px;
}
#menu-collapse{
position: fixed;
top: 50%;
z-index: 1;
background: red;
}
#content {
position: fixed;
top: 70px;
right: 0px;
bottom: 0px;
left: 200px;
background-color: rgb(27, 118, 0);
}
#some-block{
position:absolute;
display:inline;
background-color: yellow;
width: 200px;
height: 2500px;
}