This question already has answers here:
How can I horizontally center an element?
(133 answers)
CSS center display inline block?
(9 answers)
Closed 4 years ago.
I was wondering how I can center inline-blocks. Any help?
I tried doing margin-top: x em; but all it did was have the image go down. This inline-block is deliberately inside an image block. If you have links that show me how to position inline-blocks, I will gladly take them into consideration, as I am still learning how to do CSS and HTML.
#img-banner article {
border: solid thick white;
box-sizing: border-box;
display: inline-block;
width: 14em;
position: static;
}
#img-banner h1 {
font-family: 'Lobster', cursive;
text-align: center;
color: white;
font-size: 27px;
}
<span id="img-banner">
<article>
<h1>Introduction</h1>
</article>
</span>
It would be important that you include the properties of the containing span as this is the parent you need to align on. Assuming you want to vertically center your element, you need to ask yourself if the height of your containing element is known and that will guide you towards the proper centering technique.
https://css-tricks.com/centering-css-complete-guide/
You will find alot of common centering techniques on this website. You can consider your inline-block element as a block-level element.
Personally, I like making sure my container is position:relative and then add top:50%; transform: translateY(-50%) to my unknown height positioned elements.
Edit : As noted in your comments, this HTML is invalid. Only inline elements are allowed in a span. I don't know why I overlooked this.
Related
This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
Closed 1 year ago.
This question is about understanding how flex effect the display of child elements. The centering is peripheral and not the point of the question. Please see comments below if the question does not make sense.
From my reading of how flex works it should do the opposite and make the child elements behave like block elements.
However, in this minimal example it makes the paragraph element behave like an inline element.
I am using flex to center content.
<style>
/* display flex should cause child elments to behave like block elements
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
*/
#wrapper{
display:flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
#image{
margin: 0px;
border: 1px dotted #888888;
}
#percentage{
margin: 0px;
font-size: 40px;
font-weight: bold;
border: 1px dotted #888888;
}
</style>
<div id="wrapper" >
<img id="image" src="https://upload.wikimedia.org/wikipedia/commons/1/1c/Crystal_128_penguin.png" alt="wikimedia" width="128" height="128">
<p id="percentage"> 10%</p>
</div>
Basically, flexbox has a default flex-direction of row that's why the elements are inline, to make them appear on like block elements you can set flex-direction to column.
This question already has answers here:
How can I vertically align elements in a div?
(28 answers)
Vertically align text next to an image?
(26 answers)
Flexbox: center horizontally and vertically
(14 answers)
How do I vertically align text in a div?
(34 answers)
Vertically centering a div inside another div [duplicate]
(24 answers)
Closed 4 years ago.
I am trying to center align a span inside a div, which also contains an img element.
.element {
display: inline-block;
}
.element img {
height: 40px;
width: 40px;
border-radius: 50%;
margin-right: 20px;
}
.element span {
display: inline-block;
}
<div class="element">
<img src="https://images.pexels.com/photos/35646/pexels-photo.jpg?auto=compress&cs=tinysrgb&dpr=1&w=500">
<span>hello</span>
</div>
or see this fiddle
However the text wont vertical align. I have looked primary at this question. However vertical-align: middle does nothing here.
I also looked at this question. However I will rather avoid anything position: relative & position: absolute workarounds for this. I also tried messing the line-height with no luck.
I even tried to set height: 100%on the span, as this question suggests, but that does nothing either.
I basically looked at bunch of questions here on SO, it seems like css is so weird about this, that there basically is 12 approaches for a simple thing like this. Yet I can't seem to get 1 of them to work in my occasion.
What is up with this behavior?
EDIT:
Marked as duplicate to How to Vertical align elements in a div? - I have explained that these solutions with line-height and vertical align doesn't work in my case as explained in the original question. The accepted solution did not work in this case. How is it a duplicate?
The answer here is probably to use flexbox. If your flex-direction is row (which is default), you can use align-items to center the elements vertically and justify-content to justify the row to the left (the "start" of the flex container). Let me know if you have any questions!
.element {
align-items: center;
display: flex;
justify-content: flex-start;
}
.element img {
height: 40px;
width: 40px;
border-radius: 50%;
margin-right: 20px;
}
.element span {
display: inline-block;
}
<div class="element">
<img src="https://images.pexels.com/photos/35646/pexels-photo.jpg?auto=compress&cs=tinysrgb&dpr=1&w=500">
<span>hello</span>
</div>
Use flexbox for this. Sample:
.element {
display: flex;
align-items: center;
}
Use align-items: center for vertical align and justify-content: center; if you need also horizontal align center.
This question already has answers here:
Why do inline elements behave like block level elements when floated?
(4 answers)
Closed 4 years ago.
I have a very simple HTML & CSS structure that I use for practice.
Please check the fiddle link attached.
I was working on a span tag and gave it some height & width but it was not rendering.
I know the reason. span is an inline tag and it does not respect height and width properties.
But suddenly I gave it a float property and it rendered on the page.
My question is how float property is affecting an inline element, even when it does not have any content inside.
body {
margin: 0;
padding: 0;
font-size: 10px;
line-height: 1.5em;
}
div.wrapper {
background: pink;
width: 200px;
height: 200px;
}
span.round{
width:60px;
height:60px;
background: orange;
float:right; /** not using float, vanishes the whole span **/
}
<div class="wrapper">
<span class="round"></span>
</div>
Copied from Mozilla Developer section:
"As float implies the use of the block layout, it modifies the computed value of the display values".
As of the result, the tag will be automatically block type.
This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
How can I vertically align elements in a div?
(28 answers)
How do I vertically center text with CSS? [duplicate]
(37 answers)
How can I center text (horizontally and vertically) inside a div block?
(27 answers)
Closed 4 years ago.
Thats not a div I want I just cant get to post it because it does not meet some requirement. I have never used anything like that before, just need help for a college project that requires programming.
I have this: enter image description here
I just want the text to be centered.
jsfiddle .net /etr1ok47/#&togetherjs=VfasppUOxh
using flex in this-p class:
display: flex;
flex-direction: column;
justify-content: center;
Try applying the following to your containing div (.parent) and the wrapper around your text (.child).
.parent {
height: 300px;
line-height: 300px;
text-align: center;
}
.child {
line-height: normal;
display: inline-block;
vertical-align: middle;
}
You can add padding to the top of your p element in css if you are needing the words in the circle to be vertically better centered.
p.this-p{
vertical-align: middle;
}
If the problem is the p itself not centering in the li element then define the parent with display: table and the element itself with vertical-align: middle and display: table-cell
p.this-p{ vertical-align: middle; align: center}
Above is when you use p element with padding. Same is applicable for other element
This question already has answers here:
How do I vertically center text with CSS? [duplicate]
(37 answers)
Closed 5 years ago.
I have been looking for tutorials how to center text in link.
Nothing helped. Could you help me out?
HTML:
<nav>
<a class="but1" href="#"><strong>ART</strong></a>
<a class="but2" href="#"><strong>VIDEOS</strong></a>
<a class="but3" href="#"><strong>ABOUT</strong></a>
<a class="but4" href="#"><strong>CONTACT</strong></a>
</nav>
CSS:
nav a {
font-size: 500%;
text-decoration: none;
color: white;
Height: 25%;
width: 100%;
position:absolute;
text-align: center;
vertical-align: middle;
transform: translate(-50%, -100%);
left: 50%;
}
.but1 {
background-color: #e24e42;
top: 25%;
}
.but2 {
background-color: #e9b000;
top: 50%;
}
.but3 {
background-color: #eb6e80;
top: 75%;
}
.but4 {
background-color: #008f95;
top: 100%;
}
I am starter in Web development. To be exact 2 days. If there are HUGE Mistakes, don't judge me.
Vertical alignment in CSS was a nightmare just a few years ago.
To try something like vertical-align: middle; means that you didn't even bothered to google it at all.
I woke up from that nightmare the moment I found flexbox. Take a look at this super complete guide on how to flexbox.
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
I think it's appropriated to create navigation menus, I use flexbox every time for that purpose.
EDIT:
I have been there and I know how hard small things can get in the beginning. Keep in mind that if you want to get serious in web design this will happen a lot and you must know how to deal with it alone.
Take a look at this fiddle: http://jsfiddle.net/ZTz7Q/2651/
You will notice that we only apply css flexbox properties on the parent element, defining how the children should be displayed:
align-items: center This is responsible for vertically center your content on the main axis (horizontal by default).
justify-content: flex-start This will align your content on the main axis, pretty much like text-align:left
Take some time to understand the code instead of copy and pasting it. It will help you to improve.