Dynamically specify height of parent based on the tallest child element? - html

I am trying to best figure out how to set two columns' a-elements to 100% of the parent element, without specifying this height, but having it chosen based on the taller of the two columns. At the same time, I want to vertically align the a-element within it's column.
Current code:
<div class="post-nav group">
<div class="post-nav-prev">
<div class="post-nav-border border-reset-right">
<?php previous_post('%', '', 'yes'); ?>
</div>
</div>
<div class="post-nav-next">
<div class="post-nav-border">
<?php next_post('%', '', 'yes'); ?>
</div>
</div>
</div>
CSS:
.post-nav {
}
.post-nav-prev, .post-nav-next {
float: left;
width: 50%;
height: 100%;
}
.post-nav-prev a, .post-nav-next a {
display: flex;
justify-content: center; /* align horizontal */
align-items: center; /* align vertical */
height: 100%;
padding: 10px;
}
.post-nav-prev a {
padding-left: 25px;
}
.post-nav-next a {
padding-right: 25px;
}
.post-nav-border {
border: 1px solid #e3e3e3;
height: 100%;
}
What I eventually run into as the problem is when the two post titles isn't the same amount of lines. I originally had a specified height, but this isn't optimal for the responsive aspect of the website.
So to clarify: How would I go about having the tallest of '.post-nav-prev a', and '.post-nav-next a' specify a height for .post-nav, so the other of the two a elements also get 100% height. I've seen suggestions for display: table, but I couldn't quite get that to work, as well as keeping the vertical alignment of display: flex.
Any suggestions?

The simplest solution (without JS) is indeed to fiddle with the display property and set it the container to table and children to table-cell (you might as well change the actual markup to be a table, the result is the same). With this, vertically aligning becomes easy as hell and the CSS & HTML are much simpler. Here's how:
.post-nav {
display: table;
border-collapse: collapse;
}
.post-nav-prev a, .post-nav-next a {
display: block;
padding: 10px;
}
.post-nav-prev, .post-nav-next {
display: table-cell;
vertical-align: middle;
width: 50%;
height: 100%;
border: 1px solid #e3e3e3;
text-align: center;
}
<div class="post-nav group">
<div class="post-nav-prev">text 1</div>
<div class="post-nav-next">
text 2 is too long and falls in 2 lines for the example case
</div>
</div>

if youre open to jquery, would something like this work for you?
$(document).ready(function() {
adjust();
function adjust() {
if ($('.left a').height() > $('.right a').height()) {
$('.right a').css("min-height", $('.left a').height());
}
if ($('.right a').height() > $('.left a').height()) {
$('.left a').css("min-height", $('.right a').height());
}
}
$('.addLeft').click(function() {
$('.left a').append('<br/>Adding more');
adjust();
});
$('.addRight').click(function() {
$('.right a').append('<br/>Adding more');
adjust();
});
});
.left, .right {
display: inline-block;
width: 40%;
vertical-align: top;
}
a {
height: 100%;
display:inline-block;
}
.left a {
background-color: lightblue;
}
.right a {
background-color: lightgray;
}
.container {
background-color: gray;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Add more to Left
<br/>
Add more to Right
<br/><br/>
<div class="container">
<div class="left">
A element
</div>
<div class="right">
A element<br/>More content here
</div>
</div>

Related

column to top html style two square text rows

I have an HTML page and I want to have a table that has four or five section and this is my first section with two columns but my text on the left side I can't get to the top. I'm tried adding padding-top: 0; adjusting the margin, aligning. I think maybe the combo on the div either adding to the sidebar or mycolumn in the table row since I want to rows in the left column.
/*CSS*/
#sidebar {
width: 750px;
}
.table-row {
display: table;
width: 100%;
margin-top: 0px;
}
.mycolumn {
display: table-cell;
padding: 15px;
}
#content {
width: 100%;
}
div {
outline: none;
}
#sidebar1 {
width: 750px;
}
.table-row2 {
display: table;
width: 100%;
}
.mycolumn1 {
display: table-cell;
padding: 15px;
}
#content1 {
/* width:100%; */
}
<!-- HTML -->
<div class="container">
<div class="row">
<div class="table-row">
<div class="mycolumn" id="sidebar">
<h1 class="promo_slogan"> Bring everyone together to build better products.</h1>
</div>
<div class="mycolumn" id="content"><img src="file:///C:\Develop\CodeSamples\manage-landing-page-master\images\illustration-intro.svg">
</div>
</div>
</div>
</div>
Try to use display: flex;instead of display: table;
And then set your h1 margin: 0;, because the h1 tag has default margin.
You use container and row as classes, but you don't define it in your css. Are you using bootstrap?
You also have padding in mycolumn. This will add padding left, top, right, and bottom.
Also consider:
.mycolumn { vertical-align: top; }
to force to the top.

2 divs, side by side, with right-hand div taking up remainder of containing div

I have the classic two divs side-by-side problem, which usually I have no problem with (float: left both divs and add a clear:both div after them).
My requirements are making this more complicated to solve...
I would like the left-hand div to occupy, as a column, the left hand side of the containing div (the left hand div will hold a number, ie '1.')
I would like the right-hand div to occupy the remaining space to the right of the left div - and most importantly I would like it NOT to drop below the left-hand div when there is insufficient 'space' for it to fit. Instead, I would like the right-hand div to remain in position and for the text within to WRAP, staying to the right of the left-hand div. Surely this is simple!
I do NOT want to set arbitrary width values because the length of the number in the left-hand div will vary, affecting the distance between the number and the right-hand text.
Here is some example html:
<div class="popup-container"> // set to width: 300px; in css
<div class="popup-text">
<div class="float-left">
<h3>2.<.h3>
</div>
<div class="float-left">
<h3>Example text here, long enough to wrap around<.h3>
</div>
<div class="clear"></div>
</div>
</div>
And the css:
.popup-container {
width: 300px;
}
.popup-text h3 {
line-height: 1.25;
padding: 0px 8px 0px 0px;
}
.float-left {
float: left;
}
.clear {
clear: both;
}
OK, I think that's about it. If anyone knows how to have the left div operate as a column, against which the text in the right-hand div remains justified left (instead of dropping 'below' the left hand div), that would be swell.
EDIT
Thanks for all the answers. I should have mentioned (!!) it has to work in IE8. I know. But it really does. Big organisation, not updating its machines, unfortunately.
Flexbox and CSS Tables can both do that.
Support
Flexbox is IE10+
CSS Tables are IE8+
FLEXBOX
.popup-container {
width: 300px;
border:1px solid grey;
}
.popup-text {
display: flex;
}
.popup-text h3 {
line-height: 1.25;
padding: 0px 8px 0px 0px;
}
.left {
flex: 0 0 auto;
background: #c0ffee;
}
.right {
flex:1;
background: yellow;
}
<div class="popup-container">
<div class="popup-text">
<div class="left">
<h3>2.</h3>
</div>
<div class="right">
<h3>Example text here, long enough to wrap around</h3>
</div>
</div>
</div>
CSS Tables
.popup-container {
width: 300px;
border:1px solid grey;
}
.popup-text {
display: table
}
.popup-text h3 {
line-height: 1.25;
padding: 0px 8px 0px 0px;
}
.left {
background: #c0ffee;
display: table-cell;
}
.right {
background: yellow;
display: table-cell;
}
<div class="popup-container">
<div class="popup-text">
<div class="left">
<h3>2.</h3>
</div>
<div class="right">
<h3>Example text here, long enough to wrap around</h3>
</div>
</div>
</div>
Use display:flex;
.popup-container {
width: 300px;
}
.popup-container .popup-text {
display: flex;
}
.popup-text h3 {
line-height: 1.25;
padding: 0px 8px 0px 0px;
}
.float-left {
float: left;
}
.clear {
clear: both;
}
<div class="popup-container">
<!-- set to width: 300px; in css -->
<div class="popup-text">
<div class="float-left">
<h3>2.</h3>
</div>
<div class="float-left">
<h3>Example text here, long enough to scroll</h3>
</div>
<div class="clear"></div>
</div>
</div>
Here is a solution using display: flex
.popup-container {
width: 300px;
background-color: coral;
}
.popup-text {
display: flex;
}
.popup-text div.two {
flex: 1;
background-color: cornflowerblue;
}
.popup-text h3 {
line-height: 1.25;
padding: 0px 8px 0px 0px;
}
<div class="popup-container">
<!-- set to width: 300px; in css -->
<div class="popup-text">
<div class="one">
<h3>2.</h3>
</div>
<div class="two">
<h3>Example text here, long enough to scroll</h3>
</div>
</div>
</div>

side bar with a no previsible width in CSS/HTML

Is there a way to achieve the following behavior in css/html :
Please note the green side bar has not to be responsive but I cannot give it a fixed width with
width: XX px;
because it can contain more or less elements, so no idea of XX in advance.
The brown bar has to be responsive and takes all the remaining width.
Thanks in advance for any trick! I have tried tables but with no success as we can't specify a div to restrict its with to what is necessary.
You can achieve that easily with flexbox. Here's the example:
http://codepen.io/anon/pen/JKXXNE
#container {
display:flex;
}
#sidebar, #content {
height: 100px;
}
#sidebar {
background-color: green;
}
#content {
background-color: brown;
flex: 1;
}
You can use Flexbox, and if you set flex: 1 on right div it will take rest of free space and width of left div will still be dynamic.
.parent {
display: flex;
border: 2px solid black;
}
.left {
background: #22B14C;
padding: 10px;
}
.right {
background: #EFE4B0;
padding: 10px;
flex: 1;
}
span {
margin: 0 20px;
}
<div class="parent">
<div class="left"><span>Span</span><span>Span</span></div>
<div class="right">Right</div>
</div>
This can also be done with CSS Table layout you just need to set width: 100% on .right div and it will take rest of free space
.parent {
display: table;
border: 2px solid black;
}
.left {
background: #22B14C;
display: table-cell;
padding: 10px;
}
.right {
background: #EFE4B0;
display: table-cell;
width: 100%;
padding: 10px;
}
span {
margin: 0 20px;
}
<div class="parent">
<div class="left"><span>Span</span><span>Span</span></div>
<div class="right">Right</div>
</div>
For older browsers, use display: table
html, body{
height: 100%;
margin: 0;
}
.tbl{
display:table;
}
.row{
display:table-row;
}
.cell{
display:table-cell;
}
.content{
width: 100%;
}
#left_col {
background: orange none repeat scroll 0 0;
width: 1%;
}
#right_col {
background: green none repeat scroll 0 0;
width: 100%;
}
<div class="tbl content">
<div class="row">
<div id="left_col" class="cell">
wide content <br>
content <br>
wider contentcontent <br>
</div>
<div id="right_col" class="cell"></div>
</div>
</div>
Another way to achieve this without using flexbox can be:
Working Fiddle:
https://jsfiddle.net/y00e5w6m/
(Note i have used sample css and input just to showcase how this can be done. This should be tuned a bit according to requirements)
Sample Output:
Html:
<div style="float:left;width:100%;border:1px solid #000;">
<div id="dynamic-content" style="float:left;background-color:#090;border:1px solid #900">
<div style="float;left;">
Mango
</div>
<div style="float;left;margin-left:5px;">
Banana
</div>
<div style="float;left;margin-left:5px">
Orange
</div>
</div>
<div id="other-content" style="float:left;background-color:#630;border:1px solid #009;">
</div>
</div>
JS:
var items=["mango","grapes","banana"];
var windowWidth = $(window).width();
console.log(windowWidth);
var dynamicContentWidth = $("#dynamic-content").width();
console.log(dynamicContentWidth);
var otherContentWidth = dynamicContentWidth >= windowWidth ? windowWidth : windowWidth-dynamicContentWidth-20;
console.log(otherContentWidth);
$("#other-content").width(otherContentWidth);
$("#other-content").height($("#dynamic-content").height());

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

CSS: Having 3 div on the same line with the middle one taking the remaining space

I'm building a toolbar, I'd like the yellow part in the following example to take the whole space left (in white):
http://jsfiddle.net/MWjGH/1/
<div class="left"> Some content </div>
<span class="middle"> This should fill the space left </span>
<div class="right"> Some other content </div>
with css:
.left {
float: left;
background-color: #ddd;
display: inline-block;
}
.middle {
background-color: yellow;
display: inline-block;
}
.right {
float: right;
background-color: #ddd;
display: inline-block;
}
Edit: the content of left and right is dynamic, it can change, so I don't want to set width on them
I don't know if that suits you because of a slight HTML change:
<div class="left"> Some content </div>
<div class="right"> Some other content </div>
<span class="middle"> This should fill the space </span>
But I believe it is what you want,
CSS:
.left {
float: left;
background-color: #ddd;
}
.middle {
background-color: yellow;
display: block;
overflow:hidden;
}
.right {
float: right;
background-color: #ddd;
}
DEMO :http://jsfiddle.net/pavloschris/MWjGH/12/
Put the middle div after the floated divs:
<div class="left"> Some content </div>
<div class="right"> Some other content </div>
<div class="middle"> This should fill the space left </div>
Then, don't change any of the display properties so they stay on block (the default for div)
.left {
float: left;
background-color: #ddd;
}
.middle {
background-color: yellow;
}
.right {
float: right;
background-color: #ddd;
}
Fiddle: http://jsfiddle.net/hLmj7/
If you do't have a fixed width for the two side columns, you can always display:table-cell.
.left {
background-color: #ddd;
display: table-cell;
}
.middle {
background-color: yellow;
display: table-cell;
width:100%;
}
.right {
background-color: #ddd;
display: table-cell;
}
JSFiddle example.
With this you're then able to add min-width to the outer columns without having to keep changing the width of the middle element.
JSFiddle example with min-width applied.
I would wrap your divs in a wrapper, and assign the background-color to the wrapper div
Then, you don't need to specify width at all.
jsfiddle
Html:
<div class="toolbar">
<div class="left"> Some content </div>
<div class="middle"> This should fill the space left </div>
<div class="right"> Some other content </div>
</div>
CSS:
.toolbar {
background-color: yellow;
}
.left {
float: left;
background-color: #ddd;
display: inline-block;
}
.middle {
display: inline-block;
}
.right {
float: right;
background-color: #ddd;
display: inline-block;
}
Consider adding clearfix to the wrapper div as the divs inside are floating :)
try this:
don't right align your last div
make all your containers float:left
and give percentage width to each of your containers, so that their total sum should be 100%;
working fiddle
if you don't want to enforce a static width do this:
give each of your containers a width:auto, but be notified, that,
if the total sum of the width of each of the containers turns out to be more than that of parent( body in your case) contaienr, then line-break would occur,
a div will slide down to the next row.
see this fiddle