How can i center image in any div in HTML & CSS ?
I have a nav bar that uses images as links for other pages, the nav bar is multiple divs each has an image and anther div contains a words, how can I center this img in the div
The CSS:
#Nav
{
height: 150px;
margin-top: 50px;
}
#Nav ul
{
list-style-type: none;
padding: 50px;
padding-top: 0px;
width: 100%;
}
#Nav li
{
float: left;
font-size: 12px;
font-weight: bold;
letter-spacing: 0;
margin: 30px;
}
#Nav a
{
display: inline-block;
padding: 5px;
width: 70px;
height: 100px;
color: black;
text-decoration: none;
}
#Nav a:hover
{
border: 2px solid #ddd;
border-radius: 4px;
box-shadow: 0 0 2px 1px rgba (0, 140, 186, 0.5);
}
#img img
{
align-self: middle;
width: 50px;
height: 50px;
}
#desc
{
margin-top: 15px;
text-align: center;
}
The html:
<li> <a target="_blank" href="#">
<div id="img"> <img src="C:/Users/hp1/Desktop/Website/Pictures/Accessories.jpg" alt="Accessories">
<div id="desc"> Accessories </div>
</div>
</a> </li>
You need to change your CSS for image like below:-
#img img
{
display: block;
margin: 0 auto;
}
You can see Many Demos Here-
Note that align-self is for elements inside a flexbox container which is not your case, you can try with text-align: center; on img.
Or if you wish to go full flexbox, set the img container to:
.containerClass {
display: flex;
flex-flow: column wrap;
justify-content: center;
}
With this setup align-self on the img is not need since justify-content on it's container will do.
Flexbox to the rescue:
.parent {
width:200px; height:200px;
border:2px solid #000;
display: flex; /* Make it flex */
flex-direction: column; /* wrap children elements to columns */
align-items: center; /* Center children horizontally */
justify-content: center; /* Center children vertically */
}
<div class="parent ">
<img src="http://placehold.it/100x100/cf5">
<p>Green-ish</p>
</div>
<div class="parent ">
<img src="http://placehold.it/100x100/5fc">
<p>Blue-ish</p>
</div>
align-self: center; is valid, align-self: middle; is not valid.
However, you can probably achieve what you're wanting with text-align: center;. Despite it being an image, you can still center using text-align as images are, by default, inline elements just like text.
#img img
{
align-self: center;
width: 50px;
height: 50px;
}
OR
#img img
{
text-align: center;
width: 50px;
height: 50px;
}
First you have to change the nature of div to table then make its align to center like this
#img img
{
display:table;
align: center;
width: 50px;
height: 50px;
}
Related
So I am trying to create a logo and a menu icon in the header but for some reason, they are always overflowing the height of the header which I have strictly specified! Why is that ?
And I know I can hide out the overflowing items by using overflow:hidden; property but it is not always a good case.
For example, I tried to create a hamburger icon but I could not because of this overflow issue. The menu lines were working as if the entire element is shown but I had to hide it out so that it could fit into the header.
Here is the code -
<header>
<div class="logo">
Elvis
</div>
<div class="menu">
Hamburger Menu
</div>
</header>
In CSS -
*{
padding:0px;
margin:0px;
}
header{
height: 60px;
display: flex;
justify-content: space-between;
align-items: center;
border: 2px solid red;
}
.logo {
font-size: 33px;
text-decoration: none;
padding: 20px 30px;
background-color: green;
color: white;
}
.menu {
height: 100px;
width: 100px;
background-color: #bd4439;
}
Here is the codepen link -
https://codepen.io/raghav-sharma333/pen/eYeZYGO
Here is the image of the issue -
Overflowing content
So I just want to know :
Why is it happening?
&
How can it be prevented?
Basically you are forcing your elements to be higher than the header itself by giving them static heights (height 100px on the menu and padding-top/bottom 30px on the logo)
I updated your pen: https://codepen.io/penmasterx/pen/wvPGaGz
Using height 100%, so the elements adapt to the header.
Let me know if this solves your problem. If not, let me know in more detail what you're trying to accomplish.
What I added to the pen:
.logo {
height: 100%;
display: flex;
align-items: center;
/* removed padding top/bottom */
}
.menu {
height: 100%;
}
In such cases, it is better to use the position to manage the inheritance of the elements
I modified your code:
*{
padding:0px;
margin:0px;
}
header{
height: 60px;
align-items: center;
border: 2px solid red;
position: relative;
}
.wrapper{
display: flex;
justify-content: space-between;
width: 100%;
height: 100%;
position: absolute;
}
.logo {
font-size: 30px;
text-decoration: none;
padding: 20px 30px;
background-color: green;
max-height: 100%;
color: white;
}
.menu {
height: 100%;
width: 100px;
background-color: #bd4439;
}
<header>
<div class="wrapper">
<div class="logo">Elvis</div>
<div class="menu">Hamburger Menu</div>
</div>
</header>
First: the reason you use a 33px font which adds padding, then you use a height:100px on the menu while on your header you put a height:60px
you also need to add align-self: center on your flex-box
*{
padding:0px;
margin:0px;
}
header{
height: 60px;
display: flex;
justify-content: space-between;
align-items: center;
align-self: center;
border: 2px solid red;
}
.logo {
font-size: 17px;
text-decoration: none;
padding: 20px 30px;
background-color: green;
color: white;
}
.menu {
height: 60px;
width: 100px;
background-color: #bd4439;
}
I did it like 'Ali Memar' answer but the difference is the position of the texts. they are now in the middle of the div.
*{
padding:0px;
margin:0px;
}
header{
height: 60px;
align-items: center;
border: 2px solid red;
position: relative;
}
.wrapper{
display: flex;
justify-content: space-between;
width: 100%;
height: 100%;
position: absolute;
}
.logo {
font-size: 30px;
text-decoration: none;
padding: 20px 30px;
background-color: green;
max-height: 100%;
color: white;
text-align: center;
display: flex;
align-items: center
}
.menu {
height: 100%;
width: 100px;
background-color: #bd4439;
text-align: center;
display: flex;
align-items: center
}
<header>
<div class="wrapper">
<div class="logo">Elvis</div>
<div class="menu">Hamburger Menu</div>
</div>
</header>
I'm currently designing a header bar for a site I'm working on. I would like the header to show a logo at the left, the title in the center, and the user account on the right.
This mockup is what I'm envisioning. Note that the dotted boxes denote a div.
I've made some progress on creating it in HTML/CSS, but I can't seem to get the title to center to the viewport.
As you can see, the title is centering itself between the logo and the account info divs, instead of centering itself on the viewport. This ends up making the page just look a little bit off, and so I would really like to center the title to the viewport.
Here's my code:
.headerBarArea {
background-color: #7785A2;
border: 0;
padding: 0;
width: 100%;
display: inline-block;
text-align: center;
}
.logoArea {
display: inline;
text-align: center;
float: left;
border: lawngreen;
border-style: dashed;
border-width: 1px;
}
.minesLogo {
width: 96px;
}
.titleArea {
display: inline-block;
text-align: center;
border: lawngreen;
border-style: dashed;
border-width: 1px;
}
.siteTitle {
color: white;
}
.pageTitle {
color: white;
}
.userAccountArea {
display: inline;
text-align: center;
float: right;
border: lawngreen;
border-style: dashed;
border-width: 1px;
}
.userAccountIcon {
float: left;
width: 35px;
}
.userAccountText {
float: right;
}
<div className="headerBarArea">
<div className="logoArea">
<img src="assets/mines_logo_stacked.png" className="minesLogo" />
</div>
<div className="titleArea">
<h2 className="siteTitle">This is my site Title</h2>
<h3 className="pageTitle">Page Title</h3>
</div>
<div className="userAccountArea">
<img src="assets/user_account.png" className="userAccountIcon" />
<span className="UserAccountText">John Smith (Student)</span>
</div>
</div>
Any ideas on what I could do to make the title div centered to the viewport, instead of centering between the two divs?
html code
<div class="flex-container">
<div class="flex-item">1</div>
<div class="align-self-center">
<span class="siteTitle">This is my site Title</span>
<br>
<div class="text-center ">Page Title</div>
</div>
<div class="flex-item align-self-end third-item">3</div>
</div>
CSS code
.flex-container {
/* We first create a flex layout context */
display: flex;
/* Then we define the flow direction
and if we allow the items to wrap
* Remember this is the same as:
* flex-direction: row;
* flex-wrap: wrap;
*/
flex-flow: row wrap;
/* Then we define how is distributed the remaining space */
justify-content: space-between;
}
.flex-item {
background: tomato;
padding: 5px;
width: 200px;
height: 150px;
margin-top: 10px;
line-height: 150px;
color: white;
font-weight: bold;
text-align: center;
}
.third-item {
height: 100px;
}
.text-center{
text-align: center;
}
.align-self-end{
align-self:end;
}
.align-self-center{
align-self:center;
}
code output
code solution
used flex to place the items used .flex-container as parent div where flex items are placed in .justify-content: space-between; is used to place space in between the items. align-self:center; is used to place Page Title at center
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
codepen
.headerBarArea{
display:flex;
justify-content: space-between;
align-items: center
}
It is an easy way to the layout.
you can try it.
Try adding margin-left:auto; and margin-right:auto; to the .titleArea class .
I would suggest using a flex-box though.
Replace your css code with this to make title div centered in all the viewport.
.headerBarArea {
background-color: #7785A2;
border: 0;
padding: 0;
width: 100%;
display:flex;
justify-content: space-between;
}
.logoArea , .titleArea , .userAccountArea {
border: lawngreen;
border-style: dashed;
border-width: 1px;
}
.minesLogo {
width: 96px;
}
.titleArea {
text-align: center;
}
.siteTitle , .pageTitle{
color: white;
}
.userAccountIcon {
float: left;
width: 35px;
}
.userAccountText {
float: right;
}
I have a div, containing an image, and another div below it for description, am aligning them on top of each other with flexbox, however I have a hidden div that I want to show in place of the image when I hover over the tile, but currently my code is just not working and am unable to give this div the full height of the area on hover. Here's my code:
:root{
--colorOrange: #ef5b2b;
--colorGrey: #f5f5f5;
--colorBlue: #0a2756;
}
.presenter {
width: 23%;
max-height: 320px;
box-sizing: border-box;
display: flex;
flex-direction: column;
margin-bottom: 2rem;
}
.presenter img{
width: 100%;
}
.presenter-solid{
background: var(--colorBlue);
color: white;
font-weight: 700;
text-transform: uppercase;
font-family: Poppins;
display:none;
}
.presenter:hover img{
display:none;
}
.presenter:hover .presenter-solid{
display:block;
height: 100%;
}
<div class="presenter">
<div class="presenter-image-holder">
<img src="https://cdn2.vectorstock.com/i/thumb-large/80/91/person-gray-photo-placeholder-little-boy-vector-22808091.jpg" />
<div class="presenter-solid">
<span>TOM CRUISE</span>
</div>
</div>
<div class="presenter-role">
<span class="role-name">Hollywood Actor</span>
</div>
</div>
It's also giving me a flicker effect when trying to hover. How can I fix my code? What am I doing wrong?
use display: flex; align-items: center; justify-content: center to center the text.
:root{
--colorOrange: #ef5b2b;
--colorGrey: #f5f5f5;
--colorBlue: #0a2756;
}
.presenter {
display: inline-block;
position: relative;
margin-bottom: 20px;
}
.presenter-image-holder {
position: relative; /* this must be added to the parent element */
width: 250px; /* width of the image box */
height: 250px; /* height of the image box */
margin-bottom: 10px;
}
.presenter img{
max-width: 100%;
height: auto;
}
.presenter-solid{
background: var(--colorBlue);
color: white;
font-weight: 700;
text-transform: uppercase;
font-family: Poppins;
display:none;
}
.presenter:hover img{
display:none;
}
.presenter:hover .presenter-solid{
height: 100%;
width: 100%;
display: flex;
align-items: center; /* to vertically center the text */
justify-content: center; /* to horizontally center the text */
}
<div class="presenter">
<div class="presenter-image-holder">
<img src="https://cdn2.vectorstock.com/i/thumb-large/80/91/person-gray-photo-placeholder-little-boy-vector-22808091.jpg" />
<div class="presenter-solid">
<span>TOM CRUISE</span>
</div>
</div>
<div class="presenter-role">
<span class="role-name">Hollywood Actor</span>
</div>
</div>
I'm working on a website and wanted to see how I could vertically center h2 and the sub-heading with respect to the div that contains the image.
Basically, I have a container with display: flex and flex-direction: column. I have three columns.
The first column contains a profile image along with a header and sub-header. What's the best way to vertically center the .title div?
Here's a link to the jsfiddle
give display: flex to class profile-container, then you can align it's child profile image and title class to vertically center like below:
.profile-container{
display : flex;
align-items : center;
height : 200px; //provide wrapper height
}
This should achieve your requirement.
Try using absolute positioned, with a -50% transform.
#import url('https://fonts.googleapis.com/css?family=Lato');
html {
width: 100%;
height: 100%;
}
body {
background-color: #fafafa;
display: flex;
justify-content: center;
align-items: center;
height: 100%;
}
.container {
background: white;
width: 1000px;
height: 600px;
border: 1px solid rgba(0, 0, 0, .09);
box-shadow: 0 1px 4px rgba(0, 0, 0, .04);
display: flex;
flex-direction: column;
}
/*set up vertical column layouts for the three containers*/
.profile-container,
.work-container,
.social-media-container {
flex: 1;
padding: 1.5rem;
}
.profile-container {
align-self: center;
position: relative;
width: 100%;
}
.profile-image {
display: inline-block;
background-image: url('https://organicthemes.com/demo/profile/files/2012/12/profile_img.png');
background-repeat: no-repeat;
background-position: 0px 0px;
background-size: 100%;
height: 200px;
width: 200px;
border-radius: 50%;
}
.title {
display: inline-block;
position: absolute;
top: 50%;
transform: translateY(-50%);
padding-left: 25px;
}
.header-name {
font-family: 'Lato', sans-serif;
display: inline-block;
margin: 0px;
}
<div class="container">
<div class="profile-container">
<div class="profile-image"></div>
<div class="title">
<h2>Tarang Hirani</h2>
<h6>Software Engineer</h6>
</div>
</div>
<div class="work-container">
These are my projects
</div>
<div class="social-media-container">
Social media link
</div>
</div>
Link for reference: https://jsfiddle.net/kzj4omo8/3/
flexbox should make things a lot simpler it seems, but I can't seem to get the text links and images in this menu to align vertically. The text links and images are centered vertically and horizontally instead. What's missing? I'm probably also over doing it as if I were using tables and vertical-align. I want everything to align to the bottom.
* {
padding: 0;
margin: 0;
}
header > div {
display: flex;
position: relative;
}
header > div > a {
border: solid 1px #000;
float: left;
align-self: flex-end;
height: 279px;
line-height: 279px;
}
header > div > ul {
border: solid 1px #000;
float: left;
align-self: flex-end;
height: 279px;
line-height: 279px;
}
header .menu li {
display: inline-block;
float: left;
height: 279px;
line-height: 279px;
}
header .menu li a {
display: block;
align-self: flex-end;
height: 279px;
line-height: 279px;
}
<header>
<div>
<a href="#">
<img src="http://eu2013.ie/media/eupresidency/content/imagegalleryitems/twitter-bird-blue-on-white-small.png" />
</a>
<ul class="menu">
<li>A</li>
<li>B</li>
<li style="background: url('http://printready.co/wp-content/uploads/2012/08/facebook-like-icon-300x279.png') no-repeat; width: 300px; height: 279px;"></li>
</ul>
</div>
</header>
https://jsfiddle.net/rpbwdvxe/1/
Set flex on header > div's children, and align-self on its grandchildren:
header > div {
display: flex;
}
header > div > * {
display: flex;
}
header > div > * > * {
align-self: flex-end;
}
Working Fiddle
Try this
header > div > a {
border: solid 1px #000;
float: left;
height: 110px;
line-height: 110px;
display: table-cell;
vertical-align: middle;
}