perfect circle in css with border-radius doesn't work - html

the circle tend be oval, what I want is perfect circle. border-radius 100% isn't work I wonder why..
http://jsfiddle.net/8gD2m/1/
.badge {
display: inline-block;
min-width: 10px;
padding: 3px 7px;
font-size: 12px;
font-weight: lighter !important;
line-height: 1;
color: #fff !important;
text-align: center;
white-space: nowrap;
vertical-align: baseline;
background-color: #d73d33;
border-radius: 50px;
position: relative;
top: -3px;
}

Here is a JSfiddle with some changes:
JSFiddle for round badge
The main changes are:
padding: 0px;
width: 50px;
height: 50px;
line-height: 50px;
Having a line-height equal to the container height will center the text vertically. This only works if the text fits on a single line.
Edit: (copied code from JSFiddle)
.badge {
display: inline-block;
padding: 0;
width: 50px;
height: 50px;
line-height: 50px;
font-size: 12px;
font-weight: lighter !important;
color: #fff !important;
text-align: center;
white-space: nowrap;
vertical-align: baseline;
background-color: #d73d33;
border-radius:50px;
position: relative;
top: -3px;
}
<span class="badge badge-success">8</span>

if it's not perfect circle check display: inline-block and border-radius: 50%:
.cirlce {
height: 20px;
width: 20px;
padding: 5px;
text-align: center;
border-radius: 50%;
display: inline-block;
color:#fff;
font-size:1.1em;
font-weight:600;
background-color: rgba(0,0,0,0.1);
border: 1px solid rgba(0,0,0,0.2);
}

check this out
.badge {
display: inline-block;
min-width: 10px;
padding: 7px;
font-size: 12px;
font-weight: lighter !important;
line-height: 1;
color: #fff !important;
text-align: center;
white-space: nowrap;
vertical-align: baseline;
background-color: #d73d33;
border-radius: 50px;
position: relative;
top: -3px;
}

The main trick for making it a perfect circle is distributing the padding of the element(container) evenly => then setting border-radius: 50% or border-radius: 100%.
So you can get rid of the height and width declaration and use absolute positioning and padding to control the height and width
or same height and width and uniform padding value
.element-class {
Position: absolute;
padding: 10em or 10% or with any unit;
border-radius: 50% or 100%;
}
OR
.element-class {
height: 10em;
width: 10em;
padding: 10em; **
border-radius: 50%;
}

I had the same issue. When I added a 100% border-radius, my picture turned into an oval. That is because my picture is wider than it is tall. Imagine smoothing the edges of a rectangle. If you want your image to be circular, you have to make sure the height and width dimensions are the same. You could try setting them by doing the following:
css height: 200px; width: 200px; (so the point is having equal height and with in your CSS)
This will make sure that your image is circular, however, it may cause your image to stretch and become distorted because your originally image is NOT a perfect square I presume.

You can use vw unit for width and height.
Like the sample below:
div {
background-color: green;
width: 20vw;
height: 20vw;
border-radius: 100%;
text-align: center;
line-height: 20vw;
color: white;
}
<div>resize device</div>

Related

Only shrink flexbox horizontally

I've been trying to make a custom share button (for google classroom), and it seemed a flexbox was the best way to center the text. I'm making a share button, so I have an image and text. This is my current code for it:
a {
border-color: green;
border-style: solid;
color: white;
border-radius: 3px;
text-decoration: none;
background-color: green;
font-family: calibri;
display: flex-shrink;
justify-content: center;
align-items: center;
}
img {
padding: 5px;
height: 32px;
}
span {
padding: 5px;
}
<a href="https://classroom.google.com/share?url=http://example.com">
<img src="https://ktibow.github.io/classroom-logo.png"\>
<span>Share to Classroom</span>
</a>
It's supposed to make a nice rounded button, with the Google Classroom logo on the left and the text on the right. But instead, the background of the button shrinks down past the image.
I've tried using it without flex-shrink. Then it fills the whole page, which isn't how share buttons usually are, even though it does cover the background of the image. I've also tried setting the height of the span and the font size too. The height doesn't do anything, and if I do font size, then it gets cut off. I've tried flex-shrink: 0 too on the image. Does anybody know how to solve this?
New Design
I work with give the a tag display: inline-block
And move the img to position: absolute
I think it's look better now
a {
text-decoration: none;
display: inline-block;
margin: 10px 5px 10px 24px;
position: relative;
}
img {
position: absolute;
border-radius: 50%;
height: 38px;
top: -10px;
left: -18px;
}
span {
padding: 3px 5px 3px 24px;
background-color: green;
border-radius: 3px;
color: white;
font-family: calibri;
}
<a href="https://classroom.google.com/share?url=http://example.com">
<img src="https://ktibow.github.io/classroom-logo.png"\>
<span>Share to Classroom</span>
</a>
Or as ppl wrote with display: inline-flex
a {
text-decoration: none;
display: inline-flex;
align-items: center;
margin: 10px 5px 10px 24px;
background-color: green;
border-radius: 3px;
padding: 5px;
}
img {
height: 38px;
}
span {
padding: 5px;
color: white;
font-family: calibri;
}
<a href="https://classroom.google.com/share?url=http://example.com">
<img src="https://ktibow.github.io/classroom-logo.png"\>
<span>Share to Classroom</span>
</a>
a {
border-color: green;
border-style: solid;
color: white;
border-radius: 15px;
text-decoration: none;
background-color: green;
font-family: calibri;
display: flex-shrink;
justify-content: center align-items: center;
position: absolute;
display: flex;
}
img {
padding: 10px;
height: 30px;
}
span {
margin-top: 15px;
}
I don't know if this is what you are trying to achieve.
not the best way though

DIV's side by side within a container

I am working on an internal program for work that is essentially built on PHP. My problem is that I have a a header, a side navigation, the main content (to the right of the nav) and a footer. Rough Layout Picture
My issue is that I have two DIV's within a container, the nav is set to a percentage with a minimum width, and the content section is set to take the remaining space. In total both the nav and content should take about 91% of the screen real estate. Whats Happening after shrinking the browser a bit
My CSS looks like this for the fields I think are relevant:
.container{
width: 100%;
float: inline-block;
}
.header{
float: left;
text-align: left;
background-color: lightblue;
width: 100%;
vertical-align: middle;
display: block;
border-radius: 15px;
}
.header h1{
font-weight: bold;
font-size: 40px;
text-indent: 50px;
}
.msg_alert{
background-color: green;
color: white;
width: 95%;
padding: 5px;
border-radius: 10px;
}
.err_msg_alert{
background-color: red;
color: white;
width: 95%;
padding: 5px;
border-radius: 10px;
}
.menu{
float: left;
width: 13%;
border: 3px solid grey;
padding: 5px;
background-color: lightgrey;
border-radius: 15px;
margin-top: 20px;
margin-right: 20px;
min-width: 200px;
}
.menu a{
float: left;
color: black;
text-align: left;
padding: 14px;
text-decoration: none;
box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);
margin: 3px;
background-color: lightblue;
width: 40%;
min-width: 150px;
border-radius: 15px;
}
.menu a:hover{
background-color: grey;
color: black;
}
.menu ul{
list-style-type: none;
margin: 0;
padding: 0;
}
.menu li{
padding: 8px;
margin-bottom: 7px;
}
.content{
float: left;
width: 78%;
padding: 20px;
margin-top: 20px;
margin-left: 20px;
/*border: 3px solid red;*/
}
.footer{
display: inline-block;
width: 100%;
background-color: lightgrey;
border-radius: 15px;
font-size: 12px;
text-align: center;
margin-top: 5px;
padding-bottom: 10px;
}
I'm not sure what I've done wrong. Everything displays properly if the browser is in full screen but when I shrink it down to about 3/4's of the browser size the nav stays where it should be but the contents move below.
I have setup a mobile version which works perfectly but the desktop mode is what I am having issues with.
Thank you for the help in advance.
here is the solution-
<div class="header"></div>
<div class="menu_content">
<div class="menu"></div>
<div class="content"></div>
</div>
<div class="footer"></div>
.menu{
width: 13%;
border: 3px solid grey;
padding: 5px;
background-color: lightgrey;
border-radius: 15px;
margin-top: 20px;
margin-right: 20px;
min-width: 200px;
}
.content{
width: calc(100% - 21.7%);
padding: 20px;
margin-top: 20px;
margin-left: 20px;
border: 3px solid red;
}
.menu_content {
display: flex;
align-items: flex-start;
}
.menu_content::after {
content: '';
clear: both;
display: block;
}
Well, although the widths of .menu and .content might add up to 100% or less in a wider format, due to the min-width pixel setting of .menu, they will become wider than 100% when you decrease the window width, since the 200px of min-width: 200px; will become much more than the 13% width you define for it. So (since both are floats), .content will go below .menu, because there isn't enough space anymore for it next to .content.
To avoid that, you can wrap both of these in a div container and assign display: flex to that. Additionally, add flex-shrink: 0; (= allowed to get smaller) to .content. This should basically do the trick. (There are other details , but just check out some tutorial about flex - it's really not complicated at all.)
Another approach would be to define the menu width as 200px (fixed) and the width for .content as width: calc(100% -200px) - The full width of the parent minus 200px, whatever the width of the parent is.
(This doesn't calculate padding, margins etc. - you would have to consider that in the "real" values you use)

Create Button with toggle and text with a single element

I'm trying to create a button out of a single html element. The button needs to have a toggle slider and the text needs to be aligned vertically and horizontally. So I thought I can make use of :before element to help me make that happen. Here is what I have tried:
div {
width: 140px;
height: 40px;
background-color: #B3B3B3;
color: #FFF;
float: left;
clear: both;
border-radius: 5px;
font-size: 12px;
}
div:before {
content: '';
display: block;
width: 20px;
height: 36px;
background-color: #4D4D4D;
position: relative;
left: 2px;
top: 2px;
float: left;
border-radius: 5px;
}
<div>Text Value</div>
I have 2 problems with the above code:
I can't position the text how I want and I have tried using text-align and position to move it around.
I am using a float, which means that it will affect behavior of other elements around it, and I really don't want that.
Is what I want possible with a single element?
Here is the JS Fiddle: https://jsfiddle.net/m3q5Lcjy/
EDIT: The centered text should not be centered on the whole element, but on the light gray area.
This is how I would do this:
Array.from(document.querySelectorAll('.toggler')).forEach((item) => {
item.addEventListener('click', e => {
item.classList.toggle('active');
})
});
.toggler {
display: flex;
align-items: center;
justify-content: center;
position: relative;
box-sizing: border-box;
padding-left: 24px;
width: 140px;
min-height: 40px;
background-color: #B3B3B3;
color: #FFF;
border-radius: 5px;
font-size: 12px;
text-align: center;
cursor: pointer;
transition: padding .25s ease;
}
.toggler.active {
padding: 0 24px 0 0;
}
.toggler:before {
content: '';
display: block;
width: 20px;
background-color: #4D4D4D;
position: absolute;
bottom: 2px;
left: 2px;
top: 2px;
border-radius: 5px;
/* transition to make it look smoother */
transition: left .4s ease;
z-index: 1;
}
.toggler.active:before {
left: calc(100% - 22px);
}
<div class="toggler">Text Value</div>
<hr />
<div class="toggler active">Text Value realllllyy long</div>
<hr />
<div class="toggler">Text Value really far too long for this tiny, tiny, ohhh so tiny button. I recommend using shorter text though, but it won't break not even if you have like 10 or more lines.</div>
If anything about this implementation is unclear, feel free to ask.
Use flexbox to center your text vertically and horizontally. Then use absolute positioning on your pseudo element. Make sure parent element has relative positioning applied so absolute positioned pseudo stays within the parent.
div {
display: flex;
align-items: center;
justify-content: center;
position: relative;
box-sizing: border-box;
padding-left: 24px; /* 20px for :before width, 4px for :before offset */
width: 140px;
height: 40px;
background-color: #B3B3B3;
color: #FFF;
border-radius: 5px;
font-size: 12px;
}
div:before {
content: '';
display: block;
width: 20px;
height: 36px;
background-color: #4D4D4D;
position: absolute;
left: 2px;
top: 2px;
border-radius: 5px;
}
<div>Text Value</div>
You could place the text in a paragraph.
<div class="thediv">
<p class="theText">
enter text here
</p>
</div>
.thediv{
Your own style.
}
.theText{
position: absolute;
transform: translate(-50%, -50%);
top: 50%;
left: 50%;
}
I don't see why you would want it be in one element.
If you do want that, you should give the div a padding.
div{
background-color: #B3B3B3;
color: #FFF;
float: left;
border-radius: 5px;
font-size: 12px;
padding: 20px 70px;
}

Navigation bar breaks into a new line instead of resizing

So basically I have made a bunch of conflicting divs, but they all managed to work except for my navigation bar. I've tried sitting the different containers inside my top container to automatically fit to the screen by using percentage instead of pixels. But whenever I open up my site on smaller screens the icons in the bar ends up jumping down to the next line?
How do I achieve resizing the buttons and the image automatically so they can all fit into one line, by using pure CSS? Also the main div for the navigation bar is supposed to be above the background split.
If you take a look at my CSS I've also tried setting
.navbar>li {
display: inline-block;
This turned out to have no effect at all
CSS:
.top {
background-color: ;
margin-left: 3%;
margin-right: 3%;
margin: 0 auto;
}
.navbar {
background-color: #FDF3E7;
border-radius: 5px;
margin-left: 15%;
margin-right: 15%;
border: 10px double #C63D0F;
padding-top: 1px;
padding-left: 10%;
padding-right 10%;
padding-bottom: 0px;
position: relative;
display: inline-block;
width: 60%;
}
.navbar>li {
display: inline-block;
margin: 0 auto;
}
.hot-container {
min-height: 40px;
margin-top: 20px;
width:80%;
text-align: center;
position: relative;
display: inline-block;
margin: 0 auto;
}
a.btn {
display: inline-block;
color: #666;
background-color: #eee;
text-transform: uppercase;
letter-spacing: 2px;
font-size: 12px;
padding: 10px 15px;
border-radius: 2px;
-moz-border-radius: 2px;
-webkit-border-radius: 2px;
border: 1px solid rgba(0,0,0,0.3);
border-bottom-width: 1px;
position: relative;
width: 78%
}
Heres the fiddle: https://jsfiddle.net/z400tzkb/

Textarea padding inconsistency in Firefox and Chrome

I have a padding on my textarea element and I would like the content to remain padded as you scroll within the textarea. It is working as expected in Firefox but not in Chrome. The below image shows the difference in output:
CSS:
textarea {
width: 250px;
height: 160px;
padding: 15px;
font-family: Arial;
font-size: 12px;
line-height: 18px;
border: 1px solid #CCCCCC;
overflow: auto;
resize: none;
}
In Chrome, the top and bottom padding only appears at the beginning and end of the text content. Here is a jsfiddle to demonstrate:
http://jsfiddle.net/LkE6f/
How can I make the padding in Chrome appear/render in the same way as it does in Firefox?
You could do something like this, it's not very flexible (fixed width), but you can expand on it. It fixes the issue in Chrome and doesn't break Firefox. It uses pseudo-elements on #container, which work in IE8+
textarea {
width: 250px;
height: 160px;
padding: 15px;
font-family: Arial;
font-size: 12px;
line-height: 18px;
border: 1px solid #CCCCCC;
overflow: auto;
resize: none;
display: block;
}
#container:before, #container:after {
display: block;
height: 15px;
background-color: #FFF;
position: absolute;
left: 1px;
width: 225px;
content:'';
}
#container:before {
top: 1px;
}
#container:after {
bottom: 6px;
}
Here's a jsFiddle.
Update: Added display: block to textarea to fix IE positioning issue.
Update 2: Alternative solution which takes its width from the #container div and for which you'd need to set the right value based on the width of the scrollbar of the browser, the 17px value is ok in Chrome at the moment. A pro with this solution is that you can set the width of the textarea to anything by changing the width of the #container, and the pseudo-elements will scale accordingly. jsFiddle.
#container {
width: 260px;
margin: 20px auto;
position: relative;
}
textarea {
width: 100%;
height: 160px;
padding: 15px;
font-family: Arial;
font-size: 12px;
line-height: 18px;
border: 1px solid #CCCCCC;
overflow: auto;
resize: none;
display: block;
}
#container:before, #container:after {
display: block;
height: 15px;
background-color: #FFF;
position: absolute;
left: 1px;
right: 17px;
content:'';
}
#container:before {
top: 1px;
}
#container:after {
bottom: 1px;
}
Best answer:
Embrace the difference between browsers; the web is not uniform and your design will never be 100% identical across browsers.
Work around answers:
If you don't care about the scrollbar having a gap at the top and bottom, you can use borders and an outline like this.
OR
This can be achieved with a pseudo element, if you are happy wrapping each textarea in a div. Should display correctly on IE8+, FF and Chrome.
Have a fiddle!
HTML
<div class="textareaWrap">
<textarea>Content</textarea>
</div>
CSS
textarea {
position: relative;
width: 250px;
height: 160px;
font-family: Arial;
padding: 15px;
font-size: 12px;
line-height: 18px;
border: 1px solid #CCCCCC;
resize: none;
}
.textareaWrap {
position: relative;
}
.textareaWrap:after {
position: absolute;
content:'';
display: block;
width: 232px;
height: 15px;
background: #FFF;
z-index: 1;
bottom: 5px;
left: 1px;
}
.textareaWrap:before {
position: absolute;
content:'';
display: block;
width: 232px;
height: 15px;
background: #FFF;
z-index: 1;
top:1px;
left: 1px;
}
Try the below solution for the textarea
textarea {
-moz-appearance: textfield;
-moz-binding: url("chrome://global/content/platformHTMLBindings.xml#inputFields");
-moz-user-select: text;
background-color: -moz-field;
border: 2px inset threedface;
color: -moz-fieldtext;
cursor: text;
font: -moz-field;
width:250px;
height:150px;
letter-spacing: normal;
line-height: normal !important;
padding: 1px 0;
text-align: start;
text-indent: 0;
text-rendering: optimizelegibility;
text-shadow: none;
text-transform: none;
word-spacing: normal;
}
Fiddle link Link
Regards
Mahadevan