Why can't I use flex with <button> elements? [duplicate] - html

This question already has answers here:
How can I horizontally center an element?
(133 answers)
How to center a button within a div?
(16 answers)
Closed 5 months ago.
I have the following code in index.html
h1 {
margin-top: 10px;
margin-bottom: 10px;
display: flex;
justify-content: center;
}
#count-el {
font-size: 50px;
display: flex;
justify-content: center;
}
#increment-btn {
background: red;
color: white;
font-size: 40px;
display: flex;
justify-content: center;
}
<h1>People entered:</h1>
<h2 id="count-el">0</h2>
<button id="increment-btn">Increment</button>
Flex works in all elements except for the <button> element. It doesn't center it. Why is that?

Most browsers display button elements as inline-block by default, so, it won't occupy 100% of parent's width. If you apply width 100% it will center the text, just like h1, h2. If you want to center the button itself you can use margin: 0 auto; property.
h1 {
margin-top: 10px;
margin-bottom: 10px;
display: flex;
justify-content: center;
}
#count-el {
font-size: 50px;
display: flex;
justify-content: center;
}
#increment-btn {
width: 100%;
background: red;
color: white;
font-size: 40px;
display: flex;
justify-content: center;
}
<h1>People entered:</h1>
<h2 id="count-el">0</h2>
<button id="increment-btn">Increment</button>

add to your button class margin-left: auto; margin-right:auto;
should look like that
#increment-btn {
background: red;
color: white;
font-size: 40px;
display: flex;
justify-content: center;
margin-left: auto;
margin-right: auto;
}

For your problem you need to read this article:
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
You should add
margin: auto;
display: block;
to #increment-btn style.
Other solution is to add a div parent element to the button and add flex style there:
display: flex;
justify-content: center;
I made a codepen for you: https://codepen.io/kovtib/pen/vYjgXrP

h1 {
margin-top: 10px;
margin-bottom: 10px;
display: flex;
justify-content: center;
}
#count-el {
font-size: 50px;
display: flex;
justify-content: center;
}
#increment-btn {
background: red;
color: white;
font-size: 40px;
display: flex;
justify-content: center;
margin-inline: auto;
}
<h1>People entered:</h1>
<h2 id="count-el">0</h2>
<button id="increment-btn">Increment</button>
Add margin-inline: auto to your button styles.

Related

how do I place 2 buttons on top of each other while keeping them centered? [duplicate]

This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
Closed 4 months ago.
I would like to put 2 buttons on top of each other while still keeping both of them centered. I managed to put them on top of each other through the display: block, and through the flex-direction: column. However, I still don't know how to center them. The text-align doesn't seem to work. could somebody explain to me why this doesn't work, please?
button{
border: none;
padding-top: 10px;
padding-bottom: 10px ;
color: white;
font-weight: bold;
width: 200px;
margin-bottom: 5px;
border-radius: 5px;
}
.btn-group{
display: flex;
flex-direction: column;
text-align: center;
}
#increment-btn{
background: darkred;
}
#save-btn{
background-color: green;
}
This is the HTML:
<div class="btn-group">
<button id="save-btn" onclick="save()">SAVE</button>
<button id="increment-btn" onclick="increment()">INCREMENT</button>
</div>
You may use align-items: center; , it is a flex-direction: column;, so it will be align on the horizontal axis :)
button {
border: none;
padding-top: 10px;
padding-bottom: 10px;
color: white;
font-weight: bold;
width: 200px;
margin-bottom: 5px;
border-radius: 5px;
}
.btn-group {
display: flex;
flex-direction: column;
align-items: center;
}
#increment-btn {
background: darkred;
}
#save-btn {
background-color: green;
}
<div class="btn-group">
<button id="save-btn" onclick="save()">SAVE</button>
<button id="increment-btn" onclick="increment()">INCREMENT</button>
</div>
Aside, flex or grid children do not really need to be reset to block if it is an inline element , they become part(cells) of the generated grid . (float is also ignored)
Use align-items: center; in your .btn-group instead of text-align: center;.
For further information this site is very helpful.

How to align text vertically and horizontally in CSS button [duplicate]

This question already has answers here:
How can I center text (horizontally and vertically) inside a div block?
(27 answers)
Flexbox: center horizontally and vertically
(14 answers)
Closed 12 months ago.
I am trying to align text horizontally and vertically inside buttons. I checked tons of solutions, no joy.
This is what I have right now:
<div class="buttonx">
Text
text text<br>text
text
text
text
</div>
<style>
.buttonx {
display: flex;
flex-wrap: wrap;
}
.buttonx a {
display: table;
background-color: #a00c1a;
width: 120px;
line-height: 20px;
margin: 10px;
text-decoration: none;
color: rgb(0, 0, 0);
text-align: center;
align-items: center;
color: white;
}
</style>
The text comes out as aligned horizontally, but not vertically. Please help.
Change only your buttonx class like that:
.buttonx {
display: flex;
flex-wrap: wrap;
gap: 10px;
justify-content: center;
}
.buttonx {
display: flex;
flex-wrap: wrap;
gap: 10px;
justify-content: center;
}
.buttonx a {
display: flex;
align-items: center;
justify-content: center;
background-color: #a00c1a;
width: 120px;
line-height: 20px;
margin: 10px;
text-decoration: none;
color: rgb(0, 0, 0);
text-align: center;
color: white;
height:40px;
}
<div class="buttonx">
Text
text text<br>text
text
text
text
</div>
Note I also remove from your buttonx a class some css properties.
.buttonx {
display: flex;
flex-wrap: wrap;
}
.buttonx a {
display: flex;
align-items: center;
justify-content: center;
background-color: #a00c1a;
width: 120px;
line-height: 20px;
margin: 10px;
text-decoration: none;
color: rgb(0, 0, 0);
text-align: center;
color: white;
}
<div class="buttonx">
Text
text text<br>text
text
text
text
</div>

How do i put button under h3 with display flex [duplicate]

This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
Closed 1 year ago.
I have div with h3 and button inside of it, and my div has display:flex; with justify-content:center; align-items:center; and when i insert these propeties, my button sticks on the right side of the h3 i tried creating div and putting button in it, but that just breakes the view
So question is, how do i put button under h3? Do i have missing flex properties?
.tittle-block {
display: flex;
justify-content: center;
align-items: center;
background: url(../img/background.png) 0% no-repeat;
padding: 0;
min-height: 495px;
}
.tittle {
text-align: center;
width: 555px;
color: #fff;
font-size: 42px;
margin: 0;
}
.button {
display: flex;
justify-content: center;
align-items: center;
flex-direction: wrap;
border: 1px solid white;
border-radius: 5px;
background: none;
color: #fff;
font-size: 25px;
}
<div class="tittle-block">
<h3 class="tittle">Text here</h3>
<button type="button" class="button">View more</button>
</div>
All you need to do is add flex-direction: column.
But, first, you've a syntax error:
<div class="tittle-block> , you need to close the class tag, like this <div class="tittle-block">
.tittle-block{
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background: url(../img/background.png) 0% no-repeat;
padding: 0;
min-height: 495px;
}
.tittle{
text-align: center;
width: 555px;
color: #000;
font-size: 42px;
margin: 0;
}
.button{
display: flex;
justify-content: center;
align-items: center;
flex-direction: wrap;
border: 1px solid white;
border-radius: 5px;
background: none;
color: #000;
font-size: 25px;
}
<div class="tittle-block">
<h3 class="tittle">Text here</h3>
<button type="button" class="button">View more</button>
</div>
ps: I edited the font to be #000 so that you can see it, change it back to your original #fff.

Justify-content: space-between works incorrect

i have the following code
header {
width: 100%;
height: 65px;
background: #fff;
font-size: 11px;
font-weight: bold;
text-transform: uppercase;
display: flex;
justify-content: space-between;
align-items: center;
color: #252c3a;
}
#menu {
width: 100%;
display: flex;
margin-left: 28%;
}
#menu div {
width: 100px;
height: 65px;
display: flex;
align-items: center;
justify-content: center;
position: relative;
}
<header>
<div id="logo">
<img src="img/header/logo.png" alt="Logo">
</div>
<div id="menu">
<div>Home</div>
<div>Club-Life</div>
<div>Training</div>
<div>Instructors</div>
<div>Contact</div>
</div>
Chrome inspect
The width of the other blocks is 100%, but the header width gets bigger than the block below. I use justify-content: space-between.
Remove width & margin
Add flex-wrap on the header
header {
width: 100%;
height: 65px;
background: #fff;
font-size: 11px;
font-weight: bold;
text-transform: uppercase;
display: flex;
justify-content: space-between;
align-items: center;
color: #252c3a;
flex-wrap:wrap;
}
#menu {
display: flex;
flex-wrap:wrap;
}
There are few things we need to do:
In header:
1. Remove width
In #main:
1. Remove width
2. Remove margin
In #menu div:
1. Remove everything and just add margin left
Demo: https://codepen.io/Bibeva/pen/povddJr
Final code:
header {
height: 65px;
background: #fff;
font-size: 11px;
font-weight: bold;
text-transform: uppercase;
display: flex;
justify-content: space-between;
align-items: center;
color: #252c3a;
}
#menu {
display: flex;
}
#menu div {
margin: 0 0 0 30px;
}
#menu {
display: flex;
}
that's all , Can you try this ?

Horizontal blank space though margin property is not set [duplicate]

This question already has answers here:
CSS margin terror; Margin adds space outside parent element [duplicate]
(7 answers)
How to remove margin space around body or clear default css styles
(7 answers)
How can I get rid of margin around my HTML content?
(5 answers)
Closed 3 years ago.
I ran into this problem while learning CSS. I tried to search for this but couldn't find any proper answers. Some lead me to margin collapsing, but it just doesn't happen to horizontal margins.
#nav-bar {
position: fixed;
top: 0px;
left: 0px;
height: 60px;
width: 100%;
display: flex;
justify-content: flex-end;
align-items: center;
background-color: #be3144;
}
#nav-list {
display: flex;
margin-right: 4rem;
}
.nav-link {
text-decoration: none;
color: white;
padding: 0 1.6rem 0 1.6rem;
height: 60px;
weight: 100%;
display: flex;
align-items: center;
font-size: 1.3rem;
}
#welcome-section {
background-color: #3a3d40;
height: 100vh;
display: flex;
flex-direction: column;
text-align: center;
align-items: center;
justify-content: center;
margin-top: 60px;
}
<nav id="nav-bar">
<div id="nav-list">
About
Work
Contact
</div>
</nav>
<section id="welcome-section">
<h1>Hey I am Mimic</h1>
<h4>a web developer</h4>
</section>
The #welcome-section below nav-bar has blank space both left and right sides, though I didn't set any margin properties.
Also, any advices and suggestions in styling HTML/CSS are appreciated. Thanks in advance.
It's because the <body> of the page has margin as default.
Get rid of that by adding the below, and it should work...
body {
margin:0;
}
body {
margin:0;
}
#nav-bar {
position: fixed;
top: 0px;
left: 0px;
height: 60px;
width: 100%;
display: flex;
justify-content: flex-end;
align-items: center;
background-color: #be3144;
}
#nav-list {
display: flex;
margin-right: 4rem;
}
.nav-link {
text-decoration: none;
color: white;
padding: 0 1.6rem 0 1.6rem;
height: 60px;
weight: 100%;
display: flex;
align-items: center;
font-size: 1.3rem;
}
#welcome-section {
background-color: #3a3d40;
height: 100vh;
display: flex;
flex-direction: column;
text-align: center;
align-items: center;
justify-content: center;
margin-top: 60px;
}
<nav id="nav-bar">
<div id="nav-list">
About
Work
Contact
</div>
</nav>
<section id="welcome-section">
<h1>Hey I am Mimic</h1>
<h4>a web developer</h4>
</section>