Centered fixed div with width fit to content - html

I am trying to have a fixed div (position: fixed) in the center of the page. So far that works with this css:
#msg{
border: 1px solid black;
position: fixed;
z-index: 9999;
background-color: white;
bottom: 0px;
top: 0px;
left: 0px;
right: 0px;
margin: auto;
width: 100px;
height: 100px;
padding: 5px;
}
<div id="msg"> Hello </div>
The only thing that is not working, is trying to get the size of the div (width, if possible also height) to automatically match the size of the content in it.
So basically a normal div like this, but then fixed and centered:
#msg2{
border: 1px solid;
display: inline-block;
}
<div id="msg2"> hello </div>
I am looking for a non-javascrpit solution

You can use translate to perfect center without pre-knowing the width and/or height of the box.
The solution is to put 50% from top and left and then translate to the opposite -50% (X and Y axis):
#msg2{
border: 1px solid;
display: inline-block;
position: fixed;
top:50%;
left:50%;
-webkit-transform: translate(-50%, -50%); /* iOS needed */
transform: translate(-50%, -50%);
}
<div id="msg2"> hello </div>

For a very robust way to center your #msg, consider the following technique.
You just need two containers!
1. The outer container :
should have display: table;
2. The inner container :
should have display: table-cell;
should have vertical-align: middle;
should have text-align: center;
3. The #msg box itself :
should have display: inline-block;
should have eg. text-align: left; or text-align: right; if you don't want your text-alignment centered.
You can add any content you want to the content box without caring about its width or height!
Demo :
body {
margin : 0;
}
#outer-container {
position : fixed;
display: table;
width: 100%;
height: 100%;
background: #ccc;
}
#inner-container {
display: table-cell;
text-align: center;
vertical-align: middle;
}
#msg {
text-align: left;
display: inline-block;
background: #fff;
padding : 20px;
border : 1px solid #000;
}
<div id="outer-container">
<div id="inner-container">
<div id="msg">
Hello
</div>
</div>
</div>
See also this Fiddle!

Related

Having trouble centering an image in CSS

I am trying to horizontally center an image within a div. However, I haven't been able to. I've tried setting the vertical-align to middle and the margin to auto, 0 auto, and every variation I can think of. Nothing works. Here is the code for how it is currently set up:
img {
border: 0;
margin: 0 auto;
}
.intro img {
border-radius: 50%;
vertical-align: middle;
padding: 10px;
}
The image is in the intro div. Any advice you can give would be helpful.
If you want to center your image both horizontally & vertically, this should do the trick :
.intro {
display: table;
width: 500px; /* works with any width */
height: 150px; /* works with any height */
background: #ccc;
}
.imgcontainer {
display: table-cell;
vertical-align: middle;
text-align: center;
}
img {
background: #fff;
padding: 10px;
border-radius: 50%;
}
<div class="intro">
<div class="imgcontainer">
<img src="http://s.gravatar.com/avatar/bf4cc94221382810233575862875e687?r=x&s=50" />
</div>
</div>
Use position:relative in parent .intro and use the code shown below in img, it will work with any width and height
display:block;
margin:auto;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0
Snippet
.intro {
border: dashed red; /* demo */
display:inline-block; /* demo */
vertical-align:top; /* demo */
position: relative
}
.intro img {
border-radius: 50%;
vertical-align: middle;
display: block;
margin: auto;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0
}
.intro:first-of-type {
width: 200px;
height: 200px;
}
.intro:last-of-type {
width: 400px;
height: 300px;
}
<div class="intro">
<img src="//lorempixel.com/100/100" />
</div>
<div class="intro">
<img src="//lorempixel.com/100/100" />
</div>
The css style margin: 0 auto; should do the horizontal part of the trick.
For the vertical part you also need to take care of the parent.
Look at How to vertically align an image inside div for more info.
vertical-align works only in table cells. Try to use Flexbox. The element containing your image should have CSS properties:
display: flex;
align-items: center;

Absolute center horizontal and vertical a div with fluid width and height?

how to make absolute center horizontal and vertical a div with fluid width and height using css?
Thanks in advance for helping.
#div_parent{
background:#ccc;
position:relative;
}
.div_child{
background-color:#338BC7;
position: absolute;
left: 50%;
width: auto;
height: auto;
padding: 20px;
top:25%;
background: blue;
color: white;
text-align: center;
border:1px solid #ccc;
}
<div id="div_parent">
<div class="div_child">
<p>Centered In The Middle Of The Page.</p>
</div>
</div>
A couple of problems with your code:
You do not have a width and height specified on your html and body, without which any of descendent elements wouldn't have a reference to set their positions and/or dimensions in percent units.
You do not have dimensions (width/height) specified on your #div_parent, without which you cannot position its absolutely positioned child (which is relative to it) to the vertical center. Moreover, as you want to position your .div_child to the center of the page, why do you have a parent wrapped around it anyway.
Apart from fixing the above, a trick which is usually used to align elements both horizontally and vertically is to use transform: translate to shift it back by 50%.
Like this:
.div_child {
position: absolute; left: 50%; top: 50%;
transform: translate(-50%, -50%);
...
}
Fiddle: http://jsfiddle.net/abhitalks/Lnqvqnkn/
Snippet:
* { box-sizing: border-box; paddin:0; margin: 0; }
html, body { height: 100%; width: 100%; }
#div_parent{ height: 100%; width: 100%; background: #ccc; position: relative;}
.div_child {
background-color: #338BC7;
position: absolute; left: 50%; top: 50%;
transform: translate(-50%, -50%);
width: auto; height: auto;
padding: 20px; color: white; text-align: center; border: 1px solid #ccc;
}
<div id="div_parent">
<div class="div_child">
<p>Centered In The Middle Of The Page.</p>
</div>
</div>
When I need fluid width, I prefer using this method:
CSS
.background { display: table; width: 100%; height: 100%; position: absolute; left: 0; top: 0; }
.background > div { display: table-cell; vertical-align: middle; text-align: center; }
HTML
<div>
<div>
<p>Centered In The Middle Of The Page.</p>
</div>
</div>
Demo on jsfiddle.
Hope it works for you.

Circles inside circle and vertical centering text

I am trying to draw 2 outer circle around a circle and keeping the text as vertically centered.
I am able to draw a circle outside a circle but not the 3rd one.
Html
<div id="content">
<div id="outer-circle">
<p>text</p>
</div>
</div>
Demo : http://jsfiddle.net/squidraj/7vusbo0v/1/
The text is also not centering horizontally.
Any help is highly appreciated. Thanks in advance.
Here is my solution, based on your code:
Creating a "3rd circle" by using the parent #container
centering the text by using the display:table-cell(which allows you to vertical align elements)
#content {
border-radius: 50%;
height: 320px;
width: 320px;
position: relative;
box-shadow: 0 0px 0 10px green;
margin: 10px;
}
#outer-circle {
background: #385a94;
border-radius: 50%;
height: 300px;
width: 300px;
position: absolute;
box-shadow: 0 0px 0 10px black;
top: 10px;
left: 10px;
display: table;
}
#outer-circle p {
display: table-cell;
vertical-align: middle;
text-align: center;
}
<div id="content">
<div id="outer-circle">
<p>text</p>
</div>
</div>
I was editing my answer by the time it got accepted and received comments on, but no matter what I'm giving the other solution i was typing at the time:
Applying the border property to your #outer-circle would do the "3rd circle" since you are using box-shadow on it.
to vertical align the text, same solution as the 1st one.
#outer-circle {
background: #385a94;
border-radius: 50%;
height: 300px;
width: 300px;
position: absolute;
box-shadow: 0 0px 0 10px green;
top: 10px;
left: 10px;
display: table;
border: 10px solid black;
margin:10px;
}
#outer-circle p {
display: table-cell;
vertical-align: middle;
text-align: center;
}
<div id="content">
<div id="outer-circle">
<p>text</p>
</div>
</div>
There is nothing different with them being in a circle, so follow the normal centering rules
The specifics are dependant on what browsers you need to support
Patrick's reference is correct. Give the following a try:
#outer-circle p {
text-align: center;
width: 100%;
position: absolute;
top: 50%;
transform: translateY(-50%);
margin-top: -5px;
}
Note that I've added a negative top margin, which accounts for your border width.
set you css
#outer-circle p {
bottom: 0;
left: 0;
/* position: absolute; */
right: 0;
text-align: center;
padding: 50% 0;
top: 0;
}
see https://jsfiddle.net/fwzfoncy/

Vertically center 2 floating divs

I have a problem. I want to achieve something like this:
I have a div with fixed height, and 2 other divs inside, with variable / unknown height, which I want to have
a) vertically centered
b) floating left /right
Right now I am trying something like this.
<div class="wrapper">
<div class="left">This is left</div>
<div class="right">This should be right</div>
</div>
.wrapper:before {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
}
.left {
display: inline-block;
vertical-align: middle;
}
.right {
display: inline-block;
vertical-align: middle;
}
Everything is perfectly centered, but the right div is next to the left one, and not on the right side. As soon as I start to put in
float: right;
into my right class, it is on the right side, but not centered anymore. And I have no clue how to achieve this.
Thank you in advance!
There is a really cleaver answer to this at http://zerosixthree.se/vertical-align-anything-with-just-3-lines-of-css/ It suggests this code:
.element {
position: relative;
top: 50%;
transform: translateY(-50%);
}
There are other solutions to this problem also, but this is the most simple. You can then just float each box left or right.
EDIT: another link with a lot of ways of doing this http://css-tricks.com/centering-css-complete-guide/
Try using Flexbox, e.g.
.wrapper {
display: flex;
justify-content: space-between;
align-items: center;
align-content: center;
}
.left {
display: inline-block;
vertical-align: middle;
background: red;
}
.right {
vertical-align: middle;
background: green;
}
http://jsfiddle.net/hafpuvtq/
More info: http://css-tricks.com/snippets/css/a-guide-to-flexbox/
You have to set the html, body elements of height: 100% and margin and padding of 0 outside the container class first before declaring any of the following classes:
HTML
<body>
<div class="container">
<div class="box1"></div>
<div class="box2"></div>
</div>
</body>
CSS
html, body {
height: 100%;
padding: 0;
margin: 0;
}
.container {
position: relative;
top: 50%;
height: 100px;
}
.box1 {
height: 100px;
width: 100px;
background-color: red;
float: left;
}
.box2 {
height: 100px;
width: 100px;
background-color: green;
float: right;
}
The left and right both have to contain floats; left box for float: left; and right box for float: right;
That's right - floating an element removes it from the document flow, so it can't align itself to its parent element's line-height. Instead, put a wrapper div around each of the two child elements, and float the wrappers, left and right respectively. Make sure their height is 100%, and then vertically align the children inside them, as you currently are.
See http://jsfiddle.net/conLs2fd/6/.
this answer is just css
.wrapper {
position: relative;
height: 200px;
border: 1px solid gray;
}
.left {
position: absolute;
top: 0;
bottom: 0;
left: 0;
width: 100px;
height: 100px;
margin: auto;
background-color: lightgray;
display:inline-block;
}
.right {
position: absolute;
top: 0;
bottom: 0;
right: 0;
width: 100px;
height: 100px;
margin: auto;
background-color: gray;
display:inline-block;
}
<div class="wrapper">
<div class="left child">This is left</div>
<div class="right child">This should be right</div>
</div>
Here is one way of doing it that involves using text-align: justify on the .wrapper parent block. If you can specify the height of .wrapper, you
can set line-height to the same value of the height.
Add a :after pseudo-element of height: 0 to force a second line for the line box containing the elements, which will allow the justification to work.
.wrapper {
border: 1px dotted gray;
height: 100px; /* for demo only */
line-height: 100px;
text-align: justify;
}
.wrapper:after {
content: '';
display: inline-block;
height: 0;
width: 100%;
}
.left, .right {
border: 1px dotted blue;
line-height: 1.2;
}
.left {
display: inline-block;
vertical-align: middle;
}
.right {
display: inline-block;
vertical-align: middle;
}
<div class="wrapper">
<div class="left">This is left</div>
<div class="right">This should be right</div>
</div>

how do I position a circle div at the end of another div that acts as a bar?

I am trying to make a circle div act as a decorative part of a bar and I'm having a hard time positioning the circle. The idea is that the bar and circle act as a section divider. I want to have a bar and then a circle at the end of it (hopefully, the circle has text). I am also trying to make it a responsive circle. I tried setting the width to a certain % and then height as auto but that didn't work too well. Here is my jsfiddle.(http://jsfiddle.net/dbartolome/vzkbjh5h/1/)
The HTML and CSS code so far:
<div class="divider"><div class="circle"></div></div>
and CSS
div{
display: inline-block;
vertical-align: top;
}
.divider{
display: block;
width: 80%;
background-color: #20ffd0;
height: 20px;
text-align: center;
position: relative;
}
.circle{
position: relative;
width: 100px;
height: 100px;
border-radius: 50%;
background-color: #BADA55;
text-align: right;
}
Here is a simplified version: http://jsfiddle.net/vzkbjh5h/4/
.divider{
display: block;
width: 80%;
background-color: #20ffd0;
height: 20px;
text-align: center;
position: relative;
}
.circle{
width: 20px;
height: 20px;
border-radius: 50%;
background-color: #BADA55;
float: right;
margin-right: -10px;
}
I like the to the <hr> answer but here's one option too:
http://jsfiddle.net/dianaavila/jr91mub8/1/
<div id="work">
<div class="divider"></div>
<div class="circle"></div>
....
</div>
.
.circle{
position: relative;
width: 100px;
height: 100px;
border-radius: 50%;
background-color: #BADA55;
text-align: right;
float:right;
margin-top:-45px;
}
.divider{
display: block;
width: 80%;
background-color: #20ffd0;
height: 20px;
text-align: center;
position: relative;
float:left;
}
I took the .circle out of the .divider container.
Float them right and left respectively.
Added a negative margin to the circle.
Don't abuse all those classes. A horizontal divider is an hr element, and you can put the circle in just fine with an absolutely positioned :after pseudo-element.
All the CSS you need then for the <hr>:
hr {
width: 80%;
background-color: #20ffd0;
height: 20px;
border:0;
position: relative;
margin:50px auto;
}
hr:after {
content:'';
position: absolute;
right:0;
top:-40px;
width:100px;
height:100px;
border-radius: 50%;
background-color: #BADA55;
}
The top:-40px corrects for the combined heights of circle and bar to center vertically, and the margin on the hr itself gives it the 'breathing space' to other content.
You can of course also apply this to hr.big or something else if you don't want to style all horizontal rules like this.
Sample here.
You can position the .circle absolutely to the bar.
Here is an updated fiddle
I've changed .circle to
.circle{
position: absolute;
width: 100px;
height: 100px;
border-radius: 50%;
background-color: #BADA55;
text-align: center;
left: 100%;
top: -80px;
line-height: 100px;
}
Adjust top parameter to change circle's vertical position. Line-height is equal to the circle's height to make the text centered vertically. Text-align: center; is for horizontal.