How can I vertically center an element? [duplicate] - html

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 7 months ago.
How can I vertically center the button?
Image of a problem
I started to learn HTML&CSS and I don't know how can I align the button to vertical center of a div in a proper way.
None of text-align:center etc. doesn't work for me.
Second question: How can I make the outer one div same height as a middle-div?
p {
font-family: Roboto, Arial;
margin-top: 0;
margin-bottom: 0;
}
.user {
font-weight: 500;
}
.photo {
width: 46px;
border-radius: 28px;
}
.user-name,
.user-description,
.status {
width: 150px;
}
.avatar,
.middle-div,
.follow {
display: inline-block;
}
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght#400;500;700&display=swap" rel="stylesheet">
<div>
<div class="avatar">
<img class="photo" src="av1.jpg">
</div>
<div class="middle-div">
<div>
<div class="user-name">
<p class="user">
oliver
</p>
</div>
<div class="user-description">
<p class="user-desc">
the cat
</p>
</div>
<div class="status">
<p class="status-desc">
Popular
</p>
</div>
</div>
</div>
<div class="follow">
<button class="follow-button">Follow</button>
</div>
</div>

I have edited your code with the changes. I added the card class on the card and made it a flexbox by adding the following display: flex;
and align-items: center;.
p {
font-family: Roboto, Arial;
margin-top: 0;
margin-bottom: 0;
}
.user {
font-weight: 500;
}
.photo {
width: 46px;
border-radius: 28px;
}
.user-name,
.user-description,
.status {
width: 150px;
}
.card {
display: flex;
align-items: center;
}
<div class="card">
<div class="avatar">
<img class="photo" src="av1.jpg">
</div>
<div class="middle-div">
<div>
<div class="user-name">
<p class="user">
oliver
</p>
</div>
<div class="user-description">
<p class="user-desc">
the cat
</p>
</div>
<div class="status">
<p class="status-desc">
Popular
</p>
</div>
</div>
</div>
<div class="follow">
<button class="follow-button">Follow</button>
</div>
</div>

Related

How to center images and a slider in one line [duplicate]

This question already has answers here:
Center one and right/left align other flexbox element
(11 answers)
Keep the middle item centered when side items have different widths
(12 answers)
Closed 1 year ago.
I would like to have 3 buttons in the middle and a slider on the right side, these on the same line. The problem is that when I center the buttons and float the slider to the right, the buttons are not in the middle anymore. So it looks like this (ignore the top slider):
.container {
text-align: center;
}
.music-buttons {
width: 50px;
height: 50px;
}
.volume-control {
margin-right: 10px;
float: right;
}
<div>
<input type="range" class="volume-control">
</div>
<div class="container">
<img class="music-buttons" src="https://via.placeholder.com/50">
<img class="music-buttons" src="https://via.placeholder.com/50">
<img class="music-buttons" src="https://via.placeholder.com/50">
</div>
It might be a mess, but I was just trying out stuff.
Try using this CSS code :
.container{
text-align: center;
}
.music-buttons{
width: 50px;
height: 50px;
}
.volume-control{
margin-right: 10px;
position: absolute;
right: 10px;
top: 22px;
}
<div class="div-range">
<input type="range" class="volume-control">
</div>
<div class="container">
<img class="music-buttons" src="../icons/skip-start-circle.svg">
<img class="music-buttons" src="../icons/play-circle.svg">
<img class="music-buttons" src="../icons/skip-end-circle.svg">
</div>
I hope this will help.
.container{
text-align: center;
}
.music-buttons{
width: 50px;
height: 50px;
}
.volume-control{
text-align: center;
height: 50px;
}
<div class="container">
<div class="row">
<div class="col">
<img class="music-buttons" src="https://img.icons8.com/ios/50/000000/skip-to-start--v1.png">
<img class="music-buttons" src="https://img.icons8.com/ios/50/000000/pause--v1.png">
<img class="music-buttons" src="https://img.icons8.com/external-kiranshastry-lineal-kiranshastry/64/000000/external-skip-multimedia-kiranshastry-lineal-kiranshastry.png">
<input type="range" class="volume-control">
</div>
</div>
</div>

HTML/css Flex centering div [duplicate]

This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
Closed 1 year ago.
I'm trying to mimic a website. I'm using flex for my divs, but they are not properly aligning.
What mine looks like: https://i.imgur.com/4k5ln1T.png
What I want it to look like: https://i.imgur.com/Wl6DY1t.png
My Code:
<div class="div1">
<img src="https://i.imgur.com/AbZMoEL.png" class="image" alt="cookie picture">
<p class=""> cookies are great! </p>
<a class="modulbutton" href="#open1">Open modal link</a>
<div class="div1">
<img src="https://i.imgur.com/kdOyxwV.png" class="image" alt="icecream picture">
<p class=""> Ice cream is great! </p>
<a class="modulbutton" href="#open1">Open modal link</a>
<div id="open1" class="modalwindow">
<div class="msgblock">
<h1>Heading</h1>
<p>some text</p>
<img src="2.jpg" alt="missing">
<div class=""> Close</div>
</div>
</div>
</body>
<footer>
<style>
.div1 {
width: 900px;
height: 400px;
border: 3px solid blue;
display: flex;
align-items: center;
}
.modulbutton{
background-color: green;
color: white;
padding: 3%;
text-decoration: none;
}
Any help would be greatly appreciated, I can't seem to align the description text and modal button properly on the bottom of the image.
Thanks!
what you need to do is put the p tag and a tag in a div under div1 then use flex direction in css on your div 1
<div class="div1">
<img src="https://i.imgur.com/AbZMoEL.png" class="image" alt="cookie picture">
<div> <p class="indent"> cookies are great! </p>
<a class="modulbutton" href="#open1">Open modal link</a>
</div>
</div>
.div1 {
width: 900px;
height: 400px;
border: 3px solid blue;
margin: 10px;
display: flex;
flex-direction: column;
align-items: center;
}

How to i place text under images that i have aligned to the center of the page?

I have the following lines of HTML and CSS to align 3 images to the centre of the page.
HTML:
<div class="row2">
<div align="center;" class="icon">
<img src="image/character.png" />
</div>
<div align="center;" class="icon">
<img src="image/online.png" />
</div>
<div align="center;" class="icon">
<img src="image/leaderboard.png" />
</div>
</div>
CSS:
.row2 {
display: flex;
align-items: center;
justify-content: center;
}
.icon {
position: relative;
display: inline-block;
float: left;
padding: 100px;
}
I assumed if i added text under the images it would align itself under the image, however it appears beside the image. how would i get the text to show up underneath?
Just put the text inside a <div>, which is a block element and will wrap below the image.
.row2 {
display: flex;
align-items: center;
justify-content: center;
}
.icon {
position: relative;
display: inline-block;
float: left;
padding: 50px;
}
.icon__text {
color: dodgerblue;
font-weight: bold;
text-align: center;
}
<div class="row2">
<div align="center;" class="icon">
<img src="https://picsum.photos/100" />
<div class="icon__text">Text 1</div>
</div>
<div align="center;" class="icon">
<img src="https://picsum.photos/100" />
<div class="icon__text">Text 2</div>
</div>
<div align="center;" class="icon">
<img src="https://picsum.photos/100" />
<div class="icon__text">Text 3</div>
</div>
</div>
Remove the float: left; from .icon – it contradicts the centering anyway.
Or, if you inist on that for some reason, add clear: both to the tag into which your text is wrapped (probably a <p> tag, but you didn't describe that). This will put the text under any floated element above it.

Align Img Vertically within Div HTML and CSS

I have found the following solution for aligning an img vertically within a div
https://stackoverflow.com/a/7310398/626442
and this works great for a basic example. However, I have had to extend this and I want a row with two bootstrap col-md-6 columns in it. In the first column I want a 256px image, in the second I want a h1, p and a button. I have to following HTML:
<div class="home-costing container">
<div class="row">
<div class="frame">
<span class="helper"></span>
<div class="col-md-6">
<img src="http://www.nijmegenindialoog.nl/wp-content/uploads/in.ico" height="256" width="256" />
</div>
<div class="col-md-6">
<h2>Header</h2>
<p>
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA<br /><br/>
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
</p>
<a class="btn btn-default"
href='#Url.Action("Index", "Products")'
role="button">
Learn More
</a>
</div>
</div>
</div>
</div>
and the following CSS:
.home-costing {
color: #fff;
text-align: center;
padding: 50px 0;
border-bottom: 1px solid #ff6500;
}
.home-costing h2 {
text-transform: uppercase;
font-size: 60px;
}
.home-costing p {
font-size: 18px;
}
.home-costing .frame {
height: 256px;
width: 256px;
border: 0;
white-space: nowrap;
text-align: center;
margin: 1em 0;
}
.home-costing .helper {
display: inline-block;
height: 100%;
vertical-align: middle;
}
.home-costing img {
vertical-align: middle;
max-height: 256px;
max-width: 256px;
}
The problem is that now the second column is no longer contained and the text does not wrap and goes off to the right.
How can I center align my image in the first column with the text in the right column and still get the correct wrapping in the second column?
Fiddler: https://jsfiddle.net/Camuvingian/1sc40rm2/2/
Your HTML needed updated, in Bootstrap, the div order should ALWAYS go .container > .row > .col- * - *, your code however went .container > .row > .frame > .col- * - *. I have corrected your HTML and now your code works.
HTML:
<div class="home-costing container">
<div class="row">
<div class="col-md-6">
<img src="http://www.nijmegenindialoog.nl/wp-content/uploads/in.ico" height="256" width="256" class="center-block" />
</div>
<div class="col-md-6">
<h2>UserCost</h2>
<p>Hello, I'm a paragraph</p>
<a class="btn btn-default"
href='#Url.Action("Index", "Products")'
role="button">
Learn More
</a>
</div>
</div>
</div>
</div>
Link to finished code example:
Codepen - Updated & working code
This fixes the word wrap issue also on the p tag.
CSS:
p {
font-size: 18px;
word-wrap: break-word;
}

CSS div's containing classes

So my logic of Div ID's and Classes must be WAY off.
Heres whats going on:
As you can see the blocks which say PS don't align center with the slider (Which is inside a container.
Here is my css:
/*Front Page Buttons */
#frontpage-Button-Cont {
height: 350px;
}
.button-cont {
width: 175px;
float: left;
margin-left: 10px;
margin-top: 10px;
height: 250px;
}
.thumbnail {
color: #fff;
font-size: 5em;
background: #1f4e9b;
width: 175px;
height: 135px;
text-align: center;
}
.pheader {
color: #DC143C;
min-width: 175px;
text-align: center;
}
.paragraph {
text-align: center;
}
#Align-content {
margin: 0 auto;
}
And here is the html:
<div id="frontpage-Button-Cont">
<div id="Align-content">
<div class="button-cont">
<div class="thumbnail">
PS
</div>
<div class="paragraph">
<div class="pheader">
HEADER
</div>
<p>dadaasdasdadadad
</div>
</div>
<div class="button-cont">
<div class="thumbnail">
PS
</div>
<div class="paragraph">
<div class="pheader">
HEADER
</div>
<p>dadaasdasdadadad
</div>
</div>
<div class="button-cont">
<div class="thumbnail">
PS
</div>
<div class="paragraph">
<div class="pheader">
HEADER
</div>
<p>dadaasdasdadadad
</div>
</div>
<div class="button-cont">
<div class="thumbnail">
PS
</div>
<div class="paragraph">
<div class="pheader">
HEADER
</div>
<p>dadaasdasdadadad
</div>
</div>
</div>
</div>
My theory is that I'm using classes incorrectly?
Thanks.
You can Add this to your CSS
#frontpage-Button-Cont {
width:100%;
}
#Align-content {
display:table;
}
With this your margin:o auto can work
View This Demo http://jsfiddle.net/VGPeW/
You need to make sure that the containing div (in this case frontpage-Button-Cont) is the same width as your slider above it. Then add the property text-align:center to the container. That should fix your issue.