I'm a tables guy, but I'll need to drag and drop some divs, so I tried doing it tabeless (the right way).
This is what I want to do:
The space between all elements should be 24px. My main problem is having the divs (1,2,3) occupying 100% of available space. The width: 100% its sending them beyond the main container.
This is my code so far:
html
<div id="mainContainer">
<div id="topContainer">Just the top one
</div>
<div id="table">
<div id="Line1Container">
<div id="container1" class="container">1
</div>
<div id="container2" class="container">2
</div>
<div id="container3" class="container">3
</div>
</div>
<div id="Line2Container">
<div id="container4" class="container">4
</div>
<div id="container5" class="container">5
</div>
<div id="container6" class="container">6
</div>
</div>
</div>
</div>
And my css
#mainContainer {
border: 1px solid lightgray;
position:fixed;
top: 80px;
bottom:20px;
left:80px;
right:80px;
overflow-y: scroll;
overflow-x: hidden;
border-top-left-radius: 10px;
border-bottom-left-radius: 10px;
}
#topContainer {
border: 1px solid lightgray;
border-radius: 10px;
margin-left: 24px;
margin-right: 24px;
margin-top: 24px;
}
#table {
display: table;
margin: 24px;
width: 95%;
}
#Line1Container, #Line2Container {
display: table-row;
}
.container {
border: 1px solid lightgray;
display: table-cell;
width: 33%;
border-radius: 10px;
}
As you see I tried the table-cell approach, but before I have tried the float: left approach.
Thanks
Fiddle
You can't properly use px values with % values together with dynamic sizes.
You should use x% instead of 24px.
And you can use float: left on the "cells"
How about using a table for separating the divs? that way with the td padding there will always be 24px between them
check out this fiddle:
http://jsfiddle.net/5zfEq/
added:
#Line1Container {
padding:12px;
}
#inner-table {
width: 100%;
}
#inner-table td {
padding: 12px;
}
based on #Edifice fiddle .... thanks ;)
Related
trying to create table with 100% width content, but "width" did not work. can anyone help plz. how can I fix it? And there is "div" inside table cos I can't padding content another way. I need paddin from table borders, and i need bottom lines between table rows. without "border-collapse" "border-bottom" did not work for table rows. So i need add one more div just to do padding :(. Can anyone show me how to do it right without adding one more "div" inside table.
.tabble {
display: table;
border-collapse: collapse;
border-spacing: 10px;
border-radius: 6px;
width: 100%;
}
.table__row {
display: table-row;
width: 100%;
border-bottom: 1px solid red;
}
.table__row:last-child {
border: none;
}
.table__cell {
display: table-cell;
width: 20%;
padding: 10px;
text-align: left;
}
.padding-div {
padding: 5px 20px;
width: 100%"
}
</style>
<div class="tabble">
<div class="padding-div">
<div class="table__row">
<div class="table__cell">1</div>
<div class="table__cell">1</div>
</div>
<div class="table__row">
<div class="table__cell">2</div>
<div class="table__cell">2</div>
</div>
</div>
</div>```
There are a number of things wrong with your table.
There is a typo, " instead of ; in the CSS for .padding-div.
The table cells have width:20%, which interferes with the width calculations. The browser won't know whether to make the two cells together 40% or 100% wide. Remove this.
And the biggest problem, the padding div. You can't have an ordinary div inside a table and with a table row inside. Remove this div. If you want space around the whole table, apply a margin to the table itself.
Then it will have the desired result. That is, if what you want is to make the whole table as wide as the viewport.
.tabble {
display: table;
border-collapse: collapse;
border-spacing: 10px;
border-radius: 6px;
width: calc(100% - 40px); /* changed */
margin: 5px 20px; /* new */
}
.table__row {
display: table-row;
width: 100%;
border-bottom: 1px solid red;
}
.table__row:last-child {
border: none;
}
.table__cell {
display: table-cell;
/*width: 20%;*/ /* removed */
padding: 10px;
text-align: left;
}
.padding-div {
padding: 5px 20px;
width: 100%
}
<div class="tabble">
<div class="table__row">
<div class="table__cell">1</div>
<div class="table__cell">1</div>
</div>
<div class="table__row">
<div class="table__cell">2</div>
<div class="table__cell">2</div>
</div>
</div>
Note: if you do want the table to have a border, as you said in the comments, the solution changes a bit. Then the table itself needs to have a padding, and that means you can't use border-collapse, you'll have to use border-spacing:0 instead, which also means you'll need to apply the inside borders to the cells rather than the rows.
.tabble {
display: table;
border-spacing: 0; /* changed */
border-radius: 6px;
width: calc(100% - 40px); /* changed */
padding: 5px 20px; /* new */
border: 1px solid tan; /* purely for demo purposes */
}
.table__row {
display: table-row;
width: 100%;
}
.table__cell {
display: table-cell;
padding: 10px;
text-align: left;
}
.table__row:not(:last-child) .table__cell {
border-bottom: 1px solid red;
}
<div class="tabble">
<div class="table__row">
<div class="table__cell">1</div>
<div class="table__cell">1</div>
</div>
<div class="table__row">
<div class="table__cell">2</div>
<div class="table__cell">2</div>
</div>
</div>
I am currently trying to figure out a way to be able to have a layout that has a bottom-up, content-oriented resizing behavior.
I have the following situation: https://codepen.io/Flash1232/pen/JJYPVQ
What is wrong here is obviously that the wrapper divs do not wrap around the table divs. Now is there any solution for this involving just plain CSS and HTML or do I have to write something in JS like "set wrapper width to the width of its inner div"?
Thanks in advance for any clues!
Man i solved my problem with display:flex on parent element :)
You may want to consider using a flexbox. Please see below. If there is anything that needs to be different, just let me know.
.outer-div {
position: absolute;
background: black;
width: 800px;
left: 0;
top: 0;
bottom: 0;
overflow: auto;
padding: 10px;
}
.area {
overflow: auto;
display: flex;
border: 5px solid red;
background: white;
margin: 10px 40px 10px 10px;
}
.column {
background: green;
border: 5px solid blue;
}
.wrapper {
display: flex;
border: 5px solid yellow;
justify-content: center;
align-items: center;
}
.table {
white-space: nowrap;
}
.violet {
background: violet;
width: 120%;
height: 80px;
}
.red {
background: red;
width: 150%;
height: 80px;
margin-bottom: 20px;
}
.icons {
Background: yellow;
float: right;
height: auto;
width: auto;
}
<div class="outer-div">
<div class="area">
<div class="column">
<div class="wrapper">
<div class="table red">
<span>***Table Content***</span>
</div>
</div>
<div class="wrapper">
<div class="table violet">
<span>***Table Content***</span>
</div>
</div>
</div>
<div class="column">
<div class="wrapper">
<div class="table violet">
<span>***Table Content***</span>
</div>
</div>
</div>
</div>
<div class="icons">
<p>Icon</p>
<p>Icon</p>
<p>Icon</p>
<p>Icon</p>
<p>Icon</p>
<p>Icon</p>
</div>
</div>
You should read the definition of the width attribute.
https://developer.mozilla.org/en/docs/Web/CSS/width
Percentages: refer to the width of the containing block
If you set width to 150%, you explicitly say, that the child should be bigger than the parent. You can not expect, that the parent has the same width like the child, if you force the child to be wider.
Here is my problem. I have inline-block divs inside another div.
.timeEvents {
width: 100%;
overflow: hidden;
text-align: center;
}
.timeline {
border: 1px solid;
}
.events1, .events2 {
border: 1px solid;
}
.ev1, .ev3 {
border: 1px solid red;
}
.ev2 {
margin: 0 auto;
border: 1px solid red;
display: inline-block;
}
.mDiv {
display: inline-block;
padding-left: 12px;
padding-right: 12px;
border: 1px solid blue;
}
<div class="timeEvents">
<div class="events1">
<div class="ev1">Data Field 1</div>
</div>
<div class="timeline">
<div class="ev2">
<div class="mDiv">5</div>
<div class="mDiv">10</div>
<div class="mDiv">15</div>
<div class="mDiv">20</div>
<div class="mDiv">25</div>
</div>
</div>
<div class="events2">
<div class="ev3">Data Field 2</div>
</div>
</div>
I want the .ev2 to be wrapped around its children which are inline. Then, the two data fields, respectively .ev1 and .ev3 placed above and below, should have the same width as .ev2. Which means that all divs with a red border (in my JSFiddle) should have the same width (dynamic, I don't know it) and that width should not be 100% as it's in the jsFiddle example: https://jsfiddle.net/mzjqw2wx/17/.
EDIT - I updated my fiddle. I don't want to lose the outside 100% divs, I want to align the three red sections to have the same width, the page and the outside divs all remain 100%. The tip to use inline-block on the wrapper was great, it did what I wanted with the middle one. I wanted to align all red containers and I did it with jQuery.
You need to also set display: inline-block; for the common wrapper (and give text-align: center to its parent)
body { text-align: center; }
.timeEvents {
display: inline-block;
margin: 0 auto;
}
JSFiddle
Result:
That's pretty easy to implement using Flexbox.
Just assign display: flex; to .ev2 and flex-grow: 1; to the the .myDiv class.
You can see it in the following code:
.timeEvents {
width: 100%;
overflow: hidden;
text-align: center;
}
.timeline {
border: 1px solid;
}
.events1, .events2 {
border: 1px solid;
}
.ev1, .ev3 {
border: 1px solid red;
}
.ev2 {
margin: 0 auto;
border: 1px solid red;
display: flex;
}
.mDiv {
display: inline-block;
padding-left: 12px;
padding-right: 12px;
border: 1px solid blue;
flex-grow: 1;
}
<div class="timeEvents">
<div class="events1">
<div class="ev1">Data Field 1</div>
</div>
<div class="timeline">
<div class="ev2">
<div class="mDiv">5</div>
<div class="mDiv">10</div>
<div class="mDiv">15</div>
<div class="mDiv">20</div>
<div class="mDiv">25</div>
</div>
</div>
<div class="events2">
<div class="ev3">Data Field 2</div>
</div>
</div>
Check out CSS-Trick's Complete guide to Flexbox for more information.
Use display:table to timeEvents and remove width:100% will make as per your expected.
.timeEvents {
display: table;
overflow: hidden;
text-align: center;
}
Fiddle
So I have a variable number of elements in a div that has a variable width. The elements inside have a fixed space between them, 5px, but each one needs to expand to fill the full width of the space of the outer div with padding, so the text can be centerized.
Example:
.button-container{
width: 100%;
height: 50px;
}
.button-container .button{
min-width: 75px;
display: inline-block;
text-align: center;
border: 1px solid #FF0000;
}
.button-container .button + .button-container .button{
margin-left: 5px;
}
<div class='button-container'>
<div class='button'>B1</div>
<div class='button'>B2</div>
<div class='button'>B3</div>
<div class='button'>B4</div>
</div>
So how can I make the padding inside of the button class elements have a dynamic left and right padding to fill the space of the button-container class div?
Ideally, the solution will be a CSS only solution, as I don't want to have jQuery to do the spacing.
CSS tables would work here.
.button-container {
width: 100%;
height: 50px;
display: table;
border-collapse: separate;
border-spacing: 5px;
}
.button-container .button {
display: table-cell;
text-align: center;
border: 1px solid #FF0000;
vertical-align: middle;
}
<div class='button-container'>
<div class='button'>B1</div>
<div class='button'>B2</div>
<div class='button'>B3</div>
<div class='button'>B4</div>
</div>
You can use flexbox:
.button-container {
display: flex; /* Magic begins */
}
.button-container > .button {
flex: 1; /* Distribute the width equally */
text-align: center;
margin-left: 5px;
border: 1px solid #FF0000;
}
.button-container > .button:first-child {
margin-left: 0;
}
<div class='button-container'>
<div class='button'>B1</div>
<div class='button'>B2</div>
<div class='button'>B3</div>
<div class='button'>B4</div>
</div>
This question already has answers here:
Align <div> elements side by side
(4 answers)
Closed 4 years ago.
I am trying to render a section of my pafe as in this following image. http://i.imgur.com/63q9Syr.jpg
while I am getting it to work fine for smaller screens (using media queires) I am not able to get it for for screen size > 768px. It either makes the 2 boxes overlap or the space on the either sides of the boxes aren't even. Is there a way I can fix it?
<section class="carousel price-carousel">
<div class="container">
<div class="price-Container">
<div class="month-column">
<h4>Monthly</h4>
<p>$9.95</p>
<p class=”sub-text”>per computer</p>
</div>
<div class="year-column">
<h4>Yearly</h4>
<p>$99</p>
<p class=”sub-text”>Save 20% when you pay anually</p>
</div>
</div>
</div>
</section>
JSFiddle: http://jsfiddle.net/d4gyo5s8/
Instead of floats, I would use inline blocks as follows.
.container {
margin: 0 auto;
max-width:1050px;
}
.price-carousel{
background-color: #eee;
float:left;
height:auto;
margin-top: 10px;
padding-bottom: 10px;
width:100%;
}
.price-Container {
text-align: center; /* this will center the month and year columns */
}
.price-carousel .month-column{
background-color: #fff;
border: 1px solid;
display: inline-block; /* add this line instead of float */
height:120px;
margin-left: 0;
margin-top:35px;
text-align: center;
width:240px;
}
.price-carousel .year-column{
border: 1px solid;
background-color: #fff;
display: inline-block; /* add this line instead of float */
height:120px;
margin-left: 30px;
margin-right: -10%;
margin-top:35px;
text-align: center;
width:240px;
}
.price-carousel .year-column h4, .price-carousel .month-column h4{
background-color: #676767;
color: #fff;
height: 25px;
margin-top: 0px;
padding-top:5px;
width: 100%;
}
<section class="carousel price-carousel"> <!--Price section -->
<div class="container">
<div class="price-Container">
<div class="month-column">
<h4>Monthly</h4>
<p>$9.95</p>
<p class=”sub-text”>per computer</p>
</div>
<div class="year-column">
<h4>Yearly</h4>
<p>$99</p>
<p class=”sub-text”>Save 20% when you pay anually</p>
</div>
</div>
</div>
</section>
I'll just post an updated version of the JSFiddle
Basically I removed the float :left|right and I added the CSS display: inline-block so that your two announcements indeed act as inline-blocks. As you have text-align : center then the blocks will automatically center on the screen. Feel free to add some margin if you want to increase the space between them.
http://jsfiddle.net/um0nyna3/
HTML:
<div class="wrapper">
<div class="leftcol">
test
</div>
<div class="rightcol">
test
</div>
</div>
CSS:
.wrapper {
width: 500px;
margin: 0 auto;
}
.leftcol {
float: left;
width: 49%;
background-color: lightblue;
margin-right: .5%;
margin-left: .5%;
}
.rightcol {
float: left;
width: 49%;
background-color: lightgreen;
margin-right: .5%;
margin-left: .5%;
}
Heres a good base for you to start off with.
Basically to get it even for a responsive site you need to set all widths in percentages. Any padding/margin on left or right also need to be percentages. Test this out. I didn't add any media queries as this should give you a good base.