How can I align to center my button in CSS? - css

I have the following button in CSS, and I can't align it to center:
.btn {
background-color: #44c767;
-moz-border-radius: 28px;
-webkit-border-radius: 28px;
border-radius: 28px;
border: 1px solid #18ab29;
display: inline-block;
cursor: pointer;
color: #ffffff;
font-family: 'Raleway', sans-serif;
font-size: 17px;
padding: 16px 64px;
text-decoration: none;
text-shadow: 0px 1px 0px #2f6627;
}
And when it goes to center, I want it to go a little bit lower(bottom).

Centering the button???
1. Horizontally center the button inside a parent?
You can use display: flex; justify-content: center on parent element.
.parent{
display: flex;
justify-content: center;
}
.btn {
background-color: #44c767;
-moz-border-radius: 28px;
-webkit-border-radius: 28px;
border-radius: 28px;
border: 1px solid #18ab29;
display: inline-block;
cursor: pointer;
color: #ffffff;
font-family: 'Raleway', sans-serif;
font-size: 17px;
padding: 16px 64px;
text-decoration: none;
text-shadow: 0px 1px 0px #2f6627;
}
<div class="parent">
<button class="btn">Button</button>
</div>
OR You can also use text-align: center on parent element.
.parent{
text-align: center;
}
.btn {
background-color: #44c767;
-moz-border-radius: 28px;
-webkit-border-radius: 28px;
border-radius: 28px;
border: 1px solid #18ab29;
display: inline-block;
cursor: pointer;
color: #ffffff;
font-family: 'Raleway', sans-serif;
font-size: 17px;
padding: 16px 64px;
text-decoration: none;
text-shadow: 0px 1px 0px #2f6627;
}
<div class="parent">
<button class="btn">Button</button>
</div>
2. Vertically center the button inside a parent?
You can use display: flex; align-items: center on parent element.
.parent{
height: 100vh;
background: #f00;
display: flex;
align-items: center;
}
.btn {
background-color: #44c767;
-moz-border-radius: 28px;
-webkit-border-radius: 28px;
border-radius: 28px;
border: 1px solid #18ab29;
display: inline-block;
cursor: pointer;
color: #ffffff;
font-family: 'Raleway', sans-serif;
font-size: 17px;
padding: 16px 64px;
text-decoration: none;
text-shadow: 0px 1px 0px #2f6627;
}
<div class="parent">
<button class="btn">Button</button>
</div>
3. Horizontally and Vertically center the button inside a parent?
You can use display: flex; justify-content: center; align-items: center on parent element.
.parent{
height: 100vh;
background: #f00;
align-items: center;
display: flex;
justify-content: center;
}
.btn {
background-color: #44c767;
-moz-border-radius: 28px;
-webkit-border-radius: 28px;
border-radius: 28px;
border: 1px solid #18ab29;
display: inline-block;
cursor: pointer;
color: #ffffff;
font-family: 'Raleway', sans-serif;
font-size: 17px;
padding: 16px 64px;
text-decoration: none;
text-shadow: 0px 1px 0px #2f6627;
}
<div class="parent">
<button class="btn">Button</button>
</div>
=> Move the button a little-bit-lower inside a parent as well as keeping it slightly centered?
You can use margin-top: 5vh; on button.
.parent{
height: 100vh;
background: #f00;
align-items: center;
display: flex;
justify-content: center;
}
.btn {
margin-top: 50vh;
background-color: #44c767;
-moz-border-radius: 28px;
-webkit-border-radius: 28px;
border-radius: 28px;
border: 1px solid #18ab29;
display: inline-block;
cursor: pointer;
color: #ffffff;
font-family: 'Raleway', sans-serif;
font-size: 17px;
padding: 16px 64px;
text-decoration: none;
text-shadow: 0px 1px 0px #2f6627;
}
<div class="parent">
<button class="btn">Button</button>
</div>

Related

Positioning an icon on the border of a "dynamic" div

Context
I'm trying to show an icon icon-delete-2a.png on the border of a div.
I can't seem to bring the icon forward using z-index.
Furthermore I am also looking at 'the best way' to dynamically position the icon using CSS. When innerText would equal "TestTestTest", the width of my userItem is adjusted, but the icon should shift to the right as well. Calculating the length of the userItem with JS to then adjust it's style ain't that practical.
Related issues
Several other people asked about this, but unfortunately, the proposed solutions (setting position: relative; on the parent and setting position: absolute; z-index: 1 on the child) didn't seem to resolve my issue. See:
CSS - Add icon on left of border with relative position
z-index not working with fixed positioning
Position div on the border of another div
Minimal Working Example
.useritem {
position: relative;
height: 15.625px;
padding-left: 6.25px;
padding-right: 6.25px;
display: inline-block;
text-align: center;
font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
font-size: 10px;
font-weight: 400;
line-height: 1.5;
color: #ffffff;
background: #ff0000;
/* Center slide text vertically */
align-items: center;
border: 1px solid #ff0000;
border-radius: 10px;
margin-bottom: 1px;
margin-right: 3px;
overflow: auto;
word-wrap: break-word;
}
.icon_delete {
position: absolute;
bottom: 7.5px;
left: 20px;
display: flex;
align-items: center;
justify-content: center;
z-index: 1;
}
.useritem:focus {
background: #ffffff;
color: #000000;
border-radius: 10px;
border: 1px solid #000000;
}
[contenteditable] {
outline: 0px solid transparent;
}
<body>
<div class="useritems">
<div class="row">
<div class="useritem" id="Location_Explore_UserItem_00" contenteditable="true" style="border: 2px solid black; background: rgb(255, 255, 255); color: rgb(0, 0, 0);">Test<img class="icon_delete" src="https://i.ibb.co/jTQckJm/icon-delete-2a.png" width="15" height="15">
</div>
</div>
</div>
</body>
This happens because of overflow attribute. Set the overflow value to initial or scroll on .useritem and you will see the icon outside.
.useritem {
position: relative;
height: 15.625px;
padding-left: 6.25px;
padding-right: 6.25px;
display: inline-block;
text-align: center;
font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
font-size: 10px;
font-weight: 400;
line-height: 1.5;
color: #ffffff;
background: #ff0000;
/* Center slide text vertically */
align-items: center;
border: 1px solid #ff0000;
border-radius: 10px;
margin-bottom: 1px;
margin-right: 3px;
word-wrap: break-word;
overflow: initial;
}
.icon_delete {
position: absolute;
bottom: 7.5px;
right: -8px;
display: flex;
align-items: center;
justify-content: center;
z-index: 1;
}
.useritem:focus {
background: #ffffff;
color: #000000;
border-radius: 10px;
border: 1px solid #000000;
}
[contenteditable] {
outline: 0px solid transparent;
}
<body>
<div class="useritems">
<div class="row">
<div class="useritem" id="Location_Explore_UserItem_00" contenteditable="true" style="border: 2px solid black; background: rgb(255, 255, 255); color: rgb(0, 0, 0);">Test<img class="icon_delete" src="https://i.ibb.co/jTQckJm/icon-delete-2a.png" width="15" height="15">
</div>
</div>
</div>
</body>
You have used "overflow" css that's why it's not showing. Remove overflow css you can see your icon position.

Adding a fav icon in a button using only CSS

https://codepen.io/danielworton/pen/ZZLEMO
Hello, I'm trying to re-create the button in the codepen linked above and I have an issue adding the stars without changing the script. Also the different colored double border ~ If anyone has any clue to a solution it would be very welcome!
#My solution is as follows:
a {
border: solid 3px #212121;
border-radius: 12px;
background-image: linear-gradient(0deg,#D3D3D3 0%,#D3D3D3 50%,#e8e8e8 51%);
color: #212121;
padding: 20px;
text-align: center;
text-decoration:none;
text-transform: uppercase;
font-family: "Lucida Console", Courier, monospace;
font-size: 18px;
width: 180px;
height: 14px;
display: inline-block;
margin: auto;
}
You can use pseudo elements to add more elements:
a {
border: solid 2px #555;
border-radius: 12px;
background-image: linear-gradient(0deg, #D3D3D3 0%, #D3D3D3 50%, #e8e8e8 51%);
color: #555;
padding: 20px 20px;
text-align: center;
text-decoration: none;
text-transform: uppercase;
font-family: "Arial";
font-size: 18px;
width: 180px;
height: 14px;
display: inline-block;
margin: auto;
position: relative;
display: inline-flex;
align-items: center;
justify-content: center;
text-shadow: 1px 1px 1px white;
font-weight: 600;
box-shadow: 0 0 0 6px #D3D3D3, 0 0 0 8px #555;
}
a:after, a:before {
content: '★';
position: absolute;
width: 100%;
left: 0;
top: 0;
display: flex;
align-items: center;
height: 100%;
padding: 0 20px;
box-sizing: border-box;
font-size: 24px;
line-height: 24px;
}
a:before {
justify-content: flex-end;
}
<a href="#">
Checkout
</a>
Although, the amount of borders on that button is kind of ridiculous to expect from one element I think.
Edit: you can add multiple borders with boxshadows

submit button does not work after using display inline

I don't know what else to do. I am a newbie and doing what I thought was
simple coding. I have tried everything including display:inline and
display:inline important;". I am trying to put the submit button on the
same line with the same style attributes (blue, white words, etc.).
Please help!
<form action="/action_page.php">
<input type="textarea" name="firstname" placeholder="Enter your address
to determine if we buy in your neighborhood" style=
"border-top-left-radius: 4px;
border-top-right-radius: 4px;
border-bottom-left-radius: 4px;
border-bottom-right-radius: 4px;
border-top: 1px solid #d6d4d4;
border-right: 1px solid #d6d4d4;
border-bottom: 1px solid #d6d4d4;
border-left: 1px solid #d6d4d4;
height: 52px;
width: 618px;
padding-top: 15px;
padding-right: 15px;
padding-bottom: 15px;
padding-left: 15px;
margin-top: 5px;
margin-right: 5px;
margin-bottom: 5px;
margin-left: 5px;
color: #161616;
line-height: 1.1;
box-sizing: border-box;
font-family: open sans;
font-weight: 400;
text-align: left;
text-align-last: left;
font-size: 16px;
background-color: #fff;
background-image: none;
overflow: hidden;
box-shadow: 0 0 0 rgba(0,0,0,.5);">
<input type="submit" value="Get Started" style="font-weight: 400; line-
height: 3.1;
font-size: 13px;
color: #ffffff;
background-color: #12208e;
border-top-left-radius: 5px;
border-top-right-radius: 5px;
border-bottom-left-radius: 5px;
border-bottom-right-radius: 5px;
border-top: 0;
border-right: 0;
border-bottom: 0;
border-left: 0;
height: 50px;
width: 160px;
padding-top: 0;
padding-right: 14px;
padding-bottom: 0;
padding-left: 14px;
margin-top: 5px;
margin-right: 5px;
margin-bottom: 5px;
margin-left: 5px;
color: #fff;
line-height: 1;
box-sizing: border-box;
font-family: open sans;
font-weight: 700;
text-align: center;
text-align-last: center;
font-size: 20px;
background-color: #12208e;
background-image: none;
overflow: hidden;
text-shadow: 1px 1px 1px #000;">
</form>
There are quite a few things that are causing errors with your code. First of all, to get your desired layout add in a div that wraps around the two input elements like so:
<div class="form-row">
<input type="textarea" name="firstname" placeholder="Enter your address to determine if we buy in your neighborhood" class="name-input">
<input type="submit" value="Get Started" class="btn-submit">
</div>
Apply these styles to that div to make the two input elements appear "on one-line":
.form-row {
display: flex;
flex-direction: row;
width: 100%;
font-weight: 400;
}
You also had a spelling mistake in the styles for your button, where you had line- height: 3.1;, however I removed that line as you were overriding it with another line-height rule further down.
html {
font-family: open sans;
}
.form-row {
display: flex;
flex-direction: row;
width: 100%;
font-weight: 400;
}
.name-input {
border-radius: 4px;
border: 1px solid #d6d4d4;
height: 52px;
width: 618px;
padding: 15px;
margin: 5px;
color: #161616;
line-height: 1.1;
box-sizing: border-box;
font-family: open sans;
text-align: left;
font-size: 16px;
background-color: #fff;
box-shadow: 0 0 0 rgba(0, 0, 0, .5);
}
.btn-submit {
color: #ffffff;
background-color: #12208e;
border-radius: 5px;
border: 0;
height: 50px;
width: 160px;
padding: 0 14px;
margin: 5px;
color: #fff;
line-height: 1;
box-sizing: border-box;
font-weight: 700;
text-align: center;
font-size: 20px;
text-shadow: 1px 1px 1px #000;
}
<form action="/action_page.php">
<div class="form-row">
<input type="textarea" name="firstname" placeholder="Enter your address to determine if we buy in your neighborhood" class="name-input">
<input type="submit" value="Get Started" class="btn-submit">
</div>
</form>
Note: Since you are setting a fixed width on those two elements, they won't fit on one line on very small screens, or if other elements are added to it.
I greatly recommend splitting your CSS into a separate CSS file like in this snippet. It will allow you to better see what is going on. Also if you use a decent IDE (code editor) it will pick up these errors for you as well.
Here are some resources you can use to learn more about CSS and HTML. Good luck!

DIV's background ain't visible around <textarea> placed within

I want a text-area with black background to be within DIV with white background. Text-area should not fill the complete DIV so the DIV's white color is still seen around text-area. DIV itself should occupy only 80% of the screen's width (or browser's tab). The problem is that the DIV's white background ain't seen around text-area.
.mydiv {
vertical-align: middle;
width: 80%;
text-align: left;
padding-top: 10px;
padding-left: 20px;
padding-right: 20px;
background: white;
}
.mytextarea {
height: 100px;
width: 100%;
position: relative;
padding: 2px 2px;
font-size: 12px;
font-weight: bold;
float: left;
border-radius: 10px;
font-family: 'Helvetica', cursive;
color: white;
text-decoration: none;
background-color: black;
border-bottom: 4px solid #2980B9;
border: 2px solid blue;
outline: 0;
}
You can use display:inline-block; instead of float:left;
.mytextarea {
height: 100px;
width: 100%;
position: relative;
padding: 2px 2px;
font-size: 12px;
font-weight: bold;
display: inline-block;
border-radius: 10px;
font-family: 'Helvetica', cursive;
color: white;
text-decoration: none;
background-color: black;
border-bottom: 4px solid #2980B9;
border: 2px solid blue;
outline: 0;
}
Jsfiddle
.mydiv {
vertical-align: middle;
width: 80%;
text-align: left;
padding-top: 10px;
padding-left: 20px;
padding-right: 20px;
background: red;
}
.mytextarea {
height: 100px;
width: 100%;
position: relative;
padding: 2px 2px;
font-size: 12px;
font-weight: bold;
display: inline-block;
border-radius: 10px;
font-family: 'Helvetica', cursive;
color: white;
text-decoration: none;
background-color: black;
border-bottom: 4px solid #2980B9;
border: 2px solid blue;
outline: 0;
}
<div class="mydiv">
<textarea class="mytextarea"></textarea>
</div>

Show text and icon in the middle of a button

I am coding a button with html5 and css3. Here are how it looks in different sizes:
You can see the icons are not in the middle. I know why it happens. But how can I set the icons in middle? Here is my code:
<i class="icon">a</i><span>start</span>
CSS:
.blue-pill {
display: block;
text-decoration: none;
width: 135px;
height: 50px;
margin-bottom: 15px;
position: relative;
background-color: #002032;
border: none;
color: #FFF;
text-transform: uppercase;
font-family: 'Open Sans Condensed', sans-serif;
font-weight: 700;
font-size: 14px;
padding: 0;
border-radius: 50px;
cursor: pointer;
z-index: 2;
text-align: center;
line-height: 50px;
-webkit-box-shadow: 0px 5px 0px #1488ae;
-moz-box-shadow: 0px 5px 0px #1488ae;
box-shadow: 0px 5px 0px #1488ae;
}
.blue-pill .icon {
display: inline-block;
margin: 0 5px;
color: #e57125;
font-size: 20px;
}
.blue-pill span {
display: inline-block;
}
.blue-pill.centered {
margin-left: auto;
margin-right: auto;
}
.blue-pill.inline {
display: inline-block;
margin-left: 5px;
margin-right: 5px;
}
I am using icon fonts for icons. Here is what I want:
add vertical-align:middle to .blue-pill .icon and .blue-pill span
Example

Resources