How can I make my block elements to align vertically? [duplicate] - html

This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
How can I vertically align elements in a div?
(28 answers)
Closed 4 years ago.
I have a text and an image. These are floated to left and right, and because float isn't working with inline-block elements, I can't say to these elements vertical-align: middle;. So, in this case, how can I align vertically these elements to each other?
.floatLeft {
float: left;
}
.sitename {
font-size: 1.7em;
}
.floatRight {
float: right;
}
.innerNav-container:before {
content: "";
display: inline-block;
vertical-align: middle;
height: 100%;
}
<nav>
<div class="innerNav-container">
<p class="floatLeft sitename innerNav-padding">Text</p>
<img class="floatRight items innerNav-padding" src="/ico/img.png" alt="img">
<div style="clear: both;"></div>
</div>
</nav>

Use flexbox for this. More easy and more useful. Don't use float
nav {
display:flex;
justify-content:space-between;
align-items:center;
}
<nav>
<p>Text</p>
<img class="floatRight items innerNav-padding" src="https://picsum.photos/50" alt="img">
</nav>

Related

How do I center <h2> and <img> elements that are inline-block? [duplicate]

This question already has answers here:
How can I horizontally center an element?
(133 answers)
CSS center display inline block?
(9 answers)
How can I center text (horizontally and vertically) inside a div block?
(27 answers)
Closed 22 days ago.
I am trying to align h2 and img (icon) elements and I know that I should add display: inline-block but they shifted to the left of the section. How can I center them?
HTML:
<h2>Coffee</h2>
<img src="https://cdn.freecodecamp.org/curriculum/css-cafe/coffee.jpg" alt="coffee icon"/>
CSS:
img {
display: inline-block;
vertical-align: -8px;
}
h2 {
display: inline-block;
text-align: center;
}
Screenshot
Use a <div> to center everything inside (text and image) using text-align:
<style>
div
{
text-align: center;
}
</style>
<div>
<h2>Coffee</h2>
<img src="https://cdn.freecodecamp.org/curriculum/css-cafe/coffee.jpg" alt="coffee icon"/>
</div>
You can wrap both h2 and img tags in parent div so you can apply flexbox property easily and center both element.
div{
display:flex;
justify-content:center;
align-items:center;
}
div img{
margin-left:10px;
}
<div>
<h2>Coffee</h2>
<img src="https://cdn.freecodecamp.org/curriculum/css-cafe/coffee.jpg" alt="coffee icon"/>
</div>
To center the text "Coffee" and the cup image inside a section as shown in the screenshot, you can add text-align: center to the <section> element in CSS. This will align all elements inside the section to the center.
section {
text-align: center;
}
Making <h2> as inline-block, the width become the length of the text "Coffee". Therefore, text-align: center doesn't work.

Vertically center two floating images [duplicate]

This question already has answers here:
How can I vertically align elements in a div?
(28 answers)
Flexbox: center horizontally and vertically
(14 answers)
How to align flexbox columns left and right?
(5 answers)
How should I align one element to the right and one element to the left inside a containing element?
(7 answers)
In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?
(6 answers)
Closed 2 years ago.
I need to put two logo images on the same line, vertically centered relative to each other.
Something like this:
This alone I could easily achieve with CSS vertical-align: middle;, but I also need the images to be at the very left and and the very right respectively; so normally I would do it with float: left and float: right, but then vertical-align stops working...
Another constraint is that I do not write HTML manually, but rather it is generated by a tool (pandoc, to be precise).
So long story short, I have the following two options for HTML:
Option 1:
<div id="logo-block">
<p>
<img id="logo1" src="https://upload.wikimedia.org/wikipedia/commons/4/4b/McDonald%27s_logo.svg" />
</p>
<p>
<img id="logo2" src="https://www.festisite.nl/static/partylogo/img/logos/burger-king.png" />
</p>
</div>
Option 2:
<div id="logo-block">
<p>
<img id="logo1" src="https://upload.wikimedia.org/wikipedia/commons/4/4b/McDonald%27s_logo.svg" />
<img id="logo2" src="https://www.festisite.nl/static/partylogo/img/logos/burger-king.png" />
</p>
</div>
Could you please help me styling any of these HTML snippets, so that I get the two images vertically centered within the "logo-block" boundaries, with "logo1" at the very left and "logo2" at the very right?
Solved by CSS Flexbox:
The best move is to just nest a flexbox inside of a flexbox. All you have to do is give the child align-items: center. This will vertically align the text inside of its parent. Using display: flex you can control the vertical alignment of HTML elements.
Option 1 snippet:
#logo-block {
display: flex;
align-content: center;
align-items: center; border:2px solid red; max-width:500px; justify-content:space-between;
}
#logo-block img {
max-width: 100px;
}
<div id="logo-block">
<p>
<img id="logo1" src="https://upload.wikimedia.org/wikipedia/commons/4/4b/McDonald%27s_logo.svg" />
</p>
<p>
<img id="logo2" src="https://www.festisite.nl/static/partylogo/img/logos/burger-king.png" />
</p>
</div>
Option 2 snippet:**
#logo-block {
width: 500px;
border: 2px solid red;
}
#logo-block > p {
display: flex;
align-content: center;
align-items: center; justify-content: space-between;
}
#logo-block img{
max-width: 100px;
}
<div id="logo-block">
<p>
<img id="logo1" src="https://upload.wikimedia.org/wikipedia/commons/4/4b/McDonald%27s_logo.svg" />
<img id="logo2" src="https://www.festisite.nl/static/partylogo/img/logos/burger-king.png" />
</p>
</div>
Please use display: table-cell method if tool (pandoc + wkhtmltopdf) doesn't support CSS Flexbox:
#logo-block {
display: table;
width:100%;
max-width:500px;
border:2px solid red;
}
#logo-block p {
display:table-cell;
vertical-align:middle;
}
#logo-block p:last-child {
text-align:right;
}
#logo-block img {
max-width: 100px;
}
<div id="logo-block">
<p> <img id="logo1" src="https://upload.wikimedia.org/wikipedia/commons/4/4b/McDonald%27s_logo.svg" /> </p>
<p> <img id="logo2" src="https://www.festisite.nl/static/partylogo/img/logos/burger-king.png" /> </p>
</div>
I prefer to use images as background with background-size:contain, background-position: center. This way, the image size and image shape no longer matter. It'll show as large as the canvas allows. This is also very responsive:
#logo-block >div{
display: inline-block;
width: 100px;
height: 100px;
margin: 10px;
background-size: contain;
background-position: center;
background-repeat: no-repeat;
border: 1px dotted grey; /* Just for demo, to show the 'canvas' */
}
<div id="logo-block">
<div style="background-image: url('https://upload.wikimedia.org/wikipedia/commons/4/4b/McDonald%27s_logo.svg');"></div>
<div style="background-image: url('https://www.festisite.nl/static/partylogo/img/logos/burger-king.png');"></div>
</div>

How to vertically center text? [duplicate]

This question already has answers here:
How to remove the space between inline/inline-block elements?
(41 answers)
How can I center text (horizontally and vertically) inside a div block?
(27 answers)
How can I vertically align elements in a div?
(28 answers)
Flexbox: center horizontally and vertically
(14 answers)
Closed 3 years ago.
I am trying to create a promotion where a picture is on the left and text on the right. How would I vertically center the text so that it appears in the middle of the photo? I have tried using margin and vertical-align, but they did not work.
In addition, why does the div with the text not appear on the same line as the div with the image, even though their widths are both set at 50%?
Here is my HTML and CSS:
.main-promotion .image {
display: inline-block;
vertical-align: top;
width: 50%;
height: 100%;
}
.main-promotion img {
width: 100%;
}
.main-promotion .description {
display: inline-block;
text-align: center;
width: 50%;
height: 100%;
font-size: 2vw;
}
<div class="main-promotion">
<div class="image">
<img src="https://images.newscientist.com/wp-content/uploads/2019/01/14144335/art-800x533.jpg" alt="Image Here">
</div>
<div class="description">
<h3> Get access to the morning routine used by world-class performers for FREE! </h3>
<p> We'll send it to you immediately! </p>
<button> Click me! </button>
</div>
</div>
Thank you in advance for your help!
See Flex display, with align-items:center;
.main-promotion{
display: flex;
align-items: center;
}
You can then also remove inline-block and vertical-align properties from your children properties.
Add css attribute position and float:
.main-promotion .image {
display: inline-block;
vertical-align: top;
width: 50%;
height: 100%;
position:relative; float:left
}
.main-promotion img {
width: 100%;
}
.main-promotion .description {
display: inline-block;
text-align: center;
width: 50%;
height: 100%;
font-size: 2vw;
position:relative; float:left
}
<div class="main-promotion">
<div class="image">
<img src="https://images.newscientist.com/wp-content/uploads/2019/01/14144335/art-800x533.jpg" alt="Image Here">
</div>
<div class="description">
<h3> Get access to the morning routine used by world-class performers for FREE! </h3>
<p> We'll send it to you immediately! </p>
<button> Click me! </button>
</div>
</div>
Sounds like you need display: flex on your parent div.
And then alter the size of the img a bit.
https://codepen.io/raqem/pen/BgpYwj

How to make all elements in div stay on one line [duplicate]

This question already has answers here:
Does UL have default margin or padding [duplicate]
(2 answers)
What are the default margins for the html heading tags (<h1>, <h2>, <h3>, etc.)?
(4 answers)
Vertically align text next to an image?
(26 answers)
Flexbox: center horizontally and vertically
(14 answers)
How can I vertically align elements in a div?
(28 answers)
Closed 4 years ago.
I'm trying to make all elements stay on one line. Here is code
.logoHeader{
width:10vw;
display:inline;
}
ul li{
display: inline-flex;
}
span{
font-size:0.5em;
}
.myClass {
display: inline-flex;
}
<div class="myClass"><img class="logoHeader" src="https://s.imgur.com/desktop-assets/desktop-assets/Navbar-logo.bcf646386406b43008da913e901b916d.svg" width="500" height="53" alt="">
<ul>
<li><h5><span>A</span>Put Iva Vizina 165</h5></li>
<li><h5><span>T</span>T +382 69 710 025</h5></li>
<li><h5><span>Random Crap</span> Plaforma, 2018</h5></li>
</ul>
</div>
So i need whole list items to be aligned alongside with logo
Use align-item: center on the .myClass element:
.logoHeader {
width: 10vw;
display: inline;
}
ul li {
display: inline-flex;
}
span {
font-size: 0.5em;
}
.myClass {
display: inline-flex;
align-items: center;
}
<div class="myClass"><img class="logoHeader" src="https://s.imgur.com/desktop-assets/desktop-assets/Navbar-logo.bcf646386406b43008da913e901b916d.svg" width="500" height="53" alt="">
<ul>
<li>
<h5><span>A</span>Put Iva Vizina 165</h5>
</li>
<li>
<h5><span>T</span>T +382 69 710 025</h5>
</li>
<li>
<h5><span>Random Crap</span> Plaforma, 2018</h5>
</li>
</ul>
</div>

Vertically align multiple lines of text in a div [duplicate]

This question already has answers here:
How can I vertically align elements in a div?
(28 answers)
Closed 6 years ago.
I'm currently writing a RSS parser and I use Ractive to generate the view. I'm trying to vertically align the contents of .feed-text-offset according to the height of .feed-item.
How can I accomplish this?
Demo: https://jsfiddle.net/zzx5L4e9/
The output HTML is like this:
<div class="feed-item">
<div class="news-img-container">
<img class="news-thumbnail" src="#" style="max-width: 125px;">
</div>
<div class="feed-text-offset">
Text here
<span class="feed-item-since">1 hr</span></div>
<div style="clear: both;">
</div>
</div>
CSS
.feed-item {
padding: 2px;
background-color: green;
float:left;
}
.feed-item-since {
font-size: 0.8em;
font-weight: 400;
margin-top: 5px;
display: block;
}
.news-img-container {
width: 125px;
float: left;
}
.feed-text-offset {
margin-left: 130px;
}
Found my answer from https://stackoverflow.com/a/31078418/969894 and thanks #GCyrillus
I can apply the following to .feed-item and re-adjust the text offset to vertically align the contents of .feed-text-offset
display: flex;
align-items: center;