CSS and positioning a span element inside a div - html

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.

Related

How to center align content (e.g., image) inside a left aligned div inside a center aligned container?

How to center-align an image in a left-aligned div. It keeps aligning RIGHT (in the left aligned div).
Tried: Everything from position: absolute; to margin: auto; to attempts to override the parent div. I even threw in <span style="text-align:center"> into the HTML and that merely threw my CSS into utter chaos.
#background {
height: 150%;
background: linear-gradient(to bottom right, #33ff99 30%, #660066 60%);
}
#leftbox {
float: left;
background: #ff3399;
width: 25%;
height: 280px;
border: 2px solid #ff3399;
border-radius: 30px;
display: block;
text-align: left;
}
// I even tried adding a child div around the image I want to center as follows (to no avail)
#workshops {
width: 50%;
display: block;
margin: 0;
padding: 0;
text-align: center;
}
// The following comes AFTER the issue
#middlebox {
margin: auto;
float: left;
background: #44ff33;
width: 50%;
height: 280px;
border: 2px solid green
/* Safari 3-4, iOS 1-3.2, Android 1.6- */
-webkit-border-radius: 30px;
-moz-border-radius: 30px;
border-radius: 30px;
text-align: center;
}
#rightbox {
float: left;
background: blue;
width: 24%;
height: 280px;
border: 2px solid blue -webkit-border-radius: 30px;
-moz-border-radius: 30px;
border-radius: 30px;
text-align: center;
}
Below is the code I use on the image:
<!-- id tags-->
#leftbox {
float: left;
background: #ff3399 ;
width: 25%;
height: 280px;
border: 2px solid #ff3399;
border-radius: 30px;
display: block;
text-align: left; }
<!--The accompanying HTML follows-->
<div id="leftbox">
<a href="link.html" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image3','','imageURL.jpg',1)">
<img src="imageURL.png" name="Workshops!" width="250" id="Image3" />
</div>
</a>
</p>
This should work.
img{
position: relative;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
}

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

Overlay divs without absolute positioning and resize distortion

I want to create a line with tick marks and a ball (like a scale).
However the tutorials for this suggest using absolute positioning, or float. That works partially, but when I change the screen size, the divs shift out of place.
.line {
width: 100%;
min-height: 5px;
background-color: black;
padding: 20px;
margin-top: 20%;
}
.point {
-moz-border-radius: 50px/50px;
-webkit-border-radius: 50px 50px;
border-radius: 50px/50px;
border: solid 21px #f00;
width: 50px;
height: 50px;
background-color: red;
float: right;
overflow: visible;
position: relative;
z-index: 1000;
padding: 20px;
margin-top: -15%;
}
<div class="line"></div>
<div class="point"></div>
Wrap it in a div and do use absolute positioning for the inner divs, also, don't use margin-top percentages (https://jsfiddle.net/xv259d4p/1/):
.line {
width:100%;
min-height:5px;
background-color:black;
padding:20px;
margin-top:60px;
position: absolute;
}
.point {
-moz-border-radius: 50px/50px;
-webkit-border-radius: 50px 50px;
border-radius: 50px/50px;
border:solid 21px #f00;
width:50px;
height:50px;
background-color:red;
float: right;
overflow: visible;
position: absolute;
z-index: 1000;
padding:20px;
right: 0;
margin-top: 20px;
}
.outer {
display: block;
width: 100%;
top: 0px;
left: 0px;
position: relative;
}
<div class="outer">
<div class="line"> </div>
<div class="point"></div>
</div>

Creating a line with circle in the middle

So, I'm trying to achieve this result:
This is what I got when I tried: https://jsfiddle.net/wvdkmjge/
.container {
width: 100px;
height: 1px;
background-color: black;
}
.circle {
display: inline-block;
vertical-align: middle;
width: 10px;
height: 10px;
background-color: transparent;
border: solid 1px black;
border-radius: 50%;
}
<div class="container">
<div class="circle">
</div>
</div>
Moreover, I want that I'll not see the border line on the circle. Any suggestions?
A small amendment to your code to position the elements and you get the effect you want to achieve.
.container {
width: 100px;
height: 1px;
background-color: black;
position: relative;
}
.circle {
display: inline-block;
vertical-align: middle;
width: 10px;
height: 10px;
background-color: white;
border: solid 1px black;
border-radius: 50%;
position: absolute;
top: -6px;
left: calc(50% - 5px);
}
.blue {
margin-top: 20px;
background: #3EB2EF;
}
.blue .circle {
background: #3EB2EF;
border-color: #3EB2EF;
}
<div class="container">
<div class="circle">
</div>
</div>
<div class="container blue">
<div class="circle">
</div>
</div>
If you want to position an element depending on its parent, use position:relative for the parent and then add position relative or absolute to the child. to center something in the middle, use margin:0 auto and if it has absolute positioning also add left:0; right:0;
https://jsfiddle.net/azizn/e4ev3awj/1/
.container {
width: 100px;
height: 1px;
background-color: blue;
position:relative;
}
.circle {
display:inline-block;
width: 10px;
height: 10px;
position: absolute;
background:blue;
left:0;
right:0;
margin:0 auto;
border-radius: 100%;
top:-4px;
}
<div class="container">
<div class="circle">
</div>
</div>
a bit late to answer, but this looks like a typical <hr/> that needs some makup.
/* restyle however your needs are hr and its pseudo elements , here only one is used */
hr {
color: turquoise;
border-width: 3px;
margin: 1em;
box-shadow: 0 0 5px gray;
}
hr:before {
content: '';
border-radius: 100%;
position: absolute;
height: 20px;
width: 20px;
background: turquoise;
left: 50%;
margin: -10px;
box-shadow: inherit
}
<hr/>
Try this:
.container {
width: 100px;
height: 1px;
background-color: black;
position: relative;
}
.circle {
position: absolute;
top: -5px;
left: 50%;
margin-left: -5px;
display: inline-block;
vertical-align: middle;
width: 10px;
height: 10px;
background-color: transparent;
border: solid 1px black;
border-radius: 50%;
}
<div class="container">
<div class="circle">
</div>
</div>
Fiddle
This uses a lot of different codes then above.
class:before and class:after
Hope this helps you!

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

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.