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/
Related
I've got a PNG image icon with un-even sides (64px x 42px) and I'd like to create a circle border around it.
My html looks like this:
<span class="cat_circle">
<div class="cat_icon">
<img src="https://cdn.pbrd.co/images/GNK97WG.png">
</div>
</span>
I created a circle border around the image, but I just can't get the icon to the exact middle of the circle. The icon sits on the bottom right of the circle.
Here is my CSS for the circle border:
.cat_circle {
border: 3px solid #7E9CC2;
border-radius: 50%;
width: 25px;
height: 25px;
overflow: hidden;
position: absolute;
padding: 30px;
left: 10px;
top: 10px;
text-align: center;
vertical-align: middle;
}
I then started fiddling with the actual png image and I gave it negative margins like this:
.cat_icon {
margin-top: -10px;
margin-left: -18px;
}
I mean it seems to work and I have my icon in the middle of the circle, but is this the right way I should be approaching this??
Here is my fiddle: https://jsfiddle.net/ox0anvL7/
You can do that a lot simpler. I added three flexbox properties for the centering, but erased one HTML wrapper and quite a few of the (too complicated) CSS settings:
.cat_circle {
border: 3px solid #7E9CC2;
border-radius: 50%;
width: 70px;
height: 70px;
display: flex;
justify-content: center;
align-items: center;
}
.cat_circle img {
width: 80%;
height: auto;
}
<span class="cat_circle">
<img src="https://cdn.pbrd.co/images/GNK97WG.png">
</span>
This is a pretty good way to do it. This works for all cases too when trying to center something inside a div.
It can be better than using flex as I believe that flex is only available in versions of IE 10 onwards (source here).
.cat_circle {
position: absolute;
display: inline-block;
border: 3px solid #7E9CC2;
border-radius: 50%;
width: 70px;
height: 70px;
}
.cat_circle img {
position: absolute;
top: 50%; left: 50%;
width: 80%;
transform: translate(-50%, -50%);
}
<span class="cat_circle">
<img src="https://cdn.pbrd.co/images/GNK97WG.png">
</span>
You Can Try this way !!
<div class="image-circle">
<div>
<img class="img" src="https://cdn.pbrd.co/images/GNK97WG.png">
</div>
</div>
.image-circle {
width:25%;
}
.image-circle:after {
content: "";
background: #4679BD;
padding-bottom: 100%;
border-radius: 50%;
display: block;
width: 100%;
height:0;
-moz-border-radius: 50%;
-webkit-border-radius: 50%;
}
.image-circle div {
float:left;
width:100%;
line-height:1em;
margin-top:-0.5em;
padding-top:40%;
text-align:center;
}
I'd suggest flexbox, simplifies and reduces the need of another class.
.cat_circle {
border: 3px solid #7E9CC2;
border-radius: 50%;
width: 25px;
height: 25px;
overflow: hidden;
position: absolute;
padding: 30px;
left: 10px;
top: 10px;
/* new */
display: flex;
justify-content: center;
align-items: center;
}
<span class="cat_circle">
<img src="https://cdn.pbrd.co/images/GNK97WG.png">
</span>
I'm trying to place two images on top of each other, with both of the images horizontally and vertically centered inside their container.
One of the images will be have its opacity animated to reveal the image underneath.
The images are both the same size, but I don't know the size of the images beforehand. I also would like to do this in just pure CSS and HTML.
Here is what I ended up with.
.data-box{
border: 2px solid #d4d4d4;
border-radius: 3px;
display: flex;
height: 120px;
margin: 5px;
margin-bottom: 10px;
margin-right: 10px;
padding: 0;
position: relative;
width: 120px;
}
.logo {
align-items: center;
display: flex;
justify-content: center;
margin: auto;
position: relative;
}
.data-name {
position: absolute;
width: 100%;
height: 23px;
bottom: 0px;
right: 0px;
background: rgba(200, 200, 200, 0.3);
}
span {
position: absolute;
width: 100%;
bottom: 2px;
text-align: center;
}
img {
position: absolute;
}
<div class="data-box">
<div class="logo">
<img class="grayscale-image" src="https://placeholdit.imgix.net/~text?txtsize=8&txt=65%C3%9765&w=65&h=65" alt="">
<img class="color-image" src="https://placeholdit.imgix.net/~text?txtsize=8&txt=65%C3%9765&w=65&h=65" alt="">
</div>
<div class="data-name"><span>Flickr</span></div>
</div>
I made the images position: absolute so they would leave the normal flow of the browser and render directly on top of each other instead of next to each other.
This works correctly in Chrome, but in Firefox and Safari the image's top left corner is horizontally and vertically centered:
How can I horizontally and vertically center these images while still having them render directly on top of each other?
Solution
Add this to your code:
img {
position: absolute;
top: 0;
left: 0;
transform: translate(-50%, -50%);
}
.data-box {
border: 2px solid #d4d4d4;
border-radius: 3px;
display: flex;
height: 120px;
margin: 5px;
margin-bottom: 10px;
margin-right: 10px;
padding: 0;
position: relative;
width: 120px;
}
.logo {
align-items: center;
display: flex;
justify-content: center;
margin: auto;
position: relative;
}
.data-name {
position: absolute;
width: 100%;
height: 23px;
bottom: 0px;
right: 0px;
background: rgba(200, 200, 200, 0.3);
}
span {
position: absolute;
width: 100%;
bottom: 2px;
text-align: center;
}
img {
position: absolute;
top: 0;
left: 0;
transform: translate(-50%, -50%);
}
<div class="data-box">
<div class="logo">
<img class="grayscale-image" src="https://placeholdit.imgix.net/~text?txtsize=8&txt=65%C3%9765&w=65&h=65" alt="">
<img class="color-image" src="https://placeholdit.imgix.net/~text?txtsize=8&txt=65%C3%9765&w=65&h=65" alt="">
</div>
<div class="data-name"><span>Flickr</span>
</div>
</div>
Explanation
Although setting an element to position: absolute removes it from the normal flow, it doesn't actually position it anywhere.
The CSS offset properties (top, bottom, left and right) have an initial value of auto, which keeps an absolutely positioned element where it normally would be if it were in the document flow. As you can see, browser behavior will vary when the offsets aren't defined.
For an explanation of how the code above works, see this post: Element will not stay centered, especially when re-sizing screen
I don't think you need flexbox at all:
.data-box {position:relative; display:inline-block;}
.logo {position:relative;}
.color-image {position:absolute; top:0; left:0; bottom:0; right:0; opacity:0.5;}
.data-name {position:absolute; left:0; right:0; bottom:5px; text-align:center;}
<div class="data-box">
<div class="logo">
<img class="grayscale-image" src="https://placeholdit.imgix.net/~text?txtsize=8&txt=65%C3%9765&w=65&h=65" alt="">
<img class="color-image" src="https://placeholdit.imgix.net/~text?txtsize=8&txt=65%C3%9765&w=65&h=65" alt="">
</div>
<div class="data-name"><span>Flickr</span></div>
</div>
Could you set the img in a div, and have the behind image set as the background of the div?
It's not the most elegant solution but this works:
img {
position: absolute;
transform: translate(-50%, -50%);
}
I'll try to get straight to the point.
Here's an example that centralizes two images inside a parent.
<html>
<head>
<title>Exemple</title>
<style type="text/css">
.parent{
margin: auto auto;
width: 500px;
height: 500px;
border: 3px solid #ccc;
}
.child1, .child2{
width: 50%;
height: 50%;
margin: 25%;
background-color: rgb(226,26,60);
}
.child1{
opacity:0.5;
}
</style>
</head>
<body>
<div class="parent">
<img class="child1" src="https://placeholdit.imgix.net/~text?txtsize=8&txt=65%C3%9765&w=65&h=65" alt="">
<img class="child2" src="https://placeholdit.imgix.net/~text?txtsize=8&txt=65%C3%9765&w=65&h=65" alt="">
</div>
</body>
</html>
Use margin with percentage to align both images in the middle of the
parent div. Here I set the with and height to 50%, which means there's
50% left. That's why you set the margin to 25%, so he puts it in the
middle of the parent.
Good luck
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!
So, I have a main container that shows like the following:
I want to be able to adapt the parent div to the number of child's it receives. Let's say we remove div2. The result should be something like this:
Instead, the parent div does not stretch to the width of the div child's
Here's my code:
HTML:
<div class="main-container">
<!-- Card container -->
<div class="card-container">
<div class="card">div1</div>
<div class="card">div2</div>
<div class="card">div3</div>
</div>
<!-- Footer container -->
<div class="footer">i am a footer</div>
</div>
CSS:
.main-container {
position: fixed;
margin: 0 auto;
left: 0;
right: 0;
bottom: 0;
width: 100%;
max-width: 400px;
box-shadow: 0 0 15px #B3B3B3;
border-top-left-radius: 3px;
border-top-right-radius: 3px;
text-align:center;
}
.card-container {
color: #3B3D3D;
height:105px;
float: left;
width: 100%;
}
.footer {
color: #FFFFFF;
background: #0095D3;
height: 45px;
float: left;
width: 100%;
}
.card {
width:100px;
float:left;
}
What am I doing wrong here? I've tried the display: inline-block; solutions out there but since the parent div must be fixed to the bottom, I am not seeing the desired result.
Any help will be precious.
Thanks in advance.
Try this https://jsfiddle.net/2Lzo9vfc/136/
You can try to remove one .card on click and see what hapens here https://jsfiddle.net/2Lzo9vfc/138/
CSS
.main-container {
position: fixed;
margin: 0 auto;
left: 50%;
bottom: 0;
box-shadow: 0 0 15px #B3B3B3;
border-top-left-radius: 3px;
border-top-right-radius: 3px;
text-align:center;
display: inline-block;
transform: translateX(-50%);
}
.footer {
color: #FFFFFF;
background: #0095D3;
height: 45px;
width: 100%;
}
.card {
width:100px;
height:105px;
display: inline-block;
}
HTML
<div class="main-container">
<div class="card">div1</div>
<div class="card">div2</div>
<div class="card">div3</div>
<div class="footer">i am a footer</div>
</div>
Here you go: http://codepen.io/n3ptun3/pen/PPgWNb
You don't need to use display: inline-block.
I've left your HTML alone, and simplified some of your CSS: .card-container and .footer don't need float: left; and width: 100%;. They are both block-level elements so they will take up 100% of the width, and they don't need anything to wrap around them.
On the .main-container, you can't set margin: 0 auto; and position: fixed;. position: fixed; removes the ability for centering via margin. left: 0; and right: 0; were stretching the size of the main container, so those need to be removed. width: 100%; and max-width: 400px; were trying to fix the width issue, but that wouldn't allow resizing based on content.
Instead you need to set left: 50%; (places left edge of element at 50% of the parent's width, i.e. the viewport width, in this case) and then transform: translate(-50%); to bring the element back toward the left by 50% of its width. Thus bringing the element to the center of the window/viewport.
Now, if you remove one of the "cards," it will resize the "main-container," while keeping everything fixed to the bottom and centered.
.main-container {
position: fixed;
bottom: 0;
left: 50%;
transform: translate(-50%);
box-shadow: 0 0 15px #B3B3B3;
border-top-left-radius: 3px;
border-top-right-radius: 3px;
text-align: center;
}
.card-container {
color: #3B3D3D;
height: 105px;
}
.card {
width: 100px;
float: left;
}
.footer {
color: #FFFFFF;
background: #0095D3;
height: 45px;
}
EDIT: Based on your new information (re: the increased width or added "cards"), I've found that the issue lies with the left position on the .main-container. When you position the element by 50% and its width is more than 50% of the parent, it runs into the right side of the parent div, and you get the stacking. To fix this, you can instead remove the float: left; on .card and add display: flex; on .card-container. This will allow you to increase the width of the "cards" while keeping them from stacking.
I've updated the code here: http://codepen.io/n3ptun3/pen/PPgWNb
.main-container {
position: fixed;
bottom: 0;
left: 50%;
transform: translate(-50%);
box-shadow: 0 0 15px #B3B3B3;
border-top-left-radius: 3px;
border-top-right-radius: 3px;
text-align: center;
}
.card-container {
color: #3B3D3D;
height: 105px;
display: flex;
}
.card {
width: 100px;
// float: left;
}
.footer {
color: #FFFFFF;
background: #0095D3;
height: 45px;
}
I'm trying to create a progress bar and i have a problem aligning div inside a div.
css:
.outer {
width: 20px;
height: 190px;
border: 2px solid #ccc;
overflow: hidden;
position: relative;
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
border-radius: 4px;
display:inline-block;
}
.inner {
width: 100%;
overflow: hidden;
position: absolute;
border-top-width: 0;
background-image: url('/images/progressBar2Red.png');
background-size: 20px;
bottom: 0;
height: 0%;
display:inline-block;
}
.progress{
display: inline-block;
align-items:center;
}
html:
<div class="progress">
<label class="progNum">20</label><br />
<div class="outer">
<div class="inner"></div>
</div>
</div>
For some reason the inner div is not exactly in the middle of the outer div. This is how it looks:
How can i put the inner div exactly in the middle of the outer div?
You made this .inner element of position: absolute. Just add left: 0; and right: 0; to the .inner CSS rules.
Divs are of 100% width by default, never set a div to a 100% width unless you absolutely need it... for some reason.
EDIT
Ok I actually do not understand what DOES NOT work for you. Check this JSFiddle. I think the problem is your background.
Give the inner margin: 0 auto;
JSfiddle Demo
HTML
<div class="progress">
<label class="progNum">20</label>
<div class="outer">
<div class="inner"></div>
</div>
</div>
CSS
.outer {
height: 190px;
border: 2px solid #ccc;
overflow: hidden;
position: relative;
border-radius: 4px;
}
.inner {
width: 100%;
position: absolute;
border-top-width: 0;
background-color: red;
bottom: 0;
width: 100%;
height:20px; /* or anything else you want */
border-radius: 4px;
}
.progress{
display: inline-block;
width: 20px; /* sets width of the whole bar - everything else can be 100% */
}
.progNum {
text-align: center;
display: block;
}