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());
Related
I need to make child div with text size of container. I mean to grow text and input field to fill all container. Here is my code:
<body>
<div class="Container">
<div class="One">
<div class="Child">
<div class="MyText">text <input type="text"></div>
</div>
</div>
</div>
</body>
html,
body {
height: 100%;
margin: 0px;
font-size: 14px;
}
.Container
{
display: flex;
flex-direction: column;
border: 1px black dashed;
height: 100vh;
}
.One
{
display: flex;
border: 1px red dashed;
flex-basis: 100px;
}
.Child
{
heigth:100%;
}
.MyText input {heigth:100%;}
.MyText /* I want to make this content 100% of parents size */
{
font-size: 1vh;
}
https://jsfiddle.net/p6z6huvz/9/
If I get your question right, you try to achieve something like this?
You need to use jQuery for this.
You can adjust the width of the container in CSS and jQuery calculates the font-size
var fontwidth = $('.label').width();
var divwidth = $('.container').width();
var size = parseFloat($('.label').css('font-size'));
var fontsize = (divwidth / fontwidth) * size;
$('.label').css("font-size", fontsize);
*{
margin:0;
padding:0;
}
.label{
float:left;
line-height:.8;
float:left;
}
.container{
width:60%;
display:inline-block;
height:auto;
border:1px dashed black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="label">Text</div>
</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>
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>
I try to get a various number of divs side by side.
This number is will be dynamic.
But in total these divs should have the width of exactly 100% (not under, not over).
Is this possible and if so, how can I achieve this?
Something Like:
SCREEN SIZE
|<--------------------->|
(for 2 boxes:)
|-----------|-----------|
| | |
|-----------|-----------|
or
(for three boxes:)
|-------|-------|-------|
| | | |
|-------|-------|-------|
this jsFiddle shows you how you can achieve 3 boxes side by side. I've edited the css to being:
.left {
float:left;
width:50%;
border: 3px solid #333;
box-sizing: border-box;
background: #C0C0C0;
margin: 0;
}
which works (even after resizing),
and the html was:
<div class="left">...B1</div>
<div class="left">...B2</div>
See below for it in action:
.left {
float:left;
width:50%;
border: 3px solid #333;
box-sizing: border-box;
background: #C0C0C0;
margin: 0;
}
<div class="left">...B1</div>
<div class="left">...B2</div>
Try flex model, works like a charm!
html, body { height: 100%; width: 100%; }
.eqWrap { display: flex; }
.eq { padding: 10px; }
.eq:nth-of-type(odd) { background: yellow; }
.eq:nth-of-type(even) { background: lightblue; }
.equalHW { flex: 1; }
<div>
<h1>EQUAL WIDTH COLUMNS</h1>
<p>Add display:flex to the parent and flex:1 to the boxes</p>
<div class="equalHWrap eqWrap">
<div class="equalHW eq">boo <br> boo</div>
<div class="equalHW eq">shoo</div>
<div class="equalHW eq">shoo</div>
</div>
</div>
jsFiddle: http://jsfiddle.net/SchweizerSchoggi/y7L698nq/
i have a legend for a graph that sometimes is scrollable and sometimes isn't.
Unfortunately when the scrollbar shows up, it pushes all of the elements over to the left a bit. So they don't line up with a total (outside the scrollable area)
Example: http://jsfiddle.net/3sKVR/
A simple answer would be to just set a fixed width, but unfortunately, it has to be responsive.
Also, i can't use custom scrollbars to maintain consistency with the rest of the site and also bring down page-load times.
Any help would be greatly appreciated (with internet points!)
Cut down version of code:
HTML:
<div id="legend_cont">
<div id="legend_list">
<div id="legend">
<div class="legend_row">
<div class="legend_cell">
<div class="legend_colour" style="background-color:#ffb100"></div>
</div>
<div class="legend_cell">Merch G</div>
<div class="legend_cell legend_value">$1423.24</div>
</div>
<div class="legend_row">
<div class="legend_cell">
<div class="legend_colour" style="background-color:#ed5929"></div>
</div>
<div class="legend_cell">Merch L</div>
<div class="legend_cell legend_value">$1351.07</div>
</div>
<div class="legend_row">
<div class="legend_cell">
<div class="legend_colour" style="background-color:#3f9c35"></div>
</div>
<div class="legend_cell">Merch N</div>
<div class="legend_cell legend_value">$1194.90</div>
</div>
<div class="legend_row">
<div class="legend_cell">
<div class="legend_colour" style="background-color:#009bbb"></div>
</div>
<div class="legend_cell">Merch T</div>
<div class="legend_cell legend_value">$1188.14</div>
</div>
</div>
</div>
<div id="legend_total">Total:<span id="legend_total_value">$0.00</span>
</div>
</div>
CSS:
#legend_cont {
height: 100%;
border-left: 2px solid #ADADAD;
width: 40%;
float: right;
}
#legend_list {
height: 169px;
overflow: auto;
margin: 20px 4% 20px 7%;
}
#legend {
display: table;
width: 90%;
}
.legend_row {
display: table-row;
}
.legend_cell {
display: table-cell;
padding: 5px;
vertical-align: middle;
}
.legend_colour {
width: 10px;
height: 20px;
background-color: #c1c1c1;
border-radius: 3px;
}
.legend_value {
text-align: right;
}
#legend_total {
height: 50px;
line-height: 50px;
width: 88%;
border-top: 1px solid;
margin-left: 8%;
}
#legend_total_value {
float: right;
padding-right: 5px;
}
1) Make sure there is always a scroll bar
CSS
#legend_cont {
overflow-y: scroll;
}
2) Use js to grab the variable width of the scrollbar (example here)
3) Set the padding-right in #legend_total_value equal to that variable in jquery.
JS
$('#legend_total_value').css('padding-right', wScroll);
Try applying padding-right to compensate for the size of scrollbar when it's not there and position the total accordingly.
#legend_list {
height: 169px;
overflow: auto;
margin: 20px 4% 20px 7%;
padding-right:15px;
}
Demo