CSS for two columns which float left - html

I want to display two pieces of text side by side in columns using CSS. The left-hand column text is variable in length, and the right-hand text is fixed and always the same length.
I want the two "columns" to float to the left next to each other e.g.
[Variable Text] [Fixed text]
If the variable text is long I want it to wrap.
eg,
Here is a very long Hello World
piece of variable
text which wraps
My code works if the variable text is short, but I get unwanted whitespaces if the variable text wraps.
eg,
Here is a very long Hello World
piece of variable
text which wraps
Here is my code:
#wrapper {
margin-right: 100px;
}
#left-col {
float: left;
max-width: 100%;
background-color: #CCF;
}
#right-col {
float: left;
width: 100px;
margin-right: -100px;
background-color: #FFA;
}
#cleared {
clear: both;
}
<div id="wrapper">
<div id="left-col">Here is a very long piece of text which wraps</div>
<div id="right-col">Hello World</div>
<div id="cleared"></div>
</div>

I believe changing #left-col's property max-width to other than 100% it will work. For example:
#wrapper {
margin-right: 100px;
}
#left-col {
float: left;
max-width: 200px;
background-color: #CCF;
}
#right-col {
float: left;
width: 100px;
background-color: #FFA;
}
#cleared {
clear: both;
}
Here is a jsfiddle with various text lengths for clarity.

you could maybe update your html to something more meaningfull and easier to style without class ou id. (a <dl> is not the most efficient but shows alternative tags easy to style)
calc() can be useful here :)
#wrapper {
width:15em;/* demo purpse */
}
dl,dt,dd {
padding:0;
margin:0;
}
dt {
float: left;
clear:left;
text-align:justify;/* to fill up entire first lines when text wraps*/
max-width: calc(100% - 100px);
background-color: #CCF;
margin-top:0.1em;
}
dd {float:left;
width: 100px;
background-color: #FFA;
}
<dl id="wrapper">
<dt>1 Here is a very long piece of text which wraps</dt>
<dd>Hello World 1</dd>
<dt>2 Here is a very long piece of text which</dt>
<dd>Hello World 2</dd>
<dt>3 Here is a very long piece of text</dt>
<dd>Hello World 3</dd>
<dt>4 Here is a very long piece</dt>
<dd>Hello World 4</dd>
<dt>5 short</dt>
<dd>5</dd>
</dl>

Related

Make divs same width with pure css

Is there a pure CSS way to create a div that matches another's div width? There is no parent div.
.green-div-1 {
background-color: #00ba00;
width: 500px;
height: 20px;
}
.blue-div-1 {
background-color: #8888FF;
max-width: 90%;
float: left;
}
.green-div-2 {
background-color: #00ba00;
width: 500px;
height: 20px;
}
.blue-div-2 {
background-color: #8888FF;
max-width: 90%;
float: left;
}
<div class="green-div-1">
<div class="blue-div-1">
<label>long text long text long text long text long text long text </label>
</div>
</div>
<br/>
<div class="green-div-2">
<div class="blue-div-2">
<label>short text</label>
</div>
</div>
Example: https://jsfiddle.net/6kgjnL15/
not sure if this is exactly what you are looking for. but changing max-width to just width then setting that at 100% makes both blue divs identical.
.blue-div-1{
background-color:#8888FF; width:100%; float:left;
}
.blue-div-2{
background-color:#8888FF; width:100%; float:left;
}
Using pseudo elements, you can get the desired result. You can find the code below. However, I highly recommend using JavaScript for your problem, as you can achieve a much more stable and flexible solution.
In case the background-color is not important, try this:
.green-div-1{
background-color:#00ba00; width:500px;height:20px;
}
.blue-div-1{
max-width:90%; float:left;
}
.green-div-2{
background-color:#00ba00; width:500px;height:20px;
}
.blue-div-1::after {
content:"short text";
display: block;
height: 20px;
margin-top: 20px;
}
<div class="green-div-1">
<div class="blue-div-1">
<label>long text long text long text long text long text long text </label>
</div>
</div>
<br/>
<div class="green-div-2">
</div>
else, you could add another element to cover the space in between:
.green-div-1{
background-color:#00ba00; width:500px;height:20px;
}
.blue-div-1{
background-color:#8888FF; max-width:90%; float:left;
}
.green-div-2{
background-color:#00ba00; width:500px;height:20px;
}
.blue-div-1::after {
content:"short text";
display: block;
background-color:#8888FF;
height: 20px;
margin-top: 20px;
}
#space {
height: 20px;
background-color: white;
position: relative;
top: -40px;
}
<div class="green-div-1">
<div class="blue-div-1">
<label>long text long text long text long text long text long text </label>
</div>
</div>
<br/>
<div class="green-div-2">
</div>
<div id="space">
</div>
Actually there is no need of float: left; as they both are in separate div's
try removing the float: left from
.blue-div-1{
background-color:#8888FF; max-width:90%;
}
.blue-div-2{
background-color:#8888FF; max-width:90%;
}
here is the link: https://jsfiddle.net/s8ufxu1m/2/
Also this will help to understand more CSS Box Model: https://css-tricks.com/the-css-box-model/

Pure CSS Two Column Layout [duplicate]

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'.

How do I get a div to show up on the next line without using clear:both?

I am working on a small piece of a complex web page where there are divs that I don't directly control being placed on the right side of the page. Therefore, when I use a clear:both (as suggested here) to force my div to the next line, it will jump down the page past the content on the right side -- which I don't want.
here is a fiddle which is a stripped down version of my code -- hopefully containing the relevant portions:
https://jsfiddle.net/yyt0tkLr/
I want the 'text underneath' to show up on the next line. I know clear:both would do it but as I said, it interacts poorly with the rest of the page.
My whole widget needs to be wrapped in divs otherwise I could put the text outside of the div wrapper and it would show. The divs I have in there supply precise padding which I require -- most of the pieces need padding so I can't really just remove divs, although I could add some divs as long as they don't mess up my other formatting (wrap the long text, buttons stacked and width of text, etc.).
I do need this to work on all browsers and should display correctly even without javascript enabled.
Here is the full code:
CSS:
.images {
float:left;
border:1px solid black;
height: 100px;
padding:6px;
}
.underneath {
display: inline-block;
float: left;
width: 100%;
}
.rightcol {
float: right;
border:1px solid black;
height: 200px;
}
.row {
border:1px solid black;
padding:5px;
}
.text {
padding:10px;
}
.buttonstack {
border:1px solid black;
display: inline-block;
}
.button {
border:1px solid red;
}
.wrapper {
}
HTML
<div class='row'>
<div class='rightcol'>stuff on right</div>
<div>
<div class='images'>images</div>
<div> <span class='text'> long text long text long text long text long text long text long text long text long text long text
</span>
<div class='buttonstack'>
<div class='button'>button</div>
<div class='button'>button2</div>
</div>
</div>
</div>
<div class='underneath'>text underneath</div>
</div>
Note: I've updated my fiddle to demonstrate the div on the right side, this more closely represents my page.
You could use these styles:
.underneath {
float: left;
width: 100%;
}
Fiddle
.divinquestion {
// your sweet styles
display:inline-block; // add this
}
Try adding this to your .row
float:left;
height:100%;
So it would be
.row {
border:1px solid black;
padding:5px;
float:left;
height:100%;
}
Looks like I found my answer, clear: left added to the .underneath does the trick, like so:
.underneath {
clear: left;
}

Vertical align middle inside div columns

Is it possible, with CSS, while using a row with two column, one with an image and another with text, to have their content vertically aligned in the middle?
I've been banging my head for days over this and tried everything I could possibly think of.
This is the basic structure that I'm using which is entirely based on percentages. This is for a responsive one-page layout based on a series of sections, each with min-height set to 100%.
CSS
html, body {
width: 100%;
height: 100%;
}
section {
width: 100%;
min-height: 100%;
display:table;
height:inherit;
}
.row {
width: 100%;
height: 100%;
display:table-cell;
}
.col-left, .col-right {
float: left;
width: 50%;
height: 100%;
}
/*--this should be vertically centred--*/
.content {
}
HTML
<section>
<div class="row">
<div class="col-left">
<div class="content">
<h1>SOME TEXT</h1>
</div>
</div>
<div class="col-right">
<div class="content">
<img src="SOME IMAGE URL">
</div>
</div>
</div>
</section>
JSFiddle
.row {
...
display:table-row;
}
.col-left, .col-right {
...
display: table-cell;
vertical-align: middle;
}
Demo
You were 95% of the way there as you laid out the structure using display:table, and display:table-cell, but you floated what should have been display:table-cell, and set table-cell on what should have been table row. Use this instead:
html, body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
section {
width: 100%;
min-height: 100%;
display:table;
height:inherit;
}
.row {
width: 100%;
height: 100%;
display:table-row;
}
.col-left, .col-right {
display:table-cell;
width: 50%;
height: 100%;
vertical-align:middle;
}
.col-left {
background: MidnightBlue
}
.col-right {
background: ForestGreen;
text-align: center;
}
.content {
border: 1px dashed red;
}
h1 {
font-family: sans-serif;
color: white;
}
<section>
<div class="row">
<div class="col-left">
<div class="content">
<h1>I should be vertically aligned in the middle...<br><br>And so should the image on the right...
</h1>
</div>
</div>
<div class="col-right">
<div class="content">
<img src="https://cdn4.iconfinder.com/data/icons/miu-flat-social/60/stackoverflow-128.png">
</div>
</div>
</div>
</section>
you can try this:
html, body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
section {
width: 100%;
min-height: 100%;
display:table;
height:inherit;
}
.row {
width: 100%;
height: 100%;
display:table;
}
.col-left, .col-right {
float: left;
display:table;
vertical-align:middle;
width: 50%;
height: 100%;
}
.col-left {
background: MidnightBlue
}
.col-right {
background: ForestGreen;
text-align: center;
}
.content {
display:table-cell;
vertical-align:middle;
height:100%;
border: 1px dashed red;
}
h1 {
font-family: sans-serif;
color: white;
}
<section>
<div class="row">
<div class="col-left">
<div class="content">
<h1>I should be vertically aligned in the middle...<br><br>And so should the image on the right...
</h1>
</div>
</div>
<div class="col-right">
<div class="content">
<img src="https://cdn4.iconfinder.com/data/icons/miu-flat-social/60/stackoverflow-128.png">
</div>
</div>
</div>
</section>
For anyone interested in this topic, this issue and the answers provided raised in me another question regarding which method to apply in this situation, and in fact, for building columns in general: Floating or Display property. For any newbie or self-taught wannabe developer like my self may be interesting to know what I've concluded after research and testing.
I find my self often using different methods for building columns that revolve around Floating the elements and in one way or another always require some hacking in the end to do exactly what I want, leaving me with a feeling of inconsistency and unreliability.
One would think that something so vital for layout structure would have at this point in time some obvious, elegant and simple solution. Apparently it has, and it's called Display Table. It might have limitations in some very specific situations but in general it's all you need. It's rock solid and you can pretty much do anything you want with it. Unfortunately, I think the word "table" is still a kind of taboo. This method however is simple, comprehensible, reliable and doesn't require any hacks to behave as expected. Vertical alignment which is always a struggle is also made easy this way.
A simple structure like this is all you need:
.container {
display: table;
}
.row {
display: table-row;
}
.col {
display: table-cell;
}
For some reason (probably because of the nasty word "table"), you wont be able to find this method suggested anywhere with a simple search on basic css columns.
I thought this articles on the subject were interesting:
Give Floats the Flick in CSS Layouts
Farewell Floats: The Future of CSS Layout

div superimposed on other divs

I have a simple html code with div tags
<div class="left">Proj Name:</div>
<div class="right">must have a name</div>
<div >Shouldn't this be on a new line?</div>
and the classes are defined in a style sheet as
.left {
float: left;
width: 125px;
text-align: right;
margin: 2px 10px;
display: inline
}
.right {
float: left;
text-align: left;
margin: 2px 10px;
display: inline
}
The problem i am having is that there seems to be a super-imposition where any div tag that comes after ignores the existence of the former tags whenever there is an align element involved. Please see http://jsfiddle.net/tea0phnr/2/ for what i am talking about.
CSS
.clear {clear:both;}
HTML
<div class="left">Proj Name:</div>
<div class="right">must have a name</div>
<div class="clear"></div>
<div >Shouldn't this be on a new line?</div>
http://jsfiddle.net/tea0phnr/3/
Your floating divs are being pulled out of flow - causing the last div to resume their place in the actual flow. You'd either need to clear:both; the last div, or perhaps with a pseudo element. ( div:last-child:after )
div:last-child {
clear: both;
}
http://jsfiddle.net/hzdcu1xw/
or have it float + width: 100%; as well.
div:last-child {
float: left;
width: 100%;
}
http://jsfiddle.net/efapLo2d/ in order for it to layout accordingly.