How to center horizontal table-cell - html

I want the Content A, Content B, and Content C columns to be horizontally centered. I have this code been trying to add
http://jsfiddle.net/hsX5q/24/
HTML:margin: 0 auto to .columns-container and it doesn't work. Could anyone help?
/*************************
* Sticky footer hack
* Source: http://pixelsvsbytes.com/blog/2011/09/sticky-css-footers-the-flexible-way/
************************/
/* Stretching all container's parents to full height */
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
/* Setting the container to be a table with maximum width and height */
#container {
display: table;
height: 100%;
width: 100%;
}
/* All sections (container's children) should be table rows with minimal height */
.section {
display: table-row;
height: 1px;
}
/* The last-but-one section should be stretched to automatic height */
.section.expand {
height: auto;
}
/*************************
* Full height columns
************************/
/* We need one extra container, setting it to full width */
.columns-container {
display: table-cell;
height: 100%;
width: 300px;
margin: 0 auto;
}
/* Creating columns */
.column {
/* The float:left won't work for Chrome for some reason, so inline-block */
display: inline-block;
/* for this to work, the .column elements should have NO SPACE BETWEEN THEM */
vertical-align: top;
height: 100%;
width: 100px;
}
/****************************************************************
* Just some coloring so that we're able to see height of columns
****************************************************************/
header {
background-color: yellow;
}
#a {
background-color: pink;
}
#b {
background-color: lightgreen;
}
#c {
background-color: lightblue;
}
footer {
background-color: purple;
}
<div id="container">
<header class="section">
foo
</header>
<div class="section expand">
<div class="columns-container">
<div class="column" id="a">
<p>Contents A</p>
</div>
<div class="column" id="b">
<p>Contents B</p>
</div>
<div class="column" id="c">
<p>Contents C</p>
</div>
</div>
</div>
<footer class="section">
bar
</footer>
</div>

If you add text-align: center to the declarations for .columns-container then they align centrally:
.columns-container {
display: table-cell;
height: 100%;
width:600px;
text-align: center;
}
/*************************
* Sticky footer hack
* Source: http://pixelsvsbytes.com/blog/2011/09/sticky-css-footers-the-flexible-way/
************************/
/* Stretching all container's parents to full height */
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
/* Setting the container to be a table with maximum width and height */
#container {
display: table;
height: 100%;
width: 100%;
}
/* All sections (container's children) should be table rows with minimal height */
.section {
display: table-row;
height: 1px;
}
/* The last-but-one section should be stretched to automatic height */
.section.expand {
height: auto;
}
/*************************
* Full height columns
************************/
/* We need one extra container, setting it to full width */
.columns-container {
display: table-cell;
height: 100%;
width:600px;
text-align: center;
}
/* Creating columns */
.column {
/* The float:left won't work for Chrome for some reason, so inline-block */
display: inline-block;
/* for this to work, the .column elements should have NO SPACE BETWEEN THEM */
vertical-align: top;
height: 100%;
width: 100px;
}
/****************************************************************
* Just some coloring so that we're able to see height of columns
****************************************************************/
header {
background-color: yellow;
}
#a {
background-color: pink;
}
#b {
background-color: lightgreen;
}
#c {
background-color: lightblue;
}
footer {
background-color: purple;
}
<div id="container">
<header class="section">
foo
</header>
<div class="section expand">
<div class="columns-container">
<div class="column" id="a">
<p>Contents A</p>
</div>
<div class="column" id="b">
<p>Contents B</p>
</div>
<div class="column" id="c">
<p>Contents C</p>
</div>
</div>
</div>
<footer class="section">
bar
</footer>
</div>
This does, though, require that you reset the .column elements to text-align: left (assuming you want them left-aligned, obviously (JS Fiddle demo).

Sometimes you have things other than text inside a table cell that you'd like to be horizontally centered. In order to do this, first set up some css...
<style>
div.centered {
margin: auto;
width: 100%;
display: flex;
justify-content: center;
}
</style>
Then declare a div with class="centered" inside each table cell you want centered.
<td>
<div class="centered">
Anything: text, controls, etc... will be horizontally centered.
</div>
</td>

Short snippet for future visitors - how to center horizontal table-cell (+ vertically)
html, body {
width: 100%;
height: 100%;
}
.tab {
display: table;
width: 100%;
height: 100%;
}
.cell {
display: table-cell;
vertical-align: middle;
text-align: center; /* the key */
background-color: #EEEEEE;
}
.content {
display: inline-block; /* important !! */
width: 100px;
background-color: #00FF00;
}
<div class="tab">
<div class="cell">
<div class="content" id="a">
<p>Content</p>
</div>
</div>
</div>

Just add this class in your css
.column p{
text-align:center;
}

Related

positioning sticky sidebar right beside a centered div

I'm trying to customize my blog layout by adding a sticky sidebar. I managed to get position:sticky to work as excepted, but I can't figure out how to position the blocks where I want.
I want the main block to be centered, and the sidebar right beside the main block. here's an example of what I'm aiming for: https://theme-next.js.org/ except I want the main block to be centered.
this is the layout I want
I've tried using margin-left with the sidebar, but it doesn't work well in smaller windows, as the left margin is constant and pushes the real content away in smaller windows.
this is what happens by using margin-left
(I'm not sure why the sticky dosen't work here, but it works fine on my website. All I'm trying to figure out is how to position them where I want.
.wrapper {
display: flex;
justify-content: space-between;
}
.sidebar {
width: 80px;
background-color: #FF0000;
position: webkit-stiky;
position: sticky;
align-self: flex-start;
height: 1000px;
}
.main {
width: 100px;
background-color: #CFCFCF;
margin: auto;
height: 1600px;
}
.header {
background-color: #F3FF00;
width: 150px;
margin: auto;
}
<div class="header">
<p>
this is centered header
</p>
</div>
<div class="wrapper">
<div class="sidebar">
<p> sidebar here</p>
</div>
<div class="main">
<p>
I want this block to be centered;
</p>
</div>
</div>
There's a few things you need to do here:
Set a top property for your sticky sidebar, or it won't stick
Make your main element a flex parent since we'll need to offset its child element to make it centered with your header.
Create an inner element for your main element so you can move it to the left 80px to accommodate for the sidebar width.
.wrapper {
display: flex;
position: relative;
}
.sidebar {
width: 80px;
height: 1000px;
background-color: #FF0000;
position: sticky;
/* you need a top position set for sticky */
top: 0;
}
.main {
height: 1600px;
background-color: #eeeeee;
/* This needs to be a flex parent */
display: flex;
justify-content: center;
/* This element needs to be 100% MINUS the sidebar width of 80px */
flex: 1 0 calc(100% - 80px);
}
.main-inner {
width: 100px;
position: relative;
background-color: #CFCFCF;
/* Move the inner element 80px to the left */
margin-left: -80px;
text-align: center;
}
.header {
background-color: #F3FF00;
width: 150px;
margin: 0 auto;
}
<div class="header">
<p>this is centered header</p>
</div>
<div class="wrapper">
<div class="sidebar">
<p>sidebar here</p>
</div>
<div class="main">
<div class="main-inner">
<p>I want this block to be centered;</p>
</div>
</div>
</div>
However, I believe this is what you really want:
Make the header a flex parent as well.
.wrapper {
display: flex;
position: relative;
}
.sidebar {
width: 80px;
height: 1000px;
background-color: #FF0000;
position: sticky;
/* you need a top position set for sticky */
top: 0;
}
.main {
height: 1600px;
background-color: #eeeeee;
/* This needs to be a flex parent */
display: flex;
justify-content: center;
/* This element needs to be 100% MINUS the sidebar width of 80px */
flex: 1 0 calc(100% - 80px);
}
.main-inner {
width: 100px;
position: relative;
background-color: #CFCFCF;
/* Move the inner element 80px to the left */
text-align: center;
}
.header {
display: flex;
justify-content: flex-end;
}
.header-inner {
background-color: #F3FF00;
text-align: center;
width: calc(100% - 80px);
background-color: #F3FF00;
}
<div class="header">
<div class="header-inner">
<p>this is centered header</p>
</div>
</div>
<div class="wrapper">
<div class="sidebar">
<p>sidebar here</p>
</div>
<div class="main">
<div class="main-inner">
<p>I want this block to be centered;</p>
</div>
</div>
</div>

Position title over image

I need to create something like the image below, without using any frameworks like bootstrap. Basically, I need the image to not be full width, but to take say 80% of the screen, and the title of the webpage to be above that image. At the moment all of my content is flowing all around the page.
It should also remain the same when I make the screen smaller.
I don't know why something simple is just not working for me...
.container {
width: 100%;
}
}
#main {
background: red;
width: 80%;
margin: 0 auto;
}
img {
max-width: 100%;
width: 80%;
float: right;
display: block;
}
<div id='main'>
<div class='container'>
<!--Image-->
<div id='img-div'>
<img id='image' src='https://www.projectarm.com/wp-content/uploads/2018/01/Frida-Kahlo-Vogue-1939-New-York-foto-di-Nickolas-Murray-2.jpg' />
<div id='img-caption'>This is a caption for the image</div>
</img>
</div>
<!--Title-->
<div id='pagetitle'>
<h1 id='title'>Frida Kahlo</h1>
<span id='tagline'>A short Tribute</span>
</div>
<!--End Title-->
</div>
<div id='tribute-info'>
This is my main information about the person
This is a link for more information
</div>
I would use flex for your container, so you can swap the order and it is a more up to date way to position things than floating, then inline block for your tag lines
Please note your image tag is invalid - img tags are self closing
.container {
width: 100%;
display: flex; /* make the container flex */
flex-direction: row; /* align the children in a row */
}
#img-div {
max-width: 85%; /* 85% width */
flex-basis: 85%;
order: 2; /* put this div 2nd */
}
#image {
display: block;
width: 100%; /* make div stretch size of div */
}
#pagetitle {
box-sizing: border-box; /* make this div 15% width with a bit of padding */
padding: 20px;
max-width: 15%;
flex-basis: 15%;
order: 1; /* put this div 1st */
display: flex; /* make this flex for vertical aligning and align children in a column */
flex-direction: column;
justify-content: center; /* vcertical align center (only works with flex) */
overflow: visible; /* show overflow */
position: relative; /* make overflow appear on top of image */
z-index: 2;
}
#title {
font-size: 20px;
font-weight: normal;
}
.tag-holder {
position: relative;
}
.tagline {
display: inline-block; /* make inline block so you can add white background */
white-space: nowrap;
font-size: 30px;
font-weight: bold;
text-transform: uppercase;
background: white;
padding:0.1em 0.5em;
}
<div id='main'>
<div class='container'>
<!--Image-->
<div id='img-div'>
<img id='image' src='https://www.projectarm.com/wp-content/uploads/2018/01/Frida-Kahlo-Vogue-1939-New-York-foto-di-Nickolas-Murray-2.jpg' />
</div>
<!--Title-->
<div id='pagetitle'>
<h1 id='title'>Emilia<br>Cole</h1>
<div class="tag-holder">
<span class='tagline'>Twist</span>
<span class='tagline'>in my</span>
<span class='tagline'>reality</span>
</div>
</div>
<!--End Title-->
</div>
<div id='tribute-info'>
This is my main information about the person
This is a link for more information
</div>
Without flex:
.container {
width: 100%;
position: relative;
}
.container:after {
content: '';
display: block;
height: 0;
clear: both;
overflow: hidden;
}
#img-div {
width: 85%;
/* 85% width */
float: right;
}
#image {
display: block;
width: 100%;
/* make div stretch size of div */
}
#pagetitle {
position:absolute; /* this is for 100% height */
top:0; bottom:0;
left:0;
right:15%;
overflow: visible;
z-index: 2;
}
.center { /* center text vertically */
position:absolute;
top:50%;
left:20px;
transform:translateY(-50%);
}
#title {
font-size: 20px;
font-weight: normal;
margin-top:0;
}
.tag-holder {
position: relative;
}
.tagline {
display: inline-block;
/* make inline block so you can add white background */
white-space: nowrap;
font-size: 30px;
font-weight: bold;
text-transform: uppercase;
background: white;
padding: 0.1em 0.5em;
}
<div id='main'>
<div class='container'>
<!--Image-->
<div id='img-div'>
<img id='image' src='https://www.projectarm.com/wp-content/uploads/2018/01/Frida-Kahlo-Vogue-1939-New-York-foto-di-Nickolas-Murray-2.jpg' />
</div>
<!--Title-->
<div id='pagetitle'>
<div class="center">
<h1 id='title'>Emilia<br>Cole</h1>
<div class="tag-holder">
<span class='tagline'>Twist</span><br>
<span class='tagline'>in my</span><br>
<span class='tagline'>reality</span>
</div>
</div>
</div>
<!--End Title-->
</div>
<div id='tribute-info'>
This is my main information about the person
This is a link for more information
</div>
Try this image caption is placed on top
.container {
width: 100%;
}
}
#main {
background: red;
width: 80%;
margin: 0 auto;
}
img {
max-width: 100%;
width: 80%;
float: right;
display: block;
}
#img-caption{
position: absolute;
left: 50%;
margin-left: -50px; /* margin is -0.5 * dimension */
}
<html lang="en">
<head>
<meta charset="utf-8">
<title>Tribute Page</title>
</head>
<body>
<div id='main'>
<div class = 'container'>
<!--Image-->
<div id='img-div'>
<img id='image' src='https://www.projectarm.com/wp-content/uploads/2018/01/Frida-Kahlo-Vogue-1939-New-York-foto-di-Nickolas-Murray-2.jpg'/>
<div id='img-caption' class='alert alert-info'>This is a caption for the image</div>
</div>
<!--Title-->
<div id='pagetitle'>
<h1 id='title'>Frida Kahlo</h1>
<span id ='tagline'>A short Tribute</span>
</div>
<!--End Title-->
</div>
<div id ='tribute-info'>
This is my main information about the person
This is a link for more information
</div>
</div>
</body>
</html>
I was able to solve it simply by giving my container element a relative positioning, and my title #pagetitle element an absolute positioning, and then positioning the title top: 30px and left 30px RELATIVE to my container element. In this way, my title was positioned relative to the container, and not to the page - which would otherwise be the case had I not given the relative positioning to the container of the title.
.container {
height: 90vh;
min-height: 410px;
margin-top: 40px;
position: relative;
}
#pagetitle {
font-size: 2em;
text-transform: uppercase;
letter-spacing: 0.3em;
position: absolute;
top: 30px;
left: 30px;
}
I also gave a height to my container to make sure the content won't flow around it.
This can be easily done with CSS Grid, which is a more modern technology as well, but I preferred to stick to the traditional positioning to learn and fully understand them before skipping steps and using the easier grid system.
Whole pen and result can be seen here: https://codepen.io/commiesar/pen/GBMLza?editors=1100

css how to make columns auto fill background color to height 100%

I have the following code:
#left {
float: left;
width: 180px;
background-color: #ff0000;
}
#right {
overflow: hidden;
background-color: #00FF00;
}
<div>
<div id="left">
left
</div>
<div id="right">
right<br/> down
</div>
</div>
What I am trying to achieve is to make the left column's background-color to fill the whole div. This should be the case regardless of the height of the #right column.
so far I have tried/added
display: flex;
height: 100%
flex 1;
To the #left style but it is not working.
This has to be a CSS only solution.
You can use flexbox here. Demo:
.container {
/* become a flex-container */
/* flex-items will be stretched vertically by default */
display: flex;
}
#left {
width: 180px;
background-color: #ff0000;
}
#right {
/* occupy remaining width */
flex: 1;
background-color: #00FF00;
}
<div class="container">
<div id="left">
left
</div>
<div id="right">
right
<br/> down
</div>
</div>
Also you can use CSS Grid layout here. Demo:
.container {
/* become a grid-container */
display: -ms-grid;
display: grid;
/* first column should occupy 180px and second should occupy remaining width */
-ms-grid-columns: 180px 1fr;
grid-template-columns: 180px 1fr;
}
#left {
background-color: #ff0000;
}
#right {
background-color: #00FF00;
/* specify column for IE/Edge explicitly */
-ms-grid-column: 2;
}
<div class="container">
<div id="left">
left
</div>
<div id="right">
right
<br/> down
</div>
</div>
Need to understand why #left doesn't takes height of 100% and that's because you have used float:left property in #left. So whenever float left is used on an element it by default changes it display to inline-block and that's the reason that you have to change display of your parent div to overcome this.
Here is below using flex,
div{
display:flex;
}
div #left {
width: 180px;
background-color: #ff0000;
}
div #right {
flex:1;
overflow: hidden;
background-color: #00FF00;
}
<div>
<div id="left">
left
</div>
<div id="right">
right<br/> down
</div>
</div>
Read this to understand how float works.
.main-table {
display: table;
}
#left {
display: table-cell;
float: none;
width: 180px;
background-color: #ff0000;
}
#right {
display: table-cell;
overflow: hidden;
background-color: #00FF00;
}
<div class="main-table">
<div id="left">
left
</div>
<div id="right">
right<br/> down
</div>
</div>
Something like table may help you in this case!!

Inline-block with paragraph keeps jumping

I am trying to make 3 boxes next to each other with text in. So far I got the boxes setup right. But when I add text it jumps up and down. When I enter the same amount of paragraph tags in a div they all align. But when one div has 1 paragraph tag and the others have 2 they aren't aligned anymore.
I'm not sure how to solve this.
JSFIDDLE: https://jsfiddle.net/gegc8nuk/
.row {
max-width: 1140px;
margin: 0 auto;
}
section {
padding: 80px 0;
}
.wrap {
display: table;
/* Webkit Fix */
width: 100%;
/* set width to stop display table shrink to fit */
word-spacing: -1em;
/* hide whitespace nodes (not in webkit) - will never overlap even if zoomed */
}
.tox {
display: inline-block;
height: 200px;
width: 100%;
word-spacing: 0;
color: #fff;
padding: 5%;
/* reset parent */
}
.red {
background-color: #9a0000;
}
.green {
background-color: #4ce215;
}
.blue {
background-color: #240fc3;
}
<section>
<div class="row">
<div class="wrap">
<div class="tox red span-1-of-3">
<p>Hello</p>
</div>
<div class="tox green span-1-of-3">
<p>Hello</p>
</div>
<div class="tox blue span-1-of-3">
<p>Hello</p>
<p>Hello</p>
<p>Hello</p>
</div>
</div>
</div>
</section>
If you use once display:table; why don't you use table-cells on childs ?
https://jsfiddle.net/gegc8nuk/1/
.row {
max-width: 1140px;
margin: 0 auto;
}
section {
/*padding: 80px 0;*/
}
.wrap {
display: table;
width: 100%;
/* set width to stop display table shrink to fit
word-spacing: -1em;
hide whitespace nodes (not in webkit) - will never overlap even if zoomed */
}
.tox {
display: table-cell;
/* height: 200px; */
/*width: 100%;
word-spacing: 0;*/
color: #fff;
padding: 5%;
/* reset parent */
}
.red {
background-color: #9a0000;
}
.green {
background-color: #4ce215;
}
.blue {
background-color: #240fc3;
}
<section>
<div class="row">
<div class="wrap">
<div class="tox red span-1-of-3">
<p>Hello</p>
</div>
<div class="tox green span-1-of-3">
<p>Hello</p>
</div>
<div class="tox blue span-1-of-3">
<p>Hello</p>
<p>Hello</p>
<p>Hello</p>
</div>
</div>
</div>
</section>
With your css you have set tox with 100% width while keeping inline-block, while applied wrap as display: table; . Both are different. You can do the following:
Edit your CSS:
.wrap {
display: block;
/* Webkit Fix */
width: 100%;
/* set width to stop display table shrink to fit */
word-spacing: -1em;
/* hide whitespace nodes (not in webkit) - will never overlap even if zoomed */
}
.tox {
display: inline-block;
height: 200px;
width: 33.333%;
padding: 10px;
word-spacing: 0;
color: #fff;
vertical-align: top;
box-sizing: border-box;
/* reset parent */
}
Fiddle: https://jsfiddle.net/debraj/gegc8nuk/2/

Prevent img from stretching display: table

I have a container with a fixed height, inside is an <article> with display: table; property. I want this <article> to fill his parent, both in height and width (height: 100%;width: 100%;).
The tricky part is this table has 3 children : a header and a footer, both with variable heights, and a third, main element, that should fill the remaining space.
The problem is, in that main element, I have an <img> bigger in height than the main container, stretching the table, despite the height: 100%; (illustration here :
Is there a way to limit the img's height so that it doesn't stretch the table ? I tried height/max-height: 100% with no luck.
As the code needs to be IE9 compatible, I can't use a flexbox layout, hence the display: table-* approach.
Here's the full code and a codepen, notice how the table is bigger than its parent container (<section> with red border), due to the img : http://codepen.io/anon/pen/xGJLLa
HTML
<section>
<article class="table">
<header class="row">
<h2>My header</h2>
</header>
<div class="mainrow row">
<div class="vmiddle">
<img src="http://lorempixel.com/200/300/abstract/" alt="Myimg" />
</div>
<div>
<p>Lorem ipsum [...]</p>
</div>
</div>
<footer class="row">
My Footer
</footer>
</article>
</section>
CSS
section {
border: 3px solid red;
height: 15em;
}
.table {
display: table;
height: 100%;
width: 100%;
}
.row {
display: table-row;
vertical-align: middle;
}
header { background-color: lightgrey; }
h2 {
display: table-cell;
padding: 0.5em 0;
vertical-align: middle;
}
.mainrow { background-color: lightyellow; }
.mainrow>div {
float: left;
height: 100%;
width: 50%;
}
.vmiddle {
font-size: 0;
text-align: center;
}
.vmiddle:before {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
}
img {
max-height: 100%;
max-width: 100%;
vertical-align: middle;
}
footer { background-color: lightblue; }
Thanks