How to center an element (search form)? - html

Here's the code. How to center a search form? This form should be always at the center of the screen no matter what screen resolution is.
I tried margin: 0 auto; but it doesn't work.
Thank You.
.search-p--i {
color: #333;
font-weight: normal;
font-size: 20px;
text-align: center;
margin-bottom: 30px;
}
.search-p_wrap {
text-align: center;
}
.search-form button {
font-weight: bold;
color: #fff;
background: blue;
border: 0;
-webkit-border-radius: 28px;
-moz-border-radius: 28px;
border-radius: 28px;
padding: 10px 15px 10px 15px;
font-size: 14px;
}
.search-form input {
width: 240px;
height: 40px;
padding: 0px 5px;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4x;
border: 1px solid #ccc;
}
<div class="search-p">
<div class="search-p_wrap">
<h2 class="search-p_h2">Discover</h2>
<span class="search-p--i">Hi!</span>
</div>
<form class="search-form">
<input type="text" placeholder="Enter" required>
<button type="submit">Search</button>
</form>
</div>

Aligning text
Just add text-align: center to form and it is centered on all screen sizes.
text-align: center
This property describes how inline-level content of a block container is aligned.
form {
text-align: center; /* Add */
}
.search-p--i {
color: #333;
font-weight: normal;
font-size: 20px;
text-align: center;
margin-bottom: 30px;
}
.search-p_wrap {
text-align: center;
}
.search-form button {
font-weight: bold;
color: #fff;
background: blue;
border: 0;
-webkit-border-radius: 28px;
-moz-border-radius: 28px;
border-radius: 28px;
padding: 10px 15px 10px 15px;
font-size: 14px;
}
.search-form input {
width: 240px;
height: 40px;
padding: 0px 5px;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4x;
border: 1px solid #ccc;
}
<div class="search-p">
<div class="search-p_wrap">
<h2 class="search-p_h2">Discover</h2>
<span class="search-p--i">Hi!</span>
</div>
<form class="search-form">
<input type="text" placeholder="Enter" required>
<button type="submit">Search</button>
</form>
</div>
Flexbox solution(Modern):
align-items: center
Items are centered in the cross-axis
Note that in the example we are using flex-direction: column. otherwise just use justify-content: center for flex-direction: row
.search-p {
display: flex;
flex-direction: column; /* Cross axis alignment; simply said vertical stacking/positioning */
align-items: center; /* Center cross-axis */
}
.search-p--i {
color: #333;
font-weight: normal;
font-size: 20px;
text-align: center;
margin-bottom: 30px;
}
.search-p_wrap {
text-align: center;
}
.search-form button {
font-weight: bold;
color: #fff;
background: blue;
border: 0;
-webkit-border-radius: 28px;
-moz-border-radius: 28px;
border-radius: 28px;
padding: 10px 15px 10px 15px;
font-size: 14px;
}
.search-form input {
width: 240px;
height: 40px;
padding: 0px 5px;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4x;
border: 1px solid #ccc;
}
<div class="search-p">
<div class="search-p_wrap">
<h2 class="search-p_h2">Discover</h2>
<span class="search-p--i">Hi!</span>
</div>
<form class="search-form">
<input type="text" placeholder="Enter" required>
<button type="submit">Search</button>
</form>
</div>

I like to use the new Flexbox layout to place something in the center, a good guide is here: https://css-tricks.com/snippets/css/a-guide-to-flexbox/ Although it's rather new, but it's already supported in majority browsers and it's looking to become the new standard in the next year or so.
/* added new code */
.search-p {
display: flex;
flex-flow: column;
justify-content: center;
align-items: center;
}
.search-p--i {
color: #333;
font-weight: normal;
font-size: 20px;
text-align: center;
margin-bottom: 30px;
}
.search-p_wrap {
text-align: center;
}
.search-form button {
font-weight: bold;
color: #fff;
background: blue;
border: 0;
-webkit-border-radius: 28px;
-moz-border-radius: 28px;
border-radius: 28px;
padding: 10px 15px 10px 15px;
font-size: 14px;
}
.search-form input {
width: 240px;
height: 40px;
padding: 0px 5px;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4x;
border: 1px solid #ccc;
}
<div class="search-p">
<div class="search-p_wrap">
<h2 class="search-p_h2">Discover</h2>
<span class="search-p--i">Hi!</span>
</div>
<form class="search-form">
<input type="text" placeholder="Enter" required>
<button type="submit">Search</button>
</form>
</div>

CSS and form validation have nothing to do with each other, one is simply styling - the other is data checking which is handled either client-side by JS (bad idea without server validation) and / or server side by (always do this) so there will be no problem. (reacted on comment).
Some other problems might surface tho since text-align: center only works when elements are display: inline-block - float: left; float: right; display: block; will all break this center.
What I would suggest doing is adding a wrapper that will center the search in the form through means of margin: 0 auto - which is a much more solid way to center elements than text-align: center which as it says is meant for text (even tho I have also abused this property many many many times over).
.search-p--i {
color: #333;
font-weight: normal;
font-size: 20px;
text-align: center;
margin-bottom: 30px;
}
.search-p_wrap {
text-align: center;
}
/* Added a selector here */
.search-form .search-form-field-wrap {
display: block; /* optional - divs are block by default. */
width: 80%; /* or anything else */
margin: 0 auto; /* you're gonna want to use this for centering block elements */
}
.search-form button {
font-weight: bold;
color: #fff;
background: blue;
border: 0;
-webkit-border-radius: 28px;
-moz-border-radius: 28px;
border-radius: 28px;
padding: 10px 15px 10px 15px;
font-size: 14px;
}
.search-form input {
width: 240px;
height: 40px;
padding: 0px 5px;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4x;
border: 1px solid #ccc;
}
form {
text-align: center;
}
<div class="search-p">
<div class="search-p_wrap">
<h2 class="search-p_h2">Discover</h2>
<span class="search-p--i">Hi!</span>
</div>
<form class="search-form">
<!-- added a wrapper here -->
<div class="search-form-field-wrap">
<input type="text" placeholder="Enter" required>
<button type="submit">Search</button>
</div>
</form>
</div>

Related

How would I keep this forms contents from leaving the frame?

I made this simple css design for my web form, and the problem is that on mobile or any small screen the input and the captcha escape from the frame yet the submit button is just fine... I don't know how to fix this and I have little experience in css.
Here's what it looks like on mobile:
and here's my code:
* {
margin: 0px;
padding: 0px;
}
body {
font-size: 120%;
background: white;
}
form,
.dis {
width: 30%;
margin: 10px auto;
padding: 20px;
border: 3px solid #333333;
background: white;
border-radius: 5px 5px 10px 10px;
text-align: center;
}
input {
color: black;
padding: 5px;
width: auto;
border: 2px solid black;
border-radius: 10px 10px 10px 10px;
text-align: center;
background: white;
}
h2 {
width: 20%;
margin: 10px auto;
padding: 15px;
border: 2px solid red;
font-size: 75%;
color: red;
text-align: center;
}
<form id="dis" method="post">
<input name="username" type="username" placeholder="What's your Discord tag?" size="40"><br><br>
<input type="submit" name="submit" value="Submit"><br><br>
<center>
<div class="g-recaptcha" data-sitekey="6LfVs6sUAAAAAHi-Pm9QAKHNk_QskSE_-8F5zBrV"></div>
</center>
</form>
You can reset the value of your size attribute , or set a width to your input text. size="30" seems fine here , but to avoid side effects from font-size or family, i would advise to set also a max-width in your css.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/text#size
The size attribute is a numeric value indicating how many characters wide the input field should be. The value must be a number greater than zero, and the default value is 20. Since character widths vary, this may or may not be exact and should not be relied upon to be so; the resulting input may be narrower or wider than the specified number of characters, depending on the characters and the font (font settings in use).
* {
margin: 0px;
padding: 0px;
}
body {
font-size: 120%;
background: white;
}
form,
.dis {
width: 30%;
margin: 10px auto;
padding: 20px;
border: 3px solid #333333;
background: white;
border-radius: 5px 5px 10px 10px;
text-align: center;
}
input {
color: black;
padding: 5px;
width: auto;
border: 2px solid black;
border-radius: 10px 10px 10px 10px;
text-align: center;
background: white;
max-width:90%;
}
h2 {
width: 20%;
margin: 10px auto;
padding: 15px;
border: 2px solid red;
font-size: 75%;
color: red;
text-align: center;
}
<form id="dis" method="post">
<input name="username" type="username" placeholder="What's your Discord tag?" size="30"><br><br>
<input type="submit" name="submit" value="Submit"><br><br>
<center>
<div class="g-recaptcha" data-sitekey="6LfVs6sUAAAAAHi-Pm9QAKHNk_QskSE_-8F5zBrV"></div>
</center>
</form>
The stylish rule you are missing on your css input is the max-width property :
* {
margin: 0px;
padding: 0px;
}
body {
font-size: 120%;
background: white;
}
form,
.dis {
width: 30%;
margin: 10px auto;
padding: 20px;
border: 3px solid #333333;
background: white;
border-radius: 5px 5px 10px 10px;
text-align: center;
}
input {
color: black;
padding: 5px;
width: auto;
max-width: 95%;
border: 2px solid black;
border-radius: 10px 10px 10px 10px;
text-align: center;
background: white;
}
h2 {
width: 20%;
margin: 10px auto;
padding: 15px;
border: 2px solid red;
font-size: 75%;
color: red;
text-align: center;
}
<form id="dis" method="post">
<input name="username" type="username" placeholder="What's your Discord tag?" size="30"><br><br>
<input type="submit" name="submit" value="Submit"><br><br>
<center>
<div class="g-recaptcha" data-sitekey="6LfVs6sUAAAAAHi-Pm9QAKHNk_QskSE_-8F5zBrV"></div>
</center>
</form>
Try to use this, all I have done just adding box-sizing: border-box and width: 100% into input
* {
margin: 0px;
padding: 0px;
box-sizing: border-box;
}
body {
font-size: 120%;
background: white;
}
#media only screen and (max-width: 999px) {
form,
.dis {
width: 30%;
margin: 10px auto;
padding: 20px;
border: 3px solid #333333;
background: white;
border-radius: 5px 5px 10px 10px;
text-align: center;
}
}
#media only screen and (min-width: 1000px) {
form,
.dis {
width: 300px;
margin: 10px auto;
padding: 20px;
border: 3px solid #333333;
background: white;
border-radius: 5px 5px 10px 10px;
text-align: center;
}
}
input {
color: black;
padding: 5px;
width: 100%;
border: 2px solid black;
border-radius: 10px 10px 10px 10px;
text-align: center;
background: white;
}
h2 {
width: 20%;
margin: 10px auto;
padding: 15px;
border: 2px solid red;
font-size: 75%;
color: red;
text-align: center;
}
<form id="dis" method="post">
<input name="username" type="username" placeholder="What's your Discord tag?" size="40"><br><br>
<input type="submit" name="submit" value="Submit"><br><br>
<center>
<div class="g-recaptcha" data-sitekey="6LfVs6sUAAAAAHi-Pm9QAKHNk_QskSE_-8F5zBrV"></div>
</center>
</form>

Moving Button to the right

I have an HTML Page with a CSS. I want to align some buttons to the right. Even though in the normal HTML pages without this CSS template, the button would move to the right, it does not work anymore. Same is the case for similar buttons on the page. Align to the center does not work either.
This is what the page looks like:
input.logout {
width: 100px;
padding: 7px;
cursor: pointer;
font-weight: bold;
font-size: 80%;
background: #3366cc;
color: #fff;
border: 1px solid #3366cc;
border-radius: 10px;
}
input.logout:hover {
color: #ffff;
background: #000;
border: 1px solid #fff;
}
<form align="right">
<input class="logout" type="button" value="Logout" onclick="window.location.href='logout.php'" />
</form>
If you are just going to have this .logout button on that line, you can just make it a block-level element with display: block, and add margin-left: auto so it is pushed to the right side; without using floats, since those can cause issues down the line.
input.logout {
width: 100px;
padding: 7px;
cursor: pointer;
font-weight: bold;
font-size: 80%;
background: #3366cc;
color: #fff;
border: 1px solid #3366cc;
border-radius: 10px;
display: block;
margin-left: auto;
}
input.logout:hover {
color: #ffff;
background: #000;
border: 1px solid #fff;
}
<form>
<input class="logout" type="button" value="Logout" onclick="window.location.href='logout.php'" />
</form>
If you are going to have multiple buttons on the same line, maybe Flexbox would be the way to go instead:
.flex {
display: flex;
justify-content: flex-end;
}
input.logout {
width: 100px;
padding: 7px;
cursor: pointer;
font-weight: bold;
font-size: 80%;
background: #3366cc;
color: #fff;
border: 1px solid #3366cc;
border-radius: 10px;
margin-left: 5px;
}
input.logout:hover {
color: #ffff;
background: #000;
border: 1px solid #fff;
}
<form>
<div class="flex">
<input class="logout" type="button" value="Logout" onclick="window.location.href='logout.php'" />
<input class="logout" type="button" value="Logout" onclick="window.location.href='logout.php'" />
</div>
</form>
Just add float:right to your button css
input.logout {
width: 100px;
padding: 7px;
cursor: pointer;
font-weight: bold;
font-size: 80%;
background: #3366cc;
color: #fff;
border: 1px solid #3366cc;
border-radius: 10px;
float: right;
}
input.logout:hover {
color: #ffff;
background: #000;
border: 1px solid #fff;
}
<form align="right">
<input class="logout" type="button" value="Logout"onclick="window.location.href='logout.php'" />
</form>

Alignment of two components for an input field (Label + Field)

This is my current input field styling however for some reason which I am unable to see, they are not aligning properly next to each other.
Code:
<div class="labelInputField">Confirm Password</div><input type="text" class="inputField">
.labelInputField {
border-top-left-radius: 5px;
border-bottom-left-radius: 5px;
padding: 8px;
width: 150px;
color: white;
background: rgba(0,0,0,1);
border: 2px solid #000;
display: inline-block;
height: 30px;
margin-top: 15px;
font-size: 16px;
}
.inputField {
border-top-right-radius: 5px;
border-bottom-right-radius: 5px;
padding: 8px;
width: 180px;
color: white;
background: rgba(0,0,0,0.4);
border: 2px solid #000;
display: inline-block;
height: 30px;
font-size: 16px;
}
JSFiddle:
https://jsfiddle.net/0Lsake9j/
Any solutions?
Remove margin-top:15px and add line-height:30px; in .labelInputField style. That will make your div and input alignment proper.
.labelInputField {
border-top-left-radius: 5px;
border-bottom-left-radius: 5px;
padding: 8px;
width: 150px;
color: white;
background: rgba(0,0,0,1);
border: 2px solid #000;
display: inline-block;
height: 30px;
line-height:30px;
font-size: 16px;
}
.inputField {
border-top-right-radius: 5px;
border-bottom-right-radius: 5px;
padding: 8px;
width: 180px;
color: white;
background: rgba(0,0,0,0.4);
border: 2px solid #000;
display: inline-block;
height: 30px;
font-size: 16px;
}
<div class="labelInputField">Confirm Password</div><input type="text" class="inputField">
You can also test it here
Offload layout responsibility to a container and/or additional layers of markup.
Remove margin-top on the div
Use a <label for="[YOUR_INPUT_ID]"></label> and ensure your input has id="[YOUR_INPUT_ID]".
Add type="password" on your input, unless you have a specific reason not to.
Remove existing layouts styles from selectors that are styling the input and label (i.e. inline-block in this case.).
Apply your layout styling with an object that's sole purpose is to handle layout of your elements
.iHandleLayout {
display: flex;
/* rest of your layout styling etc. */
}
.labelInputField {
border-top-left-radius: 5px;
border-bottom-left-radius: 5px;
padding: 8px;
min-width: 150px;
color: white;
background: rgba(0,0,0,1);
border: 2px solid #000;
font-size: 16px;
}
.inputField {
border-top-right-radius: 5px;
border-bottom-right-radius: 5px;
padding: 8px;
width: 180px;
color: white;
background: rgba(0,0,0,0.4);
border: 2px solid #000;
font-size: 16px;
}
<div class="iHandleLayout">
<label class="labelInputField" for="password">Confirm Password</label>
<input type="password" class="inputField" id="password">
</div>
In the end, there are many ways to control the layout (display: inline-block, flex, table, etc), but the main principle is the separation of concerns with your styling and the markup structure as part of that separation.

How to align input element in the middle of div? [duplicate]

This question already has answers here:
How can I vertically align elements in a div?
(28 answers)
Closed 7 years ago.
For below html code,
.shoppingform {
width: 400px;
height: 800px;
background: #7CB9E8;
/* url(some img)*/
padding-left: 15px;
padding-top: 10px;
color: white;
font-size: 12px;
font-weight: bold;
border-radius: 5px;
}
.customername {
border: 1px solid white;
color: black;
font-weight: normal;
padding: 10px 2px 5px 5px;
background: #B284BE;
width: 90%;
border-radius: 5px;
}
.customername {
height: 5%;
}
.customername {
margin-top: 5px;
}
.shoppingform > div > input {
border-radius: 5px;
width: 60%;
}
.formlabel {
display: inline-block;
width: 30%;
}
<form class="shoppingform" action="someaction.php" method="get" enctype="multipart/form-data">
Step1: Your details
<br>
<div class="customername">
<label class="formlabel">Name:</label>
<input type="text">
</div>
</form>
There are multiple div elements(like customername), which above code does not have,to make question simple.
label and input text are towards top side of the div container.
How do I vertically align the label and input text in the middle of the div container? To add, there are multiple div elements in the form.
Modified your code a little to have your elements vertically aligned as suggested.
However I do advice you to think about your element positioning better, this form will likely not be good in terms of responsive behavior and layout.
.shoppingform {
width: 400px;
height: 800px;
background: #7CB9E8;
/* url(some img)*/
padding-left: 15px;
padding-top: 10px;
color: white;
font-size: 12px;
font-weight: bold;
border-radius: 5px;
text-align: center;
}
.customername {
border: 1px solid white;
color: black;
font-weight: normal;
padding: 10px 2px 5px 5px;
background: #B284BE;
width: 90%;
border-radius: 5px;
}
.customername {
height: 5%;
}
.customername {
margin-top: 5px;
}
.shoppingform > div > input {
border-radius: 5px;
width: 60%;
}
.formlabel {
display: inline-block;
width: 30%;
}
<form class="shoppingform" action="someaction.php" method="get" enctype="multipart/form-data">
Step1: Your details
<br/>
<div class="customername">
<label class="formlabel">Name:</label>
<br/>
<input type="text">
</div>
</form>
.shoppingform {
width: 400px;
height: 800px;
background: #7CB9E8;
padding-left: 15px;
color: white;
font-size: 12px;
font-weight: bold;
border-radius: 5px;
padding-top: 47.5%;
}
.customername {
margin: auto;
border: 1px solid white;
color: black;
font-weight: normal;
padding: 10px 2px 5px 5px;
background: #B284BE;
width: 90%;
border-radius: 5px;
height: 5%;
margin-top: 5px;
}
.shoppingform > div > input {
border-radius: 5px;
width: 60%;
}
.formlabel {
display: inline-block;
width: 30%;
}
<form class="shoppingform" action="someaction.php" method="get" enctype="multipart/form-data">
Step1: Your details
<br>
<div class="customername">
<label class="formlabel">Name:</label>
<input type="text">
</div>
</form>

How to use CSS to surround a number with a circle?

I would like to surround a number in a circle like in this image:
Is this possible and how is it achieved?
Here's a demo on JSFiddle and a snippet:
.numberCircle {
border-radius: 50%;
width: 36px;
height: 36px;
padding: 8px;
background: #fff;
border: 2px solid #666;
color: #666;
text-align: center;
font: 32px Arial, sans-serif;
}
<div class="numberCircle">30</div>
My answer is a good starting point, some of the other answers provide flexibility for different situations. If you care about IE8, look at the old version of my answer.
The problem with most of the other answers here is you need to tweak the size of the outer container so that it is the perfect size based on the font size and number of characters to be displayed. If you are mixing 1 digit numbers and 4 digit numbers, it won't work. If the ratio between the font size and the circle size isn't perfect, you'll either end up with an oval or a small number vertically aligned at the top of a large circle. This should work fine for any amount of text and any size circle. Just set the width and line-height to the same value:
.numberCircle {
width: 120px;
line-height: 120px;
border-radius: 50%;
text-align: center;
font-size: 32px;
border: 2px solid #666;
}
<div class="numberCircle">1</div>
<div class="numberCircle">100</div>
<div class="numberCircle">10000</div>
<div class="numberCircle">1000000</div>
If you need to make the content longer or shorter, all you need to do is adjust the width of the container for a better fit.
See it on JSFiddle.
For circle sizes varying based on the content this should work:
.numberCircle {
display: inline-block;
line-height: 0px;
border-radius: 50%;
border: 2px solid;
font-size: 32px;
}
.numberCircle span {
display: inline-block;
padding-top: 50%;
padding-bottom: 50%;
margin-left: 8px;
margin-right: 8px;
}
<span class="numberCircle"><span>30</span></span>
<span class="numberCircle"><span>1</span></span>
<span class="numberCircle"><span>5435</span></span>
<span class="numberCircle"><span>2</span></span>
<span class="numberCircle"><span>100</span></span>
It relies on the width of the content plus the margin-'s to determine the radius, then extends the height to match using the padding-'s. The margin-'s would need to be adjusted based on the font-size.
Update to remove inner element:
.numberCircle {
display: inline-block;
border-radius: 50%;
border: 2px solid;
font-size: 32px;
}
.numberCircle:before,
.numberCircle:after {
content: '\200B';
display: inline-block;
line-height: 0px;
padding-top: 50%;
padding-bottom: 50%;
}
.numberCircle:before {
padding-left: 8px;
}
.numberCircle:after {
padding-right: 8px;
}
<span class="numberCircle">30</span>
<span class="numberCircle">1</span>
<span class="numberCircle">5435</span>
<span class="numberCircle">2</span>
<span class="numberCircle">100</span>
Uses pseudo-elements to force the height. Need the zero width space for vertical alignment. Moved the line-height:0px from the outer to the pseudo so that it is at least visible when degrading for IE8.
If it's 20 and lower, you can just use the unicode characters ① ② ... ⑳
http://www.alanwood.net/unicode/enclosed_alphanumerics.html
the easiest way is using bootstrap and badge class
<span class="badge">1</span>
This version does not rely on hard-coded, fixed values but sizes relative to the font-size of the div.
http://jsfiddle.net/qod1vstv/
CSS:
.numberCircle {
font: 32px Arial, sans-serif;
width: 2em;
height: 2em;
box-sizing: initial;
background: #fff;
border: 0.1em solid #666;
color: #666;
text-align: center;
border-radius: 50%;
line-height: 2em;
box-sizing: content-box;
}
HTML:
<div class="numberCircle">30</div>
<div class="numberCircle" style="font-size: 60px">1</div>
<div class="numberCircle" style="font-size: 12px">2</div>
You can use the border-radius for this:
<html>
<head>
<style type="text/css">
.round
{
-moz-border-radius: 15px;
border-radius: 15px;
padding: 5px;
border: 1px solid #000;
}
</style>
</head>
<body>
<span class="round">30</span>
</body>
</html>
Play with the border radius and the padding values until you are satisfied with the result.
But this won't work in all browsers. I guess IE still does not support rounded corners.
I am surprised nobody used flex which is easier to understand, so I put my version of answer here:
To create a circle, make sure width equals height
To adapt to font-size of number in the circle, use em rather than px
To center the number in the circle, use flex with justify-content: center; align-items: center;
if the number grows (>1000 for example), increase the width and height at same time
Here is an example:
.circled-number {
color: #666;
border: 2px solid #666;
border-radius: 50%;
font-size: 1rem;
display: flex;
justify-content: center;
align-items: center;
width: 2em;
height: 2em;
}
.circled-number--big {
color: #666;
border: 2px solid #666;
border-radius: 50%;
font-size: 1rem;
display: flex;
justify-content: center;
align-items: center;
width: 4em;
height: 4em;
}
<div class="circled-number">
30
</div>
<div class="circled-number--big">
3000000
</div>
Late to the party, but here is a bootstrap-only solution that has worked for me. I'm using Bootstrap 4:
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<body>
<div class="row mt-4">
<div class="col-md-12">
<span class="bg-dark text-white rounded-circle px-3 py-1 mx-2 h3">1</span>
<span class="bg-dark text-white rounded-circle px-3 py-1 mx-2 h3">2</span>
<span class="bg-dark text-white rounded-circle px-3 py-1 mx-2 h3">3</span>
</div>
</div>
</body>
You basically add bg-dark text-white rounded-circle px-3 py-1 mx-2 h3 classes to your <span> (or whatever) element and you're done.
Note that you might need to adjust margin and padding classes if your content has more than one digits.
My solution here - this easily allows for different sizes and colors and ties into a CMS for editorial control. For IE degrading to squares.
HTML:
<div class="circular-label label-outer label-size-large label-color-pink">
<div class="label-inner">
<span>Fashion & Beauty</span>
</div>
</div>
CSS:
.circular-label {
overflow: hidden;
z-index: 100;
vertical-align: middle;
font-size: 11px;
-webkit-box-shadow:0 3px 3px rgba(0, 0, 0, 0.2);
-moz-box-shadow:0 3px 3px rgba(0, 0, 0, 0.2);
box-shadow: 3px 3px 3px rgba(0, 0, 0, 0.2);
}
.label-inner {
width: 85%;
height: 85%;
-moz-border-radius: 50%;
-webkit-border-radius: 50%;
border-radius: 50%;
border: 2px dotted white;
vertical-align: middle;
margin: auto;
top: 5%;
position: relative;
overflow: hidden;
}
.label-inner > span {
display: table;
text-align: center;
vertical-align: middle;
font-weight: bold;
text-transform: uppercase;
width: 100%;
position: absolute;
margin: auto;
margin-top: 38%;
font-family:'ProximaNovaLtSemibold';
font-size: 13px;
line-height: 1.0em;
}
.circular-label.label-size-large {
width: 110px;
height: 110px;
-moz-border-radius: 55px;
-webkit-border-radius: 55px;
border-radius: 55px;
margin-top:-55px;
}
.circular-label.label-size-med {
width: 76px;
height: 76px;
-moz-border-radius: 38px;
-webkit-border-radius: 38px;
border-radius: 38px;
margin-top:-38px;
}
.circular-label.label-size-med .label-inner > span {
margin-top: 33%;
}
.circular-label.label-size-small {
width: 66px;
height: 66px;
-moz-border-radius: 33px;
-webkit-border-radius: 33px;
border-radius: 33px;
margin-top:-33px;
}
It's not too difficult to see how to do this. The bigger question is whether it is possible to make the dimensions of the circle scale to content.
Currently I don't think it is possible. Anyone?
Here's a demo on JSFiddle and a snippet:
/* Creating a number within a circle using CSS */
.numberCircle {
font-family: "OpenSans-Semibold", Arial, "Helvetica Neue", Helvetica, sans-serif;
display: inline-block;
color: #fff;
text-align: center;
line-height: 0px;
border-radius: 50%;
font-size: 12px;
min-width: 38px;
min-height: 38px;
}
.numberCircle span {
display: inline-block;
padding-top: 50%;
padding-bottom: 50%;
margin-left: 1px;
margin-right: 1px;
}
/* Some Back Ground Colors */
.clrGreen {
background: #51a529;
}
.clrRose {
background: #e6568b;
}
.clrOrange {
background: #ec8234;
}
.clrBlueciel {
background: #21adfc;
}
.clrMauve {
background: #7b5d99;
}
<span class="numberCircle clrGreen"><span>8</span></span>
<span class="numberCircle clrRose"><span>80</span></span>
<span class="numberCircle clrOrange"><span>800</span></span>
<span class="numberCircle clrMauve"><span>8000</span></span>
.numberCircle {
border-radius: 50%;
width: 40px;
height: 40px;
display: block;
float: left;
border: 2px solid #000000;
color: #000000;
text-align: center;
margin-right: 5px;
}
<h3><span class="numberCircle">1</span> Regiones del Interior</h3>
Late to the party but here's the solution I went with https://codepen.io/jnbruno/pen/vNpPpW
Required no extra work.
Thanks John Noel Bruno
.btn-circle.btn-xl {
width: 70px;
height: 70px;
padding: 10px 16px;
border-radius: 35px;
font-size: 24px;
line-height: 1.33;
}
.btn-circle {
width: 30px;
height: 30px;
padding: 6px 0px;
border-radius: 15px;
text-align: center;
font-size: 12px;
line-height: 1.42857;
}
<div class="panel-body">
<h4>Normal Circle Buttons</h4>
<button type="button" class="btn btn-default btn-circle">
<i class="fa fa-check"></i>
</button>
<button type="button" class="btn btn-primary btn-circle">
<i class="fa fa-list"></i>
</button>
</div>
Do something like this in your css
div {
width: 10em; height: 10em;
-webkit-border-radius: 5em; -moz-border-radius: 5em;
}
p {
text-align: center; margin-top: 4.5em;
}
Use the paragraph tag to write the text. Hope that helps
Improving the first answer just get rid of the padding and add line-height and vertical-align:
.numberCircle {
border-radius: 50%;
width: 36px;
height: 36px;
line-height: 36px;
vertical-align:middle;
background: #fff;
border: 2px solid #666;
color: #666;
text-align: center;
font: 32px Arial, sans-serif;
}
The answer of thirtydot is right but is missing a little point. You need to add position: relative , if you want to have centered value in the circle and include also different range of number.
For example 123;
HTML:
<div class="numberCircle">30</div>
CSS:
.numberCircle {
border-radius: 50%;
behavior: url(PIE.htc); /* remove if you don't care about IE8 */
width: 36px;
height: 36px;
padding: 8px;
position: relative;
background: #fff;
border: 2px solid #666;
color: #666;
text-align: center;
font: 32px Arial, sans-serif;
}
but an easiest solution is to use Bootstrap
<span class="badge" style ="float:right">123</span>
Heres my way of doing it, using square method. upside is it works with different values, but you need 2 spans.
.circle {
display: inline-block;
border: 1px solid black;
border-radius: 50%;
position: relative;
padding: 5px;
}
.circle::after {
content: '';
display: block;
padding-bottom: 100%;
height: 0;
opacity: 0;
}
.num {
position: absolute;
top: 50%;
transform: translateY(-50%);
}
.width_holder {
display: block;
height: 0;
overflow: hidden;
}
<div class="circle">
<span class="width_holder">1</span>
<span class="num">1</span>
</div>
<div class="circle">
<span class="width_holder">11</span>
<span class="num">11</span>
</div>
<div class="circle">
<span class="width_holder">11111</span>
<span class="num">11111</span>
</div>
<div class="circle">
<span class="width_holder">11111111</span>
<span class="num">11111111</span>
</div>
You can use
span.red {
background: red;
border-radius: 0.8em;
-moz-border-radius: 0.8em;
-webkit-border-radius: 0.8em;
color: #ffffff;
display: inline-block;
font-weight: bold;
line-height: 1.6em;
margin-right: 15px;
text-align: center;
width: 1.6em;
}
span.grey {
background: #cccccc;
border-radius: 0.8em;
-moz-border-radius: 0.8em;
-webkit-border-radius: 0.8em;
color: #fff;
display: inline-block;
font-weight: bold;
line-height: 1.6em;
margin-right: 15px;
text-align: center;
width: 1.6em;
}
span.green {
background: #5EA226;
border-radius: 0.8em;
-moz-border-radius: 0.8em;
-webkit-border-radius: 0.8em;
color: #ffffff;
display: inline-block;
font-weight: bold;
line-height: 1.6em;
margin-right: 15px;
text-align: center;
width: 1.6em;
}
span.blue {
background: #5178D0;
border-radius: 0.8em;
-moz-border-radius: 0.8em;
-webkit-border-radius: 0.8em;
color: #ffffff;
display: inline-block;
font-weight: bold;
line-height: 1.6em;
margin-right: 15px;
text-align: center;
width: 1.6em;
}
span.pink {
background: #EF0BD8;
border-radius: 0.8em;
-moz-border-radius: 0.8em;
-webkit-border-radius: 0.8em;
color: #ffffff;
display: inline-block;
font-weight: bold;
line-height: 1.6em;
margin-right: 15px;
text-align: center;
width: 1.6em;
}
<h1><span class="grey">1</span>A grey circle with number inside</h1>
<h1><span class="red">2</span>A red circle with number inside</h1>
<h1><span class="blue">3</span>A blue circle with number inside</h1>
<h1><span class="green">4</span>A green circle with number inside</h1>
<h1><span class="pink">5</span>A pink circle with number inside</h1>
Thank to https://wpsites.net/web-design/colored-numbered-circles-using-pure-css-html/
Something like this could work (for numbers 0 to 99):
.circle {
border: 0.1em solid grey;
border-radius: 100%;
height: 2em;
width: 2em;
text-align: center;
}
.circle p {
margin-top: 0.10em;
font-size: 1.5em;
font-weight: bold;
font-family: sans-serif;
color: grey;
}
<body>
<div class="circle">
<p>30</p>
</div>
</body>
You work like with a standard block, that is a square
This is feature of CSS 3 and it is not very well suporrted, you can count on firefox and safari for sure.
.circle {
width: 10em;
height: 10em;
-webkit-border-radius: 5em;
-moz-border-radius: 5em;
border: 1px solid black;
}
<div class="circle"><span>1234</span></div>