Understanding divs - html

I'm struggling to understand divs.
I want to have the 'nav' panel expand vertically as required. The second issue I have is that I can't seem to get padding to work. Any changes I make tend to end up with the 'section' div drop below the 'nav' div.
Please see below jsfiddle and code.
Thanks in advance.
https://jsfiddle.net/s59cwy9s/
<div id="container">
<div id="nav">
test
</div>
<div id="section">
test
<br><br><br><br>
test
<br><br><br><br>
test
</div>
</div>
#container
{
width: 1156px;
margin-top: 0;
margin-left: auto;
margin-right: auto;
box-shadow: 5px 5px 10px rgb(0,0,0);
position: relative;
background-color: transparent;
height: auto;
}
#header
{
background-color:black;
color:white;
text-align: center;
padding:5px;
}
#nav
{
line-height:30px;
background-color:#eeeeee;
min-height: 100px;
min-width: 80px;
float:left;
padding: 15px;
display: inline-block;
height: auto;
}
#section
{
/*float: none;*/
padding: 10px;
display: block;
/*position: absolute;*/
/*overflow: auto;*/
background-color: white;
width: auto;
height: auto;
}

This may be due to the fact that your name bar doesn't span the height of the webpage completely. Try something like height :100% for the navbar. It might do the trick.

Here is some help :
https://jsfiddle.net/6ubhyL5k/
Some advices :
Take time to really understand how the page flow works (float : left/right) so you will then understand how padding and margin work when you have floating div
Use what you really know and don't improvise :)
Don't use br to make spaces between blocks (margin and padding are what you should use)
Take a look at how bootstrap works and never forget the responsive design

First I will recommend is using box-sizing attribute
It contains any type of padding or borders within the container's width and height. Find more about it Here. So i suggest:
*
{
box-sizing:border-box;
/* Use browser prefixes if u want support for other browsers */
}
Second is add a class to the container which contains elements wit float css attribute like clearfix and add this code:
.clearfix:after {
visibility: hidden;
display: block;
font-size: 0;
content: " ";
clear: both;
height: 0;
}
or you can just create a div after the container containing elements with float css attribute and clear it.
<div class='clear'></div>
.class
{
clear:both;
}
Using float as much as it is useful brings about a problem in layout if not properly used. https://css-tricks.com/all-about-floats/
My Solution:
html,body {height:auto; width:100%; background:red; }
* { box-sizing:border-box; margin:0; padding:0; display:block; position:relative; }
#container
{
min-width:800px;
height:auto;
margin:0 auto;
width:100%;
}
#nav
{
float:left;
width:30%;
padding: 15px;
line-height:30px;
background-color:#eeeeee;
min-height: 100px;
min-width: 80px;
background:white;
}
#section
{
float:left;
width:70%;
padding:0 100px;
background:yellow;
}
.clearfix:after
{
visibility: hidden;
display: block;
font-size: 0;
content: " ";
clear: both;
height: 0;
}
Hope It Helps You. Though i recommend researching more on layouts since there's other layout which will give you less problem than floats.

Try
#section{
clear:both;
}
JSfiddle
clear:both allows floated divs to stop continuing on the same line with the other floated ones, and drop below.
Update: https://jsfiddle.net/s59cwy9s/2/
You could fix your issue by giving a margin-right to the #nav

Related

Element move down when text is added

I have an issue with elements. When I type any text in any place inside search_groups_wrapper, the right colum fall to the bottom. If there is no text, it is displayed as should be. What is the problem?
Here is jsfiddle - http://jsfiddle.net/csYU8/ with problem. Remove 'Here is some text' and element will move top.
I can't find what causes this but I found a solution:
Just add the styles below to your #search_wrapper
top:0;
position:absolute;
CSS
#search_wrapper
{
height:50px;
width:100%;
top:0;
position:absolute;
}
DEMO
Add align: left to your search_groups_wrapper and existing_groups_wrapper.
FIDDLE DEMO
instead of using display:inline-block you should use float:left property. and to clear the float you can use clear:left on parent div.
or you want to still use the display: inline-block.
#search_groups_wrapper
{
display:inline-block;
height:100%;
margin:0;
padding:0;
border:0;
width:70%;
position:relative;
top:0;
vertical-align:top; /*I've just added this line*/
}
Here is the another working Demo . http://jsfiddle.net/kheema/csYU8/7/
#search_groups_wrapper {
border: 0 none;
display: inline-block; /*Better Remove this line and add float left*/
float: left;
height: 100%;
margin: 0;
position: relative;
top: 0;
width: 70%;
}
#existing_groups_wrapper {
border: 0 none;
display: inline-block; /*Better Remove this line and add float left*/
float: left;
height: 100%;
margin: 0;
width: 30%;
}
Here is a Demo. http://jsfiddle.net/csYU8/

Why "Inline-block" doesn't work properly in this CSS?

Please check the CSS below.
/*rex is the container of ex,ex2,ex3*/
div.rex{
height:200px;
border:0px;
margin:60px auto;
padding: 0;
vertical-align:top;
}
div.ex{
width:34%;
height:200px;
background-color:#4f443a;
display:inline-block;
margin: 0;
padding: 0;
vertical-align:top;
}
div.ex2{
width:0.5%;
height:200px;
display:inline-block;
margin: 0;
padding: 0;
vertical-align:top;
}
div.ex3{
width:65.5%;
height:200px;
background-color:#7e8547;
display:inline-block;
margin: 0;
padding: 0;
vertical-align:top;
}
The result in browser:
What I need:
This is actually expected behavior in HTML. Because you are using inline-block, any newline character or whitespace you have after the element and before another inline element, will be counted as a space. If you want the blocks to stack side by side like in your picture, your HTML would need to be like this.
<div class="rex">
<div class="ex"></div><div class="ex2"></div><div class="ex3"></div>
</div>
working demo
It's not very pretty, but then again, I would recommend using another approach, possibly floating the elements instead.
Refer to here for a more in depth explanation of why this occurs.
How to remove the space between inline-block elements?
Just extending answer giving by #Tristan here.
You have repeated the css code unnecessarily. You can minify it by using multiple css like :
.ex, .ex2, .ex3 {
display: inline-block;
vertical-align: top;
margin: 0;
padding: 0;
height: 100%; /* no need of height: 200px; here */
} /* if you need to extend it to parent height */
/* then use height: 100% */
OR
div.rex > div { /* code here */ }
You can keep elements side by side by using any of the below approaches:
Using display: table-cell
Using float: left
Using display: inline-block (check #Tristan's solution)
Add float:left; to your div.ex, div.ex2 and div.ex3 instead.
JSFIDDLE
UPDATE:
Add position:absolute to second and third div if float is not a choice.
FIDDLE
UPDATE 2:
Add this to only 3rd div if you need that space in between.
FIDDLE
You can sue comments like that (this looks little better in code):
<div class="rex">
<div class="ex"><!--
--></div><div class="ex2"></div><!--
--><div class="ex3"></div>
</div>
My trick is to set font-size:0; in the parent element, because it's the font-size that is causing the math to not add up perfectly ( about a 4px overlap per div, depending on screen size ) and causes one div to appear under the other.
.rex{
display:block;
font-size:0;
}
.ex{
width:34%;
height:200px;
background-color:#4f443a;
display:inline-block;
vertical-align:top;
margin: 0 .5% 0 0; /*small space between divs*/
padding: 0;
font-size:16px; /*restore font size*/
}
.ex2{
width:65.5%;
height:200px;
background-color:#7e8547;
display:inline-block;
vertical-align:top;
margin: 0;
padding: 0;
font-size:16px;
}

IE and Chrome inline-block css different behavior

I have a code where #page_field is parent and have two children. So, I want these two blocks go one after another. What I faced is that chrome display left_block correctly: pager_separator_design is 30px wide, so 30px + 120px (relative left) give me 150px and this is what should be done.
However IE, do not add width of pager_separator_design so total left 120 is wrong. Firefox do like Chrome.
What can I do?
HTML
<div id="page_field">
<div id="pager_separator_design">
</div>
<div id="left_block">
</div>
</div>
CSS
#page_field
{
margin-right: auto;
margin-left:0px;
width: 1000px;
background-color: #FFFFFF;
height:auto;
display: table;
}
#pager_separator_design
{
position:relative;
display: inline-block;
left: 120px;
background-image: url('separator_new.png');
width:30px;
height: 100%;
z-index:10;
}
#left_block
{
vertical-align: top;
position:relative;
left:120px;
display: inline-block;
width:850px;
margin:0;
padding:0;
}
I am using IE10.
Display inline-block has a margin issue when trying to align elements horizontally. I think it's about 6px that shouldn't be there (usually in IE).
Try adding margin: 0 0 0 -6px to your elements to see if that solves the issue.
Caveat: Not sure if I fully understood the issue, so this is a bit of a guess.
Hopefully, today, I found a solution and the problem:
I used table, table-row and table-cell. + added addtional emply cell at the left.
And problem was that I made IE show sites like IE 7. I did it many weeks ago and forget to tur n it off.
My css:
#page_field
{
margin: 0px;
padding:0px;
margin-left:0px;
width: 1000px;
background-color: #FFFFFF;
display: table;
position:relative;
}
#middle
{
display:table-row;
}
#left_block
{
width:120px;
display: table-cell;
margin:0;
padding:0;
}
#pager_separator_design
{
position:relative;
display: table-cell;
background-image: url('separator_new.png');
background-repeat:repeat-y;
width:30px;
height: 100%;
z-index:10;
margin:0;
padding:0;
}
#right_block
{
vertical-align: top;
position:relative;
display: table-cell;
width:850px;
margin:0;
padding:0;
}
And code is:
<div id="page_field">
<div class="middle">
<div id="left_block">
</div>
<div id="pager_separator_design">
</div>
<div id="right_block">
</div>
</div>
</div>
I hope it will help somebody.

How to set up width percentage basis?

I have the following problem:
I'd like to create a html page where a #sidebar spans a constant 27px and a #content spans the remaining part of the screen. The #content is divided into two areas splitting it at 40% - 60%.
<html>
<body>
<div id="sidebar">
</div>
<div id="content">
<div id="forty-percent">
</div>
<div id="sixty-percent">
</div>
</div>
</body>
</html>
I have tried to make the following css:
#sidebar{
width:27px;
}
#content{
position:absolute;
padding-left:27px;
width:100%;
}
But then I cannot divide the content into 40%-60%, because percentages are calculated from the width of the #content and not from its area inside.
What am I doing wrong? Can you please help?
UPDATE:
The demo of the NOT working version:
http://jsbin.com/iseqon/1/edit
Ideally the dashed boxes should be side-by-side, inside the blue box.
This may suit more your needs. If you want to have a better control of your #sidebar & #content vertical alignment, you must use inline-block to have a CSS only solution.
You can view it live here: http://codepen.io/jpsirois/pen/dvbmEy
* {
/* This prevent padding to be added on defined width */
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
body {
font-size: 0; /* Need to be set to 0 to properly use inline-block here */
color: white; /* For a better preview purpose only */
}
#sidebar,
#content {
display: inline-block; /* Allow vertical-align control (float didn’t) */
font-size: 16px; /* Reset font-size to normal */
vertical-align: middle; /* Demo of vertical-alignement */
}
#sidebar {
width: 27px;
background: darkred;
height: 50px; /* For a better preview purpose only */
margin-right: -27px; /* This allow #content to be inlined aside */
}
#content {
font-size: 0; /* Need to be set to 0 to properly use inline-block here */
width: 100%;
padding-left: 27px;
}
#forty-percent,
#sixty-percent {
height: 100px;/* For a better preview purpose only */
display: inline-block;
font-size: 16px; /* Reset font-size to normal */
}
#forty-percent {
width: 40%;
background: darkgreen;
}
#sixty-percent {
width: 60%;
background: darkblue;
}
You need this to float the #sidebar and give an equal margin-left to the #content, and also float the two inner boxes so they can sit side by side..
#sidebar {
width:27px;
float:left;
}
#content {
margin-left:27px;
overflow:auto;
}
#forty-percent {
width:40%;
float:left;
}
#sixty-percent {
width:60%;
float:left;
}
and also to not use the # char in the actual id
Demo at http://jsfiddle.net/gaby/8a7CN/
(your fixed jsbin demo at http://jsbin.com/iseqon/4/edit you need to keep in mind that borders add to the width so it cannot work with percentages very well)
how about having a parent div that would be relative and then having the div inside float right or left with absolute position within container. when the parent container is pos relative and the child is pos absolute, the children with position with respect to their container. In other words, something like that (untested but should give you the right idea)
#wrapper {
position:relative;
width:100%;
margin:50px;
}
#leftCol {
width:60%;
background-color:yellow;
}
#rightCol {
width:40%;
position:absolute;
right:0px;
top:0px;
height:100px;
background-color:red;
}
</style>
<div id='wrapper'>
<div='leftCol'>
</div>
<div id='rightCol'>
</div>
</div>
I am using your HTML only change the CSS.
My CSS is
#sidebar
{
width:27px;
min-width:27px;
float:left;
}
#content
{
float:right;
width:100%-28px;
min-width:100%-28px;
}
#forty-percent
{
width:40%;
float:left;
}
#sixty-percent
{
width:60%;
float:right;
}
Hope this will help you.Thanks.

Centering two transparent columns using CSS

So, right to the point, here's what I want (minus the poor quality)...
http://www.hbperspective.com/alt3/site.jpg
And here's what I've got...
http://www.hbperspective.com/alt3/
I'm trying to get those two transparent columns to be centered as they are in the pic. With this CSS layout I'm having a heck of a time figuring out how to do that without causing all kinds of other problems. Here is my styling...
* {
padding: 0;
margin: 0;
}
body {
background: #000000 url('background_div.png') repeat-y center top;
color: #ffffff;
font-family: Arial, Helvetica, sans-serif;
margin: 0 auto;
}
#wrapper {
background: url('background_header_transparent.png') no-repeat center top;
width: 100%;
display: table;
overflow: hidden;
}
.overlay {
position: absolute;
top: 0;
bottom: 0;
left: 0;
width: 100%;
background: #000000;
filter:alpha(opacity=50);
-moz-opacity:0.5;
-khtml-opacity: 0.5;
opacity: 0.5;
}
.container {
float: left;
position: relative;
margin-top: 100px;
}
.content {
position: relative;
float: left;
}
#contentColumn{
width: 540px;
}
#sidebarColumn {
width: 190px;
margin-left: 20px;
float: left;
display: inline;
}
#contentColumn .content {
width: 500px;
padding: 10px;
}
#sidebarColumn .content {
width: 170px;
padding: 10px;
}
* html #contentColumn .overlay { height: expression(document.getElementById("contentColumn").offsetHeight); }
* html #sidebarColumn .overlay { height: expression(document.getElementById("sidebarColumn").offsetHeight); }
The markup is pretty simple, probably be just easier to look at it from the link provided. So, like I said I'm not really sure what to do at this point to get it working the way I want. Any ideas?
div#container {
width:500px; /* Same width as both columns */
margin:auto; /* Will center the container */
}
div#col1 {
float:left; /* allows side-by-side columns */
width:250px;
}
div#col2 {
float:left;
width:250px;
}
div.clear {
clear:both; /* Stops columns from extending past border of container */
}
<div id="container">
<div id="col1"></div>
<div id="col2"></div>
<div class="clear"></div>
</div>
And for extra credit, avoid using expressions :) Instead, perform any needed logic like that with javascript, via a framework like jQuery.
There are so many gotchas creating CSS columns I would suggest using a framework instead of rolling your own. There are lots of gotchas which are browser defendant and that you may not see unless you check in IE, FF, Safari, Opera, etc.
A few good frameworks are:
YUI Grids
Blueprint CSS
Blocks (new experimental)
Rules for centering things in CSS:
The thing you're centering must be assigned a width
The margins on that block must be assigned to auto
I have it working on your site in FF3 and IE7 using
div#wrapper {
width:800px;
margin: auto;
}
div#contentColumn
{
margin-left: 20px;
}
If you want to fix up the logo (see top right) then add an extra container div immediately inside the wrapper, and apply the above width/margin to the container (as suggested by Jonathan Sampson.)