I want four divs to appear on the same line. I can make three of them do it but the other won't unless I make the total width that the four divs take up less than 100%.
This shouldn't happen though, right? They should be able to, in total, take up 100% of the page width if there is nothing else 'in the way'? Below is a snippet with my code of what I mean.
* {
margin: 0;
padding: 0;
}
html,
body {
overflow: hidden;
font-size: 10px;
}
.wrapper {
position: absolute;
bottom: 0;
width: 100%;
margin-bottom: 0.5%;
margin-left: 0.5%;
margin-right: 0.5%;
}
.inner {
display: inline-block;
vertical-align: top;
width: 25%;
}
.half {
width: 12.5%;
}
<div class="wrapper">
<div class="inner" style="background-color: red;">Div 1</div>
<div class="inner" style="background-color: blue;">Div 2</div>
<div class="inner" style="background-color: green;">Div 3</div>
<div class="inner">
<div class="inner half" style="background-color: purple;">Div 4 - First Half</div>
<div class="inner half" style="background-color: Teal;">Div 4 - Second Half</div>
</div>
</div>
I haven't checked to see if aligning the divs on the same line using float: left will make a difference to the problem as I need to use display: inline-block for aligning other thighs in the divs in my actual code.
So does any one know how to get the last one to appear on the same line?
Because you need to add float: left; to inner to make it behave as expected.
Also Adam is right, 12.5% will be 12.5% of the already 25% width container. I've removed the inner class from the half divs and changed their width to 50%.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html,
body {
overflow: hidden;
font-size: 10px;
}
.wrapper {
position: absolute;
bottom: 0;
width: 100%;
margin-bottom: 0.5%;
margin-left: 0.5%;
margin-right: 0.5%;
}
.inner {
display: inline-block;
vertical-align: top;
width: 25%;
float:left;
}
.half {
float:left;
width: 50%;
}
<div class="wrapper">
<div class="inner" style="background-color: red;">Div 1</div>
<div class="inner" style="background-color: blue;">Div 2</div>
<div class="inner" style="background-color: green;">Div 3</div>
<div class="inner">
<div class="half" style="background-color: purple;">Div 4 - First Half</div>
<div class="half" style="background-color: Teal;">Div 4 - Second Half</div>
<div style="clear:both;"></div>
</div>
</div>
Your CSS doenst contain a class thats called "inner half" so you are combining two. You have multiple width's. Your first calls is inner so it takes 25% + 12.55
Try clearfix. Just apply a class="clearfix" to the parent element. This is the easier and the more modern way around this, as compared to floats. The bigger advantage is that you can re-use it all over your HTML DOM much more easily than the approach you're taking.
.clearfix:after {
visibility: hidden;
display: block;
font-size: 0;
content: " ";
clear: both;
height: 0;
}
.clearfix { display: inline-block; }
/* start commented backslash hack \*/
* html .clearfix { height: 1%; }
.clearfix { display: block; }
/* close commented backslash hack */
Related
I would like to have a table of elements (pictures) arranged horizontally, and such that the elements partially overlap, in order to save some space.
I attempted to solve it through CSS. Here is my code,
<!DOCTYPE html>
<html>
<head>
<style>
div.tableel
{
height : 100px;
width : 100px;
display: table-cell;
position: relative;
}
div.tableel ~ div.tableel
{
left: -30px;
font-size: 24pt;
}
div.row
{
display: table-row;
}
div.table
{
display: table;
}
</style>
</head>
<body>
<div class="table">
<div class="row">
<div class="tableel" style="background-color: red;">
reg
</div>
<div class="tableel" style="background-color: blue;">
ge
</div>
<div class="tableel" style="background-color: yellow;">
rg
</div>
</div>
</div>
</body>
</html>
What I do not understand is why the font is set correctly, but the third element is not shifted to the left, as I would expect to.
Thanks!
What I do not understand is why the font is set correctly, but the
third element is not shifted to the left
It is being shifted exactly as you intended it to.
The reason you are not able to see it is because when the second div shifts to left, it creates a gap between itself and the third, which is being filled up by the third.
This fiddle will help you see this: http://jsfiddle.net/abhitalks/byayxob0/
If you want to stick to doing it this way, then you will have to get the last div to move left by double the amount:
div.tableel:last-child { left: -60px !important; }
See here: http://jsfiddle.net/abhitalks/byayxob0/1/
But, this will be a problem for you if you have indeterminate number of divs. You will have to change the left property cumulatively, which CSS cannot do for you.
That said, a simpler way to do what you want, would be to just keep the divs inline and then use negative margins to control the overlap.
Demo Fiddle: http://jsfiddle.net/abhitalks/4gaotuwL/
Demo Snippet:
div.tableel {
height: 100px; width: 100px;
display: inline-block;
}
div.tableel:nth-child(1) { background-color: rgba(255,0,0,0.5); }
div.tableel:nth-child(2) { background-color: rgba(0,0,255,0.5); }
div.tableel:nth-child(3) { background-color: rgba(255,255,0,0.5); }
div.tableel:not(:first-child) {
margin-left: -10px;
}
<div class="table">
<div class="row">
<div class="tableel">reg</div>
<div class="tableel">ge</div>
<div class="tableel">rg</div>
</div>
</div>
It's because the second element is moved 30px to the left so that it would be 30px further away from the 3rd element, then when you shift the 3rd element 30px to the left it just closes the gap you made. If you want to move the 3rd element to overlap the second by 30px, you'd need to move it 60px to catch up to it and overlap it.
.table {
width: 300px;
table-layout: fixed;
display: table;
}
.tableel {
height: 100px;
width: 100px;
display: table-cell;
position: relative;
vertical-align: middle;
text-align: center;
}
.row {
display: table-row;
}
.default .tableel ~ .tableel {
left: -30px;
}
/* I just moved the second block*/
.scene-1 .tableel:nth-child(2) {
left: -30px;
}
.scene-2 .tableel:nth-child(2) {
left: -30px;
}
/* I also moved the third block - but 60pixels. (as 30pixel will move it beside 2nd block*/
.scene-2 .tableel:nth-child(3) {
left: -60px;
}
<h2>Your code</h2>
<div class="table default">
<div class="row">
<div class="tableel" style="background-color: rgba(255,0,0,0.7);">
reg
</div>
<div class="tableel" style="background-color: rgba(0,255,0,0.7);">
ge
</div>
<div class="tableel" style="background-color: rgba(0,0,255,0.7);">
rg
</div>
</div>
</div>
<h2>Scene 1</h2>
<div class="table scene-1">
<div class="row">
<div class="tableel" style="background-color: rgba(255,0,0,0.7);">
reg
</div>
<div class="tableel" style="background-color: rgba(0,255,0,0.7);">
ge
</div>
<div class="tableel" style="background-color: rgba(0,0,255,0.7);">
rg
</div>
</div>
</div>
<h2>Scene 2</h2>
<div class="table scene-2">
<div class="row">
<div class="tableel" style="background-color: rgba(255,0,0,0.7);">
reg
</div>
<div class="tableel" style="background-color: rgba(0,255,0,0.7);">
ge
</div>
<div class="tableel" style="background-color: rgba(0,0,255,0.7);">
rg
</div>
</div>
</div>
You should do left: -60px; to the third element in order to go -30px left because the second element is -30px left.
UPDATE
If you are having an unknown number of divs and wanted to make this work regardless of how many divs you have without having to specify incrementing left values then I suggest using a negative margin.
Simply change this:
div.tableel {
height : 100px;
width : 100px;
display: table-cell;
position: relative;
}
To This:
div.tableel {
height : 100px;
width : 100px;
display: inline-block;
position: relative;
}
and then give the blocks a negative margin by replacing this:
div.tableel ~ div.tableel
{
left: -30px;
font-size: 24pt;
}
to this:
div.tableel {
font-size: 24pt;
opacity: 0.5;
margin-left: -30px;
}
So looking at the code and various answers; I think you're getting confused on the
div.tableel ~ div.tableel
{
position: relative;
left: -30px; /* <------------------This */
font-size: 24pt;
}
left: -30px portion.
Looking at your 3 boxes. You have Box number 1, 2 and 3. Based on your selector; You are moving box 2 to the left -30px and moving box number 3 to the left by -30px.
When you move the first box to the left by -30px; you are creating a gap between box 2 and box 3 of 30px:
This means that Box 3 needs -30px to close the gap. and an additional -30px to overlap so box 3 needs to be -60px for this to work:
-30px:
-60px:
So in short.. I would replace this portion of your code:
div.tableel ~ div.tableel
{
left: -30px;
font-size: 24pt;
}
with this simplified fix:
div.tableel{
font-size: 24pt;
}
div.tableel:nth-child(2){
left: -30px;
}
div.tableel:nth-child(3){
left: -60px;
}
Here is the Fiddle: https://jsfiddle.net/8uhnwhwt/
I'm currently designing a dashboard and I'm looking for a good/right way to align multiple divs in a container. I want the divs taking up maximum space left and right with the right margins .
<div class="wrapper">
<div class="container">
<div class="1"></div>
<div class="2"></div>
<div class="3"></div>
<div class="4"></div>
</div>
</div>
.container {
margin: 15px;
}
What is the best way to achieve this?
The easiest way would be to use display and the table-layout properties :
http://jsfiddle.net/3Q4qv/
.wrapper {
border:solid;
display:table;
border-collapse:separate;
border-spacing:15px;
/* give it width if you wish */
}
.container {
display:table-row;
}
.container > div {
display:table-cell;
border:solid;
/* let's give a min size since it is empty */
height:2em;
min-width:2em;
}
you can even make a mix with padding and border-spacing : http://jsfiddle.net/3Q4qv/1/
.wrapper {
border:solid;
display:table;
border-collapse:separate;
border-spacing:5px;
padding:10px;
}
Check out this JSFiddle: http://jsfiddle.net/4Y3jS/3/ You should set a specific width on your container class, and then float each of the inner divs left.
HTML:
<div class="container">
<div class="box-1"><img style="width:50px; height: 50px; " src="http://www.mlahanas.de/Greeks/images/sq1.jpg"></img></div>
<div class="box-2"><img style="width:50px; height: 50px; " src="http://www.mlahanas.de/Greeks/images/sq1.jpg"></img></div>
<div class="box-3"><img style="width:50px; height: 50px; " src="http://www.mlahanas.de/Greeks/images/sq1.jpg"></img></div>
<div class="box-4"><img style="width:50px; height: 50px; " src="http://www.mlahanas.de/Greeks/images/sq1.jpg"></img></div>
</div>
CSS:
.container {
width: 300px;
}
.box-1, .box-2, .box-3, .box-4 {
float: left;
padding: 0 2px;
}
Use any width in your container like:
.container {
width: 400px;
}
Then you can set the style for all elements inside. Look that if you want to add border at the elements, try to use the box-sizing property.
.box {
width: 21%;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
float: left;
margin: 0 2%;
}
Try something like this: JSFiddle
Basically floating the blocks and letting their width be determined by a percentage of the parent (up to 25% since there are 4 - notice that I used only 23% and then I placed a 1% margin around all blocks giving you a 2% space between any two blocks -- adjust these numbers as you see fit).
.container {
margin: 15px;
}
.box {
float: left;
width: 23%;
margin: 0 1%;
}
<div class="container">
<div class="1 box"></div>
<div class="2 box"></div>
<div class="3 box"></div>
<div class="4 box"></div>
</div>
If you don't want space between the boxes, this approach really shines - use width:25%; margin: 0;
Alternatively, you could use display: inline-block;
<div class="wrapper">
<div class="container">
<div class="1 box"></div><!--
--><div class="2 box"></div><!--
--><div class="3 box"></div><!--
--><div class="4 box"></div>
</div>
</div>
.box {
display: inline-block;
width: 25%;
margin: 0;
}
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
I have the following html/css code: http://jsfiddle.net/J3YZ8/4/
HTML:
<div id="headerDiv">HeaderPanel</div>
<div id="bodyDiv">
<div id="loginContainer">LoginPanel</div>
<div id="contentContainer">Content</div>
<div id="menuContainer">MenuPanel</div>
</div>
<div id="footerDiv">FooterPanel</div>
CSS:
* {
padding: 0px;
margin: 0px;
}
html {
height: 100%;
}
body {
direction: rtl;
height: 100%;
font-family: verdana, arial, helvetica, sans-serif;
font-size: 75%;
}
#headerDiv {
height: 20%;
margin-bottom: 1%;
}
#footerDiv {
height: 10%;
margin-top: 1%;
}
#headerDiv,
#footerDiv {
clear: both;
background-color: #FF5500;
}
#bodyDiv {
height: 68%;
margin: 0% 2%;
}
#loginContainer {
background: green;
margin-bottom: 1%;
}
#menuContainer {
background: blue;
margin-top: 1%;
}
#loginContainer,
#menuContainer {
display: inline-block;
width: 29%;
margin-left: 1%;
height: 49%;
}
#contentContainer {
width: 69%;
height: 100%;
background: yellow;
float: left;
margin-right: 1%;
}
If you use this code on your browser (without jsfiddle) you will see there is no margin between the blue div (menuContainer) and the footer. In jsfiddle the margin is not equal to the margin between the yellow div (contentContainer) and the footer although it should be the same. How can I fix it?
More details:
this is image from jsfiddle result:
this is image from full screen result:
Does anyone knows how to fix it??
I do see a margin below the blue panel.
A height of 100% the html element does not mean "not higher than the window". If you don't want to scroll the page you could set overflow:hidden on the html. But then you won't see the footer.
<div id="headerDiv">HeaderPanel</div>
<div id="bodyDiv">
<div id="loginContainer">LoginPanel</div>
<div id="contentContainer">Content</div>
<div id="menuContainer">MenuPanel</div>
</div>
</div>
<div id="footerDiv">FooterPanel</div>
One of the main problems is that you have a second closing div with no opening - this can through IE in quirks mode and also cause other issues when working with floats and clears in CSS.
<div id="headerDiv">HeaderPanel</div>
<div id="bodyDiv">
<div id="loginContainer">LoginPanel</div>
<div id="contentContainer">Content</div>
<div id="menuContainer">MenuPanel</div>
</div>
<div id="footerDiv">FooterPanel</div>
Above is corrected code that should fix it - at least a start.
Are you looking to build a fluid height and width layout?
Also you need to clear the floats before you start the footer.
<div id="headerDiv">HeaderPanel</div>
<div id="bodyDiv">
<div id="loginContainer">LoginPanel</div>
<div id="contentContainer">Content</div>
<div id="menuContainer">MenuPanel</div>
</div>
<div style="clear:both;"></div>
<div id="footerDiv">FooterPanel</div>
There is a working sample of the code maintaining your margin.
I've searched other questions and, while this problem seems similar to a couple of others, nothing I've seen so far seems to address the issue that I'm having.
I have a div which contains a number of other divs, each of which is floated left. These divs each contain a photo and a caption. All I want is for the group of photos to be centered within the containing div.
As you can see from the code below, I've tried using both overflow:hidden and margin:x auto on the parent divs, and I've also added a clear:both (as suggested in another topic) after the photos. Nothing seems to make a difference.
Thank you. I appreciate any suggestions.
<div style="position: relative; margin: 0 auto; overflow: hidden; text-align: center;">
<h4>Section Header</h4>
<div style="margin: 2em auto;">
<div style="float: left; margin: auto 1.5em;">
<img src="photo1.jpg" /><br />
Photo Caption
</div>
<div style="float: left; margin: auto 1.5em;">
<img src="photo2.jpg" /><br />
Photo Caption
</div>
<div style="float: left; margin: auto 1.5em;">
<img src="photo3.jpg" /><br />
Photo Caption
</div>
<div style="clear: both; height: 0; overflow: hidden;"> </div>
</div>
</div>
First, remove the float attribute on the inner divs. Then, put text-align: center on the main outer div. And for the inner divs,
use display: inline-block. Might also be wise to give them explicit widths too.
<div style="margin: auto 1.5em; display: inline-block;">
<img title="Nadia Bjorlin" alt="Nadia Bjorlin" src="headshot.nadia.png"/>
<br/>
Nadia Bjorlin
</div>
With Flexbox you can easily horizontally (and vertically) center floated children inside a div.
So if you have simple markup like so:
<div class="wpr">
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>
</div>
with CSS:
.wpr
{
width: 400px;
height: 100px;
background: pink;
padding: 10px 30px;
}
.wpr span
{
width: 50px;
height: 50px;
background: green;
float: left; /* **children floated left** */
margin: 0 5px;
}
(This is the (expected - and undesirable) RESULT)
Now add the following rules to the wrapper:
display: flex;
justify-content: center; /* align horizontal */
and the floated children get aligned center (DEMO)
Just for fun, to get vertical alignment as well just add:
align-items: center; /* align vertical */
DEMO
I accomplished the above using relative positioning and floating to the right.
HTML code:
<div class="clearfix">
<div class="outer-div">
<div class="inner-div">
<div class="floating-div">Float 1</div>
<div class="floating-div">Float 2</div>
<div class="floating-div">Float 3</div>
</div>
</div>
</div>
CSS:
.outer-div { position: relative; float: right; right: 50%; }
.inner-div { position: relative; float: right; right: -50%; }
.floating-div { float: left; border: 1px solid red; margin: 0 1.5em; }
.clearfix:before,
.clearfix:after { content: " "; display: table; }
.clearfix:after { clear: both; }
.clearfix { *zoom: 1; }
JSFiddle: http://jsfiddle.net/MJ9yp/
This will work in IE8 and up, but not earlier (surprise, surprise!)
I do not recall the source of this method unfortunately, so I cannot give credit to the original author. If anybody else knows, please post the link!
The following solution does not use inline blocks. However, it requires two helper divs:
The content is floated
The inner helper is floated (it stretches as much as the content)
The inner helper is pushed right 50% (its left aligns with center of outer helper)
The content is pulled left 50% (its center aligns with left of inner helper)
The outer helper is set to hide the overflow
.ca-outer {
overflow: hidden;
background: #FFC;
}
.ca-inner {
float: left;
position: relative;
left: 50%;
background: #FDD;
}
.content {
float: left;
position: relative;
left: -50%;
background: #080;
}
/* examples */
div.content > div {
float: left;
margin: 10px;
width: 100px;
height: 100px;
background: #FFF;
}
ul.content {
padding: 0;
list-style-type: none;
}
ul.content > li {
margin: 10px;
background: #FFF;
}
<div class="ca-outer">
<div class="ca-inner">
<div class="content">
<div>Box 1</div>
<div>Box 2</div>
<div>Box 3</div>
</div>
</div>
</div>
<hr>
<div class="ca-outer">
<div class="ca-inner">
<ul class="content">
<li>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</li>
<li>Nullam efficitur nulla in libero consectetur dictum ac a sem.</li>
<li>Suspendisse iaculis risus ut dapibus cursus.</li>
</ul>
</div>
</div>
display: inline-block; won't work in any of IE browsers. Here is what I used.
// change the width of #boxContainer to
// 1-2 pixels higher than total width of the boxes inside:
#boxContainer {
width: 800px;
height: auto;
text-align: center;
margin-left: auto;
margin-right: auto;
}
#Box{
width: 240px;
height: 90px;
background-color: #FFF;
float: left;
margin-left: 10px;
margin-right: 10px;
}
Solution:
<!DOCTYPE HTML>
<html>
<head>
<title>Knowledge is Power</title>
<script src="js/jquery.js"></script>
<script type="text/javascript">
</script>
<style type="text/css">
#outer {
text-align:center;
width:100%;
height:200px;
background:red;
}
#inner {
display:inline-block;
height:200px;
background:yellow;
}
</style>
</head>
<body>
<div id="outer">
<div id="inner">Hello, I am Touhid Rahman. The man in Light</div>
</div>
</body>
</html>
In my case, I could not get the answer by #Sampson to work for me, at best I got a single column centered on the page. In the process however, I learned how the float actually works and created this solution. At it's core the fix is very simple but hard to find as evident by this thread which has had more than 146k views at the time of this post without mention.
All that is needed is to total the amount of screen space width that the desired layout will occupy then make the parent the same width and apply margin:auto. That's it!
The elements in the layout will dictate the width and height of the "outer" div. Take each "myFloat" or element's width or height + its borders + its margins and its paddings and add them all together. Then add the other elements together in the same fashion. This will give you the parent width. They can all be somewhat different sizes and you can do this with fewer or more elements.
Ex.(each element has 2 sides so border, margin and padding get multiplied x2)
So an element that has a width of 10px, border 2px, margin 6px, padding 3px would look like this:
10 + 4 + 12 + 6 = 32
Then add all of your element's totaled widths together.
Element 1 = 32
Element 2 = 24
Element 3 = 32
Element 4 = 24
In this example the width for the "outer" div would be 112.
.outer {
/* floats + margins + borders = 270 */
max-width: 270px;
margin: auto;
height: 80px;
border: 1px;
border-style: solid;
}
.myFloat {
/* 3 floats x 50px = 150px */
width: 50px;
/* 6 margins x 10px = 60 */
margin: 10px;
/* 6 borders x 10px = 60 */
border: 10px solid #6B6B6B;
float: left;
text-align: center;
height: 40px;
}
<div class="outer">
<div class="myFloat">Float 1</div>
<div class="myFloat">Float 2</div>
<div class="myFloat">Float 3</div>
</div>