I am using display: table-cell so that I can easily do height:100% the child elements inside to get 100% height.
The problem is that when I have multiple elements inside or padding or margins, the parent does not stretch and instead the contents poke through. Putting a overflow: hidden will not work as I need the children to fit inside the parent properly.
Mark up:
<div class="container">
<div class="subcontainer">
<h4>title</h4>
<div class="menu">
<p>test</p>
<p>test</p>
</div>
<div class="content">
<p>content</p>
<p>content</p>
</div>
</div>
</div>
CSS:
p{
padding:0;
margin:0;
}
html{
height: 100%;
overflow-y: scroll;
background: white;
}
body{
height: 100%;
margin: 0;
padding: 0;
background: red;
}
.container{
height: 100%;
display: table;
margin: 0;
padding: 0;
}
.subcontainer{
display: table-cell !important;
height: 100%;
}
h4{
padding: 0;
padding-bottom: 5px;
background: blue;
margin: 0;
border-bottom: 1px solid black;
margin-bottom: 5px;
}
.menu, .content{
background: green;
height: 100%;
display: inline-block;
float:left;
width: 200px;
padding: 20px;
}
.content{
background: purple;
width: 400px;
}
Fiddle: http://jsfiddle.net/ZQFrM/
The poking through is due to increased height inside caused by the following:
The existence of the h4 on top of the other elements.
Padding and margin on h4.
Padding on menu and content.
What can be done to resolve this problem? I definitely want to use the display: table-cell as I need the children to be able to stretch vertically to fill the parent.
you have to remove the float:left and display:inline-block from '.menu, .content' also you have to apply a display:table-cell. So both div can be align horizontally.
Here is the Code http://jsfiddle.net/kheema/ZBLY7/7/
Here is the CSS..
p{
padding:0;
padding:0;
}
body{
margin: 0;
padding: 0;
background: red;
}
.container{
height: 100%;
display: table;
margin: 0;
padding: 0;
}
.subcontainer{
display: table-cell !important;
height: 100%;
}
h4{
padding: 0;
padding-bottom: 5px;
}
.menu, .content{
background: green;
height: 100%;
/*display: inline-block;
float:left;*/
display: table-cell;
width: 200px;
}
.content{
background: purple;
width: 400px;
}
Related
I'm trying to figure out how to align 2 floating divs vertically. Each of the divs has a flexible height.
Here is what i have so far: http://jsfiddle.net/VLRpc/1/
<div class="section">
<div class="container">
<div class="wrap">
<div class="column left">
<h1>Software</h1>
<p>I use many software applications to achieve the best results.</p>
</div>
<div class="column right"></div>
</div><!-- end wrap -->
</div><!-- end container -->
</div><!-- end section -->
.container {
width: 100%;
height: 100%;
display: table;
position: absolute;
top: 0;
left: 0;
margin: 0;
padding: 0;
}
.wrap {
vertical-align: middle;
display: table-cell;
padding: 0;
margin: 0;
border: 1px solid red;
}
/* Columns */
.column {
width: 45%;
height: auto;
float:left;
text-align: center;
border: 1px solid black;
}
.right {
margin-left:0;
height: 250px
}
.left {
margin-left:5%;
}
However the div on the left gets pushed to the top of the larger div which is on the right. I need both of these divs to be vertically centered.
Any ideas?
Here's how you could make it flexible (no fixed heights, just keep those .column containers vertically centered regardless of their content): set .column to display:inline-block and to vertical-align: middle inside your table-cell .wrap div.
.column {
display: inline-block;
width: 45%;
height: auto;
text-align: center;
border: 1px solid black;
vertical-align: middle;
}
http://jsfiddle.net/VLRpc/2/
Here you go, just change the margin-top value in the left column, and all should be good. If you want more content in that column, add some padding-top too. Here is the changed css for it:
.container {
width: 100%;
height: 100%;
display: table;
position: absolute;
top: 0;
left: 0;
margin: 0;
padding: 0;
}
.wrap {
vertical-align: middle;
display: table-cell;
padding: 0;
margin: 0;
border: 1px solid red;
}
/* Columns */
.column {
width: 45%;
height: auto;
text-align: center;
border: 1px solid black;
margin:0;
}
.right {
margin-left:0;
height: 250px;
float: right;
margin-right: 1em;
}
.left {
float: left;
margin-top: 50px;
margin-left: 1.4em;
}
I can't find an answer that is not on a fixed image size so I'm going to ask it.
I have a div with an image in it and that picture could be any size. I need it to auto scale AND auto align. I can scale it fine but vertically aligning it is a bit of a challenge. I need it to be center vertically aligned.
HTML + CSS
<div id="myDiv">
<img src="./img/example.png"></img>
</div>
#myDiv {
position: absolute;
height: 100%;
width: 100%;
top: 0%;
left: 0%;
text-align: center;
}
#myDiv img {
height: auto;
width: auto;
max-height: 70%;
max-width: 70%
}
This example forces both horizontal and vertical alignment of an image inside a box; in this specific case, constrained to 130x130px. Change the width and height defined as 130px in 2 separate places each in the css to change the constrained size.
[edit: added simplified example showing minimum required setup]
Simplified example:
html:
<div class="pic">
<img src="/path/to/pic.jpg"/>
</div>
css:
.pic {
display: inline-block;
width: 130px;
height: 130px;
outline: solid 1px #cccce3;
font-size: 0;
text-align: center;
}
.pic:before {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
}
.pic img {
max-width: 130px;
max-height: 130px;
display: inline-block;
vertical-align: middle;
}
Complete example:
Original codepen showing more complex example: http://codepen.io/anon/pen/culvD .
Here's the html:
<ul class="pics">
<li>
<div class="pic">
<img src="/path/to/pic1.jpg"/>
</div>
</li>
<li>
<div class="pic">
<img src="/path/to/pic2.jpg"/>
</div>
</li>
</ul>
and here's the css:
ul.pics {
margin: 0;
padding: 0;
list-style-type: none;
text-align: center;
}
ul.pics li {
display: inline-block;
width: 130px;
margin: 4px;
padding: 6px;
background-color: #e6e6ec;
outline: solid 1px #cccce3;
}
ul.pics li .pic {
height: 130px;
font-size: 0;
text-align: center;
}
ul.pics li .pic:before {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
}
ul.pics li img {
max-width: 130px;
max-height: 130px;
display: inline-block;
vertical-align: middle;
}
to center align an element to the center asign it a width, then margin-left and right to auto.
The goal is to center this layout on the screen. I have a min-width of 600px and max-width of 1200px, so there is potentially a lot of white space around the layout.
I've been trying lots of different methods, but am confused about the display:table attribute and how it behaves in this scenario.
Here is the jsFiddle and code: http://jsfiddle.net/7M9rg/6/
Thanks so much for any advice on this!
HTML:
<div id="wrapper">
<div id="header">
</div>
<div id="main">
<div id="side">
<div id="side-stuff">
<ul>
<li>Home</li>
</ul>
</div>
</div>
<div id="content">
<p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has</p>
</div>
</div>
<div id="footer">© 2013 </div>
</div>
CSS:
/*css reset*/
html,body {position:relative;margin:0;padding:0;min-height:100%;width:100%;
height:100%;}
div,p,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,form,fieldset,input,
textarea,p,blockquote,th,td, figure {margin:0;padding:0;}
ol,ul {list-style:none;}
li {list-style-type: none;}
* { -moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing:
border-box; }
html, body {
font-family: Helvetica;
height: 100%; /*important for equal height columns*/
min-width: 650px;
}
#wrapper{
height: 100%; /*important for equal height columns*/
padding-bottom:130px; /*This must equal the height of your header*/}
#header{
height: 130px; /*This must equal padding bottom of wrap*/
display:block;
padding: 5px;
color: #fff;
border: thin solid #ebebeb;
border-radius: 10px;
margin: 10px;
background-image: url(Images/gradient.png);
background-repeat: repeat-x;
width: 99%;}
#main {
position: relative;
height: 100%; /*important for equal height columns*/
width: 99%;
overflow:auto;
display: table; /* This is needed fo children elements using display table cell*/
table-layout: fixed;
padding-bottom: 50px; /*This needs to match footer height*/
overflow: auto;
margin-left: 10px;}
#side{
background-color: #fff;
width: 150px;
margin: 10px;
vertical-align: top;
padding-top: 20px;
padding-right: 10px;
display: table-cell;
border-radius: 10px;
border: thin solid #CCC;}
#side-stuff{
display: block;
padding-left: 10px;}
#content{
background-color: #fff;
padding: 10px;
display: table-cell; /*To make sibling columns equal in height*/
margin-bottom:10px;
border-radius: 10px;
border: thin solid #CCC;}
#content-stuff{
width: auto;
height: auto;}
#footer{
position: relative;
height: 40px;
margin-top: -40px; /* margin-top is negative value of height */
margin-left: 10px;
clear: both; /* Use if floating elements */
color: #999;
width: 99%;
border: thin solid #ebebeb;
border-radius: 10px;
background-image: url(Images/footer_gradient.png);
background-repeat: repeat-x;
background-position: bottom;}
Add this to your #wrapper CSS:
max-width: 1200px;
margin: 0 auto;
http://jsfiddle.net/tshz6/
you need to specify width for your <body> or #wrapper element. min-width only specifies the minimum width it can be shrunk to, and width and max-width is set to auto, which is all available width (in this case the screen width).
Try updating your html, body rule to following
html, body {
font-family: Helvetica;
height: 100%;
margin: 0 auto;
min-width: 650px;
max-width: 1200px;
}
I have a grid of fixed size divs, and this must remain CSS-only.
Inside each div, I have a random size image inside.
I need the image to scale to the div size while keeping the aspect ratio, and also be centered both horizontally and vertically within the div.
#holder {
width: 800px;
margin: 0 auto;
}
.tile {
display: inline-block;
padding: 10px 15px;
border: 1px solid black;
text-align: center;
/*vertical-align: middle;*/
width: 300px;
height: 300px;
}
.tile img {
/*vertical-align: middle;*/
outline: 1px dashed red;
max-height:100%;
max-width:100%;
}
Can't center vertically. Everything else seems to work fine.
UPDATE: Also this doesn't work when the img is smaller than the div.
width: 100%; and height: auto; (or height: auto !important; in case there is a height attribute on the img element) usually does the trick.
Sorry misread the question, this fixes the aspect ratio but not the centering. This tread might give some valuable ideas.
The problem here is that the block height is unknown.
Here's a solution that work on firefox/chrome:
#holder {
width: 800px;
margin: 0 auto;
}
.tile {
padding: 10px 15px;
border: 1px solid black;
width: 300px;
height: 300px;
display: table;
}
.tile-layout {
width: 100%;
height: 100%;
display: table-cell;
vertical-align: middle;
}
.tile img {
outline: 1px dashed red;
width:100%;
}
Using this html template:
<div id="holder">
<div class="tile">
<div class="tile-layout">
<img src="{{ img }}"/>
</div>
</div>
</div>
#holder {
display:table;
width: 800px;
margin: 0 auto;
height:100%;
possition:relative;
}
.tile {
display: table-cell;
border: 1px solid black;
text-align: center;
vertical-align: middle;
}
.tileInner{
display:block;
width: 300px;
height: 300px;
padding: 10px 15px;
}
.tile img {
display:table-cell;
vertical-align: middle;
outline: 1px dashed red;
max-height:100%;
max-width:100%;
}
html
<div id="holder">
<div class="tile">
<div class="tileInner">
<img src="imageUrl"/>
</div>
</div>
</div>
I have css code to do layout. i have basic header panel, footer, left panel, and center panel. I want to have left panel and center panel automatically stretch to bottom(blue and gray part all the way to black footer). is there any way to do that?
following are my codes.
thank you,
body {
text-align: center;
}
.wrapper {
position: relative;
width: 960px;
font-size: 0.9em;
margin: 0 auto -20px;
text-align: left;
}
.header {
height: 125px;
background-color:purple;
}
.footer {
position: relative;
width: 960px;
margin: 0 auto;
background-color:black;
}
.footer a {
color: #fff;
text-decoration: underline;
border: 0;
}
.footer p {
position: absolute;
left: 0;
bottom: 4px;
width: 960px;
padding: 0;
color: #fff;
font: 0.8em arial,sans-serif;
text-align: center;
}
html, body {
height: 100%;
}
.wrapper {
min-height: 100%;
height: auto !important;
height: 100%;
margin: 0 auto -20px; /* the bottom margin is the negative value of the footer's height */
background-color:yellow;
}
.footer, .push {
height: 20px; /* .push must be the same height as .footer */
}
.leftPanel{
width:200px;
background-color:blue;
float:left;
height: 100%;
}
.centerPanel{
width:760px;
background-color:gray;
float:left;
height: 100%;
}
dl,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,form,body,html,div,p,blockquote,fieldset,legend,input,select,textarea
{ margin:0; padding:0 }
<div class="wrapper">
<div class="header">
<h1>header</h1>
</div>
<div class="leftPanel">
leftPanel
</div>
<div class="centerPanel">
center Panel
</div>
<div class="push"></div>
</div>
<div class="footer">
<p>footer</p>
</div>
</body>
This is a very work-aroundish solution, but here it goes: http://jsfiddle.net/Us5Cn/
The only way to get elements to stretch to a percentage portion of the height of the view port is to anchor them to the bottom.
See here for specifics.