The task I encountered looks standard: I have a fixed height container and 3 div's inside it. I want the 2nd div to be stretched between the top and the bottom div's. When the contents of the 2nd div overflows - I would like to show the scroll bars.
I know how to accomplish this task using the absolute positioning. A question is: can I do it using the table on divs?
An additional requirement: if possible, I would like to avoid setting header's height as fixed.
I have tried to code it in my fiddle, but, as you see, I failed.
CSS:
.container {
height: 500px;
background-color: gainsboro;
}
.table {
display: table;
height: 100%;
}
.table > div {
display: table-row;
}
.table > div > div {
display: table-cell;
vertical-align: middle;
border: 1px solid black;
overflow: scroll;
}
.center > div {
height: 100%;
}
.content {
height: 700px;
}
HTML:
<div class="container">
<div class="table">
<div>
<div>XXX</div>
</div>
<div class="center">
<div>
<div class="content"></div>
</div>
</div>
<div>
<div>YYY</div>
</div>
</div>
</div>
Here is one possible solution:
<div class="container">
<div class="header">
<div>Top Header Block</div>
</div>
<div class="center">
<div class="content">
<p>Lorem ipsum ...</p>
<p>Lorem ipsum ...</p>
</div><!-- .content -->
</div><!-- .center -->
<div class="footer">
<div>Bottom Footer Block</div>
</div>
</div>
and the CSS:
.container {
height: 200px;
}
.header, .footer {
background-color: gainsboro;
}
.center {
height: inherit;
}
.content {
background-color: #F0F0F0;
height: inherit;
overflow: auto;
}
Since you are fixing the height of the container, you inherit the height both in the .center and the .content <div>'s.
If you tweak the container height, the center div expands but the header and footer div's stay the same height.
Use overflow on the content div to allow for scrolling.
Fiddle Reference: http://jsfiddle.net/audetwebdesign/nae5z/
Your way was right, just make a few changes (See this Fiddle):
html, body, .container, .table {
margin: 0;
height: 100%;
}
#header,#footer {
height: 1px;
}
This should work because tables cells get increased in height if the content needs it.
Just a hint: You may improve the whole thing, for example I would use HTML 5 and the <header/> and <footer/> elements. But that was not part of your question. Anyway, here is another update to your fiddle:
<div>
<header>
<div>XXX</div>
</header>
<main>
<div>
<div class="content"></div>
</div>
</main>
<footer>
<div>YYY</div>
</footer>
</div>
With CSS:
html, body, body > div {
margin: 0;
height: 100%;
}
body {
background-color: gainsboro;
}
body > div {
display: table;
width: 100%;
}
body > div > * {
display: table-row;
}
body > div > * > div {
display: table-cell;
vertical-align: middle;
border: 1px solid black;
}
header, footer {
height: 1px;
}
main is very new to HTML 5, just if you're wondering.
Related
How to align item from header (search bar), to have the same margin like element container under header which has max-width:1296px and margin:auto?
My header has fixed position.
This is how it's look
This is how it's need to have the same margin like cateogries-container
<div className="container">
<div className="header-container">
<div className="header-search-container">
<SearchBar /> --> has width:100%
</div>
</div>
<div className="categories-container">
...
</div>
</div>
header-container {
width: 100%;
height: 80px;
}
.header-search-container {
width: 426px;
}
.categories-container {
max-width: 1296px;
margin: auto;
padding-bottom: 69px;
padding-right: 48px;
padding-left: 48px;
}
I don't quite understand the overall organization of your layout from the code you presented, but I can assume that the structure was originally built incorrectly.
You just have to wrap your header (search-bar) in the same container as your categories with a class change for more correct semantics.
Usually pages have a container that is centered and has a certain width, this is used to align all the content.
A simple example:
body {
font-family: sans-serif;
}
.container {
padding: 5px 0;
width: 500px;
margin: 0 auto;
}
nav {
padding: 5px 0;
background: teal;
color: #FFFFFF;
}
<body>
<main>
<nav>
<div class="container">
navbar content
</div>
</nav>
<section>
<div class="container">
section content
</div>
</section>
</main>
</body>
I'm trying to create this layout in css:
Several blogs (e.g. http://css-tricks.com/how-to-create-a-horizontally-scrolling-site/) mentioned that the best way to do this is using the table layout. And it works actually, but the problem is i cant scroll the individual vertical panels, they always occupy their actual content. I tried to set a height explicitly for each but it's always ignored.
The CSS:
main {
width: 100%;
overflow: auto;
.cycle-progress{
display: table;
table-layout: fixed;
width: 100%;
}
.cycle-progress > div{
display: table-cell;
width: 600px;
height: 1000px;
h1{
background-color: #ffde17;
text-transform: uppercase;
}
}
.cycle-progress > div:nth-child(even){
background-color: white;
}
}
The HTML
<main>
<div class="cycle-progress">
<!--ng-init="timeline = $('.timeline').timeline()" -->
<div ng-repeat="pc in production_cycles" ng-controller="ProductionCycleCtrl" data-pcid="{{pc.id}}">
<h1 class="text-center m-t-none">{{pc.name}}</h1>
<div class="row">
<div class="col-lg-4 padder-lg">
<img src="/src/img/pc-init.svg" style="width: 200px" />
</div>
<div class="col-lg-8">
<div timeline class="timeline"></div>
</div>
</div>
<!--<div style="height: 1000px; background-color: red"></div>-->
</div>
</div>
</main>
A flexbox sized to occupy as much horizontal space as necessary should work:
body {
display: flex;
max-height: 100vw;
flex-direction: column;
}
.container {
display: flex;
width: fit-content;
}
.column {
max-height: 100%
overflow-y: auto;
}
For browsers that don't support flexboxes styling the columns as inline-blocks setting their parents to no-wrap might work too.
If you have a choice of setting explicit height(As I see in your post), then the solution is simple.
Wrap everything inside the table cell with a div and set height and overflow.
The HTML:
<div ng-repeat="pc in production_cycles" ng-controller="ProductionCycleCtrl" data-pcid="{{pc.id}}">
<div class="wrapper-element">
<h1 class="text-center m-t-none">{{pc.name}}</h1>
<div class="row">
<!--more content-->
</div>
</div>
</div>
The CSS:
.cycle-progress > div {
display: table-cell;
width: 600px;
> .wrapper-element {
height: 1000px;
overflow: auto;
}
}
This question already has answers here:
How to center an element horizontally and vertically
(27 answers)
Closed 8 years ago.
How to center the text inside that box?
Here's how I want elements sorted out:
In a smaller screen, elements stack on top of each other, and the .text-box div, that contains the text I want to center, has a fixed height. For larger widths, the .text-box div should have a height igual to larger image's height minus shorter image's height
See the Fiddle here
HTML
<div id="wrapper">
<div class="box">
<img class="img-large" src="http://placekitten.com/900/800" alt=""/>
</div>
<div class="box">
<img src="http://placekitten.com/900/400" alt=""/>
</div>
<div class="box-text">
<div class="vcenter-outer">
<div class="vcenter-inner">
<p>center this text vertically</p>
</div>
</div>
</div>
</div>
CSS
#wrapper {
background: #ffeaea;
height: 100vh;
}
img {
width: 100%;
display: block;
}
.img-large {
height: 100vh;
width: auto;
}
.box {
position: relative;
width: 100%;
overflow: hidden;
}
.box-text {
text-align: center;
background: yellow;
height: 100%;
}
#media only screen and (min-width: 768px) {
.box {
float: left;
width: 50%;
}
}
.vcenter-outer {
background: yellow;
display: table;
}
.vcenter-inner {
background: lightblue;
display: table-cell;
vertical-align: middle;
}
Why does the .text-box div span through the whole wrapper?
Basicly You could use display:flex and float together
.trio {
height:100vh;
width:100vh;
}
.trio>div {
float:left;
display:flex;
align-items:center;
justify-content:center;
width:50vh;
height:50vh;
box-shadow:inset 0 0 0 2px;
}
.trio .first {
height:100vh;
}
<div class="trio">
<div class="first">
<p>Center</p>
</div>
<div class="next">
<p>Center</p>
</div>
<div class="next">
<p>Center</p>
</div>
</div>
display:table works too with an extra level of div inbricated as you did , same idea: float + vh
codepen to play with
I have a responsive website with max-width set to 1000px, but I need to fit background picture that will overlap one of the divs and also place full page-width bottom borders to other divs.
The code i have is like this:
.container {
width: 100%;
max-width: 1000px;
}
.logotest {
background-color: #03b9e5;
height: 50px;
}
.navtest {
background-color: #e4ed00;
height: 25px;
}
.socialtest {
background-color: #ab801a;
height: 25px;
}
.main {
height: 750px;
background: url(background.jpg) no-repeat top center;
margin: auto;
}
.line {
border-bottom: 1px solid black;
}
.container:after {
clear: both;
content: ".";
display: block;
height: 0;
visibility: hidden;
}
<body>
<div class="container" id="first">
<div class="logotest">
</div>
<div class="socialtest">
</div>
<div class="navtest">
</div>
</div>
<div class="line"></div>
<div class="main line" id="second">
</div><div class="container">
<div id="third">
</div>
</div>
</body>
I get the first div with correct width and bottom border going across the full page width, second div has got the background picture showing, but the max-width of 1000px does no longer apply. The bottom border is shown correctly (dividing second and third div) and the third div has got the correct max-width applied again.
What am I doing wrong/not doing to get the max-width for the second div?
YOUR SOLUTION
If the browser support of background-size property is good enough for you, you can use background-size: cover;. Check here or here to see browser support.
Here is the code snippet to show how it works. Be sure to position your background-image to center center if you want it to always be centered.
.container {
width: 100%;
max-width: 300px;
margin: 0 auto;
}
.line {
border-bottom: 1px solid black;
}
.logotest {
background-color: #03b9e5;
height: 50px;
}
.navtest {
background-color: #e4ed00;
height: 25px;
}
.socialtest {
background-color: #ab801a;
height: 25px;
}
.main {
height: 250px;
background: url(http://lorempixel.com/250/250) no-repeat center center;
background-size: cover; /* This does the magic */
}
.container:after {
clear: both;
content: ".";
display: block;
height: 0;
visibility: hidden;
}
<body>
<div class="container" id="first">
<div class="logotest">
</div>
<div class="socialtest">
</div>
<div class="navtest">
</div>
</div>
<div class="line"></div>
<div class="main" id="second">
<div class="container">Put your content in here.</div>
</div>
<div class="line"></div>
<div class="container">
<div id="third">
</div>
</div>
<div class="line"></div>
</body>
LAST (BUT NOT LEAST)
You might want to check this great article about the state of responsive images in web design, that will help you if you are going into responsive web design: Responsive images done right.
I have code like this:
<div class="more-options clearfix">
<div class="boxes">
<img alt="#" src="images/icons/onl-marketing.png">
<p>Online Marketing News</p>
</div>
<!-- end .boxes -->
<div class="boxes">
<img alt="#" src="images/icons/str-success.png">
<p>Stories Success</p>
</div>
<!-- end .boxes -->
I want to vertical align p tags middle in div, images float left and div.box:last-child has margin-left: 1%; I try this css but it doesn't work:
div.more-options {
margin: 15px 0;
div:first-child {
.fleft;
}
div:last-child {
margin-left: 1%;
}
div.boxes {
padding: 20px;
width: 49.5%;
background: #ecf0f1;
min-height: 25px;
display: table-cell;
vertical-align: middle;
img {
.fleft;
margin-right: 25px;
.img-responsive;
max-width: 25%!important;
}
p {
.fontfc(26px, #666666);
}
}
/* end div.boxes */
}
/* end div.more-options */
How can I vertical align text and keep margin 1% div.boxes:last-child?
here you have some fixes:
p and img with display:inline
it's working the vertical-align:middle;
making the parent display: table
EDIT: you are using padding and width: you will need an extra div for this, and with tables you need an extra cell for "separation", check this out:
updated link with images vertical aligned too:
the fiddle
html:
<div class="more-options clearfix">
<div class="boxes">
<div class="in">
<img alt="#" src="http://icons.iconarchive.com/icons/artbees/paradise-fruits/256/Banana-icon.png">
<p>Online Marketing News</p>
</div>
</div>
<!-- end .boxes -->
<div class="separation"></div>
<div class="boxes">
<div class="in">
<img alt="#" src="http://icons.iconarchive.com/icons/artbees/paradise-fruits/256/Banana-icon.png">
<p>Stories Success</p>
</div>
</div>
<!-- end .boxes -->
css:
div.more-options {
margin: 15px 0;
div:first-child {
}
div.separation {
width: 1%;
display: table-cell;
}
div.in{
padding:20px;
}
div.boxes {
width: 49.5%;
background: #ecf0f1;
min-height: 25px;
display: table-cell;
vertical-align: middle;
img {
display: inline-block;
margin-right: 25px;
.img-responsive;
max-width: 25%!important;
vertical-align: middle;
}
p {
.fontfc(26px, #666666);
display: inline-block;
}
}
/* end div.boxes */
}
/* end div.more-options */
the result screenshot:
p elements are paragraphs - so they have a default property of display:block. Either use another element, for example a span.
Alternatively you can change the display property of the p dom element, for example to display:inline.
Update - the property of vertical align 'vertical-align:middle' should be applied to the images.
Also - your nesting of styles seems wrong, see a working example below.
Example (updated):
http://jsfiddle.net/YwV54/2/
Your Structure is ok.
In order to use vertical-align:middle you need to use table structure:
#wrapper div#main-content div.more-options div.boxes{display:table;}
#wrapper div#main-content div.more-options div.boxes p{verticle-align:middle;}
#wrapper div#main-content div.more-options div.boxes img{max-width:25% !important; remove this}
Set padding margin according to content.