Having problems with div columns - html

i want 3 columns with the same width.
i did succeed by use divs.
i made 2 divs in a div, then made another div in one of the two divs and then used css float left & right.
but was wondering if there was a different and better way? (no tables, i tried doing this without tables)
because i had trouble centering texts that were next to a image
and because i used float the white background disappears in the area where the columns were.
could someone help me either fix the problems i'm having or giving me a alternative method.

<div class="wrapper">
<div class="left"></div>
<div class="middle"></div>
<div class="right"></div>
</div>
.wrapper {
margin: 0 auto;
width: 450px;
}
.left {
float: left;
width: 150px;
background-color: red;
height:200px;
display:inline-block;
}
.middle {
background-color: yellow;
height:200px;
width: 150px;
display:inline-block;
}
.right {
float: right;
width: 150px;
background-color: blue;
height:200px;
display:inline-block;
}

I don't really get your question. But I make a solution. Here's my demo on Fiddle:
http://jsfiddle.net/asubanovsky/8d3m9/
HTML:
<div id='parent'>
<div id='first'>First</div>
<div id='second'>Second</div>
<div id='third'>Third</div>
</div>
CSS:
#parent{
width: 900px;
margin: 0 auto;
}
#first, #second, #third{
width: 300px;
float: left;
color: white;
text-align: center;
}
#first{
background-color: #222;
}
#second{
background-color: #444;
}
#third{
background-color: #666;
}

Related

CSS beginner, help creating this layout?

In the image below, on the left is the output of my html/css, on the right is what I would like the layout to look like.
I'm pretty clueless as to:
how to Center the header
why the 'upper right' text and button are being forced to the next line by the header (as opposed to orienting in the upper right
how to align the text area so that it is to the right of the image
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="outer_border">
<div class="inner_border">
<!--just use a div to represent the image -->
<div class ="image">
</div>
<span class="upper_left_text">
upper left
</span>
<span class ="header">
<h2>
Header
</h2>
</span>
<span class="upper_right_text">
upper right
</span>
<button class="button1">Button</button>
<textarea class="text_area">Text Area</textarea>
<button class="button2">Button 2</button>
</div>
</div>
</body>
</html>
.outer_border {
border: 1px solid black;
width: 600px;
height: 500px;
}
.inner_border {
border: 3px solid black;
width: 400px;
height: 300px;
float: right;
}
.image {
border: 1px solid black;
display: inline-block;
width: 50px;
height: 100px;
margin: 5px;
float: left;
}
.the_header {
text-align: center;
display: inline-block;
}
.button1 {
float: right;
}
.button2 {
float: right;
width: 80px;
height: 60px;
}
.text_area {
clear: both;
display: block;
width: 100%;
height: 150px;
margin: 5px;
/*I have no idea how to position this*/
}
.upper_left_text {
float: left;
}
.upper_right_text {
float: right;
}
I made a jsfiddle, check this one, should get you started :)
https://jsfiddle.net/fazbyxyq/
html5
<div class="right">
<div>upper left</div>
<div>header</div>
<div>upper right</div>
<div><textarea>textarea</textarea></div>
<div>button2</div>
</div>
css3
*{
box-sizing:border-box;
-moz-box-sizing:border-box;
}
.left{
float:left;
width:10%;
height:100px;
border:1px solid #000;
}
.right{
float:left;
width:89%;
margin-left:1%;
}
.right div{
float:left;
width:33%;
border:1px solid #000;
}
.right div:nth-child(2){
text-align:center;
}
.right div:nth-child(3){
text-align:right;
}
.right div:nth-child(4),.right div:nth-child(5){
width:99%;
border:0;
}
.right div:nth-child(4) textarea{
width:100%;
height:100px;
margin:10px 0;
}
.right div:nth-child(5){
text-align:right;
}
Peace out!
well, Your code was wrong in many lvl's. I have fixed it to look like in your image... but it's just a fix. Maybe not what you are looking for.
As a resume: You want a container with an image looks like a column and the rest of the html stay as another column.
Then, as you did, the image container is floating left with a fixed width of 50px but we have to add 10px more as you have given the container 5px margin (5px right and left = 10px),
Then I just add a container which will take the rest of the html. THen it's easy to give the container a float left and as its width 340px so the total of your layout is, as you want, 400px.
I have added both box-sizing: border-box; to make the border be inside the containers and not messing with the fixed widths.
Then I just have added .header {float:left;} as basically ion your code you have a class named the_headerwhich is not even used in the html. and then a bit of margin to the h2 to separete it from upper left
here you have the fiddle
The key lays in treating your layout as a layout with 2 columns. I believe the markup should look something like this:
<div id='demo'>
<div class='col1'>
<img src='http://www.placehold.it/50x100' />
</div>
<div class='col2'>
<div class='header'>
<span class='left'>left</span>
<span class='right'>
<button>button</button>
right
</span>
<h2>center</h2>
</div>
<textarea>Lorem ipsum</textarea>
<button>button</button>
</div>
</div>
to achieve the result in your image, you should add the following css:
#demo {
border: 2px solid black;
padding: 10px;
}
#demo .col1, #demo .col2 {
display: inline-block;
vertical-align: top;
}
#demo .col2 {
width: calc(100% - 60px);
}
#demo .left {
float: left;
}
#demo .right {
float: right;
}
#demo .header {
text-align: center;
}
#demo textarea {
width: 100%;
min-height: 100px;
margin: 8px 0;
}
#demo button {
float: right;
margin-left: 8px;
}
Note that I've used as little fixed dimesions as possible. Just cause it will make your layout adapt easier to different content and different screen sizes.
I've put your code next to my proposal in a fiddle. I think the code should be fairly easy and self explanatory, but feel free to ask if anything isn't clear.
Here is another fiddle that uses the "calc" operation to set the textarea the remaining width of the div.
Here is the fiddle http://jsfiddle.net/SteveRobertson/tyokk1qj/
I wrap this image in and set the height to 100% and then modify the rest of the elements to the right use CSS
.outer_border {
border: 1px solid black;
width: 600px;
height: 500px;
}
.inner_border {
border: 3px solid black;
width: 400px;
height: 300px;
}
#tall{
height:100%;
float:left;
}
.image {
border: 1px solid black;
display: inline-block;
width: 50px;
height: 100px;
margin: 5px;
float: left;
}
.the_header {
text-align: center;
display: inline-block;
}
h2 {
display:inline;
}
.button1 {
float: right;
}
.button2 {
width: 80px;
height: 60px;
display: block;
float:right;
}
.text_area {
clear: both;
display: inline;
width:auto;
height: 150px;
margin-right: 0;
}
.upper_left_text {
float: left;
}
.upper_right_text {
float: right;
}
.text_area{
width:calc(100% - 70px);
}

Two 50% width divs don't fit in parent

Sorry if it is a duplicate of something, I have searched honestly, but I still have the problem which is shown in this fiddle: http://jsfiddle.net/tfvdzzee/1/
The code here:
html
<div id="wrap">
<div id="one">1</div>
<div id="two">2</div>
</div>
css
#wrap
{
max-width: 400px;
margin: auto;
border: 2px solid black;
}
#one, #two
{
width: 50%;
background: green;
}
#two
{
float: right;
background: red;
}
I believe display: inline-block; is the best answer, as it prevents bugs of overlapping and overflowing, while also keeping its parent definitions.
JsFiddle Demo
HTML
<div id="wrap">
<div id="one">1</div><!--
--><div id="two">2</div>
</div>
CSS
#wrap
{
max-width: 400px;
margin: auto;
border: 2px solid black;
}
#one, #two
{
width: 50%;
background: green;
display: inline-block;
/* If worried about IE7 and IE6, add the two next lines */
*display: inline;
*zoom: 1;
}
#two
{
background: red;
}
Demo Fiddle
You need to both float:left the #one element as well as set overflow:hidden to the parent to ensure it wraps the children correctly.
Change your CSS to:
#wrap
{
max-width: 400px;
margin: auto;
border: 2px solid black;
overflow:hidden; /* <---ensure the parent wraps the children */
}
#one, #two
{
width: 50%;
background: green;
float:left; /* <---ensure the children display inline */
}
#two
{
float: right;
background: red;
}
Add the following style in your CSS.
#one{float:left}
DEMO
Remove the Css property for #two and add this
#one, #two
{
width: 50%;
background: green;
float: left;
}
Use float: left and you don't need float: right for #two.
#one, #two
{
width: 50%;
background: green;
float: left;
}
#two
{
background: red;
}
Fiddle example.
You will need to float both your divs. After the float, clear the float using the clearfix class.
CSS:
#one, #two{ float:left; }
.clearfix:after {
visibility: hidden;
display: block;
font-size: 0;
content: " ";
clear: both;
height: 0;
}
HTML:
<div id="wrap" class="clearfix">
<div id="one">1</div>
<div id="two">2</div>
</div>
DEMO
#wrap
{
max-width: 400px;
margin: auto;
border: 2px solid black;
}
#wrap:after{
clear:both;
content:"";
display:block;
}
#one, #two
{
width: 50%;
float:left;
background: green;
}
#two
{
background: red;
}
Try this and use clearfix on the :after pseudo element of your #wrap div.
DEMO
To Expand on the comment by sifu and answer the question in a choice of ways
The first method (Using float's)
#wrap
{
max-width: 400px;
margin: auto;
border: 2px solid black;
}
#one,#two
{
float:left;
width:50%;
}
http://jsfiddle.net/tfvdzzee/7/
Display Inline-block method
#wrap
{
max-width: 400px;
margin: auto;
border: 2px solid black;
font-size:0px;
}
#one,#two
{
width:50%;
display:inline-block;
font-size:16px;
}
http://jsfiddle.net/tfvdzzee/8/
Both methods can be used however if you are still developing for IE7 (not sure why you would) then method 2 wont work.

Horizontal div alignment

Good Day.
I am creating a somewhat percentage bar using a given width in input style. I want the given div to be horizontally align with each other but not overlapping, just following after each other. How can I possibly do that?
Here is a simple code
http://jsfiddle.net/XU3JM
<html>
<head>
<style>
#one{
background: green;
height: 20px;
}
#two{
background: blue;
height:20px;
}
#three{
background: gray;
height:20px;
}
</style>
<body>
<div id="one" style="width:20px;"></div>
<div id="two" style="width:30px;"></div>
<div id="three" style="width:40px;"></div>
</body>
</head>
</html>
The width has no constant value so I can't just put margin-left with it. I've been tweaking with this fall back code. No such luck. Thanks in advance.
Add this css:
#one, #two, #three {display:inline-block;}
http://jsfiddle.net/h35N6/
Or this css:
#one, #two, #three {float:left;}
http://jsfiddle.net/h35N6/1/
This is probably going to help
#one,#two,#three{
height: 20px;
padding: 0;
display: inline-block;
*display: inline;
zoom: 1;
}
It also supports older IE versions
http://jsfiddle.net/XU3JM/2/
Use display:inline-block; for each div to be centre aligned and wrap them in a new container with text-align: center; applied to it.
#first{
background: green;
height: 20px;
margin-left: 2px;
color: white;
float: left;
}
#second{
background: blue;
height:20px;
margin-left: 2px;
color: white;
float: left;
}
#third{
background: gray;
height:20px;
margin-left: 2px;
color: white;
float: left;
}
<div id="first" style="width:20px;">20</div>
<div id="second" style="width:30px;">30</div>
<div id="third" style="width:40px;">40</div>

Prevent float divs to start a new row. HTML & CSS

I am trying to make a simple tile grid. Here is my HTML:
<div class="tiles">
<div class="tile25x50">1</div>
<div class="tile50x50">2</div>
<div class="tile25x50">3</div>
</div>
And CSS:
.tiles {
width :100px;
}
.tile25x50 {
width: 50px;
height: 25px;
background-color: red;
float: left;
}
.tile50x50 {
width: 50px;
height: 50px;
background-color: blue;
float: left;
}
And the result is:
My question is how can I prevent the third div to be inserted in new row and instead fill the gap?
Live demo in jsfiddle.
Split layout in columns if you don't need to expand items in columns, if not use something like masonry. In simple CSS you would probably ended with dozens with wrappers and javascript either way or with one item solutions.
If you change the .tile50x50 to float:right, it works out, but I'm not sure how much this could really be extended to include more tiles properly.
This should work for modern browsers (includes IE10) - http://jsfiddle.net/yqkt8/
Instead of float, display inline-block, then use css columns.
.tiles {
width: 100px;
-webkit-column-width: 50px; -moz-column-width: 50px; column-width: 50px;
-webkit-column-gap: 0; -moz-column-gap: 0; column-gap: 0;
}
.tile25x50 {
display:inline-block;
width: 50px;
height: 25px;
background-color: red;
}
.tile50x50 {
display:inline-block;
width: 50px;
height: 50px;
background-color: blue;
}
change to float:right the float value of .tile50x50
.tile50x50 {
width: 50px;
height: 50px;
background-color: blue;
float: right;
}
You CAN use Float: right; but that would flow differently on different browsers. I would use a table with one row and multiple columns
<table>
<tr>
<td><div class="tile25x50">1</div></td>
<td><div class="tile50x50">2</div></td>
<td><div class="tile25x50">3</div></td>
<td><div class="tile50x50">4</div></td>
<td><div class="tile50x50">5</div></td>
</tr>
</table>
Ive included an edit here
EDIT:
Well if you do not want to use tables, try an un-ordered list here.
I would suggest to change the style of .tile50x50 from float left to float right.
I just fiddled around on your fiddle file and I was able to align the divs in a couple steps.
First place the div 3 between the other two divs
Now for the CSS:
.tiles {
width :100px;
}
.tile25x50 {
width: 50px;
height: 25px;
background-color: red;
float: left;
clear: both; /* Clears both sides so the div 3 drops down */
}
.tile50x50 {
width: 50px;
height: 50px;
background-color: blue;
float: left;
margin-top: -25px; /* Pushes this div back up to align with the other one */
}
There is an easier/more flexible way to do this and that would be:
(This option also removes float on the 25X50 tiles making it easier to stack.)
<div class="your-class-here"> <!-- this div will hold the two in there without having to do negative margin values -->
<div class="tile25x50">1</div>
<div class="tile25x50">2</div>
</div>
<div class="tile25x50">1</div>
Simply change tile50x50 class from float:left; to float:right;:
.tile50x50 {
width: 50px;
height: 50px;
background-color: blue;
float: right;
}
DEMO:
http://jsfiddle.net/dirtyd77/LAR3x/
You have to change the .tile50x50 css class value float:left to float:right.
Here is the complete code -
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<style type="text/css">
.tiles {
width :100px;
}
.tile25x50 {
width: 50px;
height: 25px;
background-color: red;
float: left;
}
.tile50x50 {
width: 50px;
height: 50px;
background-color: blue;
float: right;
}
.clear{
clear:both;
}
</style>
</head>
<body>
<div class="tiles">
<div class="tile25x50">1</div>
<div class="tile50x50">2</div>
<div class="tile25x50">3</div>
</div>
</body>
</html>
Please don't use Jquery or hacks like using negative margins. HTML/CSS will be just fine :)
Depending on any further layout requirements, you can do one of two things:
1) Simply remove the 50px block from the ".tiles" container, float each of them left and change the 100px width constraint of ".tiles" to 50px
fiddle.net/HHeSW/3/
HTML
<div class="tiles">
<div class="tile25x50">1</div>
<div class="tile25x50">3</div>
</div>
<div class="tile50x50">2</div>
CSS
.tiles {
float: left;
width:50px;
}
.tile25x50 {
width: 50px;
height: 25px;
background-color: red;
float:left;
}
.tile50x50 {
width: 50px;
height: 50px;
background-color: blue;
float: left;
}
2) Add a container div for each column. You can reuse the same column class for these and even drop the width constraints on ".tiles" and the column class for future flexibility :)
http://jsfiddle.net/wurwH/3/
HTML
<div class="tiles">
<div class="column">
<div class="tile25x50">1</div>
<div class="tile25x50">3</div>
</div>
<div class="column">
<div class="tile50x50">2</div>
</div>
</div>
CSS
.tiles {
width:100px;
}
.column{
float:left;
}
.tile25x50 {
width: 50px;
height: 25px;
background-color: red;
clear:left;
}
.tile50x50 {
width: 50px;
height: 50px;
background-color: blue;
}
i would suggest you two jQuery plugins:
http://masonry.desandro.com/
http://isotope.metafizzy.co/
EDIT:
Check out my cssdeck: http://cssdeck.com/labs/ojo7uw0l
I write HTML and CSS for this solution. I don't use table layout, float: right or position: absolute.
HTML
<h1>Example 1</h1>
<div class="tiles">
<div class="tile tile--one">1</div>
<div class="tile tile--two">2</div>
<div class="tile tile--one title--modified">3</div>
</div>
<h1>Example 2</h1>
<div class="tiles">
<div class="tile tile--one">1</div>
<div class="tile tile--two">2</div>
<div class="tile tile--one title--modified">3</div>
<div class="tile tile--two">2</div>
<div class="tile tile--one">1</div>
<div class="tile tile--two">2</div>
<div class="tile tile--one title--modified">3</div>
</div>
CSS
*,
*:after,
*:before {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
h1 {
margin: 40px 0;
}
.tiles {
width: 400px;
overflow: hidden;
}
.tile {
text-align: center;
float: left;
border: 1px solid #f0f0f0;
}
.tile--one {
background-color: rgba(255, 20, 20, .6);
width: 200px;
height: 100px;
}
.tile--two {
background-color: rgba(20, 20, 255, .6);
width: 200px;
height: 200px;
}
.title--modified {
margin-top: -100px;
}
I hope it can help you. This is demo
Demo
I can't actually believe people here didn't figure this out. How on the earth's sake can 3 divs with a sum width of 150px show up in a single row that accepts a width of 100px. You simply need to adjust the width in the .tiles class.
.tiles {
width :150px;
}
.tile25x50 {
width: 50px;
height: 25px;
background-color: red;
float: left;
}
.tile50x50 {
width: 50px;
height: 50px;
background-color: blue;
float: left;
}

2 Divs, variable size and fixed margin inbetween

I would like to have two divs sit next to each other. Both divs have their width set to a certain percentage. Between the two divs I want a fixed width margin of 20px. The width of div1 and div2 and the 20px margin should add up to 100% of the available space. (See screenshot below)
Heres a basic jsfiddle to get started: jsfiddle
code for jsfiddle link to work
Is this possible without javascript?
Easiest, safest way I know to do something like this is a nested <div>, using the outside div as a container for layout purposes. See here: http://jsfiddle.net/u7VzB/1/
HTML
<div id="container">
<div id="div1">div#1</div>
<div id="div2">
<div>div#2 inner</div>
</div>
</div>
CSS:
#container
{
color: white;
margin-top: 50px;
}
#div1
{
float: left;
width: 30%;
background-color: black;
}
#div2
{
float: left;
width: 70%;
}
#div2 > div {
margin-left: 20px;
background-color: blue;
}
You can also do something like this without disturbing HTML code:
#container {
color: white;
margin-top: 50px;
position: relative;
}
#div1 {
float: left;
width: 30%;
background-color: black;
}
#div2 {
float: left;
position:absolute;
left: 30%;
margin-left: 20px;
right: 0px;
background-color: blue;
}
Working Fiddle
try by setting float left, right and reduce the width
#container
{
color: white;
margin-top: 50px;
}
#div1
{
float: left;
width: 29%;
background-color: black;
}
#div2
{
float: right;
width: 69%;
background-color: blue;
}