英文:
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"等方法,但都没有成功,你能帮我吗?谢谢。
英文:
<div class="activities">
<h3>Top three activities to do at 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;
}
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 -->
<div class="activities">
<h3>
Top three activities to do at Torshov
</h3>
<div class="activities-img">
<img src="images/deniz.jpg">
<img src="images/muze.jpg">
<img src="images/bar.jpg">
</div>
</div>
<!-- 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 -->
<div class="activities">
<h3>Top three activities to do at 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>
<!-- end snippet -->
You could need "text-align" to center de content of the h3 tag
答案2
得分: 0
将图片包裹在display
属性设置为flex
的容器中,然后您可以使用justify-content
属性。
<div class="activities">
<h3>在Torshov进行的前三项活动</h3>
<div id="flex">
<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>
</div>
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.
<div class="activities">
<h3>Top three activities to do at Torshov</h3>
<div id="flex">
<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>
</div>
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
你可以简单地使用
<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 :-
<div class="activities">
<h3>Top three activities to do at 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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论