How to position my h1(my name is..) and <p>( A wed…) next to my image, to the right of the image

huangapple go评论73阅读模式
英文:

How to position my h1(my name is..) and <p>( A wed...) next to my image, to the right of the image

问题

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.

我是Web开发的初学者,我正在尝试一个小项目来应用我迄今为止学到的知识,但我遇到了难题,感到沮丧,没有学到什么。

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.

我想将我的h1和p放在图像的右侧,该怎么做?还要将图像和文本居中在页面中央。我感到很困惑。经过一段时间后,我屈服于ChatGPT的帮助,我感到很内疚,但我把事情搞得更糟了。

I have learned about flexbox many months ago and forgot it, some of the code here was given by ChatGPT.

几个月前我学过flexbox,但现在忘了,这里的一些代码是由ChatGPT提供的。

I don't write on Stack Overflow because I am anxious and nervous of people's judgement. Please forgive me for any mistakes.

我不在Stack Overflow上发帖,因为我感到焦虑,害怕别人的评判。请原谅我任何的错误。

Below is my HTML and CSS code. I am still struggling with Bootstrap, so I am not using it in this project.

以下是我的HTML和CSS代码。我仍然在努力学习Bootstrap,所以在这个项目中没有使用它。

<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>
.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;
}

I want it to look something like this: Image with text next to it

英文:

**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.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.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: &#39;Droid serif&#39;, serif;
  font-size: 36px;
  font-weight: 400;
  font-style: italic;
  line-height: 44px;
  margin: 0 0 12px;
  text-align: center;
}

.heading {
  background-image: url(&quot;https://mcdn.wallpapersafari.com/medium/50/40/Tnh47t.jpg&quot;);
  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;
}

<!-- language: lang-html -->

&lt;div class=&quot;heading&quot;&gt;
  &lt;h1&gt;The World of me: Portfolio&lt;/h1&gt;
&lt;/div&gt;

&lt;div class=&quot;my-info&quot;&gt;
  &lt;div&gt;
    &lt;img class=&quot;me&quot; src=&quot;CSS/images/me.jpg&quot; alt=&quot;Nevin&quot;&gt;
  &lt;/div&gt;
  &lt;div class=&quot;personal&quot;&gt;
    &lt;h1 class=&quot;name&quot;&gt;My name is &lt;strong&gt;Nevin Dabechuran&lt;/strong&gt;&lt;/h1&gt;
    &lt;p class=&quot;job&quot;&gt;&lt;em&gt;A Web designer&lt;/em&gt;&lt;/p&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

I want it to look something like this:Image with text next to it

答案1

得分: 0

1.首先,不要这样做。你有两个问题,对吗?

  1. h1p 放到图片的右侧。要做到这一点,只需重新排列 html 元素:
<div class="my-info">
    <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>
        <img class="me" src="CSS/images/me.jpg" alt="Nevin">
    </div>
</div>
  1. 将它们居中对齐,
.my-info {
    display: flex;
    justify-content: center;
}

就这样了,如果还有疑问,请随时提问。毕竟这个地方就是由疑问和问题构建起来的。 😊

英文:

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 the html elements:

&lt;div class=&quot;my-info&quot;&gt;
    &lt;div class=&quot;personal&quot;&gt;
        &lt;h1 class=&quot;name&quot;&gt;My name is &lt;strong&gt;Nevin Dabechuran&lt;/strong&gt;&lt;/h1&gt;
        &lt;p class=&quot;job&quot;&gt;&lt;em&gt;A Web designer&lt;/em&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div&gt;
        &lt;img class=&quot;me&quot; src=&quot;CSS/images/me.jpg&quot; alt=&quot;Nevin&quot;&gt;
&lt;/div&gt;
  1. centering them,

     .my-info {
         display: flex;
         justify-content:center;
     }
    

thats it ig. just feel free to ask further doubt. after all this place is built up of doubts and questions😂

答案2

得分: 0

你已经以百分比给定了图像宽度,这就是为什么它占用了那么多空间。此外,我已经添加了用于对齐内容的代码。

<div class="heading">
  <h1>我的世界:个人作品集</h1>
</div>

<div class="my-info">
  <div>
    <img class="me" src="https://png.pngtree.com/element_pic/17/09/23/fa56a61d288b1922002f4f7edf5ce3e6.jpg" alt="Nevin">
  </div>
  <div class="personal">
    <h1 class="name">我的名字是 <strong>Nevin Dabechuran</strong></h1>
    <p class="job"><em>Web设计师</em></p>
  </div>
</div>
.my-info {
  display: flex;
  align-items: center;
  justify-content: start;
}

.my-info img {
  border-radius: 50%;
  width: 70px;
  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;
}

.personal {
  align-items: start;
}
英文:

you have given image width in % that is why its taking that much space.Also i have added code for aligning the content

&lt;div class=&quot;heading&quot;&gt;
  &lt;h1&gt;The World of me: Portfolio&lt;/h1&gt;
&lt;/div&gt;

&lt;div class=&quot;my-info&quot;&gt;
  &lt;div&gt;
    &lt;img class=&quot;me&quot; src=&quot;https://png.pngtree.com/element_pic/17/09/23/fa56a61d288b1922002f4f7edf5ce3e6.jpg&quot; alt=&quot;Nevin&quot;&gt;
  &lt;/div&gt;
  &lt;div class=&quot;personal&quot;&gt;
    &lt;h1 class=&quot;name&quot;&gt;My name is &lt;strong&gt;Nevin Dabechuran&lt;/strong&gt;&lt;/h1&gt;
    &lt;p class=&quot;job&quot;&gt;&lt;em&gt;A Web designer&lt;/em&gt;&lt;/p&gt;
  &lt;/div&gt;
&lt;/div&gt;
.my-info {
  display: flex;
  align-items: center;
  justify-content: start;
  
}

.my-info img {
  border-radius: 50%;
  width: 70px;
  height: auto;
  margin-right: 20px;
}

.my-info div {
  display: flex;
  flex-direction: column;
  justify-content: center;
}

h1 {
  color: #f1efe8;
  font-family: &#39;Droid serif&#39;, serif;
  font-size: 36px;
  font-weight: 400;
  font-style: italic;
  line-height: 44px;
  margin: 0 0 12px;
  text-align: center;
}

.heading {
  background-image: url(&quot;https://mcdn.wallpapersafari.com/medium/50/40/Tnh47t.jpg&quot;);
  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;
}
.personal{
  align-items:start;
}

答案3

得分: 0

你的CSS将你的名字和工作元素定位到图像的右侧,但要将其移到父div的右侧,使用display: blockmargin-left: auto。看起来你也想要右边有一些间距,所以我们可以使用margin-inline来同时设置两者。

要将.my-info div居中,只需将其宽度缩小到内容的宽度(默认情况下它占据了整个屏幕宽度,所以它已经是“居中”了),然后使用margin: auto来在两侧应用相同大小的边距。

在Web开发中,居中对很多人来说一直是个问题。这里有一篇来自w3c的文章可以帮助你。还有一个很不错的工具叫做howtocenterincss,可以用来自动生成CSS以实现你想要的效果。

你的CSS中也有一些多余的部分。我已经将它们注释掉并添加了注释来解释它们的作用。

建议在你的代码中添加CSS重置,比如在body元素中添加margin: 0。这可以消除来自user agent stylesheet的一些不可预测性。这里有一段来自Kevin Powell的视频,解释了这个问题(如果你还没有关注他,他对初级开发人员和初学者非常有帮助,他的课程也很值得一试)。祝你在职业生涯中好运。

P.S. 如果有任何问题,请留下评论,我会回答它们。

body {
  /* 你在几个元素中都使用了这个样式,所以我们将它设置为默认值,然后只需在需要时更改字体族 */
  font-family: Verdana, Geneva, Tahoma, sans-serif;
  color: black;
}

* {
  /* CSS重置 - 将margin设置为0。这意味着你可以从你的h1和p类中删除margin: 0 */
  margin: 0;
}

.my-info {
  display: flex;
  align-items: center;
  /* 要使你的信息容器居中,只需将宽度缩小到内容的宽度,然后使用margin:auto在左右两侧添加空间。 */
  width: fit-content;
  margin: auto;
}

.my-info img {
  border-radius: 50%;
  width: 25%;
  /* 不需要height: auto; height: auto是默认值 */
  display: block; /* 这允许我们定位图像。将图像设置为块级元素是推荐的 */
  margin-inline: auto 20px;
  /* 删除了margin-right: 20px;,因为已经在margin-inline中声明 */
}

.my-info div {
  /* 不需要这个 - 所以删除了它,my.info h1已经将文本居中对齐,所以这里不需要再次设置 */
}

/* 将这个样式从h1更改为.heading-title,并在你的HTML中添加这个类 - 这是因为它也会样式化'my-info' div中的h1,所以我们最终会重复样式化它两次 */
.heading-title {
  color: #f1efe8;
  font-family: 'Droid serif', serif;
  font-size: 36px;
  line-height: 44px;
  margin: 0 0 12px;
  /* 不需要text-align: center;,因为我们在.heading div中使用了place-items:center,它会自动将内容居中对齐 */
}

.heading {
  background-image: url("https://mcdn.wallpapersafari.com/medium/50/40/Tnh47t.jpg");
  background-size: cover;
  height: 50vh;
  /* 不需要display: flex; justify-content: center; align-items: center; 使用grid,可以省略一行CSS :-) */
  display: grid;
  place-items: center;
}

.name {
  font-size: 36px;
}

.job {
  font-size: 26px;
  text-align: center;
}

.name, .job, .heading-title {
  /* 你在上面的CSS中一直重复这个,只需在一行中声明它,这样如果需要更改,只需更改一次 */
  font-style: italic;
  font-weight: 400;
}
<div class="heading">
  <h1 class="heading-title">The World of me: Portfolio</h1>
</div>
<div class="my-info">
  <div>
    <img class="me" src="https://picsum.photos/id/433/200/200" 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>
英文:

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 and margin-left:auto. You seem to want a margin right as well so we can set both using margin-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 use margin: 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.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

body {
/* you use this in a few elements so we&#39;ll set this as the default in your body, then you only need to add font-family if you need to change away from this */
font-family: Verdana, Geneva, Tahoma, sans-serif;
color:black;
}
* { 
/* css reset - set margin to 0. It means you can remove margin:0 from your h1 and p classes below */
margin:0;
}
.my-info {
display: flex;
align-items: center;
/*To center your info container, set the width to shrink to the content and then use margin:auto to add space evenly left and right.*/
width: fit-content;
margin:auto;
}
.my-info img {
border-radius: 50%;
width: 25%;
/* height: auto; not needed - height: auto is the default */
display:block;  /*this allows us to position the image. Setting images to block level elements is recommended imho*/
margin-inline: auto 20px;
/*margin-right: 20px; deleted this, already declared in margin-inline */
}
.my-info div {
/* Not needed - so deleted it my.info h1 already aligns your text in the center so you don&#39;t need to do this here. It&#39;s already covered
display: flex;
flex-direction: column;
justify-content: center; 
*/
}
/* changed this from h1 to .heading-title and added the class to your html - this is because it&#39;s also styling the h1 in your &#39;my-info&#39; div so we end up styling that one twice */
.heading-title {
color: #f1efe8;
font-family: &#39;Droid serif&#39;, serif;
font-size: 36px;
line-height: 44px;
margin: 0 0 12px;
/* text-align: center; not needed as we&#39;ve used place-items:center in the .heading div which automatically centers the content */
}
.heading {
background-image: url(&quot;https://mcdn.wallpapersafari.com/medium/50/40/Tnh47t.jpg&quot;);
background-size: cover;
/*width: 100vw; you don&#39;t need this as block level elements are 100% of the width of the viewport by default */
height: 50vh;
/*display: flex;
justify-content: center;
align-items: center; use grid here, you can save yourself a line of CSS :-) */
display:grid;
place-items:center;
}
.name {
font-size: 36px;
}
.job {
font-size: 26px;
text-align: center;
}
.name, .job, .heading-title {
/* we keep repeating this in your css above. Just declare it in one line so if you need to change it, you only need to change it once */
font-style: italic;
font-weight: 400;
}

<!-- language: lang-html -->

&lt;div class=&quot;heading&quot;&gt;
&lt;h1 class=&quot;heading-title&quot;&gt;The World of me: Portfolio&lt;/h1&gt;
&lt;/div&gt;
&lt;div class=&quot;my-info&quot;&gt;
&lt;div&gt;
&lt;img class=&quot;me&quot; src=&quot;https://picsum.photos/id/433/200/200&quot; alt=&quot;Nevin&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;personal&quot;&gt;
&lt;h1 class=&quot;name&quot;&gt;My name is &lt;strong&gt;Nevin Dabechuran&lt;/strong&gt;&lt;/h1&gt;
&lt;p class=&quot;job&quot;&gt;&lt;em&gt;A Web designer&lt;/em&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案4

得分: 0

以下是翻译好的部分:

你可以通过使用浮动(旧方式)或弹性盒子(flexbox)来实现这一点。

这是一个基于弹性盒子(flexbox)的示例,基于你的要求:

.profile {
    display: flex;
    align-items: center;
    column-gap: 10px;
    margin: 20px;
    border: 1px solid black;
    padding: 5px;
    width: 300px;
    background: #eaeaea;
}

.profile .thumb {
    width: 100px;
    height: 100px;
}

.profile .thumb img {
    width: 100%;
    height: auto;
}

.profile .name {
    margin: 0;
    font-family: serif;
    font-weight: bold;
    font-size: 18px;
    color: black;
}

.profile .job {
    margin: 0;
    font-family: sans-serif;
    font-size: 14px;
    color: black;
}
<div class="profile">
    <div class="thumb">
        <img src="https://randomuser.me/api/portraits/lego/1.jpg" alt="Nevin">
    </div>
    <div class="info">
        <h1 class="name">Nevin Dabechuran</h1>
        <p class="job">Web designer</p>
    </div>
</div>
英文:

You can achieve this by using float (the old way) or flexbox.

Here is a flexbox example based on your

> I want it to look something like this

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.profile {
display: flex;
align-items: center;
column-gap: 10px;
margin: 20px;
border: 1px solid black;
padding: 5px;
width: 300px;
background: #eaeaea;
}
.profile .thumb {
width: 100px;
height: 100px;
}
.profile .thumb img {
width: 100%;
height: auto;
}
.profile .name {
margin: 0;
font-family: serif;
font-weight: bold;
font-size: 18px;
color: black;
}
.profile .job {
margin: 0;
font-family: sans-serif;
font-size: 14px;
color: black;
}

<!-- language: lang-html -->

&lt;div class=&quot;profile&quot;&gt;
&lt;div class=&quot;thumb&quot;&gt;
&lt;img src=&quot;https://randomuser.me/api/portraits/lego/1.jpg&quot; alt=&quot;Nevin&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;info&quot;&gt;
&lt;h1 class=&quot;name&quot;&gt;Nevin Dabechuran&lt;/h1&gt;
&lt;p class=&quot;job&quot;&gt;Web designer&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月13日 18:31:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76004403.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定