Page content appearing underneath sidebar - html

I am creating a html layout with a sidebar. But my header and content are appearing underneath my sidebar instead of next to it.
.container { position:relative; padding:10px; top:0px; right: 0; left: 0; height: 1200px;}
#sidebar {
position:relative;
top:0; bottom:0; left:0;
width:200px;
height: 1000px;
background: gray;
}
#header { border:1px solid #000; height:300px;
padding:10px; margin-left: 200px;
}
#content { border:1px solid #000; height:700px; margin-left: 200px;;
padding:10px;
}
<div class="container">
<div id="sidebar">
Link1
</div>
<div id="header">
<h2 class="title">Title</h2>
<h3>Header content</h3>
</div>
<div id="content">
<center>
<p>Hello</p>
</center>
</div>
</div>
Thanks

Add "display: inline-block;" to the elements that you want to display next to each other.

Just add
#sidebar {
float:left;
}
.container { position:relative; padding:10px; top:0px; right: 0; left: 0; height: 1200px;}
#sidebar {
position:relative;
top:0; bottom:0; left:0;
width:200px;
height: 1000px;
background: gray;
float:left;
}
#header { border:1px solid #000; height:300px;
padding:10px; margin-left: 200px;
}
#content { border:1px solid #000; height:700px; margin-left: 200px;;
padding:10px;
}
<div class="container">
<div id="sidebar">
Link1
</div>
<div id="header">
<h2 class="title">Title</h2>
<h3>Header content</h3>
</div>
<div id="content">
<center>
<p>Hello</p>
</center>
</div>
</div>

I have introduced .inner-container and defined two flexboxes. CSS is simplified.
* {
box-sizing: border-box;
}
.container {
display: flex;
}
.inner-container {
display: flex;
flex-flow: column;
width: 80%;
}
#sidebar {
width: 20%;
background: gray;
}
#header {
border: 1px solid black;
height: 300px;
padding: 10px;
}
#content {
border: 1px solid black;
padding: 10px;
}
<div class="container">
<div id="sidebar">
Link1
</div>
<div class="inner-container">
<div id="header">
<h2 class="title">Title</h2>
<h3>Header content</h3>
</div>
<div id="content">
<center>
<p>Hello</p>
</center>
</div>
</div>
</div>

you should just try editing the
position: fixed
this will solve your problem.

You should try to change your position: relative; to position: absolute;. You can then adjust the position of your divs using a margin.
.container {
position:relative;
padding:10px;
top:0px;
right: 0;
left: 0;
height: 1200px;
}
#sidebar {
position:absolute;
top:0; bottom:0; left:0;
width:200px;
height: 1000px;
background: gray;
}
#header { border:1px solid #000; height:300px;
padding:10px; margin-left: 200px;
margin-top:-10px;
}
#content {
border:1px solid #000;
height:700px;
margin-left: 200px;
padding:10px;
}
<div class="container">
<div id="sidebar">
Link1
</div>
<div id="header">
<h2 class="title">Title</h2>
<h3>Header content</h3>
</div>
<div id="content">
<center>
<p>Hello</p>
</center>
</div>
</div>
Working fiddle: https://jsfiddle.net/khs8j3gu/2/
Good luck!

Related

Positioned div doesn't behave as intended

The div inside another div won't go to the right but just stays to the left. The code below is some what look like chat box the first div with green background has a position of 0px left and it works but the second div has 0px right and it still stick to the left please help me with this it bothers me for 2 day without right solution
<html>
<head>
</head>
<body>
<div style="width:100% height:100%; position: relative; top:0px; left:0px; background-color:white;">
<div style="width:200px; height:100px; position: relative; top:10px; left:0px; background-color:green; font-size:20px;"><p>1</p></div>
<div style="width:200px; height:100px; position: relative; top:10px; right:0px; background-color:red; color: white; font-size:20px;"><p>2</p></div>
</div>
</body>
</html>
position: relative means the div is positioned "relative to itself". So right: 0px just means "move the div 0px to the right of where it would normally be"... not to the right edge of the container.
You could use position: relative on the container, and apply position: absolute to the children instead, but assigning top values will be cumbersome.
Info about position
An alternative might be adding display: flex to the wrapper. Then you can use margin-left: auto to push a div to the right.
.wrapper {
background: lightgrey;
display: flex;
flex-direction: column;
}
div > div {
width: 200px;
height: 100px;
margin-bottom: 10px;
}
.left {
background: green;
}
.right {
background: red;
margin-left: auto;
}
<div class="wrapper">
<div class="left">
<p>1</p>
</div>
<div class="right">
<p>2</p>
</div>
</div>
.parent{
width:100%;
position: relative;
clear: both;
}
.incoming {
float: left;
max-width: 80%;
background-color: #ccc;
padding: 4px;
overflow: auto;
}
.reply {
float: right;
max-width: 80%;
background-color: powderblue;
padding: 4px;
}
<div class="parent">
<div class="incoming"><p>Incoming chat that takes up a maximum of 80%
of screen width.</p></div>
<div class="reply"><p>Reply, that also does the same, but from the right of the screen.</p></div>
</div>
Edited to reflect updated requirement
Using relative element with top, left, bottom and right properties is useless. you have to change it to absolute value.
<div style="width:100% height:100%; position: relative; background-color:white;">
<div style="width:200px; height:100px; position: absolute; top:10px; left:0px; background-color:green; font-size:20px;"><p>1</p></div>
<div style="width:200px; height:100px; position: absolute; top:10px; right:0px; background-color:red; color: white; font-size:20px;"><p>2</p></div>
</div>
UPDATE
here is another way to position elements
<div style="width:100%; height:100px; background-color:white;">
<div style="width:200px; height:100px; float:left; background-color:green; font-size:20px;"><p>1</p></div>
<div style="width:200px; height:100px; float:right; background-color:red; color: white; font-size:20px;"><p>2</p></div>
</div>
UPDATE#2
here is markup for your chat
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.chat {
width: 100%;
background: lightblue;
padding: 10px;
overflow: hidden;
}
.message {
clear: both;
font-family: sans-serif;
color: white;
}
.to, .from {
width: 40%;
padding: 10px;
overflow: hidden;
}
.to {
background: pink;
float: left;
}
.from {
background: lightgreen;
float: right;
}
<div class="chat">
<div class="message">
<div class="to">hi</div>
</div>
<div class="message">
<div class="to">how are u?</div>
</div>
<div class="message">
<div class="from">fine, thnks</div>
</div>
<div class="message">
<div class="to">can u help me?</div>
</div>
<div class="message">
<div class="from">sure, no problem</div>
</div>
</div>
Use float: right; instead of right:0px;.
Here is the code.
<html>
<head>
</head>
<body>
<div style="width:100% height:100%; position: relative; top:0px; left:0px; background-color:white;">
<div style="width:200px; height:100px; position: relative; top:10px; left:0px; background-color:green; font-size:20px;"><p>1</p></div>
<div style="width:200px; height:100px; position: relative; top:10px; float:right; background-color:red; color: white; font-size:20px;"><p>2</p></div>
</div>
</body>
</html>

How to avoid offset when getting position coordinates(top,left) of sibling inner div

I have a structure like this:
<body>
<div class="a">
<div class="b">
</div>
</div>
<div class="c">
</div>
</body>
CSS:
.a { position:absolute; }
.b { position:absolute; }
.c { position:absolute; }
Result:
Here block is outside "a". Coordinates: top: 100px; left: 100px
Expected:
<body>
<div class="a">
<div class="b">
<div class="c"></div>
</div>
</div>
</body>
coordinates: top: 100px; left: 100px (the same), but "c" is inside "b".
As a result, using same coordinates, we get offset in first case. How can i avoid it? Thanks!
Made two snippets;
1) Hard fix with your current structure: (Use position:relative on parent container and position:relative on children)
.wrapper{
width:100px;
position:relative;
}
.a{
width:100px;
height:80px;
border:1px solid black;
position:relative;
}
.b{
width:60px;
height:40px;
border:1px solid black;
position:absolute;
margin: auto;
z-index:1000;
top: 0; left: 0; bottom: 0; right: 0;
}
.c{
width:60px;
height:40px;
border:1px solid black;
top:75%;
left:80%;
z-index:1000;
position:absolute;
}
<div class="wrapper">
<div class="a">
<div class="b">
</div>
</div>
<div class="c"></div>
</div>
2) Suggestion, cleaner fix:
.a{
width:120px;
height:100px;
border:1px solid black;
position:relative;
}
.b{
position:absolute;
width:80px;
height:60px;
border:1px solid black;
top:0; left:0; right:0; bottom:0;
margin:auto;
}
.b-wrapper{
position:relative;
width:100%;
height:100%;
}
.c{
position:absolute;
width:80px;
height:60px;
border:1px solid black;
top:100%;
left:100%;
}
<div class="a">
<div class="b">
<div class="b-wrapper">
<div class="c"></div>
</div>
</div>
</div>
I actually don't understand your question well. Bet here is the snippet i made,
exactly how you picture it.
.container{
max-width: 200px;
position:relative;
display:inline-flex;
}
.a{
position:relative;
width: 200px;
height:200px;
border: 2px solid #222;
}
.b{
position:absolute;
border: 2px solid #f69;
height: 150px;
width: 150px;
left:50%;
top: 50%;
transform: translate(-50%, -50%);
}
.c{
position:absolute;
height: 50px;
width: 50px;
border: 2px solid brown;
bottom: -30px;
right: -30px;
}
<div class="container">
<div class="a"> a
<div class="b">
b
</div>
</div>
<div class="c">c
</div>
</div>
Have you tried use class 'b' instead of 'c'?
<body>
<div class="a">
<div class="b">
</div>
</div>
<div class="b">
</div>

column-count and position fixed on Microsoft Edge

On a current project I have a similar structure (here I have very simplified the structure):
http://jsfiddle.net/6j5ouhz4/3/
HTML
<div class="container">
<div class="columns">
<div class="column1">
<div class="openFlexbox"> OPEN </div>
<div class="flexbox">TEST
<span class="close">X</span>
</div>
</div>
<div class="column1">
<div class="openFlexbox"> OPEN </div>
<div class="flexbox">TEST
<span class="close">X</span>
</div>
</div>
<div class="column1">
<div class="openFlexbox"> OPEN </div>
<div class="flexbox">TEST
<span class="close">X</span>
</div>
</div>
<div class="column1">
<div class="openFlexbox"> OPEN </div>
<div class="flexbox">TEST
<span class="close">X</span>
</div>
</div>
</div>
</div>
CSS
.container {
border:1px solid black;
width:600px;
min-height:200px;
margin: 0 auto;
background: #ddd;
display:flex;
display: -ms-flexbox;
}
.columns {
column-gap: 8em;
column-count: 2;
}
.column1 {
display: block;
border:1px solid red;
width:200px;
height: 200px;
margin:10px;
position:relative;
}
.flexbox {
display:none;
position: fixed;
top:0;
left:0;
width:100%;
height:100%;
background: #aaa;
font-size:30px;
text-align:center;
z-index: 9999
}
.flexbox.open {
display:block;
}
.close {
border:1px solid #fff;
padding: 5px;
}
.openFlexbox {
background: #a6dbea;
padding: 10px 0;
text-align:center;
display:inline-block;
position:absolute;
width: 100px;
left: 50%;
margin-left:-50px;
top: 40%;
}
JS
jQuery('.openFlexbox').on('click',function(e) {
jQuery(this).next('.flexbox').addClass('open');
});
jQuery('.close').on('click',function() {
jQuery('.flexbox').removeClass('open');
});
Firefox and Chrome don't have any problem, on Microsoft edge the modalbox appear "halfsize" occupying the half area of column where this block is located (in this example instead, it does not appear at all).
Actually, by removing the relative position, the problem disappears, but the "position: relative" I use to center the button..
but the way, the relative position shouldn't effect the fixed positions.
There is a fix for this problem?
the 'openflexbox' does not cover the whole area but maybe this would work for you?
css:
.flexbox {
display:none;
/*position: fixed;*/
top:0;
left:0;
width:100%;
height:100%;
background: #aaa;
font-size:30px;
text-align:center;
z-index: 9999;
}
js:
jQuery('.openFlexbox').on('click',function(e) {
jQuery(this).next('.flexbox').addClass('open');
$('.openFlexbox').css('display', 'none');
});
jQuery('.close').on('click',function() {
jQuery('.flexbox').removeClass('open');
$('.openFlexbox').css('display', 'inline-block');
});

How can I do this layout?

I have this sample:
link
CODE HTML:
<div class="container">
<div class="image-container">
<img src="http://media.caranddriver.com/images/16q2/667343/2016-ford-focus-rs-vs-subaru-wrx-sti-vw-golf-r-comparison-test-car-and-driver-photo-667344-s-original.jpg" alt="car1" title="car1" />
</div>
<div class="desc">
details
</div>
</div>
CODE CSS:
body{
background:black;
}
.container{
background:#f3f4f6;
border-bottom:5px solid #db5207;
}
.image-container,.desc{
display:inline-block;
vertical-align:top;
}
.desc{
background:red;
}
img{
width:612px;
height:412px;
border:10px solid white
}
I put an image to better understand what they want to do
basically I want the image to be over container and divul "desc" to be by end.
You can help me solve this problem? What is the best way to do this?
Thank you in advance!
Try this here code it may be solve your problem
*{margin:0;padding:0;}
.container{margin:100px 0;height:200px;border:5px solid red;position:relative;}
.image-container{height:300px;width:30%;border:5px solid blue;position:absolute;right:55%;top:-30%;}
.image-container img{height:300px;width:100%;}
.desc-container{height:190px;width:50%;border:5px solid green;float:right;}
<div class="container">
<div class="image-container">
<img src="http://media.caranddriver.com/images/16q2/667343/2016-ford-focus-rs-vs-subaru-wrx-sti-vw-golf-r-comparison-test-car-and-driver-photo-667344-s-original.jpg" alt="car1" title="car1" />
</div>
<div class="desc-container">
details
</div>
</div>
Check the following code for your answer. Also you can verify the codepen https://codepen.io/sasikumarhx/pen/VKmQod
HTML:
<div class="container">
<div class="left">
<div class="image-container">
<img src="http://media.caranddriver.com/images/16q2/667343/2016-ford-focus-rs-vs-subaru-wrx-sti-vw-golf-r-comparison-test-car-and-driver-photo-667344-s-original.jpg" alt="car1" title="car1" />
</div>
</div>
<div class="right">
<div class="desc">
details
</div>
</div>
</div>
CSS:
body{
background:black;
}
.container{
background:#f3f4f6;
border:5px solid #db5207;
height:250px;
}
.right{
float:right;
width:49%;
}
.left{
float:left;
width:49%;
}
.image-container{
}
.desc{
background:red;
}
img{
width:50%;
height:130%;
border:10px solid white;
float:right;
}
please check below code:
just need to change in css:
body{
background:black;
}
.container{
background:#f3f4f6;
border-bottom:5px solid #db5207;
margin-top: 50px;
height: 380px
}
.image-container,.desc{
display:inline-block;
vertical-align:top;
}
.desc{
background:red;
min-height: 380px;
display: inline-block;
}
img{
width:612px;
height:412px;
border:10px solid white;
position: relative;
top:-20px;
}
Please check the following if it satisfies your requirement:
<!DOCTYPE html>
<html>
<head>
<title>Solution</title>
<style type="text/css">
#container {
background-color: #DCDCDC;
position: relative;
left: 100px;
top: 100px;
width: 800px;
height: 200px;
padding: 5px;
}
#image {
background-color: #F0E68C;
width: 200px;
height: 255px;
position: relative;
left: 150px;
top: -80px;
}
#details {
background-color: #FF7F50;
position: relative;
width: 400px;
left: 380px;
top: -310px;
height: 190px;
}
h2 {
margin-top: 5px;
margin-left: 5px;
}
</style>
</head>
<body>
<div id="container">
<h2>container</h2>
<div id="image">
<h2>image</h2>
</div>
<div id="details">
<h2>details</h2>
</div>
</div>
</body>
</html>

Why my content is covered on my footer even I use clear:both to footer?

html,
body{
height: 100%;
margin:0;
padding:0;
}
#fake-content {
height:900px;
width:100px;
}
.readbook .col-sm-12 {
position: relative;
border-top:2px solid #dddddd;
}
.book-body {
position: absolute;
left:0;
top:0;
right:0;
height:100px;
padding:0px 70px 0px;
overflow-y:auto;
}
.body-inner {
margin-top: 20px;
}
.body-inner {
border: 1px solid red;
z-index:1;
height:500px
}
.navbar-footer {
background-color: #f8f8f8;
}
.footer {
text-align: center;
}
.footer p {
font-size: 10px;
margin:0;
text-align:center;
padding-top:53px;
display:inline-block;
}
.wrapper {
min-height: 100%;
height: auto !important;
height: 100%;
margin: 0 auto -120px;
}
.navbar-footer, .push {
height: 120px;
}
.last-content {
margin-bottom:144px;
}
.navbar-footer {
clear: both;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<div class="wrapper">
<div id="fake-content"></div>
<div class="container readbook">
<div class="row">
<div class="col-sm-12">
<div class="book-body">
<div class="body-inner">
<div class="page-wrapper">
<h4>2.Test</h4>
<p>
Content
</p>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="push"><!-- not put anything here --></div>
</div>
<!-- footer -->
<div class="navbar-footer">
<div class="container">
<div class="row">
<div class="col-sm-12">
<div class="footer">
<p>CO.,LTD # 2015 ALL Rights Reserved.</p>
</div>
</div>
</div>
</div>
</div>
I use position: absolute to my content for some reason so I put clear:both to footer in css file.However, the content still cover on my footer.
What can I do to not have content cover on footer?
Is clear:both not allowed to work for position:absolute?
Because clear: both clears floats, nothing else. you have not any floats to be cleared.
P.S. your design has many problems with it. I suggest you to begin reading on CSS.