Pure CSS Two Column Layout [duplicate] - html

This question already has answers here:
How to make a stable two column layout in HTML/CSS
(6 answers)
Closed 4 months ago.
I am currently using Jekyll, and I am attempting to create something that looks like this, where the code is on the right and the explanations are on the left.
The output from Jekyll's markdown processor will look something like this:
<p>Some explanation goes here</p>
<pre> // some code goes here </pre>
<p>Another example...</p>
<pre> // more example code goes here </pre>
So far, I have been able to achieve the two-column look by using float in CSS and making width: 50%;.
pre {
float: right;
width: 50%;
}
h1, h2, h3, h4, h5, h6, p, a {
float: left;
width: 50%;
margin-right: 50%;
}
However, this results in the <pre> tags being below the text I want, whereas I want the code to the right of the text.
What would be the best way to solve this problem using pure CSS?
Thanks!

Here is a simple demo.
HTML:
<div class="left">
<p>Some explanation goes here</p>
<p>Another example...</p>
</div>
<div class="right">
<pre> // some code goes here </pre>
<pre> // more example code goes here </pre>
</div>
CSS:
div.left {
float: left;
width: 50%;
}
div.right {
float: right;
width: 50%;
}

Two block elements have the width 50%, margin is also 50%, and that's 150%. Browser max. width is 100%, so you need to eliminate margin and any border around elements (border also have some width, no matter how small..) in order to make float works.
You may set width of the two block elements on, for example, 45 % (without any margin), and because they are floating right and left, you'll have the 10 % gap between them.
Ancor is not a block element, to make behave like such you'll need to write in css:
a {display: block}
'pre' element needs 'overflow' set to 'auto' or 'hidden'.

Move the pre tag above the left column in the HTML - floating elements to the right often means they need to appear before the left side items in the HTML. Also, wrapping both columns in a common div will allow you to clear any previous columns.
You can use the calc property for widths....
<div class="wrap">
<div class="rightcol">
<pre> //Code output </pre>
</div>
<div class="leftcol">
<h1>Some Text here</h1>
</div>
<div class="clear"></div>
</div>
You can loop the above HTML and use it as often as you want. It will use the same CSS and create 2 columns in every iteration.
.wrap {
clear: both;
padding: 10px;
margin: 20px;
border: 1px solid #000;
background: #fff;
}
.rightcol {
width: calc(50% - 22px);
background: #eee;
color: #333;
border: 1px solid #aaa;
float: right;
padding: 10px;
display: inline-block;
height: 200px; /*this is just for the fiddle*/
}
.leftcol {
width: calc(50% - 22px);
display: inline-block;
padding: 10px;
}
h1 { margin:0; padding:0;}
.clear { clear: both; }
Here's a jsFiddle Sample
Some minor CSS media queries for the left and ride side would allow this to be responsive.

Weave: http://kodeweave.sourceforge.net/editor/#f336823273b963b2c364bc34bd11a1d5
If you want resizable columns take a look into JqxSplitter. (requires JQuery)
html, body {
height: 100%;
}
body {
background: #dedede;
}
.content {
padding: 10px;
margin: 20px;
border: 1px solid #000;
background: #fff;
}
.desc, .code {
width: 43%;
}
.desc {
display: inline-block;
padding: 10px;
}
.code {
display: inline-block;
float: right;
background: #eee;
color: #333;
border: 1px solid #aaa;
height: 100%;
padding: 10px;
}
<div class="wrapper">
<div class="content">
<div class="desc">
<h3>Data Organization</h3>
Data on Quandl is organized into databases and datasets.
<p>A dataset is a single time series, with one or more columns. Column 0 of a dataset is always the date column. Columns 1 to n are data columns.</p>
<p>A database is a collection of datasets from a single publisher and/or on a single subject.</p>
<p>The Quandl API provides methods to access both dataset and database objects, provided you know their Quandl codes.</p>
</div>
<pre class="code">html, body {
height: 100%;
}
.lorem {
border: 1px solid #000;
}
.ispum {
float: left;
}
.door {
float: right;
}</pre>
</div>
<div class="content">
<div class="desc">
<h3>Quandl Codes</h3>
Every database on Quandl has a unique string identifier called the database_code.
<p>Every dataset on Quandl belongs to one and exactly one database. Every dataset thus has a database_code as well as a dataset_code associated with it. Both of these are required to uniquely identify the dataset.</p>
<p>The combination of database_code and dataset_code is called the Quandl code.</p>
</div>
<pre class="code">html, body {
height: 100%;
}
.lorem {
border: 1px solid #000;
}
.ispum {
float: left;
}
.door {
float: right;
}</pre>
</div>
</div>

This is very simple. Add float:left to paragraphs and code blocks. Use clear:left on p's. Make sure there is enough space for two elements next to each other. Add overflow:auto to the container. Like this: http://codepen.io/anon/pen/grqRPr. Add some padding if you want a 'gutter'.

Related

Nesting divs but its not working?

My div should contain two more divs inside (in-left and in-right), but in-right isn't working. How am I supposed to align it with in-left?
#left {
position: absolute;
top: 76%;
left: 20%;
color: black;
border: 2px solid black;
border-radius: 15px 15px;
padding-left: 15px;
padding-right: 15px;
font-size: 1em;
text-align: center;
background-image: url("pink.jpg");
height: 1000px;
width: 800px;
background-size: 900px 1000px;
border: 1px solid black;
background-repeat: no-repeat;
box-shadow: 7px 7px 18px white;
}
#in-left {
top: 87%;
left: 22%;
color: black;
font-size: 1.5em;
text-align: left;
height: 650px;
width: 400px;
font-family: AR CENA;
border-right: 1px solid white;
}
#in-right {
top: 87%;
left: 50%;
color: black;
font-size: 1.5em;
text-align: right;
height: 650px;
width: 400px;
font-family: AR CENA;
}
<div id="left"><br>
<center>
<img src="acoe.jpg" alt="it's me" height="200" width="250"><img src="jer.jpg" alt="it's me" height="200" width="250"><img src="ako ulit.jpg" alt="it's me" height="200" width="250"></center>
<div id="in-left">
<center>
<h2>
Hobbies
</h2>
</center>
<ul>
<u><b><li>Biking 🚵</li></u></b>
I bike around the subdivision every other day, alone and sometimes with my friends. I really enjoy the solitude and the way the air hits my hair, and I can proudly say that biking is my relaxation technique.
<u><b><li>📖 Reading books and short stories 📖</li></u></b>
I usually spend my time indoors, and reading has been a big help for me to ease my boredom. I enjoy the horror genre because of the feeling of thrill and excitement it gives me. Reddit:
<img src="reddit.png" height="25" width="25">
<u><b><li>📽 Watching movies 🎥</li></u></b>
<u><b><li>🎧 Listening to music 🎶</li></u></b>
<u><b><li>Playing Videogames 🎮</li></u></b>
<u><b><li>🍔 Eating 🍳</li></u></b>
</ul>
</div>
<div id="in-right">
<center>
<h2>
Interests:
</center>
</h2>
</div>
</div>
use the float property of CSS. thanks
float : right;
Using flexbox will help you to achieve a solution easily. Check the snippet.
div {
border: 1px solid #ddd;
padding: 10px;
}
.container {
display: flex;
}
.in-left, .in-right {
flex-grow: 1;
}
<div class="container">
<div class="in-left">
Left
</div>
<div class="in-right">
Right
</div>
</div>
There are a number of options that allow you to achieve what you're looking for here, but before I start listing them, a quick piece of advice when it comes to HTML and CSS: "The more you try to do, the more difficult it will become, try to look for the simplest solution".
With that in mind, let's look for a few simple solutions which let you achieve what you're looking for.
Option 1: Float
float is a brilliant property which allows you to align div elements within their parent container. It can work really well, however you need to be careful because (as the MDN documentation states):
...the element is taken from the normal flow of the web page...
What this means is that your parent container won't be sized to contain your div anymore. To fix this, you can use the clear property on the parent's ::after pseudo-element, which will force it to resize correctly.
.parent {
background: red;
color: white;
}
.parent::after {
content: "";
display: block;
clear: both;
}
.left {
float: left;
background: blue;
padding: 10px;
}
.right {
float: right;
background: green;
padding: 10px;
}
<div class="parent">
<div class="left">
My first div
</div>
<div class="right">
My second div
</div>
</div>
Option 2: Inline Blocks
The next option takes advantage of the display property which allows you to configure how the Browser renders the element. Specifically, it allows you to configure the rendering box used by the Browser. By default a <span> element uses the inline display mode, while a div uses the block display mode. These correspond to (roughly) horizontal and vertical layout ordering as you can see in the following example:
<div>
<span>First</span>
<span>Second</span>
</div>
<div>
<span>Third</span>
<span>Fourth</span>
</div>
What display: inline-block allows us to do is instruct the browser to render the blocks as normal, but arrange them horizontally as though they were part of the normal text flow. This works really well and is much better supported by older browsers than Option 3 (but not as well as Option 1).
.parent > div {
display: inline-block;
}
.parent {
background: red;
color: white;
}
.first {
background: blue;
padding: 10px;
}
.second {
background: green;
padding: 10px;
}
<div class="parent">
<div class="first">First</div>
<div class="second">Second</div>
</div>
Option 3: Flexbox
The coolest option, albeit the newest and therefore least supported by older browsers, is using the new flexbox layout mode. It's currently still in draft state, but a lot of modern browsers support it already.
Flexbox lets you do the same kind of thing as Option 2 but with much better control over how things get arranged, the spacing between them, how they flow onto other lines and so on. There's a lot that can be covered and I'm not going to do that all here, but the part that applies to you is this:
.parent {
display: flex;
flex-direction: horizontal;
justify-content: space-between;
background: red;
color: white;
}
.first {
padding: 10px;
background: blue;
}
.second {
padding: 10px;
background: green;
}
<div class="parent">
<div class="first">First</div>
<div class="second">Second</div>
</div>
As this is a school project, my suggestion is that you spend some time reading up on (and experimenting with) the various options here and getting a feel for what they do. Which one you end up using is a lot less important than learning how to use them in the first place. Best of luck with it.
I have updated your code so it will look more cleaner. I have also created a class inlineblock to the CSS and added to both div elements inside the #left parent element. In your HTML code there are syntax errors like in closing tags.
Here is the link I have created for you https://jsfiddle.net/beljems/fyyqvm1t/13/.
Hope this will help you :)
Just try to use "float: left"
Here u have tutorial for using this
CLICK
If u want to delete the "float" on rest space of site u need to use "clear: both"

CSS child div tag not spanning inside parent div tag

I created 1 parent id named "mod1" and 2 child classes named "left" and right".
When I call the left and right classes inside the parent mod1 id, they are overflowing from the parent id's height. I don't want to explicitly mention height of parent id "mod1", I simply want it to stretch as per the child classes within. The problem is that the parent id is not actually CONTAINING both it's child classes i.e. none of the child are having background color as #888 and the border seems to appear right above them.
This is my html code
<head>
<style>
body
{
max-width: 600px;
margin: auto;
}
#mod1
{
background-color: #888;
border: 1px solid black;
}
#mod1 .left
{
float: left;
width: 75%;
}
#mod1 .right
{
float: left;
width: 25%;
}
</style>
</head>
<body>
<div id="mod1">
<div class="left">
Book Accomodation + Deals With The Best In The Business
</div>
<div class="right">
VIEW ON THE WEB
</div>
</div>
</body>
The reason why the parent's height appears to collapse is because when you float an element, it is taken out of the document flow and therefore does not contribute to the computation of the parent container's final dimensions. If all the children are floated, then the parent's height will collapse to zero.
The solution is rather simple: use overflow: hidden to clear the float in the parent element. However, if you have overflowing content that you want to show (like a dropdown menu), you will have to employ the clearfix solution.
#mod1
{
background-color: #888;
border: 1px solid black;
overflow: hidden;
/* overflow: auto; will also work fine */
}
You can see from the snippet below that adding the rule works:
body
{
max-width: 600px;
margin: auto;
}
#mod1
{
background-color: #888;
border: 1px solid black;
overflow: hidden;
}
#mod1 .left
{
float: left;
width: 75%;
}
#mod1 .right
{
float: left;
width: 25%;
}
<div id="mod1">
<div class="left">
Book Accomodation + Deals With The Best In The Business
</div>
<div class="right">
VIEW ON THE WEB
</div>
</div>
The CSS property float causes the element to "collapse", which is why the container element looks like it's not containing the left and right elements. I usually make a dummy div below them, inside the container class, and give it the property clear: both;
<div id="mod1">
<div class="left">
Book Accomodation + Deals With The Best In The Business
</div>
<div class="right">
VIEW ON THE WEB
</div>
<div class="dummy"></div>
</div>
and give it the style:
.dummy {
clear: both;
}
See other possible solutions: How do you keep parents of floated elements from collapsing?
You have to clear your floats when using floats (most of the time).
Use flex instead. Then you don't have to clear floats. It makes things like this easier.
body {
max-width: 600px;
margin: auto;
}
#mod1 {
display: flex; /* Tells the browser you want children to use flex */
font-family: sans-serif;
color: white;
}
.left,
.right {
flex: 1; /* Tells the browser to take "one piece of pie" for the width (or height, depending on flex-direction) */
padding: 1em;
}
.left {
background: orange;
}
.right {
background: darkorange;
}
<div id="mod1">
<div class="left">
Book Accomodation + Deals With The Best In The Business
</div>
<div class="right">
VIEW ON THE WEB
</div>
</div>

CSS float is not working

After thoroughly researching for the way to fix this I have still not found the answer I seek. I finally decide to post my problem on stackoverflow.com because I finally give up trying to find the answer. What I get as a result is two boxes with content on top and one box on the bottom.
Here is the CSS code:
#content_area
{
overflow: hidden;
display: inline-block;
background: white;
margin-top: 20px;
margin-right: 110px;
margin-left: 110px;
margin-bottom: 20px;
}
.box
{
display:inline-block;
width: 33.33%;
float: left;
background: #FFFFFF;
padding: 15px;
}
Here is the HTML Code:
<div>
<div class="box">
//enter text here
</div>
<div class="box">
//enter text here
</div>
<div class="box">
//enter text here
</div>
</div>
The problem is your padding, as mentioned above.
Here is a fiddle with the padding removed and colours added: http://jsfiddle.net/gj0wmgym
.box
{
display:inline-block;
width: 33.33%;
float: left;
background: #FFFFFF;
}
The problem with your code is that your .box class assigns a width of 33%, with additional padding. This leads to a total width of more than 100%. Padding is added to the .box's initial width because that's how the default box model works in CSS.
To fix this problem, add this line to the .box's style declarations:
box-sizing: border-box;
You can see a live demo here. If you want to learn more about the box model, this article by Chris Coyier is an excellent reference.
From what I can tell, your floats are working correctly.
Your html was missing the id attribute, so make sure to add that to your html.
What you are probably expecting is for the floats to not wrap to the next line, which is because the padding gets added to the width size (the elements are greater than 33%). You instead need to set the box sizing attribute see this article
* {
box-sizing:border-box;
}
#content_area
{
overflow: hidden;
display: inline-block;
background: white;
margin-top: 20px;
margin-right: 110px;
margin-left: 110px;
margin-bottom: 20px;
}
.box
{
display:inline-block;
width: 33.33%;
float: left;
background: #FFFFFF;
padding: 15px;
}
<div id="content_area">
<div class="box">
//enter text here
</div>
<div class="box">
//enter text here
</div>
<div class="box">
//enter text here
</div>
</div>

Advice regarding wrapping text inside a div

I'm creating a contact card style layout, with a photo and text next to it, as demonstrated in this fiddle here:
http://jsfiddle.net/L7pWv/5/
HTML:
<div class="container">
<div class="contact-card">
<div class="photo"></div>
<div class="details">
<span class="name">My Name</span>
<span class="description">This is some really long text that should wrap nicely when things all work OK</span>
</div>
<div class="clearfix"></div>
</div>
<div class="contact-card">
<div class="photo"></div>
<div class="details">
<span class="name">My Name 2</span>
<span class="description">Short description</span>
</div>
<div class="clearfix"></div>
</div>
</div>
CSS:
.container {
width: 350px;
}
.contact-card {
background-color: whitesmoke;
border-bottom: 1px solid #ddd;
white-space: nowrap;
}
.contact-card .photo {
float: left;
width: 80px;
height: 50px;
background-color: tan;
margin: 10px;
}
.contact-card .details {
display: inline-block;
margin: 10px 0;
}
.contact-card .name {
display: block;
font-weight: bold;
line-height: 1em;
}
.contact-card .description {
display: block;
font-size: 0.8em;
color: silver;
line-height: 1em;
white-space: normal;
}
.clearfix {
clear: both;
}
As you can see from running the fiddle, when the text is really long, it does wrap eventually, based upon my white-space setting, but it exceeds the size of the contact card before doing so. I could put a right margin of 90px on the "description" class to keep the text within the bounds (which works), but I can't help but feel this is wrong. I'd like it to naturally want to stay within its parent's bounds, but can't think of the best way to achieve that. Any ideas?
Consider making these changes:
.contact-card {
display: inline-block;
}
.contact-card .details {
display: block;
}
This will keep each card displaying inline while keeping the text of the card inside the block without specifying a margin.
Kind of a tricky one, as I don't know what uses you'll be putting this in, but I'd probably do it with these changes.
Get rid of
<div class="clearfix"></div>
It's not needed if you make a simple addition like:
.contact-card {
float:left;
}
Then change .contact-card .details to this:
.contact-card .details {
padding: 10px 0;
}
That should give you the "The width of the details element should really be dictated by the parent." behaviour you're after
http://jsfiddle.net/L7pWv/6/
I suggest just don't use inline-block for this. You don't want the .detail element overflow on it's parent element. Because you already floated your photo, you can just place the element next to the photo element.
Note that you should use padding when you want space inside the element and use margin when you want it outside of the element.
There is no need for white-space: nowrap; as you floated the photo.
jsFiddle
The only thing i changed is the use of padding and margin and removed the white-space .
CSS:
.contact-card .details {
display: inline-block;
margin: 10px 0;
width:70%;
}
Fiddle: http://jsfiddle.net/L7pWv/2/

Left-align right-aligned div in case there is too less space without media queries

I am developing the HTML/CSS of a product item detail view. In case there is enough space, I want to left-align the price and article number (.Left) and right-align two links (.Right). If there is too less space, I want to left-align all the data and display each info on a separate line. Is there any way I can achieve this without JavaScript and without media queries? As my product item detail view is within multiple nested responsive elements, I would need to add several breakpoints in order to right-align, left-align, right-align etc. which is unsexy.
My HTML is as follows:
<div class="Specification">
<div class="InfoLine">
<div class="Left">CHF 99.90</div>
<div class="Right">LINK1</div>
</div>
<div class="InfoLine">
<div class="Left">Article-No: 31-4AB</div>
<div class="Right">LINK2</div>
</div>
</div>
Thanks for your help!
Edit:
Right now, my CSS looks like this:
.Specification .InfoLine
{
border: 1px dotted pink;
clear: both;
line-height: 40px;
}
.Specification .InfoLine .Left
{
border: 1px dotted green;
float: left;
display: inline-block;
width: 295px;
max-width: 295px;
}
.Specification .InfoLine .Right
{
border: 1px dotted blue;
display: inline-block;
min-width: 185px;
float: left;
text-align: left;
}
... which produces the following output:
a) Not OK: Enough space - .Right box should be right-aligned (not only the box, but also the text)
b) OK: Not enough space - .Right box is left-aligned below the .Left box
try this:
.Left{
float:left;
}
.Right{
float:right;
}
fiddle here
Try
.selectorName{
display:inline-block;
}
and remove float property from the code..