Vertically center align container div - html

I'm creating a toolbar style navigation menu, where the content is added dynamically into the container.
I want to align the container div's center vertically to the center of the screen.
Here is my approach
http://cssdeck.com/labs/cmwvyjud
I know that this is not how it should be, but I'm unable to find alternative ways of doing this vertical alignment. Any help is appreciated.

If you know the height of the content that needs to be centered you can take half of that height and use something like margin-top:-(contentHeight/2)px
See example at : http://cssdeck.com/labs/atwispr6, here I know the content is 300px, so when I take half I have 150px. So margin-top:-150px
Edit
I have updated the example, to make it dynamic
$(function(){
var $container = $("#container")
$container.css("margin-top", "-" + ($container.height()/2) + "px");
});

If you're looking for a pure CSS solution, you have 2 options. If you're willing to add an additional container, you could use display: table. If the markup cannot change, then Flexbox is what you're looking for.
Display: table
http://cssdeck.com/labs/jdzltkla
body{
background-color:black;
}
#container{
position: fixed;
display: table;
top: 0;
bottom: 0;
left: 10px;
}
.foo {
display: table-cell;
vertical-align: middle;
}
#container .foo > * {
width: 50px;
height: 50px;
background-color:white;
margin-bottom: 10px;
}
<div id="container">
<div class="foo">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
</div>
This works in just about everything, including IE8+
Flexbox
http://codepen.io/cimmanon/pen/sKqkE
body {
background-color: black;
}
#container {
position: fixed;
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-box-orient: vertical;
-moz-box-orient: vertical;
-webkit-box-direction: normal;
-moz-box-direction: normal;
-webkit-flex-direction: column;
-ms-flex-direction: column;
flex-direction: column;
-webkit-box-pack: center;
-moz-box-pack: center;
-ms-flex-pack: center;
-webkit-justify-content: center;
justify-content: center;
top: 0;
bottom: 0;
left: 10px;
}
#container * {
width: 50px;
height: 50px;
background-color: white;
margin-bottom: 10px;
}
This works in Chrome, Opera, Firefox, IE10, and Safari.

the content is 300px, screen height is (for Ex 768px), so you must compare your content with screen height, not content itself...
so content is about 40% of screen height, and you can use top:25%; for top..
or in jQuery :
var container_height = parseInt($("#container").css("height"));
$("#container").css("top", (parseInt((screen.height-container_height)/2))+"px");

Related

Can I specify a location of a text box and move it down without top

I have been developing a website using bootstrap-4 and I am using a container class (responsive class on bootstrap) on this title inside of my . The only problem I have is that my navbar has half transparency and it is going to the top of the page and if you click on the image you see a white box and I would like the text to be restricted to that size in those borders lines.
Link to image
The reason I would not like to use top or bottom is that it messes up with my responsiveness on mobile devices etc.
I also believe that one of the reasons this is getting issues is because of my header.
Here is some code
<header>
<div class="container">
<h2>"We specialise in interiors and upholstery for automobiles</h2>
<p>supercars, classic cars, modern cars and motorbikes."</p>
</div
.
Here is my css for the header
height: 30vh;
position: static;
background-size: cover;
background-image: url("https://i.gyazo.com/7802614bc17ae16529be7d3628cd3552.png";
.
Not 100% sure on any of the display properties that the .container class would use in Bootstrap as I don't use it. But adding display: flex; to .container and using flexbox will do the trick.
CSS Code:
header {
/* change height accordingly */
height: 30vh;
position: static;
background-size: cover;
background-image:url("https://i.gyazo.com/7802614bc17ae16529be7d3628cd3552.png");
width: 100%;
}
.container {
height: 100%;
width: 100%;
display:flex;
align-items: center;
justify-content: flex-end;
flex-flow: column nowrap;
}
Codepen Link
https://codepen.io/oliverheward/pen/jOEvPby
Better browser support:
header {
/* change height accordingly */
height: 30vh;
position: static;
background-size: cover;
background-image:url("https://i.gyazo.com/7802614bc17ae16529be7d3628cd3552.png");
width: 100%;
}
.container {
height: 100%;
width: 100%;
display:-webkit-box;
display:-ms-flexbox;
display:flex;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
-webkit-box-pack: end;
-ms-flex-pack: end;
justify-content: flex-end;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-ms-flex-flow: column nowrap;
flex-flow: column nowrap;
}

Buttons outside of his flexbox

Hello, i've lost half of my hair in 2 days, sorry for my english :)
If you resize the window the buttons in #header go outside of his container.
Here is the code:
#root {
background: grey;
display: flex;
flex-direction: column;
align-items: center;
}
#header {
background: red;
align-self: stretch; /* fill in main-axis */
flex: 0 0 65px; /* fixed height */
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px;
}
<div id="root">
<div id="header">
<button>PUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUSH</button>
<button>BYE.</button>
</div>
<----- resize me ---->
</div>
I think the problem is #root.width is leaded by window/body.width ?
I have a solution with min-content:
#root { min-width: min-content; ... }, but i don't want use this value.
How configure #root correctly acting as a good and beautiful container for my layout ?
Here is a codepen for playing: https://codepen.io/papawa/pen/NLKbKR
Simply there is no problem, it's a logical result, unless you decide what do you expect to get in a smaller screens then that's what you will get and the reason is that you have a button with a long 'one word text' and the solution to fix this is just by wrapping the text itself and that's how you do it:
overflow-wrap: break-word;
or
word-wrap: break-word;
Here's the overall result:
#root {
display:flex;
flex-direction: column;
align-items:center;
background:grey;
overflow:hidden;
}
#header {
align-self:stretch;
flex: 0 0 65px;
display: flex;
justify-content: space-between;
align-items: center;
background: red;
padding: 10px;
}
.btn{
width: 50vw;
/* These are technically the same, but use both */
overflow-wrap: break-word;
word-wrap: break-word;
}
<div id="root">
<div id="header">
<button class="btn">PUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUSH</button>
<button>BYE.</button>
</div>
<---- resize me ---->
</div>
FOR FURTHER READING: Handling Long Words and URLs (Forcing Breaks, Hyphenation, Ellipsis, etc)

Automatic margin doesn't center elements in div

Lately I was creating a searchbox for my website, but I wanted it to be constantly centered in every y and x dimension.
I have div container searchbox:
.searchbox {
position: absolute;
text-align: center;
width: 100%;
left: 0%;
top: 55px;
height: 115px;
background-color: black;
}
Inside searchbox container, I made special mover container:
.mover {
margin: 0 auto;
width: 50%;
}
As you see width is 50% because I thought it would center it, but it didn't, and margin is automatic, which I don't think even works without 50% width.
Full code and Result.
I think my style is kinda messed up and there are useless things which may affect automatic margin.
What may the problem be? does margin: auto; doesn't work with current position of div? What do I need to change? If not, what's the problem?
I will be very thankful if you upload solution on my current fiddle.
UPDATED ANSWER
Here is correct code: https://jsfiddle.net/uda77168/7/
First...
1. Removed all absolute, top, left, right, bottom CSS properties.
Reason: Absolute positioning is generally a bad thing to do, because it gives sites an unresponsive layout.
2. I've also removed float CSS properties.
Reason: float is not bad, but it's unnecessary if you're using flexbox.
3. Set .search {width: 100%}
Reason: make the search bar bigger.
4. Removed width properties for #condition and #stattrack.
5. Made the margins more consistent.
6. Placed <label> before <select>.
Center Vertically
1. <body> is the flexbox that will center things vertically. In order for that to work, the width and height for <html> and <body> have to be defined.
html, body {
width:100%;
height:100%;
}
body {
display: -webkit-flex;
display: flex;
-webkit-flex-direction: row;
flex-direction: row;
-webkit-align-items: center;
align-items: center;
-webkit-justify-content: center;
justify-content: center;
}
2. Next, we need to define <body> as a flexbox and give it some flexbox properties:
body {
display: -webkit-flex;
display: flex;
-webkit-flex-direction: row;
flex-direction: row;
-webkit-align-items: center;
align-items: center;
-webkit-justify-content: center;
justify-content: center;
}
You can just copy-paste flexbox code like the one above from here.
Center Horizontally
1. Create a div around .statbox, .conbox, and .rarbox, and give it a width and make it a flexbox (again, the flexbox code is copied):
<div class="horizontal-flexbox"></div>
.horizontal-flexbox {
width: 100%;
display: flex;
display: -webkit-flex;
display: flex;
-webkit-flex-direction: row;
flex-direction: row;
-webkit-align-items: center;
align-items: center;
-webkit-justify-content: center;
justify-content: center;
}
2. I've also set .statbox, .conbox, and .rarbox each to be 33.3% width long. Added together, that's 99.9% – just under 100%.
.horizontal-flexbox > div {
width: 33.3%;
margin-top: 10px;
}
3. I've also included some other stuff, but that's not important. Make sure you learn flexbox, it's real useful!
Your input.search class has a specified width in px which is larger than the container.
.search {
width: 100%;/*changed this line*/
height: 35px;
margin-top: 20px;
margin-left: 0 auto;
margin-right: 0 auto;
border: solid 1px black;
border-radius: 7px;
}
However using percentages can lead to unpredictable layouts when viewed on different screen resolutions.
Use this:
.searchbox {
display:flex;
display:-webkit-flex;
justify-content:center;
-webkit-justify-content:center;
align-items:center;
-webkit-align-items:center;
}
And
.mover{width:50%;}

Element with max-height based on the height of the viewport. Image within should scale and keep ratio

Looking for a way to make sure that the height of an article element is never greater than 65% of the viewport height. Also there's an image nested inside the element that must be contained to the height of it's parent and be able to scaledown to the max-height and keep the it's ratio (yes, the image should be fully visible, no cropping).
It's also important that img and the overlaying div .actions have the same width at all times.
Is this possible with css only?
This is just a test case, there's other elements like this, the markup is the same, but the ratio of the element within each element is unique.
Demo
http://jsfiddle.net/SpPDp/show/
Code
http://jsfiddle.net/SpPDp/
Source below
<article>
<div class="inner">
<div class="overlay">
<div class="actions">
<div class="text">Text</div>
<div class="yep">Yep</div>
<div class="heretoo">Here too</div>
</div>
</div>
<div class="content">
<img src="http://lorempixel.com/output/abstract-h-g-600-900-4.jpg">
</div>
</div>
</article>
article {
float: left;
width: 40%;
}
article .inner {
position: relative;
}
article .overlay {
position: absolute;
background: #000;
opacity: 0.7;
top: 0; right: 0; bottom: 0; left: 0;
height: 100%;
overflow: hidden;
}
article .actions {
text-align: center;
position: absolute;
bottom: 1%;
width: 100%;
color: #fff;
background: red;
}
.text {
float: left;
}
.yep {
display: inline-block;
}
.heretoo {
float: right;
}
article img {
max-width: 100%;
}
First, let me say that the ability to change even the tiniest part of your html would make it so much easy to accomplish the result.
For example, setting the image as background, and accepting the fact that it should be cropped, would make the task a no-brainer.
But you make it clear that:
Articles must be max 65% of viewport h;
The meta bar over it should always have the same width as the image;
Images must not be cropped nor distorted.
If your question is formulated correctly, which I really hope it is, since we are all trying to solve it, you imply that articles widths won't always be equal.
The solution is to let the image figure out what is best to do. First, we set it's max-size in vw and vh, then we clear the way of unwanted positionings and sizes, to allow the size of the image to go up to the container, then back down to the meta bar in the overlay. Also, notice the use of flex boxes.
Here's the CSS (I didn't touch the HTML)
article {
float: left;
overflow: hidden;
vertical-align: top;
}
.inner {
/* older browsers. you should add the other prefixes too. there are polyfills to have broader support, check link later in the answer */
display: -webkit-box;
display: -moz-box;
display: -ms-box;
display: box;
-webkit-box-orient: vertical;
-moz-box-orient: vertical;
-ms-box-orient: vertical;
box-orient: vertical;
-webkit-box-direction: reverse;
-moz-box-direction: reverse;
-ms-box-direction: reverse;
box-direction: reverse;
/* newer */
display: -webkit-flex;
display: -moz-flex;
display: -ms-flex;
display: flex;
-moz-flex-direction: column-reverse;
-ms-flex-direction: column-reverse;
-webkit-flex-direction: column-reverse;
flex-direction: column-reverse; /* the meta box is added
after the image container, and the items are arranged in column */
}
.overlay {
z-index: 2;
margin: 0 0 -25px; /* magic: this cuts the container by 25px (height of meta bar) */
}
.actions {
color: #fff;
background: rgba(200,0,0,0.8);
/* older */
display: -webkit-box;
display: -moz-box;
display: -ms-box;
display: box;
-webkit-box-pack: justify; /* distribute the labels */
-moz-box-pack: justify;
-ms-box-pack: justify;
box-pack: justify;
-webkit-box-direction: normal; /* reset order (it used to be inherited) */
-moz-box-direction: normal;
-ms-box-direction: normal;
box-direction: normal;
/* newer */
display: -webkit-flex;
display: -moz-flex;
display: -ms-flex;
display: flex;
-webkit-justify-content: space-between; /* distribute the labels */
-moz-justify-content: space-between;
-ms-justify-content: space-between;
justify-content: space-between;
/* move up */
-webkit-transform: translate(0,-25px);
-moz-transform: translate(0,-25px);
-ms-transform: translate(0,-25px);
transform: translate(0,-25px);
box-shadow: 0 -1000px 0 1030px rgba(0,0,0,.6); /* since it
can't actually know the size, the overlay is accomplished with
a shadow. as long as it is not blurred, it won't impact
performances much */
font-size: 1.4vw; /* the only hard limit now is the width
of the text */
}
.content {
z-index: -1;
position: relative;
/* commenting those 2 last properties has two effects in webkit. 1) it avoids the images to stretch. 2) when you resize your window, the images won't adapt. only on load/refresh. this should be ok though, as window resizing is not really what responsive is most useful for
--- old code ---
display: -webkit-box;
display: -ms-box;
display: -moz-box;
display: box;
--- new code ---
display: -webkit-flex;
display: -moz-flex;
display: -ms-flex;
display: flex; */
}
.content img {
max-width: 40vw; /* and here you finally set the sizes.
note that you can set a min-width too, if you want, but
if you do there will be some image ratio that will force
them to stretch */
max-height: 65vh;
}
The code: http://jsfiddle.net/frapporti/NssKa/
The result, with many images of different ratios: http://jsfiddle.net/frapporti/NssKa/embedded/result/
Off course, this is just theory and is a valid answer to your question.
If you want to use this in production you should add some good polyfill for the older browsers, perhaps you could add this polyfill. As you can see I have already added the older box model properties as fallback.
Should be, there's no reason why not. Percentage heights are very useful.
You need to add this to the image tag:
<img src="whatever.jpg" width="95%">
It can be any value, depending on how wide you want it. As long as you make sure not to specify a height, it will keep scale.
Your CSS should change to be more like this:
article {
height:65%;
max-height:65%;
/* plus whatever else you want*/
}
As long as the parent is 100% window height, the article will be 65% window beight. Clearly if it's more, the article will be more.
The other solution is JavaScript. However, since you ask for CSS I'll omit that.
CSS:
article{
height:65%;
max-height:65%;
overflow:scroll; /* or hidden */
}
article .content > img{
max-height:100%;
width:auto;
}
JS Control (Because relative elements may not have a percentage height)
$(function(){
var $article = $('article');
var $height = $(window).height() * 0.65;
$article.css({'height':$height+'px', 'max-height':$height+'px'});
});
You should first set the 'inner' to 65% and then 'content' and image to 100% height. Like this,
.inner{
height:65%;
}
.content{
height:100%;
}
.content img{
height:100%;
}
I wrote a solution using vanilla Javascript. Why have jQuery as a dependency unless you need to? (rhetorical).
var viewport = document.documentElement.clientHeight;
var el = document.getElementsByTagName("article");
Array.prototype.forEach.call(el, function(el) {
el.style.maxHeight = Math.floor((viewport/100)*65)+"px";
});
http://jsfiddle.net/PgtAY/

Make child div stretch across width of page

Suppose I have the following HTML code:
<div id="container" style="width: 960px">
<div id="help_panel" style="width: 100%; margin: 0 auto;">
Content goes here.
</div>
</div>
If I want the help_panel div to stretch across the width of the page/broswer, even if the width of the page/browser is wider than 960px (which is the width set for the container div), is it possible to do it with the above structure? I want to get the help panel div to expand beyond the width limits set in the container div if the page is wider than 960px.
I am curious if this can be accomplished with the above structure. Or will I have to put the help_panel div outside of the container div to make it work?
Thanks!
Yes it can be done. You need to use
position:absolute;
left:0;
right:0;
Check working example at http://jsfiddle.net/bJbgJ/3/
With current browsers (this question is dating a bit now), you can use the much simpler vw (viewport width) unit:
#help_panel {
margin-left: calc(50% - 50vw);
width: 100vw;
}
(usage data: http://caniuse.com/#feat=viewport-units)
From my tests, this should not break your flow while being easy to use.
Yes you can, set the position: relative for the container and position: absolute for the help_panel
you can pull it out of the flow by setting position:absolute on it, but you'll have different display issues to deal with. Or you can explicitly set the width to > 960.
You could do it with jQuery...
$("#help_panel").width($(window).width());
Otherwise, as far as css goes, I'm fairly sure you would have to sit the help_panel div on the outside of container using position:absolute styling: http://css-tricks.com/forums/discussion/2318/give-child-div-width%3A100-of-page-width-inside-parent./p1
You could take it out of the flow with position:absolute. But the helper_panel will oberlap with other stuff. (I added orders, to see the divs)
<div id="container" style="width: 960px; border:1px solid #f00;">
Text before<br>
<div id="help_panel" style="width: 100%; position:absolute; margin: 0 auto; border:1px solid #0f0;">
Content goes here.
</div>
This is behind the help_penal
</div>
Just set the width to 100vw like this:
<div id="container" style="width: 100vw">
<div id="help_panel" style="width: 100%; margin: 0 auto;">
Content goes here.
</div>
</div>
You can use 100vw (viewport width). 100vw means 100% of the viewport. vw is supported by all major browsers, including IE9+.
<div id="container" style="width: 960px">
<div id="help_panel" style="width: 100vw; margin: 0 auto;">
Content goes here.
</div>
</div>
Since position: absolute; and viewport width were no options in my special case, there is another quick solution to solve the problem. The only condition is, that overflow in x-direction is not necessary for your website.
You can define negative margins for your element:
#help_panel {
margin-left: -9999px;
margin-right: -9999px;
}
But since we get overflow doing this, we have to avoid overflow in x-direction globally e.g. for body:
body {
overflow-x: hidden;
}
You can set padding to choose the size of your content.
Note that this solution does not bring 100% width for content, but it is helpful in cases where you need e.g. a background color which has full width with a content still depending on container.
...............In HTML Format
<div id="a">Full Width</div>
...............In CSS Format
#a { background-color: green;width: 100%;height: 80px;border: 1px solid;margin: 0 auto;}
body { padding: 0;margin: 0}
Try this: it will use all of the screen width:
min-width: 100vw;
I know this post is old, in case someone stumbles upon it in 2019, this would work try it.
//html
<div id="container">
<div id="help_panel">
<div class="help_panel_extra_if_you_want"> //then if you want to add some height and width if you want, do this.
</div>
</div>
</div>
//css
#container{
left: 0px;
top: 0px;
right: 0px;
z-index: 100;
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-webkit-flex-direction: column;
-ms-flex-direction: column;
flex-direction: column;
-webkit-box-pack: start;
-webkit-justify-content: flex-start;
-ms-flex-pack: start;
justify-content: flex-start;
position: relative;
height:650px;
margin-top:55px;
margin-bottom:-20px;
}
#help_panel {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
padding-right: 24px;
padding-left: 18px;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-webkit-flex-direction: column;
-ms-flex-direction: column;
flex-direction: column;
-webkit-box-align: center;
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;
.help_panel_extra_if_you_want{
height:650px;
position: relative;
-webkit-box-orient: horizontal;
-webkit-box-direction: normal;
-webkit-flex-direction: row;
-ms-flex-direction: row;
flex-direction: row;
-webkit-box-pack: justify;
-webkit-justify-content: space-between;
-ms-flex-pack: justify;
justify-content: space-between;
align-items: center;
display: flex;
width: 95%;
max-width: 1200px;
}
SHOULD GIVE YOU SOMETHING LIKE THIS
You can do:
margin-left: -50%;
margin-right: -50%;