为什么我不能将图像居中?

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

Why I can't center the images?

问题

<div class="activities">
    <h3>在Torshov要做的前三项活动</h3>
    <img class="activities-img" src="images/deniz.jpg">
    <img class="activities-img" src="images/muze.jpg">
    <img class="activities-img" src="images/bar.jpg">
</div>
body {
    margin: 0;
}

.activities-img {
    width: 100%;
    width: 100px;
    height: 100px;
    margin-left: auto;
    margin-right: auto;
    justify-content: center;
}

我想居中这些图像,我尝试了"justify-content"、"text-align"等方法,但都没有成功,你能帮我吗?谢谢。

英文:
&lt;div class=&quot;activities&quot;&gt;
            &lt;h3&gt;Top three activities to do at Torshov&lt;/h3&gt;
            &lt;img class=&quot;activities-img&quot; src=&quot;images/deniz.jpg&quot;&gt;
            &lt;img class=&quot;activities-img&quot; src=&quot;images/muze.jpg&quot;&gt;
            &lt;img class=&quot;activities-img&quot; src=&quot;images/bar.jpg&quot;&gt; 
        &lt;/div&gt;
body {
    margin: 0;
}

.activities-img {
    width: 100%;
    width: 100px;
    height: 100px;
    margin-left: auto;
    margin-right: auto;  
    justify-content: center;
}

I want to center the images, I have tried with "justify content", "text-align" and all but didn't work, could you help me? Thanks.

答案1

得分: 1

以下是您要翻译的内容:

With flexbox, changes in your html:
使用flexbox时,更改您的HTML:

Without flexbox, but the same html
不使用flexbox,但HTML保持不变:

You could need "text-align" to center de content of the h3 tag
您可能需要使用 "text-align" 来居中h3标签的内容。

英文:

There are several ways to do this. It depends on whether you should do it with flexbox or without flexbox. Also, it depends on whether the html can be changed or should not be changed.

With flexbox, changes in your html:

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

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

.activities-img {
  display: flex;
  flex-direction: column;
  align-items: center;
}
.activities-img img{
  width: 100px;
  height: 100px;
}

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

&lt;div class=&quot;activities&quot;&gt;
  &lt;h3&gt;
    Top three activities to do at Torshov
  &lt;/h3&gt;
  &lt;div class=&quot;activities-img&quot;&gt;
    &lt;img src=&quot;images/deniz.jpg&quot;&gt;
    &lt;img src=&quot;images/muze.jpg&quot;&gt;
    &lt;img src=&quot;images/bar.jpg&quot;&gt; 
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

Without flexbox, but the same html

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

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

.activities-img {
    display: block;
    width: 100px;
    height: 100px;
    margin-left: auto;
    margin-right: auto;
}

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

&lt;div class=&quot;activities&quot;&gt;
    &lt;h3&gt;Top three activities to do at Torshov&lt;/h3&gt;
    &lt;img class=&quot;activities-img&quot; src=&quot;images/deniz.jpg&quot;&gt;
    &lt;img class=&quot;activities-img&quot; src=&quot;images/muze.jpg&quot;&gt;
    &lt;img class=&quot;activities-img&quot; src=&quot;images/bar.jpg&quot;&gt; 
&lt;/div&gt;

<!-- end snippet -->

You could need "text-align" to center de content of the h3 tag

答案2

得分: 0

将图片包裹在display属性设置为flex的容器中,然后您可以使用justify-content属性。

&lt;div class=&quot;activities&quot;&gt;
  &lt;h3&gt;在Torshov进行的前三项活动&lt;/h3&gt;
  &lt;div id=&quot;flex&quot;&gt;
    &lt;img class=&quot;activities-img&quot; src=&quot;images/deniz.jpg&quot;&gt;
    &lt;img class=&quot;activities-img&quot; src=&quot;images/muze.jpg&quot;&gt;
    &lt;img class=&quot;activities-img&quot; src=&quot;images/bar.jpg&quot;&gt; 
  &lt;/div&gt;
&lt;/div&gt;
body {
    margin: 0;
}

div#flex {
  display: flex;
  justify-content: center;
}

.activities-img {
    width: 100%;
    width: 100px;
    height: 100px;
}
英文:

Wrap the images in a container with the display property set to flex. Then you can use the justify-content property.

&lt;div class=&quot;activities&quot;&gt;
  &lt;h3&gt;Top three activities to do at Torshov&lt;/h3&gt;
  &lt;div id=&quot;flex&quot;&gt;
    &lt;img class=&quot;activities-img&quot; src=&quot;images/deniz.jpg&quot;&gt;
    &lt;img class=&quot;activities-img&quot; src=&quot;images/muze.jpg&quot;&gt;
    &lt;img class=&quot;activities-img&quot; src=&quot;images/bar.jpg&quot;&gt; 
  &lt;/div&gt;
&lt;/div&gt;
body {
    margin: 0;
}

div#flex {
  display: flex;
  justify-content: center;
}

.activities-img {
    width: 100%;
    width: 100px;
    height: 100px;
}

答案3

得分: 0

我认为您忘记了在父div .activities 中将内容居中。

body {
    margin: 0;
}

.activities {
    width: 100%;
    background-color: lightpink;
    text-align: center;
}

.activities-img {
    width: 100%;
    width: 100px;
    height: 100px;
    margin-left: auto;
    margin-right: auto;
    justify-content: center;
}
英文:

I think you forgot centering content in parent div .activities

body {
    margin: 0;
}

.activities {
  width: 100%;
  background-color: lightpink;
  text-align: center;
}
.activities-img {
    width: 100%;
    width: 100px;
    height: 100px;
    margin-left: auto;
    margin-right: auto;  
    justify-content: center;
}

答案4

得分: 0

你可以简单地使用

标签,无需更改CSS,center标签总是有帮助的,你的HTML代码应该像这样:

<div class="activities">
    <h3>Torshov的前三个活动</h3>
    <center><img class="activities-img" src="images/deniz.jpg">
    <img class="activities-img" src="images/muze.jpg">
    <img class="activities-img" src="images/bar.jpg"></center>
</div>
英文:

You can simply use the center tag, no need to change the css, center tag always helps, you HTML code should be like this :-
&lt;div class=&quot;activities&quot;&gt;
&lt;h3&gt;Top three activities to do at Torshov&lt;/h3&gt;
&lt;center&gt;&lt;img class=&quot;activities-img&quot; src=&quot;images/deniz.jpg&quot;&gt;
&lt;img class=&quot;activities-img&quot; src=&quot;images/muze.jpg&quot;&gt;
&lt;img class=&quot;activities-img&quot; src=&quot;images/bar.jpg&quot;&gt;&lt;/center&gt;
&lt;/div&gt;

huangapple
  • 本文由 发表于 2023年3月12日 08:58:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75710493.html
匿名

发表评论

匿名网友

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

确定