Remove padding on the left and right of a grid system - html

I have a grid system with 10px padding between columns. But i don't want that padding on the left and right side of the whole grid. I have tried putting the columns in a big wrapper and adding margin-left: -10px and margin-right:-10px but it just moves the grid left.
<div class="column column-1"><div class="inner"></div></div>
<div class="column column-2"><div class="inner"></div></div>
<div class="column column-3"><div class="inner"></div></div>
<div class="column column-1"><div class="inner"></div></div>
<div class="column column-1"><div class="inner"></div></div>
<div class="column column-1"><div class="inner"></div></div>
CSS:
.row,
.column {
box-sizing: border-box;
}
.column {
position: relative;
float: left;
display: inline-block;
}
.column-1 {
width: 33.3333333%;
}
.column-2 {
width: 66.6666666%;
}
.column-3 {
width: 100%;
}
.column {
min-height: 60px;
padding: 10px;
}
.inner {
height: 100px;
width: 100%;
background: black;
display:block;
position: relative;
}
JS Fiddle

I notice you have a .row class defined in your css, but aren't using it.
If you start using it, and embed your columns within a row, you can use :first-child and :last-child selectors to change the margins on the end columns.
Like so
.row .column:first-child {
padding-left: 0;
}
.row .column:last-child {
padding-right: 0;
}
<div class="row">
<div class="column column-1"><div class="inner"></div></div>
<div class="column column-2"><div class="inner"></div></div>
</div>
Fiddle

Related

3 column layout will not fill width of screen

I'm fairly new to html/css and I'm having a problem with a project. I am trying to create a 3 column layout for the bottom portion of my page. What I have right now is close to what I want, but it does not fill the width of the screen. It's all bunched on the left side and does not stretch to match the screen. I went back and followed the example from w3schools and it still didn't work. What am I missing?
HTML
<div class="row">
<div class="column">
<h1>FOLLOW ME ON <br> INSTAGRAM</h1>
</div>
<div class="column">
<h2>contact me</h2>
</div>
<div class="column">
<h1>SUBSCRIBE</h1>
</div>
</div>
CSS
.column {
float: left;
padding: 10px;
height: 300px;
text-align: center;
position: relative;
}
.column.side {
width: 25%;
}
.column.middle {
width: 50%;
}
.row:after {
content: "";
display: table;
clear: both;
}
If you wanna have padding "inside" element, then you need to set box-sizing: border-box.
.row {
width: 100%;
}
.column {
float: left;
padding: 10px;
height: 300px;
text-align: center;
box-sizing: border-box;
}
.column.side {
width: 25%;
}
.column.middle {
width: 50%;
}
<div class="row">
<div class="column side">
<h1>FOLLOW ME ON <br> INSTAGRAM</h1>
</div>
<div class="column middle">
<h2>contact me</h2>
</div>
<div class="column side">
<h1>SUBSCRIBE</h1>
</div>
</div>
Two things:
1.) As #chojnicki wrote in a comment, you need to add the classes defined in the CSS (.side, .middle) to your HTML
2.) To include the padding in the width in order to get a sum of exactly 100% overall (and not more than that to avoid the last column to go under the second one) for the added widths (25% + 50% + 25%), you need to add box-sizing: border-box; to everything (using the *selector):
* {
box-sizing: border-box;
}
.column {
float: left;
padding: 10px;
height: 300px;
text-align: center;
position: relative;
}
.column.side {
width: 25%;
}
.column.middle {
width: 50%;
}
h1 {
font-size: 24px;
}
h2 {
font-size: 18px;
}
<div class="row">
<div class="column side">
<h1>FOLLOW ME ON <br> INSTAGRAM</h1>
</div>
<div class="column middle">
<h2>contact me</h2>
</div>
<div class="column side">
<h1>SUBSCRIBE</h1>
</div>
</div>
Why you don't use .col-xs-4?
and you shouldn't use width to your cols so why do you use col?

Align 6 divs content vertical

How would i align all these 6 divs vertically in a 3x3 pattern so that the top and bottom divs content are aligned with each other so it looks good. i've tried some vertical-align: middle; with no sucess.
It's a must to be 100% responsive and that the number also is centered and aligned so whatever number gets there is aligned.
.top-right-container {
position: absolute;
border: 1px solid white;
height: 20%;
width: 50%;
right: 0;
top: 0;
}
.stats-container {
position: relative;
float: left;
border: 1px solid white;
width: 75%;
height: 80%;
}
.Agility,
.Stamina,
.Respect,
.Intelligence,
.Strength,
.Cash {
display: inline-block;
color: black;
}
.Agility,
.Intelligence {
float: left;
margin-left: 10%;
}
.Stamina,
.Strength {
margin: 0 auto;
}
.Respect,
.Cash {
margin-right: 10%;
float: right;
}
.stats-container h2 {
font-family: Marker-Felt;
margin: 0;
font-size: calc(0.7vh + 1.2vw);
}
.stats-container p {
margin: 5%;
text-align: center;
font-size: calc(0.5vh + 0.8vw);
}
.top-stats,
.bottom-stats {
width: 100%;
text-align: center;
}
<div class="top-right-container">
<div class="stats-container">
<div class="top-stats">
<div class="Agility">
<h2>Agility</h2>
<p>10</p>
</div>
<div class="Stamina">
<h2>Stamina</h2>
<p>10</p>
</div>
<div class="Respect">
<h2>Respect</h2>
<p>10</p>
</div>
</div>
<div class="bottom-stats">
<div class="Intelligence">
<h2>Intelligence</h2>
<p>10</p>
</div>
<div class="Strength">
<h2>Strength</h2>
<p>10</p>
</div>
<div class="Cash">
<h2>Cash</h2>
<p>10</p>
</div>
</div>
</div>
</div>
You can do it with the Flexbox:
* {margin:0;padding:0;box-sizing:border-box}
html, body {width:100%}
.stats-container {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100vh;
}
.top-stats,
.bottom-stats {
display: flex;
width: 100%;
text-align: center;
}
.Agility,
.Stamina,
.Respect,
.Intelligence,
.Strength,
.Cash {
flex: 1;
}
.stats-container h2 {
font-size: calc(0.7vh + 1.2vw);
}
.stats-container p {
font-size: calc(0.5vh + 0.8vw);
}
<div class="top-right-container">
<div class="stats-container">
<div class="top-stats">
<div class="Agility">
<h2>Agility</h2>
<p>10</p>
</div>
<div class="Stamina">
<h2>Stamina</h2>
<p>10</p>
</div>
<div class="Respect">
<h2>Respect</h2>
<p>10</p>
</div>
</div>
<div class="bottom-stats">
<div class="Intelligence">
<h2>Intelligence</h2>
<p>10</p>
</div>
<div class="Strength">
<h2>Strength</h2>
<p>10</p>
</div>
<div class="Cash">
<h2>Cash</h2>
<p>10</p>
</div>
</div>
</div>
</div>
responsive 2 rows and 6 boxes
Here is some code you can work with.
The container of all the divs .container will take 100% of the page eg. its <body> .
The rows .statRow will take 100% of its parent the container.
Now the boxes .box will take 33% of its parent width.
Then adding 3 of these boxes 33%+33%+33% will take up 99% of the container.
Additionally borders usually take up more space so width + border is its actual width.
This is fixed with chancing the elements box-sizing to border-box.
.container {
border: 10px solid black;
width: 100%;
box-sizing: border-box;
border-radius: 5px;
}
.statRow {
width: 100%;
display: block;
}
.box {
color: white;
display: inline-block;
text-align: center;
width: 33%;
border: 10px solid white;
box-sizing: border-box;
border-radius: 15px;
background-color: #222;
}
<div class="container">
<div class="statBubble">
<div class="box">
<h5>Agility</h5>
<p>10</p>
</div><!--
--><div class="box">
<h5>Strength</h5>
<p>10</p>
</div><!--
--><div class="box">
<h5>Stat</h5>
<p>number</p>
</div>
</div>
<div class="statRow">
<div class="box">
<h5>Wisdom</h5>
<p>100</p>
</div><!--
--><div class="box">
<h5>Stat</h5>
<p>number</p>
</div><!--
--><div class="box">
<h5>Stat</h5>
<p>number</p>
</div>
</div>
</div>

Make child full height of parent and vertically center text

I want to display a number and 2 text areas in a row.
The number should be in a "box" , with the background the height of the row and the number it's self should be vertically and horizontally centered in the "box".
I know I could do something like position: absolute; top: 0, left: 0 on the .number but this brings it out of the document flow. and the text, actual number does not get centered.
* {
box-sizing: border-box;
}
.container {
width: 40%;
}
.number {
background: skyblue;
/*position: absolute;*/
top: 0;
bottom: 0;
vertical-align: middle;
}
.row > div {
display: inline-block;
vertical-align: top;
}
.row {
background: lightgreen;
position: relative;
}
<div class="container">
<div class="row">
<div class="number">10</div>
<div class="textArea">
<div class="companyName">Top title</div>
<div class="industry">secondary text</div>
</div>
</div>
</div>
EDIT 1: You can see in the snippet that the box is not the full height of the container. That is not what I want.
EDIT 2: I guess you could cheat by using gradient but then I would have to make sure that the text area matches up to where the number box end to make the gradient look like the color is for the number "box".
Use flex display: table-cell
Update 1: show how to create "margin" wíthout using cell padding
Update 2: show a progressive enhancement to use flex when available
*{
box-sizing: border-box;
}
.container {
width: 40%;
}
.number{
background: skyblue;
}
.row > div {
display: table-cell;
vertical-align: middle;
}
.row {
background: lightgreen;
position: relative;
}
/* 3 ways to create a left margin on textArea */
.row .textArea.nr1 { border-left: 10px solid transparent; }
.row .textArea.nr2 { position: relative; left: 10px; }
.row .textArea.nr3 { padding-left: 10px; }
/* feature detect - use flex when available */
#supports (display: flex) {
.row > div {
display: block;
}
.row {
display: flex;
}
.row .number {
display: flex;
align-items: center;
}
}
<div class="container">
<div class="row">
<div class="number">10</div>
<div class="textArea nr1">
<div class="companyName">Top title</div>
<div class="industry">secondary text</div>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="number">10</div>
<div class="textArea nr2">
<div class="companyName">Top title</div>
<div class="industry">secondary text</div>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="number">10</div>
<div class="textArea nr3">
<div class="companyName">Top title</div>
<div class="industry">secondary text</div>
</div>
</div>
</div>
You can use flexbox to achieve that, all modern browsers support it, and with prefixes it also works on IE10.
* {
box-sizing: border-box;
}
.container {
width: 40%;
}
.row {
background: lightgreen;
display: flex;
}
.number {
background: skyblue;
display: flex;
align-items: center;
}
<div class="container">
<div class="row">
<div class="number">10</div>
<div class="textArea">
<div class="companyName">Top title</div>
<div class="industry">secondary text</div>
</div>
</div>
</div>
Or, use CSS table making it to work on legacy browsers too.
* {
box-sizing: border-box;
}
.container {
width: 40%;
}
.row {
background: lightgreen;
display: table;
width: 100%;
}
.number,
.textArea {
display: table-cell;
}
.number {
background: skyblue;
white-space: nowrap;
vertical-align: middle;
}
.textArea {
width: 100%;
}
<div class="container">
<div class="row">
<div class="number">10</div>
<div class="textArea">
<div class="companyName">Top title</div>
<div class="industry">secondary text</div>
</div>
</div>
</div>
*{
box-sizing: border-box;
}
.container{
width: 40%;
}
.number{
background: skyblue;
/*position: absolute;*/
top: 0;
bottom: 0;
vertical-align: middle;
height: 40px;
padding-top: 11px;
}
.row > div{
display: inline-block;
vertical-align: top;
}
.row{
background: lightgreen;
position: relative;
}
<div class="container">
<div class="row">
<div class="number">10</div>
<div class="textArea">
<div class="companyName">Top title</div>
<div class="industry">secondary text</div>
</div>
</div>
</div>

3 Column Div how to display them inline when browser resize?

I have 3 divs but the 3rd div comes down when i resize the browser.
How can i still display them inline when browser resize?
I can do it by changing the width of my container
but i want it to be 100%
This is my Code:
.box{
float :left;
width: 250px;
background: #f6f6f6;
border: 1px solid #e0e0e0;
min-height: 150px;
position: relative;
padding: 20px;
margin-right: 26px;
}
.container{
width: 100%;
}
<div class="container">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<br style="clear: left;">
</div>
Thanks :)
You need to use the CSS3 flexible box layout so that the elements do not wrap to next line and resize accordingly. The default values of flex-direction is row and flex-wrap is nowrap. So you need not set the values here.
.box {
width: 250px;
background: #f6f6f6;
border: 1px solid #e0e0e0;
min-height: 150px;
position: relative;
padding: 20px;
margin-right: 26px;
}
.container {
width: 100%;
display: flex; /* Added */
}
<div class="container">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<br style="clear: left;">
</div>
You can use like this -
.box{float:left;
max-width: 250px;
min-width: 20%;
/*max-width: 250px;*/
background: #f6f6f6;
border: 1px solid #e0e0e0;
min-height: 150px;
position: relative;
padding: 20px;
margin-right: 26px;
}
.container{
width: 100%;
}
<div class="container">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<br style="clear: left;">
</div>
Instead of specifying .box {width: 250px} make it in percentage, remove margin, add box-sizing for padding issue:
.box {
float: left;
width: 33.33%;
/* Instead of old 250px; */
background: #f6f6f6;
border: 1px solid #e0e0e0;
min-height: 150px;
position: relative;
padding: 20px;
box-sizing: border-box;
/* To include padding to width */
/*margin-right: 26px; Can't use margin for responsive gaps*/
}
.container {
width: 100%;
}
<div class="container">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<br style="clear: left;">
</div>
Added an inner div
bodY{
margin: 0;
}
.box{
float :left;
width: 250px;
background: #f6f6f6;
border: 1px solid #e0e0e0;
min-height: 150px;
position: relative;
padding: 20px;
margin-right: 26px;
}
.box:nth-child(3){
margin-right: 0;
}
.container{
overflow-x: hidden;
width: 100%;
}
.inner{
margin-right: -400px;
width: 928px;
}
<div class="container">
<div class="inner">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<br style="clear: left;">
</div>
</div>
I think it is easy if you try bootstrap.
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="col-md-4 col-sm-4 col-xs-4">
//first div contents
</div>
<div class="col-md-4 col-sm-4 col-xs-4">
//second div contents
</div>
<div class="col-md-4 col-sm-4 col-xs-4">
// third div contents
</div>
Note you should include required bootstrap files to use this classes

HTML/CSS Float Left and Top/Bottom without position absolute/fixed

I'm working on my Chemistry application, and I'm struggling with displaying div element how I imagined it could work.
My goal is to have divs floating left as on image: so when hiding red/green div everything stays in order.
Is it even possible without using absolute/fixed positioning? I really need those divs to float left and be aware of each other so I can't solve it by position absolute. I tried experimenting with adding margin, but other div cannot fit into place taken by other element margin.
Thank you for your time spent on reading this post!
Code added:
<div class='container'>
<div class='base-cell'>S</div>
<div class='base-cell'>O</div>
<div class='index-cell'>3</div>
<div class='charge-cell'>2-</div>
</div>
.container{
border: 1px solid red;
height: 1.5em;
text-align: center;
position: relative;
}
.base-cell{
position: relative;
background: red;
height: 1em;
float: left;
margin-top: 0.2em;
font-size: 1em;
border: 1px solid orange;
display: inline-block;
}
.index-cell{
position:relative;
height:0.7em;
margin-top:1.5em;
font-size:0.7em;
display:table;
background: blue;
float:left;
}
.ion-index-cell{
position: relative;
height: 1em;
font-size: 0.7em;
border: 1px solid cyan;
display: table;
background: green;
}
.charge-cell{
height: 1em;
font-size: 0.7em;
border: 1px solid blue;
display: inline-block;
float:left;
}
Edit:
Thank you for your replies, I really don't want to use middle column solution, because of another requirement: sorry for not showing full context before.
As you can see in the picture, all elements flow to the left, and I may need to hide some by using display: none. Thats why I'm looking for parentless solution:
If you flip the diagram on its side then its a lot easier to build using floats. You can use transforms to flip it back up the correct way.
.wrap {
max-width: 100px;
-webkit-transform: rotate(90deg) translate(-170px, -10px);
-moz-transform: rotate(90deg) translate(-170px, -10px);
-ms-transform: rotate(90deg) translate(-170px, -10px);
transform: rotate(90deg) translate(-170px, -10px);
-webkit-transform-origin: 0% 100%;
-moz-transform-origin: 0% 100%;
-ms-transform-origin: 0% 100%;
transform-origin: 0% 100%;
overflow: hidden;
}
.block {
height: 40px;
width: 100%;
float: left;
border: 1px solid #000;
box-sizing: border-box;
margin: 10px 0;
}
.block-left {
max-width: 40%;
border-color: #f00;
}
.block-right {
max-width: 40%;
float: right;
border-color: #0f0;
}
<div class="wrap">
<div class="block block-top"></div>
<div class="block block-left"></div>
<div class="block block-right"></div>
<div class="block block-bottom"></div>
</div>
<div class="wrap">
<div class="block block-top"></div>
<div class="block block-right"></div>
<div class="block block-bottom"></div>
</div>
<div class="wrap">
<div class="block block-top"></div>
<div class="block block-left"></div>
<div class="block block-bottom"></div>
</div>
This may help you somewhat. Its very crude html but I believe does what your looking for. It should at least help you in the direction your looking to go.
<div style="height:100%;">
<div style="float:left; width: 33%;">
Content 1
</div>
<div style="float:left; width: 33%;">
<div style="height:50%">
<div>Content 2</div>
</div>
<div style="height:50%;">
<div>Content 3</div>
</div>
</div>
<div style="float:left; width: 33%;">
Content 4
</div>
</div>
From your question, it looks like you just want to use float:left instead of position:absolute which you are using currently and still want to hide the green and red boxes, while keeping all other boxes intact.
This can be achieved by using float:left; on the boxes while setting the opacity:0; on the red and green boxes (also visibility:hidden work).
So I'm not sure how you are handling the mark up but hopefully you are doing it the proper way. It seems like you have a grid-format in place but you are not applying this on the middle column.
What you should be doing is creating three columns and then when necessary, you can hide the middle column. The red and green box can exist within the middle column. This way if you ever say wanted to add those red/green sections in the left or right column, you can easily do that.
I have created an example below. I have also added a class called hide which can apply to the different columns and/or inner boxes. Like I was mentioning above, you should be adding hide to the middle col if you want to hide everything in the middle column. Apply hide to the inner elements if you want to hide one of those.
I do some absolute positioning in the middle column but you don't actually need to do this -- you can change this to float: left and simply set a margin-top for the bottom box.
.col {
float: left;
width: 100px;
height: 200px;
border: 1px solid black;
margin-left: 10px;
position: relative;
}
.inner {
width: 100px;
height: 75px;
margin-left: auto;
margin-right: auto;
position: absolute;
}
.top {
top: 0;
border: 1px solid red;
}
.bottom {
bottom: 0;
border: 1px solid green;
}
.hide {
display: none;
<div class="col left"></div>
<div class="col middle">
<div class="top inner"></div>
<div class="bottom inner"></div>
</div>
<div class="col right"></div>
EDIT: I notice you posted your CSS and you're using display: table. For that I would like to refer you to this link.
shouldiusetablesforlayout.com
EDIT2: I see you updated your question but the overall concept applies. You are still dealing with columns but I guess in your case now, you kind of want those columns in containers.
.col-container {
float: left;
margin-bottom: 10px;
}
.col {
float: left;
width: 100px;
height: 200px;
border: 1px solid black;
margin-left: 10px;
position: relative;
}
.inner {
width: 100px;
height: 75px;
margin-left: auto;
margin-right: auto;
position: absolute;
}
.top {
top: 0;
border: 1px solid red;
}
.bottom {
bottom: 0;
border: 1px solid green;
}
.hide {
display: none;
}
<div class="col-container">
<div class="col left"></div>
<div class="col middle">
<div class="top inner"></div>
<div class="bottom inner hide"></div>
</div>
<div class="col right"></div>
</div>
<div class="col-container">
<div class="col left"></div>
<div class="col middle">
<div class="top inner hide"></div>
<div class="bottom inner"></div>
</div>
<div class="col right"></div>
</div>
<div class="col-container">
<div class="col left"></div>
<div class="col middle">
<div class="top inner"></div>
<div class="bottom inner"></div>
</div>
<div class="col right"></div>
</div>
If you view it in full page, and shrink the window size, you'll see the 3rd col-container to appear on the second line. If you want to make sure it only has two columns or things break at certain points you can adjust for that by either adding clear to certain elements, distinguishing row classes, etc.
I would use flexbox and justify-content: space-between; should be the thing you are asking for.
<article>
<div>left</div>
<div class="content">
<p>top</p>
<p>bottom</p>
</div>
<div>right</div>
</article>
article {
display: flex;
min-height: 10em;
}
article > div {
flex: 1 1 calc(33.3333% - 1em);
margin: 0.5em;
}
.content {
display: flex;
flex-direction: column;
justify-content: space-between;
}
Codepen sample (w/ -prefix-free, styling and as SCSS)
Simple ;)