How to create a 3 column layout in css? - html

I have been trying to make a complex UI for my program and I wanted to be able to have 3 columns using css in my design.
This is currently my code:
<div style="width:100px;">stuff</div>
<div style="width:100px;">stuff</div>
<div style="width:100px;">stuff</div>
But this, for some reason, will display 3 different lines of stuff.
I have tried to change some things but it didn't seem to work at all
I just want there to be 3 columns on the same block.

If you want to have 3 differnet areas on the screen, the effective method for doing that would be:
<style> .third { width: 33.33%; float: left; } </style>
<div class="third"> Something </div>
<div class="third"> Something </div>
<div class="third"> Something </div>
The class="third" is adding the css that is inside of the {}'s that I have made. - Meaning that each of the div's are given the width: 33.33% (1/3 of the screen) and a float: left which will just move the areas to be able to move out of the normal CSS and HTML scope of stacking on top of each other.
Hope this helps! :)

There are a couple ways to accomplish what you want.
Method 1: Float and width
Assign a single column class
.column {
width: 33.3%;
float: left;
}
Markup three divs with said class
<div class="column">Column 1</div>
<div class="column">Column 2</div>
<div class="column">Column 3</div>
Method 2: Inline block
Sometimes floats aren't the best option. You cna also set the display property to inline-block, although this can sometimes leave unwanted gaps in between the divs.
.column {
width: 33.3%;
display: inline-block;
}
Same HTML markup
<div class="column">Column 1</div>
<div class="column">Column 2</div>
<div class="column">Column 3</div>
Method 3: Flexbox
Flexbox according to Chris Coyier of CSS-tricks:
The Flexbox Layout (Flexible Box) module (currently a W3C Last Call Working Draft) aims at providing a more efficient way to lay out, align and distribute space among items in a container, even when their size is unknown and/or dynamic (thus the word "flex").
.row {
display: -ms-flex: // Vendor prefixes required for flexbox
display: -webkit-flex;
display: flex:
}
.column {
width: 100px;
display: -ms-inline-flex;
display: -webkit-inline-flex;
display: inline-flex;
-webkit-flex-direction: row;
-ms-flex-direction: row;
flex-direction: row;
}
Add the parent div to your HTML markup
<div class="row">
<div class="column">Column 1</div>
<div class="column">Column 2</div>
<div class="column">Column 3</div>
</div>
Cool thing about flexbox is you don't need to fill the space using set percentages, it can space your columns out with justify-content: space-between;
There is a lot more to delve into with flexbox. Floats are very simple but since you mentioned building a UI, something like flexbox will give you a wider array of tools to work with.

Alternatively, you could style them all at once without giving a class as mentioned by Jek. If you are using styling within the html, you could do this in the header:
<style>
div{
width:100px
}
</style>
You could do the same if you are using an external stylesheet. However, if you have to style the divs in different manners using class and id would be a better option. If all divs are styled in the same way, simply style the tag, which is div in your case.

Related

Container height equal to it's overlapping children with dynamic heights

Consider you are a block container with fixed width, height: auto and two elements inside of you. Each element has it's own height based on the number of items inside it. Now, you want your children to be positioned at the top, overlapping each other. But, at the same time you want your height to be equal to the height of the child with the most items.
Do you think you can handle it on your own, without asking that weird JS wizardly-guy to look after you and your children?
Also, your children seems to like dynamically changing the number of items inside of them, so you sadly can't use fixed heights here.
Desired result:
(each item is translated to the left a bit for better view)
Yay, some code!
<div class="container">
<div class="child">
<div class="item"></div>
<!-- ... -->
</div>
<div class="child">
<div class="item"></div>
<!-- ... -->
</div>
</div>
.container {
width: 300px;
}
.menu {
}
.item {
height: 30px;
}
👨‍💻 JSFiddle with example
Yes, you can do this! If you are a CSS grid.
According to the specs, if you put two elements in the same cell. they will overlap each other and with align-items: start; you can align items by their tops.
<div class="container">
<div class="child">
<div class="item"></div>
<!-- ... -->
</div>
<div class="child">
<div class="item"></div>
<!-- ... -->
</div>
</div>
/** Creating a CSS grid with a single cell */
.container {
display: grid;
grid-template-columns: 1fr;
grid-template-rows: 1fr;
grid-template-areas: 'cell';
align-items: start;
}
/** Putting both children in the first cell */
.child {
grid-area: cell;
}
.item {
height: 30px;
}
And here's the updated JSFiddle to demonstrate the solution.
CSS grid has a pretty good browser coverage. Make sure to use autoprefixer for IE though.
If you know another/better approach, please add it as well, I'm sure it will be useful.

CSS tables without nested divs?

Is it possible to create grid layouts with display: table without nested divs?
<div id="container">
<div id="article1">
<div id="article2">
<div id="article3">
<div id="article4">
<div id="article5">
<div id="article6">
</div>
The result should be for example a 3x2 table. If I apply display: table-cell to the article divs I get them all in a row. I assume there is no possibility to create a new row after 3 divs without nesting them in HTML?
It's better to use flexbox model instead, like this:
#container {
display: flex;
flex-wrap: wrap;
}
#container>div {
width: 33.3%;
flex-shrink: 0;
}
Check the example here - on the codepen.
So no need in additional div tags

How can I put two div-tags next to each other?

How can I put two div-tags next to each other? I tried this:
<div>text 1</div><div>text 2</div>
But they appear underneath each other, just as I would have written them in two lines.
Div's are block level elements, meaning they'll occupy all available space unless told otherwise. In order to display them side-by-side, you'll need to use the CSS property float.
CSS
.float-left{
float: left;
}
than in HTML just assign that class name like:
<div class="float-left"> 1 </div>
<div class="float-left"> 2 </div>
use css float:
you need float: left or right with a specified width for the divs to be aligned beside each other.
<div style="float:left; width:50%; background:yellow">text 1</div><div style="float:left; width:50%; background:green">text 2</div>
Actually, you don't need floats for something this simple. Simply changing the display type to inline will take care of this for you.
See my pen here:
http://codepen.io/KempfCreative/pen/oLNqMZ
div { display: inline; }
is all you need
for a perfect resizing use flexbox
wrap those divs in one parent
with this the inner divs will stretch completely
<div id="parent">
<div>text 1</div><div>text 2</div>
</div>
css
#parent{
display:flex;
flex-wrap:wrap;
width: 100%;
}
#parent div{
flex-grow:1;
}

Positioning two variable-width inline-blocks relatively to each other

I have two variable-width elements that I'm trying to position the following way:
If they fit next to each other on the screen or in their common container, I want them align to the opposite sides of it (i.e. the second one aligned to right).
If they don't, I want them one above the other, but both aligned to the left.
Something as simple as:
<div class="container">
<div style="display: inline-block;">
I'm a variable width left element
</div>
<div style="display: inline-block; float:right;">
I'm right-floating if there's space
</div>
</div>
takes care of the first case, but obviously when the container is small enough for the second div to be rendered below the first one, it's still right-floating which is not what I want.
Is this even possible with pure CSS? I can't use media queries because of unknown/variable widths.
This layout and behavior is possible without media queries and pure CSS using flexbox.
HTML
<!-- horizontal alignment when two boxes fit -->
<div class="container">
<div class="box box1"><span>1</span></div>
<div class="box box2"><span>2</span></div>
</div>
<!-- vertical alignment when two boxes don't fit -->
<div class="container">
<div class="box box3"><span>1</span></div>
<div class="box box4"><span>2</span></div>
</div>
CSS
.container {
display: flex;
justify-content: space-between;
flex-wrap: wrap;
width: 700px; /* for demo only; can be relative length, as well */
}
.box1 { width: 100px; }
.box2 { width: 150px; }
.box3 { width: 400px; }
.box4 { width: 500px; }
DEMO
NOTES:
When there is enough space to fit both variable-width elements on the same row, they are aligned at opposite ends of the container with justify-content: space-between.
When there is not enough space to fit both elements, they wrap with flex-wrap: wrap and align-left because the justify-content: space-between rule will left-align an element when it is alone on the row.
Note that flexbox is supported by all major browsers, except IE 8 & 9. Some recent browser versions, such as Safari 8 and IE10, require vendor prefixes. For a quick way to add all the prefixes you need, post your CSS in the left panel here: Autoprefixer.

How do you align a div vertically without using float?

When doing something like this:
<div style="float: left;">Left Div</div>
<div style="float: right;">Right Div</div>
I have to use an empty div with
clear: both;
which feels very dirty to me.
So, is there a way to align without the use of float?
Here is my code:
.action_buttons_header a.green_button{
}
<div class="change_requests_container" style="width:950px !important">
<div class="sidebar">
Preview New Version
</div>
<div class="content_container">
<div class="content">
<div class="action_buttons_header">
<a href="/changes/merge_changes/422" class="re_publish publish green_button" style="
margin: 5px 0px 5px auto;
">Apply Changes</a>
</div>
<div id="change_list_container">
<div class="changes_table">
<style type="text/css">
#original_492 .rl_inline_added {
display: none;
}
#492.change_container .actial_suggested_text_container{
display: none;
}
</style>
<div class="content_section_header_container">
<div class="content_section_header">
The Zerg |
Overview
<div class="status" id="492_status">
<div id="492_status_placeholder">
</div>
</div>
</div>
</div>
<div class="change_container" id="492">
</div>
</div>
</div>
</div>
I want the green button on the right of the horizontal bar that it's in but in the cleanest way possible.
Just trying to learn how to do CSS elegantly, cleanly, etc.
Another way to do something similar is with flexbox on a wrapper element, i.e.,
.row {
display: flex;
justify-content: space-between;
}
<div class="row">
<div>Left</div>
<div>Right</div>
</div>
In you case here, if you want to right-align that green button, just change the one div to have everything right-aligned:
<div class="action_buttons_header" style="text-align: right;">
The div is already taking up the full width of that section, so just shift the green button the right by right-aligning the text.
you could use things like display: inline-block but I think you would need to set up another div to move it over, if there is nothing going to the left of the button you could use margins to move it into place.
Alternatively but not a good solution, you could position tags; put the encompassing div as position: relative and then the div of the button as position: absolute; right: 0, but like I said this is probably not the best solution
HTML
<div class="parent">
<div>Left Div</div>
<div class="right">Right Div</div>
</div>
CSS
.parent {
position: relative;
}
.right {
position: absolute;
right: 0;
}
It is dirty better use the overflow: hidden; hack:
<div class="container">
<div style="float: left;">Left Div</div>
<div style="float: right;">Right Div</div>
</div>
.container { overflow: hidden; }
Or if you are going to do some fancy CSS3 drop-shadow stuff and you get in trouble with the above solution:
http://web.archive.org/web/20120414135722/http://fordinteractive.com/2009/12/goodbye-overflow-clearing-hack
PS
If you want to go for clean I would rather worry about that inline javascript rather than the overflow: hidden; hack :)
Another solution could be something like following (works depending on your element's display property):
HTML:
<div class="left-align">Left</div>
<div class="right-align">Right</div>
CSS:
.left-align {
margin-left: 0;
margin-right: auto;
}
.right-align {
margin-left: auto;
margin-right: 0;
}
Very useful thing have applied today in my project. One div had to be aligned right, with no floating applied.
Applying code made my goal achieved:
.div {
margin-right: 0px;
margin-left: auto;
}
You could just use a margin-left with a percentage.
HTML
<div class="goleft">Left Div</div>
<div class="goright">Right Div</div>
CSS
.goright{
margin-left:20%;
}
.goleft{
margin-right:20%;
}
(goleft would be the same as default, but can reverse if needed)
text-align doesn't always work as intended for layout options, it's mainly just for text. (But is often used for form elements too).
The end result of doing this will have a similar effect to a div with float:right; and width:80% set. Except, it won't clump together like a float will. (Saving the default display properties for the elements that come after).
No need to add extra elements. While flexbox uses very non-intuitive property names if you know what it can do you'll find yourself using it quite often.
<div style="display: flex; justify-content: space-between;">
<span>Item Left</span>
<span>Item Right</span>
</div>
Plan on needing this often?
.align_between {display: flex; justify-content: space-between;}
I see other people using secondary words in the primary position which makes a mess of information hierarchy. If align is the primary task and right, left, and/or between are the secondary the class should be .align_outer, not .outer_align as it will make sense as you vertically scan your code:
.align_between {}
.align_left {}
.align_outer {}
.align_right {}
Good habits over time will allow you to get to bed sooner than later.