Why do we need display block to center a button [duplicate] - css

This question already has answers here:
Why centering with margin 0 auto works with display:block but does not work with display:inline-block ?
(5 answers)
Why does margin-top work with inline-block but not with inline?
(3 answers)
Closed 3 years ago.
This may be basic but i read in a book that to center something inside a div you should :
margin-left: auto;
margin-right: auto;
width:70%;
so you give it a width and set auto margin.
With button it will not work and I also need to add this to make it work :
display: block;
Why in this case we need it block ?

inline/inline-block elements can't have auto value for margin.
If you want to center button without making it a block, you can use text-align: center on it's parent.
Also, button don't have to be a block if it's being centered by a flex/grid parent.
I added a few examples below.
.wrapper-center {
padding: 20px;
border: 1px solid blue;
text-align: center;
}
/* ----- */
.wrapper {
padding: 20px;
margin-top: 20px;
border: 1px solid red;
}
.centered-button {
display: block;
margin: 0 auto;
}
/* ----- */
.flex-wrapper {
display: flex;
align-items: center;
justify-content: center;
margin-top: 20px;
padding: 20px;
border: 1px solid green;
}
<div class="wrapper-center">
<button>Test</button>
</div>
<div class="wrapper">
<button class="centered-button">Test</button>
</div>
<div class="flex-wrapper">
<button>Test</button>
</div>

Display: block
This gives the section or div a whole part of the page to itself, starting from what is essentially a new line and taking up the width of the page. Margin and width statement only affect it because of this. Inline or otherwise don’t have this property requirement due to the fact that their properties become relative to other elements on the same line

Related

Centering element in multielement parent css [duplicate]

This question already has answers here:
Center one and right/left align other flexbox element
(11 answers)
Closed 1 year ago.
i have a parent div which contain two child. i want that first child to be centered while the other to be at the end of flex but using margin auto does center the first child according to the space left for him not according to the size of parent div.i mean margin on left should be different from margin on right as i have second element on the right of first one.so how to make margin calculated responsively or if there another solution to center one element and make the other at end.I would like to apologize for this long question.
HTML
<div class="parent">
<h2 class="firstchild">line of text</h2>
<div class="secondchild">
<form>
<input type="text" placeholder="search.. " name="search">
<span class="searchbutton"><button id="button" type="submit" name="search" >Search</button></span>
</form>
</div>
</div>
parent
.parent{
display: flex;
flex-wrap: wrap;
flex-direction:row;
width: 100%;
first child
.firstchild{
margin-top: 10px;
text-align: center;
margin-bottom: 5px;
margin: 0 auto;
justify-self: center;
position: absolute;
second child
.secondchild {
margin-left: 0px;
margin-bottom: 5px;
margin-right: 0px;
position: relative;
justify-self: flex-end;
Thank you for everyone who replied.
maybe i express my question wrongly but i have found the solution using java script
the solution is:
<script>
function myFunction() {
var elmnt=document.getElementById('parent');
var w=elmnt.clientWidth;
var m=(w-256.922)/2;
var r=m-240.594;
document.getElementById("child1").style.marginLeft=m+'px';
document.getElementById('child1').style.marginRight=r+'px';
}
window.onresize=myFunction;
</script>
where 256.922 and 240.594 is width of first and second child respectively.
justify-self doesn't work in flexbox. But you can use margin-left: auto to do what you want.
Your code will work if you remove all the positioning in your first and second child, also second child does not need to justify-self: flex-end.
.parent{
display: flex;
flex-wrap: wrap;
flex-direction:row;
width: 100%;
}
.firstchild{
margin-top: 10px;
text-align: center;
margin-bottom: 5px;
margin: 0 auto;
justify-self: center;
/* position: absolute; */
}
.secondchild {
margin-left: 0px;
margin-bottom: 5px;
margin-right: 0px;
/* position: relative;
justify-self: flex-end */
}
You could use margin-left: auto to make it centerlize:
.firstchild
{
margin: auto; /* to align center*/
}
or if you want to center horizontally or vertically
.firstchild
{
margin: 0px auto; /* to align center horizontally*/
margin: auto 0px; /* to align center veritcally*/
}

z-index normal div with flex div [duplicate]

This question already has answers here:
Why does z-index not work?
(10 answers)
Closed 3 years ago.
I am trying to put a button on top of 4 blocks. Those blocks are inside divs with style display:flex.
But the z-index style is not working on the button. Alse, if I give positive but less than the button z-index to the blocks, the button hides under all the blocks.
I know I can give negative z-index to the blocks, but in my project it will cause other problem. So I am wondering what's the best way to style them.
.block{
flex:1;
padding: 10px;
border: 2px solid #666999;
margin: 0 10px 10px 0;
}
#btn_box{
margin-top: -20px;
margin-bottom:-15px;
z-index: 999;
text-align: center;
}
#btn_box a{
display: inline-block;
background: #ff9999;
padding: 5px 10px;
}
#btn_box + div{
z-index: 1;
}
<div style='position:relative;'>
<div style='display:flex;'>
<div class='block'>1</div>
<div class='block'>2</div>
</div>
<div id='btn_box'>
<a>TestButton</a>
</div>
<div style='display:flex;'>
<div class='block'>3</div>
<div class='block'>4</div>
</div>
</div>
See also here:
https://jsfiddle.net/w4o8afts/
I have a workaround by making the button box a flex div, but it feels strange to do so.
Add position:relative
#btn_box{
position:relative;
margin-top: -20px;
margin-bottom:-15px;
z-index: 999;
text-align: center;
}

Flexbox alignment to end of div

How can I use flexbox to align .right to the very end of div?
My CSS:
div {
display: flex;
border: 1px dotted black;
}
.right {
}
HTML:
<div>
<span>Left</span>
<span class="right">Right</span>
</div>
Codepen Link
Thanks.
Three ways:
Use justify-content: space-between on the container.
div {
display: flex;
border: 1px dotted black;
justify-content: space-between; /* new */
}
Revised Codepen
Use auto margins on the first flex item.
div > span:first-child { margin-right: auto; }
Revised Codepen
Use auto margins on the second flex item.
.right { margin-left: auto; }
Revised Codepen
For an explanation of justify-content and auto margins, along with examples and illustrations, see this post: Methods for Aligning Flex Items along the Main Axis
As per section 8.1 in the CSS Flexible Box Layout Module, you can use auto margins in order to position the element.
In this case, you could add margin-left: auto in order to position the element to the right. In doing so, any positive free space is distributed to that side of the element which effectively positions it to the very right like in the example below.
div {
display: flex;
border: 1px dotted black;
}
.right {
margin-left: auto;
}
<div>
<span>Left</span>
<span class="right">Right</span>
</div>
Use margin-left: auto; margin-right: 0; so that the element is pushed to the right! Also you have a typo here:
<span class="righ">Right</span>
<!---------------^ t missing.
See the working snippet:
div {
display: flex;
border: 1px dotted black;
}
.right {
margin-left: auto;
margin-right: 0;
}
<div>
<span>Left</span>
<span class="right">Right</span>
</div>

CSS - Display inline block elements with a border and text-overflow

I have an inline-block element that I want to put a border-bottom on, but when the text inside that element wraps to the next line, it puts the border on the bottom of both lines of text, instead of just the bottom of the element.
Heres a demo:
http://codepen.io/Tiger0915/pen/azpeVY
And here's the pertinent SCSS:
div {
width: 150px;
text-align: center;
span {
border-bottom: 20px solid rgba(0,0,0,0.3);
}
}
How do I get it to only put the border on the bottom of the element?
Reason I have to use display: inline-block and can't just use display: block:
I need text to be able to wrap to a new line, as the screen size is
small
I can't specify a defined width on the span, it needs to change width based on whether or not the text can fit on 1 or multiple lines (depends on screen width)
The span needs to be text-align: center within the div
I changed your SCSS to this and it worked fine for me:
div {
width: 150px;
text-align: center;
span {
display: inline-block;
border-bottom: 20px solid rgba(0,0,0,0.3);
}
}
Just add display: inline-block; to the span like you said. By default, <span> elements are display: inline; not display: inline-block;.
You can wrap the span in a div and assign the border-bottom to the div.
add a wrapper div
<div>
<div class='wrap'>
<span>
This is a long sentence.
</span>
</div>
</div>
assign border-bottomis SCSS
div {
background: rgba(0,0,0,0.1);
width: 150px;
margin: 100px auto;
padding: 20px;
text-align: center;
div {
padding: 0;
border-bottom: 20px solid rgba(0,0,0,0.3);
span {
font-size: 24px;
}
}
}
Here is a working codepen.

CSS inline-block causing extra space between 2 vertical divs [duplicate]

This question already has answers here:
Why does my image have space underneath?
(3 answers)
Closed 7 years ago.
I have several div (class="badge") to display in the vertical. Not sure why I got extra space between 2 div in FF and IE (Chrome works fine).
I need them to display either no space or equal space in all browsers.
http://jsfiddle.net/2hxak/1/
HTML:
<div class="stat-badges">
<div class="badge">
<div class="stat-num">123456</div>
</div>
<div class="badge">
<div class="stat-num">0</div>
</div>
</div>
CSS:
.stat-badges {
text-align: center;
width: 55px;
}
.badge {
display: inline-block;
padding: 2px 4px;
color: #ffffff;
vertical-align: baseline;
white-space: nowrap;
background-color: #999999;
}
.badge .stat-num {
max-width: 30px;
min-width: 20px;
padding: 3px 0;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
The space will disappear if I remove overflow: hidden;. I keep overflow: hidden with ellipse to crop long text.
Change vertical-align: baseline; to vertical-align: top; in your badge class rule.
jsFiddle example
display: inline-block; is messing this up. Use float: left; instead (possibly with clear: left; to make sure every badge is on a new line). (jsFiddle)

Resources