I am trying to do the following:
- add a left background, 50% width (color red).
- add a right background, 50% width (color green).
- add a centered layer for content (had to wrap it with another layer).
(wrapper blue, content white).
I'll change the backgrounds later, but I need a 50-50 split background with a different background image on each side and with a centered layer covering on top of that.
Any improvments or suggestions? :)
Code below:
/* By forcing `height: 100%` in the html
and body tag will make sure there are no white areas
in vicinity (this is optional though, use it only if you need it */
html, body {height: 100%;}
/* -------------------------------------------- */
div
{
border: 1px solid black;
}
div#wd_bg_left
{
position: absolute;
top: 0;
bottom: 0;
left: 0;
width: 50%;
background-color: red;
z-index: 1;
}
div#wd_bg_right
{
position: absolute;
top: 0;
bottom: 0;
right: 0;
width: 50%;
background-color: green;
z-index: 1;
}
div#wd_wrapper_1
{
position: absolute;
top: 0;
bottom: 0;
left: 5%;
right: 5%;
background-color: blue;
z-index: 2;
}
div#wd_wrapper_2
{
margin: 5px auto 5px auto;
min-height: 99%;
background-color: white;
width: 1000px;
}
<div id="wd_bg_left"></div>
<div id="wd_bg_right"></div>
<div id="wd_wrapper_1">
<div id="wd_wrapper_2"></div>
</div>
A simple linear gradient would suffice if color is all you need.
html, body {
height:100%;
margin:0;
}
body {
min-height:100%;
background:linear-gradient(to right, red, red 50%, blue 50%);
}
.container {
height:100%;
width:50%; /* your width here */
margin:auto;
border:1px solid green;
}
<div class="container"></div>
Or a couple of pseudo-elements if you want background images
html,
body {
height: 100%;
margin: 0;
}
body {
min-height: 100%;
overflow: hidden;
}
.container {
height: 100%;
width: 50%;
/* your width here */
margin: auto;
border: 1px solid green;
position: relative;
}
.container::before,
.container::after {
content: '';
position: absolute;
top: 0;
height: 100%;
width: 50vw;
z-index: -1;
}
.container::before {
/* rightside */
background: #f00; /* use bg image here */
left: 50%;
}
.container::after {
/* leftside */
background: #0f0; /* use bg image here */
right: 50%;
}
<div class="container"></div>
Try this:
#wd-bg-right {
width:50vw;
Height: 100vh; /* just for demonstrating*/
Background-color: red;
Right: 0;
}
wd
-bg-right {
width:50vw;
Height: 100vh; /* just for demonstrating*/
Background-color: red;
Right: 0;
}
Hope it helps you, cheers!
Related
I want to do this:
So far I got this:
.header {
position: fixed;
top: 0;
left: 0;
width: 100%;
background: #red;
z-index: 10000;
height: 10px;
overflow: visible;
}
.header:after {
content: '';
position: relative;
top: 100%;
display: block;
margin: 0 auto;
background: red;
width: 50px;
height: 25px;
border-radius: 10px 10px 50px 50px;
}
<div class="header"></div>
Codepen.
I can't get the top radius to go outwards the half circle like in the image.
How to do this with CSS?
You cannot make a negative radius on a border.
There is the possibility to make an SVG path or radial gradient... I made a new div as circle and radial gradient on pseudo-elements. It's not perfect, but it will possibly show you the direction to solution :)
.header {
position: fixed;
top: 0;
left: 0;
width: 100%;
background: red;
z-index: 10000;
height: 10px;
overflow: visible;
}
.header-circ {
position: relative;
top: 100%;
display: block;
margin: 0 auto;
background: red;
width: 500px;
height: 250px;
border-radius: 10px 10px 250px 250px;
}
.header-circ::before, .header-circ::after {
content: "";
position: absolute;
width: 100px;
height: 100px;
z-index: -1;
}
.header-circ::before {
left:-94px;
background: radial-gradient(circle at bottom left, white 0%,white 75%,red 75%);
}
.header-circ::after {
right:-94px;
background: radial-gradient(circle at bottom right, white 0%,white 75%,red 75%);
}
<div class="header"></div>
<div class="header-circ"></div>
Here's a quote from W3C on how transform establishes a new containing block:
For elements whose layout is governed by the CSS box model, any value
other than none for the transform property also causes the element to
establish a containing block for all descendants. Its padding box will
be used to layout for all of its absolute-position descendants,
fixed-position descendants, and descendant fixed background
attachments.
And here's their example code:
<style>
#container {
width: 300px;
height: 200px;
border: 5px dashed black;
padding: 5px;
overflow: scroll;
}
#bloat {
height: 1000px;
}
#child {
right: 0;
bottom: 0;
width: 10%;
height: 10%;
background: green;
}
</style>
<div id="container" style="transform:translateX(5px);">
<div id="bloat"></div>
<div id="child" style="position:fixed;"></div>
</div>
My problem is that the W3C example doesn't work as expected in Firefox. The green div doesn't stay fixed when I scroll — try running the snippet yourself.
My own example has exactly the same problem (the red divs don't stay fixed when I scroll):
.inner-container {
width: 72vw;
height: 72vh;
transform: translateZ(0);
overflow-y: scroll;
box-sizing: border-box;
border: 2px dashed black;
}
.fixed-top {
position: fixed;
height: 20%;
width: 100%;
left: 0;
top: 0;
background: red;
opacity: 0.5;
}
.fixed-bottom {
position: fixed;
height: 20%;
width: 100%;
left: 0;
bottom: 0;
background: red;
opacity: 0.5;
}
.content {
height: 88rem;
background: repeating-linear-gradient(0deg, #fff, #fff 1%, #e2e2e2 1%, #e2e2e2 3%);
}
<div class="inner-container">
<div class="fixed-top"></div>
<div class="content"></div>
<div class="fixed-bottom"></div>
</div>
I don't get it. According to the spec, descendants with position: fixed should not move if their parent has a transform applied, but they do! What gives?
The weird thing is that I can make it work by adding a second container and applying the transform to that instead (now my red divs don't move when I scroll, which is what I want):
.outer-container {
width: 72vw;
height: 72vh;
transform: translateZ(0);
}
.inner-container {
width: 100%;
height: 100%;
overflow-y: scroll;
box-sizing: border-box;
border: 2px dashed black;
}
.fixed-top {
position: fixed;
height: 20%;
width: 100%;
left: 0;
top: 0;
background: red;
opacity: 0.5;
}
.fixed-bottom {
position: fixed;
height: 20%;
width: 100%;
left: 0;
bottom: 0;
background: red;
opacity: 0.5;
}
.content {
height: 88rem;
background: repeating-linear-gradient(0deg, #fff, #fff 1%, #e2e2e2 1%, #e2e2e2 3%);
}
<div class="outer-container">
<div class="inner-container">
<div class="fixed-top"></div>
<div class="content"></div>
<div class="fixed-bottom"></div>
</div>
</div>
But I have no idea why I need to use two containers to make this work. According to the spec I should just be able to apply a transform to the first container, which is so much cleaner. Is this actually possible with one container? I don't have time to learn Kubernetes at the moment.
Here's a CodePen if you want to play with it.
Use position: sticky
There is a fantastic demo at https://developer.mozilla.org/en-US/docs/Web/CSS/position
.inner-container {
width: 72vw;
height: 72vh;
overflow-y: scroll;
box-sizing: border-box;
border: 2px dashed black;
position: relative;
}
.fixed-top {
position: sticky;
height: 20%;
width: 100%;
left: 0;
top: 0;
background: red;
opacity: 0.5;
}
.fixed-bottom {
position: sticky;
height: 20%;
width: 100%;
left: 0;
bottom: 0;
background: red;
opacity: 0.5;
}
.content {
height: 88rem;
background: repeating-linear-gradient( 0deg, #fff, #fff 1%, #e2e2e2 1%, #e2e2e2 3%);
}
<div class="inner-container">
<div class="fixed-top"></div>
<div class="content"></div>
<div class="fixed-bottom"></div>
</div>
As demonstrated here: http://codepen.io/anon/pen/rVPqeL
I am using 3 simple divs and I want to obtain an effect of a "global" scrollbar that has to go over the header.
The html is very basic
<div class="container">
<div class="header">
</div>
<div class="content">
</div>
</div>
and here's the css:
.container {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background-color: gray;
overflow-y: scroll;
}
.header {
position: fixed;
width: 100%;
height: 50px;
background-color: red;
}
.content {
margin-top: 50px;
min-height: 2500px;
background-color: blue;
}
The scrollbar keeps going under the header div. What am I doing wrong?
The below code does the trick
http://codepen.io/anon/pen/XbOxgp
.container {
background-color: gray;
overflow-y: scroll;
}
.header {
position: fixed;
width: 100%;
height: 50px;
background-color: red;
z-index: 2;
}
.content {
z-index: 1;
width: 100%;
position: absolute;
top: 60px;
min-height: 2500px;
background-color: blue;
}
If I understand correctly you want the scrollbar always ontop. To do so change your css to the following
html{
overflow-y: scroll;
}
.container {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background-color: gray;
}
Scroll on html will allow the entire page to have scroll while keeping header static and remove scroll from container.
.container {
margin-top:50px; /* create room for header*/
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background-color: gray;
overflow-y: scroll;
}
.header {
margin-top:-50px; /* move up by 50px*/
position: fixed;
width: 100%;
height: 50px;
background-color: red;
}
fixed positioned elements have "no width and height".
Hope it helps :)
EDIT: See this pen: This
Ps. I guess you also want to remove the margin of .content
i tried with replacing position:fixed with position:sticky and added top:0 and it worked well for me, no more overlapping vertical scrollbar.
.header {
position: sticky;
top: 0;
width: 100%;
height: 50px;
background-color: red;
}
Remove overflow-y: scroll; from your .container
put the overflow-y: scroll; inside the body element:
body {
overflow-y: scroll;
}
.container {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background-color: gray;
}
I have a fixed, 100% height menu on the left and I need to create a shadow effect on its right side that would disappear after while.
See the figure that illustrates this.
How to create this effect?
Here is a fiddle: http://jsfiddle.net/52VtD/7787/
HTML:
<nav id="main-menu">
<h1>Hello</h1>
A
B
C
D
</nav>
CSS:
#main-menu {
width: 320px;
height: 100%;
top: 0;
z-index: 1000;
position: fixed;
background-color: #b4bac1;
}
You can achieve this with CSS3: box-shadow and transform.
In the example below the box-shadow is applied to a pseudo element of .menuContainer which sits underneath the .menu element and is rotated by 1° using CSS3s rotate() transform property.
html,body { /* This is only here to allow the menu to stretch to 100% */
height: 100%;
margin: 0;
}
.menuContainer {
position: relative;
height: 100%;
width: 100px;
}
.menuContainer::after {
content: "";
position: absolute;
z-index: 1;
top: -10px;
bottom: 0;
left: -7px;
background: rgba(0,0,0,0.3);
box-shadow: 10px 0 10px 0 rgba(0,0,0,0.3);
width: 100px;
transform: rotate(1deg);
}
.menu {
background: #f00;
height: 100%;
width: 100px;
position: relative;
z-index: 2;
}
<div class="menuContainer">
<div class="menu"></div>
</div>
JSFiddle
You could fake it with a pseudo-element rather than using a box-shadow as follows
JSfiddle Demo
CSS
#main-menu {
width: 50px;
height: 100%;
top: 0;
z-index: 1000;
position: fixed;
background-color: pink;
}
#main-menu:after {
content:"";
position: absolute;
top:0;
left:100%;
height:100%;
width:5%;
background: linear-gradient(to bottom, rgba(0,0,0,0.15) 0%,rgba(0,0,0,0) 100%);
}
From this jsfiddle: http://jsfiddle.net/vbaWK/3/, how can I make the fixed overlayed black rectangle appear over the blue rectangle, where they both overlap while scrolling the body. There is an added rule though, whereever there is no overlap, it should be over everything, including the overlay. Thanks.
html:
<div class="black"></div>
<div class="blue"></div>
<div class="green"></div>
<div class="overlay"></div>
css:
.black
{
background: black;
width: 100px;
height: 100px;
position: fixed;
top: 0;
left: 0;
z-index: -1;
}
.blue
{
background: blue;
width: 100px;
height: 2000px;
z-index: 4;
position: relative;
}
.green
{
background: green;
width:100px;
height: 2000px;
z-index: 2;
position: relative;
}
.overlay
{
background: white;
opacity: 0.8;
height: 100%;
width: 100%;
position: fixed;
top: 0;
left: 0;
z-index:3;
}
Thanks for any help.
Sorry I forgot to mention that when the blue rectangle is not overlapping, it should be over the overlay. There is a conflict.
Give the black rectangle the highest possible z-index, something like this:
.black
{
background: black;
width: 100px;
height: 100px;
position: fixed;
top: 0;
left: 0;
z-index: 999;
}