how can set a div center of other div with CSS? - html

for example i designed a div for border style and i designed another div to center of that , how can i set it to center of larger div?
.Profile_Photo_Border {
border: 3px solid #052d31;
height: 90px;
width: 90px;
border-radius: 3px;
}
.Profile_Photo {
background-color:#005e67;
height: 80px;
width: 80px;
border-radius: 3px;
alignment-adjust:middle;
text-align:center;
}
<div class="Profile_Photo_Border">
<div class="Profile_Photo"></div>
</div>

Add the following style display: flex; to the parent div and
margin: 0 auto;
align-self: center;
to the child div to align it center horizontally as well as vertically.
So the styles become:
.Profile_Photo_Border {
border: 3px solid #052d31;
height: 90px;
width: 90px;
border-radius: 3px;
display: flex;
}
.Profile_Photo {
background-color:#005e67;
height: 80px;
width: 80px;
border-radius: 3px;
alignment-adjust:middle;
text-align:center;
margin: 0 auto;
align-self: center;
}
See the fiddle: "https://jsfiddle.net/ukgnnp4k/"
See the screenshot:

Try changing your CSS to:
.Profile_Photo_Border {
border: 3px solid #052d31;
height: 90px;
width: 90px;
border-radius: 3px;
position: relative;
}
.Profile_Photo {
background-color: #005e67;
height: 80px;
width: 80px;
border-radius: 3px;
text-align:center;
position: absolute;
top: 50%;
left: 50%;
margin-left: -40px;
margin-top: -40px;
}
This link also might be helpful:
https://css-tricks.com/centering-css-complete-guide/

Your second div has 10px size lesser than the first one in height and width.
So to centralize the middle one add margin:5px;to the second div, Profile_Photo.

You can add this css.
.Profile_Photo_Border {
border: 3px solid #052d31;
height: 90px;
width: 90px;
border-radius: 3px;
}
.Profile_Photo {
background-color:#005e67;
height: 80%;
width: 80%;
border-radius: 3px;
text-align:center;
margin:10px auto;
}
Use this http://jsfiddle.net/18yao91v/244/

.Profile_Photo {
background-color:#005e67;
height: 80px;
width: 80px;
border-radius: 3px;
margin: 5px auto;
}

If the outer div and inner div has fixed width, then you can use css position to align inner element.
See below CSS.
.Profile_Photo_Border {
border: 3px solid #052d31;
height: 90px;
width: 90px;
border-radius: 3px;
position: relative;
}
.Profile_Photo {
background-color:#005e67;
height: 80px;
width: 80px;
border-radius: 3px;
/* alignment-adjust:middle; No need to use this. */
text-align:center;
position: absolute;
top: 5px;
left: 5px;
}
<div class="Profile_Photo_Border">
<div class="Profile_Photo"></div>
</div>

Here are my 2 cents, I used the display:table.cell css style:
.Profile_Photo_Border {
border: 3px solid #052d31;
height: 150px;
width: 150px;
border-radius: 3px;
display: table-cell; /*added*/
vertical-align: middle; /*added*/
}
.Profile_Photo {
background-color: #005e67;
height: 80px;
width: 80px;
border-radius: 3px;
text-align: center; /*added*/
margin: auto; /*added*/
}

Here's another way of centering the div inside a div irrespective of width and height - Codepen
.Profile_Photo_Border {
border: 3px solid #052d31;
height: 90px;
width: 90px;
border-radius: 3px;
position: relative;
}
.Profile_Photo {
background-color:#005e67;
height: 80px;
width: 80px;
border-radius: 3px;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
margin: auto;
}
And a guide from CSS Tricks for centering the div.
Another guide from CSS Tricks on Flexbox which is another better way.
Hope this might help you understand better.

Related

HTML circle button with border

I want to create this kind of button.
For that I tried to create button inside another button. But it not worked. Then I tried to create a button inside a circle div.Then I couldn't adjust the div and button properly. It looks like this,
Following is my code,
<div class="buton-inside" style="border-radius: 50%; background-color: #1E1C1C; height: 120px; width: 120px">
<button class="button" style="border-radius: 50%; background-color: #1eff5b; height: 100px; width: 100px; margin-left:0.5px; margin-right:0.5px; margin-top:0.5px; margin-bottom:0.5px"></button>
</div>
So how can I fix this. Please help me !
Use :before button insted use div
It makes multi border(change color as you want and add opacity)
.button {
position: relative;
border: 5px solid #f00;
}
.button:before {
content: " ";
position: absolute;
z-index: -1;
top: -10px;
left: -11px;
right: 5px;
bottom: 87px;
border: 56px solid #252523;
border-radius: 50%;
}
<button class="button" style="border-radius: 50%; background-color: #1eff5b; height: 100px; width: 100px; margin-left:0.5px; margin-right:0.5px; margin-top:0.5px; margin-bottom:0.5px"></button>
Try this easy way
HTML
Click
CSS
.click {
background: #06F;
color: #fff;
width: 70px;
height: 70px;
line-height: 70px;
display: block;
border-radius: 50%;
text-align: center;
text-decoration:none;
border:3px solid #fff;
box-shadow: 0px 0px 0px 3px #06F;
}
You don't need a second element (not even a pseudo element), You can simply achieve this with a border and a box-shadow:
button {
display: block;
padding: 50px 38px;
border-radius: 50%;
border: 3px solid black;
background: #19361e;
box-shadow: 0 0 0 3px #19361e;
color: #4bd763;
font-size: 1.5em;
margin: 30px auto;
}
body {
background: black;
}
<button>Start</button>
Add the following styles to "button-inside" div
display: flex;
align-items: center;
justify-content: center;
<div class="buton-inside" style="border-radius: 50%; background-color: #1E1C1C; height: 120px; width: 120px; display: flex; align-items: center; justify-content: center;">
<button class="button" style="border-radius: 50%; background-color: #1eff5b; height: 100px; width: 100px;"></button>
</div>
Try this. Those flex properties should put the child divs in the center of the parent. I removed the margin from your code since it might not be relevant anymore.
TRY THIS,
made some changes in your code
<div class="buton-inside" style="border-radius: 50%; background-color: #1E1C1C; height: 120px; width: 120px;text-align:center">
<button class="button" style=" border-radius: 50%;
background-color: #1eff5b;
height: 100px;
width: 100px;
margin-left: auto;
margin-right: auto;
margin-top: 10px;
margin-bottom: 10px;"></button>
</div>
<div class="parent">
<button>Start</button>
</div>
.parent {
width: 200px;
height: 200px;
border: 2px solid green;
border-radius: 50%;
display: flex;
justify-content: center;
align-items: center;
padding: 10px;
box-sizing: border-box;
}
button {
width: 100%;
height: 100%;
border-radius: 50%;
padding: 10px;
background: green;
}
You can try this link [codepen.io][1]
[1]: https://codepen.io/venumadhavdiv/pen/rvMOjN

Transparent box over an image

Alright so I am trying to a basic overlay over an image but it seems that I am doing something wrong, instead of being width and height 100% of the IMG, it is width and height 100% of the entire page
HTML
<div id="main_BodyNews">
<img src="img/main.png" alt="mainNews" />
<div class="overflow-box"></div>
</div>
And the CSS
#main_BodyNews {
width: 50%;
height: 300px;
background-color: #F2C68C;
margin-top: 50px;
margin-left: 20px;
float: left;
border-radius: 5px;
border: 1px solid #F2C68C;
}
#main_BodyNews img {
width: 100%;
height: 100%;
border-radius: 5px;
background-color: 1px solid #F2C68C;
position: relative;
}
.overflow-box {
position:absolute;
top: 0px;
left: 0px;
bottom: 0px;
right: 0px;
background-color:rgba(255,255,0,0.5);
width: 100%;
height: 100%;
}
JS fiddle: https://jsfiddle.net/0utbjwo0/
you should add position: relative; to your absolute parent div
#main_BodyNews{
position: relative;
}
#main_BodyNews {
width: 50%;
height: 300px;
background-color: #F2C68C;
margin-top: 50px;
margin-left: 20px;
float: left;
border-radius: 5px;
border: 1px solid #F2C68C;
position: relative;
}
#main_BodyNews img {
width: 100%;
height: 100%;
border-radius: 5px;
background-color: 1px solid #F2C68C;
position: relative;
}
.overflow-box {
position:absolute;
top: 0px;
left: 0px;
bottom: 0px;
right: 0px;
background-color:rgba(255,255,0,0.5);
}
<div id="main_BodyNews">
<img src="img/main.png" alt="mainNews" />
<div class="overflow-box"></div>
</div>
You can use absolute. It's just that you are setting
width: 100%;
height: 100%;
Remove that and set your margin-top and margin left. You can set your width and height for the actually dimensions of your image. If you do this, you wont have to exactly keep your overlay div within your image div.
Here is an example of one I have made for my site.
#overlay {
margin-top: 60px;
margin-left: 88px;
height: 30px;
width: 85px;
position: absolute;
}
You can temporarily set a background-color for it so that you can get a good idea of where it is placed on your page. Then adjust your margins accordingly.
It's because the position: absolute has top, right, bottom, left value of 0. You don't need to specify the height and width. To make it resize on it's parent size. You need position: relative on parent element.
#main_BodyNews {
width: 50%;
height: 300px;
background-color: #F2C68C;
margin-top: 50px;
margin-left: 20px;
float: left;
border-radius: 5px;
border: 1px solid #F2C68C;
position: relative;
}
#main_BodyNews img {
width: 100%;
height: 100%;
border-radius: 5px;
background-color: 1px solid #F2C68C;
position: relative;
}
.overflow-box {
position: absolute;
top: 0px;
left: 0px;
bottom: 0px;
right: 0px;
background-color: rgba(255, 255, 0, 0.5);
}
<div id="main_BodyNews">
<img src="img/main.png" alt="mainNews" />
<div class="overflow-box"></div>
</div>

CSS and positioning a span element inside a div

I have a div and I would like to align a span element inside it. The span element should be aligned bottom and horizontally center of its parent div.
<div style="position: relative; clear: both; float: left; border: 2px solid #999999; padding:0; margin:1px; width: 60px; height: 60px; border-radius: 50%; -webkit-border-radius: 50%; -moz-border-radius: 50%; background: url(/img/img60x60.gif) no-repeat;">
<span style="border: none; background-color: green; margin: 0; padding: 0; border: 0; position: absolute; margin-left: auto; margin-right: auto; bottom: 0;"> 123. </span></div>
At the same time, the alignment of my span element is not working. The width of the span element will change all the time. I mean that it is not a fixed width element.
I'm looking for help with this, and a cross-browser solution. No JavaScript/jQuery allowed.
.holder {
display: table;
border: 2px solid #999999;
padding:0;
margin: 1px;
width: 60px;
height: 60px;
border-radius: 50%;
-webkit-border-radius: 50%;
-moz-border-radius: 50%;
background: #00ff00;
}
.some {
display: table-cell;
vertical-align: bottom;
text-align: center;
border: none;
}
<div class="holder">
<span class="some">
123.
</span>
</div>
Something like this?
.holder {
display: table;
border: 2px solid #999999;
padding:0;
margin: 1px;
width: 60px;
height: 60px;
border-radius: 50%;
-webkit-border-radius: 50%;
-moz-border-radius: 50%;
background: #00ff00;
}
.some {
display: table-cell;
vertical-align: bottom;
text-align: center;
border: none;
}
<div class="holder">
<span class="some">
123.
</span>
</div>
There is no padding added in firefox. The space on either side of the text is in this case set by the in the span tag, so the varying results you are getting are probably due to font rendering differences between browsers. Using a CSS Reset should take care of that. Try this:
<div style="border: 2px solid #999999; padding:0; margin:1px; width: 60px; height: 60px; border-radius: 50%; -webkit-border-radius: 50%; -moz-border-radius: 50%; background: url(/img/img60x60.gif) no-repeat;">
<span style="background-color: green; display:inline-block; margin-top:45px;margin-left:16px;">123.</span></div>
Also, looking at the code, it looks like you are using a WYSIWYG editor which tends to inline css rules and space text by adding &nbsp html entities instead of using horizontal padding and text-align:center;. I would recommend adding these rules in a class and using an external css stylesheet for better performance.
Flexbox can do that:
div {
float: left;
border: 2px solid #999999;
padding: 0;
margin: 1px;
width: 60px;
height: 60px;
border-radius: 50%;
-webkit-border-radius: 50%;
-moz-border-radius: 50%;
display: inline-flex;
align-items: flex-end;
justify-content: center;
}
<div>
<span> 123. </span>
</div>
Use this CSS on the span:
.y {
display: inline-block;
background-color: green;
position: absolute;
left: 50%;
transform: translateX(-50%);
bottom: 0px;
}
The bottom "alignment height" can be adjusted with the bottom parameter.
BTW: There is no padding around the span, that's just the line height, and the non-breaking spaces you put in yourself.
.x {
position: relative;
clear: both;
float: left;
border: 2px solid #999999;
margin: 1px;
width: 60px;
height: 60px;
border-radius: 50%;
-webkit-border-radius: 50%;
-moz-border-radius: 50%;
background: url(/img/img60x60.gif) no-repeat;
}
.y {
display: inline-block;
background-color: green;
position: absolute;
left: 50%;
transform: translateX(-50%);
bottom: 0px;
}
<div class="x">
<span class="y"> 123. </span>
</div>
First of all, thank you all for your help.
Finally I modified the answers of #ashok and #Hardy. I did it all this way:
<div style="display: table-cell;
border: 2px solid #999999;
padding: 0;
margin: 1px;
width: 60px;
height: 60px;
text-align: center;
vertical-align: bottom;
border-radius: 50%;
-webkit-border-radius: 50%;
-moz-border-radius: 50%;
background: url(/img/img60x60.gif) no-repeat;">
<span style="position: relative; bottom: -4px; background-color: green;"> 123. </span>
</div>
It seems to me that this is working OK. I will modify the code and put my CSS code to an external file.

How to draw an inner div over an outer div using css?

I want to draw an inner div over an outer div for scrolling purposes. How can I change my CSS to fix this?
HTML code:
<div class="sliderPath">
<div class="slider"></div>
</div>
CSS code:
.sliderPath {
border-radius: 25px;
background: #73AD21;
padding: 20px;
width: 80%;
height: 20%;
margin: auto;
}
.slider {
border-radius: 4px;
background:#50c2de;
position: absolute;
width: 50px;
height: 50px;
left: 50%;
}
https://jsfiddle.net/ImSonuGupta/0bx6uwyn/1/
Try setting top position to small value:
.slider {
border-radius: 4px;
background:#50c2de;
position: absolute;
width: 50px;
height: 50px;
left: 50%;
top: 3px; //added this
}
And also set position: relative on parent:
.sliderPath {
border-radius: 25px;
background: #73AD21;
padding: 20px;
width: 80%;
height: 20%;
margin: auto;
position: relative; //added this
}
updated jsfiddle
Simply make .sliderPath the base position for its childs, with position:relative, and make .slider top 100% off its parent height.
.sliderPath {
border-radius: 25px;
background: #73AD21;
padding: 20px;
width: 80%;
height: 20%;
margin: auto;
position:relative;
}
.slider {
border-radius: 4px;
background:#50c2de;
position: absolute;
width: 50px;
height: 50px;
left: 50%;
top:100%
}
EDIT
if you need multiple slides, just add margin-bottom to .sliderPath equal to .slider height. So it would be:
.sliderPath {
border-radius: 25px;
background: #73AD21;
padding: 20px;
width: 80%;
height: 20%;
margin: auto;
position:relative;
margin-bottom:50px;
}

Alignment with relative and absolute positioning

How could I center the blue box inside the red one ?
I see that the left side of the blue box is exactly in the middle of the red box, but I would like to center the whole blue box, not its left side. The dimensions of the boxes are not constant. I want to align regardless of boxes dimensions. Example to play with here. Thanks !
HTML:
<div id="rel">
<span id="abs">Why I'm not centered ?</span>
</div>
CSS:
#rel {
position: relative;
top: 10px;
left: 20px;
width: 400px;
height: 300px;
border: 1px solid red;
text-align: center;
}
#abs {
position: absolute;
bottom: 15px;
width: 300px;
height: 200px;
border: 1px solid blue;
}
If you're able to change the <span> tag to a <div>
<div id="rel">
<div id="abs">Why I'm not centered ?</div>
</div>
Then this piece of CSS should work.
#rel {
position: absolute;
top: 10px;
left: 20px;
width: 400px;
height: 300px;
border: 1px solid red;
text-align: center; }
#abs {
width: 300px;
height: 200px;
border: 1px solid blue;
margin: auto;
margin-top: 50px; }
I think it's better to use more automation for the enclosed box as less changes would be needed should you change the size of the container box.
You could add left:50px to #abs if that's all you want...
#abs {
position: absolute;
bottom: 15px;
width: 300px;
height: 200px;
border: 1px solid blue;
left:50px;
}
If you are going to define dimensions like that (200px x 300px and 300px x 400px), here's how it can be centered:
#rel {
position: relative;
top: 10px;
left: 20px;
width: 400px;
height: 300px;
border: 1px solid red;
text-align: center;
}
#abs {
position: absolute;
width: 300px;
height: 200px;
border: 1px solid blue;
margin: 49px 0 0 49px;
}
You can check at my solution here at http://jsfiddle.net/NN68Z/96/
I did the following to the css
#rel {
position: relative;
top: 10px;
left: 20px;
right: 20px;
width: 400px;
height: 300px;
border: 1px solid red;
text-align: center;
display: table-cell;
vertical-align: middle;
}
#abs {
display: block;
bottom: 15px;
width: 300px;
height: 200px;
border: 1px solid blue;
margin: 0 auto;
}
This should work
#abs {
position: absolute;
left: auto;
right: auto;
bottom: 15px;
width: 300px;
height: 200px;
border: 1px solid blue;
}