**I am a beginner in Web dev, and I am trying a small project to apply what I have learnt so far but I have hit a brick wall and feel frustrated and haven’t learned anything. **
I want to position or alight my h1 and p
to the right of my image, how do I do that? As well as center my image and text in the middle of the page. I am very confused. I gave in after some time and asked ChatGPT to help, I feel very guilty but I made things worse.
I have learned about flexbox many months ago and forgot it, some of the code here was given by ChatGPT.
I don’t write on Stack Overflow because I am anxious and nervous of people’s judgement. Please forgive me for any mistakes.
Below is my HTML and CSS code. I am still struggling with Bootstrap, so I am not using it in this project.
.my-info {
display: flex;
align-items: center;
}
.my-info img {
border-radius: 50%;
width: 25%;
height: auto;
margin-right: 20px;
}
.my-info div {
display: flex;
flex-direction: column;
justify-content: center;
}
h1 {
color: #f1efe8;
font-family: 'Droid serif', serif;
font-size: 36px;
font-weight: 400;
font-style: italic;
line-height: 44px;
margin: 0 0 12px;
text-align: center;
}
.heading {
background-image: url("https://mcdn.wallpapersafari.com/medium/50/40/Tnh47t.jpg");
background-size: cover;
width: 100vw;
height: 50vh;
display: flex;
justify-content: center;
align-items: center;
}
.name {
font-size: 36px;
font-weight: 400;
color: black;
font-family: Verdana, Geneva, Tahoma, sans-serif;
margin: 0;
}
.job {
font-size: 26px;
font-weight: 400;
color: black;
font-family: Verdana, Geneva, Tahoma, sans-serif;
text-align: center;
margin: 0;
}
<div class="heading">
<h1>The World of me: Portfolio</h1>
</div>
<div class="my-info">
<div>
<img class="me" src="CSS/images/me.jpg" alt="Nevin">
</div>
<div class="personal">
<h1 class="name">My name is <strong>Nevin Dabechuran</strong></h1>
<p class="job"><em>A Web designer</em></p>
</div>
</div>
I want it to look something like this:Image with text next to it
4
Answers
first of all, dont be like that. you have two problem, right.
1.make
h1
&p
to right of the image. to do that just reorder thehtml
elements:centering them,
thats it ig. just feel free to ask further doubt. after all this place is built up of doubts and questions😂
you have given image width in % that is why its taking that much space.Also i have added code for aligning the content
Your css does position your name and job elements to the right of your image but to shift it to the right of its parent div, use
display:block
andmargin-left:auto
. You seem to want a margin right as well so we can set both usingmargin-inline
.To center your
.my-info
div, all you have to do is to shrink it to the width of the content (it’s 100% of the screen width by default so it’s already ‘in the center’ so to speak). We then usemargin: auto
to apply the same size margin either side.Centering things has, historically, been a PITA in web development. Here’s an article from w3c to help you. There’s also a nice utilty called howtocenterincss that you can use to auto generate css to achieve what you’re looking for.
You’ve also got some superflous css. I’ve commented it out and annotated it to explain how it works.
Adding a CSS reset to your code is also recommended e.g. adding margin:0 to your body element. It removes some of the unpredicatability from the user agent stylesheet. Here’s a short video from Kevin Powell to explain this (if you don’t follow him already, he’s great for junior devs and people starting out – his courses are worthwhile). Good luck in your career.
P.S. just comment if you have any questions and I’ll answer them.
You can achieve this by using float (the old way) or flexbox.
Here is a flexbox example based on your