html5/css3 DIV on DIVs layout issue - html

i have div area which is devided in to 4 equal parts, like the one atached.
now i need another div to be placed at the bottom area as an overlay to the above div. Imagine it like a text scroll area on the bottom side of the TV and the TV screen is constructed by 4 divs.
I am able to create the 5 divs. now the issue is that the 5th div(scroll area) is not going above the bottom edge of the 2 lower divs (3 and 4). I also had put z-index also but failed
can anybody share a sample for styling this.

You can solve it this way:
HTML:
<div class="area"></div>
<div class="area"></div>
<div class="area"></div>
<div class="area"></div>
<div class="overlay"></div>​
CSS:
.area{
float: left;
width: 49%;
height: 300px;
border: 1px solid black;
}
.overlay{
width: 200px;
height: 100px;
background-color: blue;
clear: both;
position: absolute;
bottom: 30px;
margin: -100px;
left: 50%;
}
​
Please note that I have used hard coded example values. The actual values depends on which context the markup is in.

Without your code it's hard to figure what's not working.
If I understand what you want this is what I would have done:
<div class="container">
<div class="block1"></div>
<div class="block2"></div>
<div class="block3"></div>
<div class="block4"></div>
<div class="overlay"></div>
</div>
css:
.container {
position: relative;
width: 600px; /* use the size you want */
height: 400px;
}
.container div {
position: absolute;
width: 50%;
height: 50%;
}
.container .block1 { top: 0; left: 0; background: pink; }
.container .block2 { top: 50%; left: 0; background: red; }
.container .block3 { top: 0; left: 50%; background: green; }
.container .block4 { top: 50%; left: 50%; background: blue; }
.container .overlay {
position: absolute;
width: 80%;
height: 100px;
left: 10%;
bottom: 30px; /* distance from the bottom */
z-index: 1;
background: yellow;
}

Related

How to do div after div in page order

I'm beginner in CSS, I can't put div after
For example :
I want to put red div after green div as the image shows:
but I want to make divs sticking to the edges of the screen .
What I tried :
.green {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 50%;
background-color: green;
margin-bottom: 5px;
}
.red {
position: absolute;
left: 0;
width: 100%;
height: 50%;
background-color: red;
}
<div class="green">
</div>
<div class="red">
</div>
Just declare top + bottom. To span the entire width you could also use: left + righ: 0.
To shorten the code you can also use the inset-property with a 3 value syntax:
inset: [top] [left/right] [bottom]
body {
margin: 0;
}
.green {
position: absolute;
inset: 0 0 50%;
background-color: green;
}
.red {
position: absolute;
inset: 50% 0 0;
background-color: red;
}
<div class="green">
</div>
<div class="red">
</div>
If you just write this code with relative positions I think will do the work. But following your example (where you set the position as absolute) I let you the following code. I don't know if it is what do you expect, if it isn't, please com back with more details.
.green {
position: absolute;
width: 100%;
top: 0;
left: 0;
width: 100%;
height: 50vh;
background-color: green;
margin-bottom: 5px;
}
.red {
position: absolute;
top:0;
left: 0;
width: 100%;
height: 50vh;
background-color: red;
transform: translateY(100%);
}
<div class="green">
</div>
<div class="red">
</div>
You can use vh (viewport width) units to set height relative to the viewport.
body {
padding: 0;
margin: 0;
}
.green {
width: 100%;
height: 50vh;
background-color: green;
}
.red {
width: 100%;
height: 50vh;
background-color: red;
}
<div class="green">
</div>
<div class="red">
</div>
It is happening because red div is overlapping your green div
so just change your code into this:
.green{
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 50%;
background-color: green;
margin-bottom: 5px;
}
.red {
position: absolute;
top: 50%;
left: 0;
width: 100%;
height: 50%;
background-color: red;
}
<div class="green"></div>
<div class="red"></div>
The big question is, what do you mean by
i want to put red div after green div
Your browser reads its given html-code from left to right and from top to bottom, until you define something different in your CSS. This means "after" can also mean an alignment of your red div on the right edge of your green div.
But all you need is basically the snippet down below.
If you want them to be align centered (like shown in your screenshot), just add "margin: 0 auto" and you good to go.
I personally can only recommend testing positioning, as a beginner, with absolute values like "px" instead of relative values like percentage. This makes it much easier to understand what's happening.
.green {
background-color: green;
height: 100px;
width: 100px;
// margin: 0 auto;
}
.red {
background-color: red;
height: 100px;
width: 100px;
// margin: 0 auto;
}
<div class="green">
</div>
<div class="red">
</div>
! of Course, if you run the code you have to remove the comment signs !

HTML DIV Stacking

I working a layout that changes the behavior of z-index.
Is this possible?
The yellow box is a dropdown menu. It should be inside the Red box.
Pretty much anything is possible with CSS3. However the element inside div 1 would need to be separate for this to work. If it's inside div 1 it will drag div 1 around with it. You'll get much more flexibility if the side div is on it's own
But for your specific example you would need something like:
HTML:
<div class="top"></div>
<div class="bottom"></div>
<div class="side"></div>
CSS:
.top {
width: 90%;
margin-left: 10%;
height: 200px;
height: 250px;
background: red;
}
.bottom {
width: 90%;
height: 200px;
height: 250px;
margin-left: 5%;
background: grey;
margin-top: -150px;
}
.side {
width: 20%;
height: 200px;
height: 250px;
margin-left: 78%;
background: yellow;
margin-top: -300px;
}
Working CodePen is here too: https://codepen.io/WebDevelopWolf/pen/mBLqxm
Not sure why this works, but it may be helpful for you:
#div1, #div2{
width: 100%;
height: 400px;
}
#div1{
background-color: red;
position: relative;
}
#div2{
background-color: green;
}
#div2{
margin-left: 50px;
margin-top: -300px;
position: relative;
}
#div1 > div{
background-color: yellow;
position: absolute;
width: 200px;
height: 200px;
right: 0;
top: 50px;
z-index: 2;
}
.as-console-wrapper{ display: none !important;}
<div id="div1">
DIV 1
<div>INSIDE DIV 1</div>
</div>
<div id="div2">
DIV 2
</div>
Here is all you need
div {
height: 100px;
width: 100px;
background: #ccc;
position: absolute;
top: 0;
left: 0;
}
.div1{
background: #f00;
}
.div2{
top: 30px;
}
.div_child{
background: #3a2525;
left: auto;
right: 0;
width: 50px;
z-index: 1;
}
<div class="div1">
1
<div class="div_child">
child
</div>
</div>
<div class="div2">
2
</div>

Set a leaning separation between two divs

i'm currently trying to create a landing page for a mobile website, and I want two main divs with an image on it. (In my example there are colors). Here is my HTML :
<ion-content>
<div class="upperblock"></div>
<div class="downblock"></div>
</ion-content>
My CSS :
.upperblock{
width: 100%;
height: 50%;
background-color: #2ec95c;
}
.downblock{
width: 100%;
height: 50%;
background: #000;
}
I would like to have a "leaning" separation, something like that (the screenshot of my result, and the red line is where I'd like the separation to be ):
Thank you in advance for any help, couldn't find anything about this !
.container {
position: relative;
overflow: hidden;
background-color: #2ec95c;
}
.upperblock {
width: 100%;
height: 200px;
background-color: #2ec95c;
color: #000;
}
.downblock {
width: 100%;
height: 200px;
background-color: #000;
color: #FFF;
}
.block {
transform: skewY(-6deg);
padding: 50px;
margin: -5% 0;
}
.block * {
transform: skewY(6deg);
}
<div class="container">
<div class="block upperblock">
<h2>content</h2>
</div>
<div class="block downblock">
<h2>content</h2>
</div>
</div>
You may want to add another div down below each div (I think :after should do the trick here) and then turn it with transform: rotate(15deg). This will turn the div.
Important note: Content in the turned box will be also turned. So you may want to seperate the content and background div.
Other way would be to create a svg image. SVG will scale perfectly and the code will look way cleaner ;)
You can also use pseudo-elements to achieve that
body {
margin: 0;
width: 100vw;
height: 100vh;
}
.upperblock {
width: 100%;
height: 50%;
background-color: #2ec95c;
position: relative;
}
.downblock {
width: 100%;
height: 50%;
background: #000;
position: relative;
}
.downblock::before {
content: "";
display: block;
width: 100%;
height: 100%;
background: inherit;
position: absolute;
top: 0;
transform: skewY(-5deg) translateY(-65%);
}
<div class="upperblock"></div>
<div class="downblock"></div>
You can append a block to the upperblock div using CSS and rotate that using transform. You'll need to play with the heights depending on your content but this will expand and be responsive.
.outer {
overflow: hidden;
}
.upperblock{
width: 100%;
min-height: 40vh;
background-color: #2ec95c;
position: relative;
}
.upperblock::after {
content: "";
display: block;
height: 120px;
background: #2ec95c;
width: calc(100% + 50px);
position: absolute;
bottom: -60px;
transform: rotate(7deg);
}
.downblock{
width: 100%;
min-height: 60vh;
background: #000;
}
<div class="outer">
<div class="upperblock"></div>
<div class="downblock"></div>
</div>

How to keep relative sized child elements within the border of the parent element?

I have a kind of "range display", where I use elements to display the current position within a range. See the example https://jsfiddle.net/juwxdb5m/ or the following code.
HTML:
<h1>Range display with fixed sizes (works correctly)</h1>
<div class="my-fixed-frame">
<div class="my-fixed-chart">
<div class="my-fixed-point" style="bottom:0%;left:0%;"></div>
<div class="my-fixed-point" style="bottom:50%;left:50%;"></div>
<div class="my-fixed-point" style="bottom:100%;left:100%;"></div>
</div>
</div>
<h1>Range display with relative sizes (works incorrectly)</h1>
<div class="my-relative-frame">
<div class="my-relative-chart">
<div class="my-relative-point" style="bottom:0%;left:0%;"></div>
<div class="my-relative-point" style="bottom:50%;left:50%;"></div>
<div class="my-relative-point" style="bottom:100%;left:100%;"></div>
</div>
</div>
CSS:
.my-fixed-frame {
background-color: gray;
height: 90px;
position: relative;
width: 160px;
}
.my-fixed-chart {
background-color: silver;
display: inline-block;
bottom: 8px; left: 8px; right: 8px; top: 8px;
margin: auto;
position: absolute;
}
.my-fixed-point {
background-color: lime;
height: 16px;
margin-bottom: -8px;
margin-left: -8px;
position: absolute;
width: 16px;
}
.my-relative-frame {
background-color: gray;
height: 90px;
position: relative;
width: 160px;
}
.my-relative-chart {
background-color: silver;
display: inline-block;
bottom: 25%; left: 25%; right: 25%; top: 25%;
margin: auto;
position: absolute;
}
.my-relative-point {
background-color: lime;
height: 50%;
margin-bottom: -25%;
margin-left: -25%;
position: absolute;
width: 50%;
}
When I use fixed sizes, I can implement the design as desired. The "point" elements are within the parent element, respectively within its frame.
But I didn't found a solution, when I use relative sizes for the child elements.
Maybe this is what you want:
https://jsfiddle.net/xoq95xaa/
The main changes are that I took the green squares out of the inner container (which is what you kind of did using negative margins in the first version), removed any margins, inserted a forth element (reacting to your comment), changed the size to 25% width and height and changed the bottom and left values to 25% steps (0, 25, 50, 75).
I found a solution which works as desired, see also https://jsfiddle.net/juwxdb5m/1/.
HTML:
<h1>Range display with relative sizes</h1>
<div class="range-display">
<div class="range-cocoon">
<div class="range-point" style="bottom:0%;left:0%;"></div>
<div class="range-point" style="bottom:33.333%;left:33.333%;"></div>
<div class="range-point" style="bottom:66.666%;left:66.666%;"></div>
<div class="range-point" style="bottom:100%;left:100%;"></div>
</div>
</div>
CSS:
.range-display {
background-color: gray;
height: 90px;
position: relative;
width: 160px;
}
.range-cocoon {
background-color: silver;
bottom: 0;
height: 75%;
left: 0;
position: absolute;
width: 75%;
}
.range-point {
background-color: lime;
height: 33.333%;
position: absolute;
width: 33.333%;
}

Adding images on both sides of a div

I'd like to ask, how to add two images on both sides of a div.
See, I got a main container on my site, and I'd like to add a little decoration on both sides, like a shadow which would foreground the actual content and place less emphasis on the background, y'know?
So, I got something like this:
page.html
...
<body>
<div id="container">
<div id="shadow-left"></div>
<div id="shadow-right"></div>
...
</div>
...
</body>
...
main.css
...
#container {
position: relative;
background: #FFF;
width: 840px;
min-height: 100vh;
margin-left: auto;
margin-right: auto;
}
#shadow-left {
// Gotta do that on the left site too
}
#shadow-right {
position: absolute;
top: 80px; // So there's a little space just for the upper nav
left: 840px;
width: 500px;
height: 100vh;
background: url('/res/img/shadow-right.png') 0 0 no-repeat;
}
...
I imagined it to look like this, but there's just NOTHING. How could I accomplish to do that?
Here is a fiddle where the main content is under-shadowed by whatever color you'd like, you can expand the shadow effect by playing around with the css and I'm sure there are plenty of css-shadow generators online. Hope this is what you're looking for.
HTML
<div id="main"></div>
CSS
#main {
width:60%;
margin-left:auto;
margin-right:auto;
background:green;
height:1000px;
box-shadow: 30px 0 19px -4px lightgreen, -30px 0 19px -4px lightgreen;
}
P.S. Excuse the green..
It seems like for this you can use floats, I hope I am understanding your question correctly.
fiddle: https://jsfiddle.net/L4cjz8p9/
HTML
<body>
<div id="container">
<div id="shadow-left"><img src="http://placehold.it/100x300"></div>
<div id="shadow-right"><img src="http://placehold.it/100x300"></div>
Main content
</div>
</body>
CSS
#shadow-left
{
float:left;
}
#shadow-right
{
float:right;
}
#container {
position: relative;
background: #FFF;
width: 840px;
min-height: 100vh;
margin-left: auto;
margin-right: auto;
}
Here is one way to position things outside e.g. a centered container. You could then fill the "shadows" with a background that repeats only over y or no repeat at all if you only would like to have something on the top of the page but not repeat when you scroll down.
The example here just has a 50px wide box at both sides next to the container.
Fiddle: http://jsfiddle.net/60yjen0a/
HTML:
<div class="container">
<div class="shadow left"></div>
<div class="shadow right"></div>
<div class="content"></div>
</div>
CSS:
.container {
position: relative;
width: 600px;
height: 1000px;
margin: 0 auto;
}
.content {
position: absolute;
width: 100%;
height: 100%;
background: #eee;
}
.shadow {
position: absolute;
height: 100%;
top: 0;
background: #ddd;
width: 50px;
}
.left {
left: -50px;
}
.right {
right: -50px;
}
With absolute position you can use left and right
#shadow-left {
position: absolute;
top: 80px;
left: 0px;
width: 500px;
height: 100vh;
background: YOUR_BACKGROUND_URL_FOR_LEFT_SHADOW;
}
#shadow-right {
position: absolute;
top: 80px;
right: 0px;
width: 500px;
height: 100vh;
background: YOUR_BACKGROUND_URL_FOR_RIGHT_SHADOW;
}
Check this on JSfiddle