英文:
horizontal align two images
问题
在我的网站上,我计划放置两张图片,但我无法垂直对齐它们。
我尝试过使用line-height
和垂直对齐,但我无法找出我错在哪里或者我缺少了哪些代码。
以下是您提供的代码片段:
.logo img {
height: 150px;
width: 150px;
margin: 20px;
padding: 3px;
margin-top: 10px;
vertical-align: middle;
}
.logo2 img {
height: 150px;
width: 150px;
margin: 20px;
padding: 3px;
margin-top: 10px;
vertical-align: middle;
}
<div class="main">
<nav>
<a href="guest_login.php" class="logo">
<img src="images/guesticon.png">
<h2 class="name">GUEST</h2>
</a>
<a href="officer_login.php" class="logo2">
<img src="images/policeicon.png">
<h2 class="name">ADMIN</h2>
</a>
</nav>
</div>
您想要实现的输出效果请参考下面的链接:
enter image description here
英文:
[enter image description here][1]On my website, I'm planning to put two images but I can't vertically align them.
I tried line-height
and vertical alignment. but I can't figure out where I went wrong or what codes I'm lacking.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.logo img {
height: 150px;
width: 150px;
margin: 20px;
padding: 3px;
margin-top: 10px;
vertical-align: middle;
}
.logo2 img {
height: 150px;
width: 150px;
margin: 20px;
padding: 3px;
margin-top: 10px;
vertical-align: middle;
}
<!-- language: lang-html -->
<div class="main">
<nav>
<a href="guest_login.php" class="logo">
<img src="images/guesticon.png">
<h2 class="name">GUEST</h2>
</a>
<a href="officer_login.php" class="logo2">
<img src="images/policeicon.png">
<h2 class="name">ADMIN</h2>
</a>
<!-- end snippet -->
https://jsfiddle.net/04bup35k/
this is the output i want to achieve
[1]: https://i.stack.imgur.com/9cpk8.png
答案1
得分: 1
要使这两个图像水平对齐,您可以在 nav
上使用 display: flex;
。
英文:
To horizontally align the two images you can use display: flex;
on nav
.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.logo img {
height: 150px;
width: 150px;
margin: 20px;
padding: 3px;
margin-top: 10px;
vertical-align: middle;
}
.logo2 img {
height: 150px;
width: 150px;
margin: 20px;
padding: 3px;
margin-top: 10px;
vertical-align: middle;
}
.logo,
.logo2 {
text-align: center;
}
nav {
display: flex;
align-items: center;
justify-content: center;
}
<!-- language: lang-html -->
<div class="main">
<nav>
<a href="guest_login.php" class="logo">
<img src="images/guesticon.png">
<h2 class="name">GUEST</h2>
</a>
<a href="officer_login.php" class="logo2">
<img src="images/policeicon.png">
<h2 class="name">ADMIN</h2>
</a>
</nav>
</div>
<!-- end snippet -->
答案2
得分: 0
Sure, here is the translated code:
/* language: lang-css */
.logo-container {
display: flex;
align-items: center;
/* add any other styles you need */
}
<!-- language: lang-html -->
<a href="guest_login.php" class="logo">
<div class="logo-container">
<img src="images/guesticon.png">
<h2 class="name">GUEST</h2>
</div>
</a>
<a href="officer_login.php" class="logo2">
<div class="logo-container">
<img src="images/policeicon.png">
<h2 class="name">ADMIN</h2>
</div>
</a>
Please note that I've provided the translated code segments only, as requested.
英文:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.logo-container {
display: flex;
align-items: center;
/* add any other styles you need */
}
<!-- language: lang-html -->
<a href="guest_login.php" class="logo">
<div class="logo-container">
<img src="images/guesticon.png">
<h2 class="name">GUEST</h2>
</div>
</a>
<a href="officer_login.php" class="logo2">
<div class="logo-container">
<img src="images/policeicon.png">
<h2 class="name">ADMIN</h2>
</div>
</a>
<!-- end snippet -->
答案3
得分: 0
以下是翻译好的部分:
如@tacoshy建议,您应该详细说明您的问题。您所说的垂直对齐是指什么?
编辑:
嗯,看起来@Kameron通过提供一个合法的答案来夺取了我的悬赏。我也为他的答案点了赞。
旧答案:
嗯,我假设您正在尝试在x
方向上对齐它们。您期望的行为无法通过使用vertical-align
来实现。相反,您可以使用CSS布局,例如CSS Flexbox或Grid来实现此目标。我将使用Flexbox,因为它易于使用。
首先,您必须使那些图像的父元素变为flex
,像这样:
nav {
display: flex;
}
现在这两个图像应该已经垂直对齐了。
奖励:
您没有遵循制作导航的最佳实践。首先,创建一个无序列表(ul
),并在其中放置两个列表项(li
)。现在在每个列表项内放置超链接。给每个列表项一个logo
类。
<div class="main">
<nav>
<ul>
<li class="logo">
<a href="guest_login.php">
<img src="images/guesticon.png" />
<h2 class="name">GUEST</h2>
</a>
</li>
<li class="logo">
<a href="officer_login.php">
<img src="images/policeicon.png" />
<h2 class="name">ADMIN</h2>
</a>
</li>
</ul>
</nav>
</div>
其次,在CSS中,您还可以减少一些冗余的代码行。注意,您重复应用相同的样式到两个图像上。您可以通过获取两者的选择器并一次应用样式来最小化它。例如:
/* 这一行以下的样式将使图像垂直对齐。 */
nav ul {
display: flex;
height: 150px;
}
.logo {
flex: 1; /* 为它们提供相等数量的自由空间 */
margin: 20px;
margin-top: 10px;
padding: 3px;
}
.logo img {
width: 150px;
}
第三,您可能希望对齐标题。您可以通过简单地在您的标题样式中添加text-align: center
来实现这一目标。例如:
.name {
text-align: center;
}
希望这对您有所帮助!
英文:
As @tacoshy suggests, you should elaborate your problem. What do you mean by aligning them vertically?
EDIT:
Hmm, seems like @Kameron robbed my bounties by giving an legitimate answer. I also upvote his answer.
Old answer:
Well, I'm assuming that you were trying to align them in x
direction. The behaviour you're expecting can't achieved by using vertical-align
. Instead, you can use CSS layouts such as, CSS Flexbox or Grid to achieve this. I'm gonna use Flexbox for this, because it's flexible to use.
First of all, you have to make the parent of those images flex
. Like this:
nav {
display: flex;
}
Both of the images should've been aligned vertically by now.
BONUS:
You are not following the best practices of making a navigation. First, make an unordered list (ul
) and put two list (li
) inside of it. Now put hyperlinks inside of each list element. Give each list element a logo
class.
<div class="main">
<nav>
<ul>
<li class="logo">
<a href="guest_login.php">
<img src="images/guesticon.png" />
<h2 class="name">GUEST</h2>
</a>
</li>
<li class="logo">
<a href="officer_login.php">
<img src="images/policeicon.png" />
<h2 class="name">ADMIN</h2>
</a>
</li>
</ul>
</nav>
</div>
Secondly, in CSS, you can also reduce some redundant lines of code. Notice that you have the same styles applied to both of the images repeatedly. You can minimize it by grabbing the selector of both and apply styles for once. For example:
/* This line following styles will make images align vertically. */
nav ul {
display: flex;
height: 150px;
}
.logo {
flex: 1; /* To give them equal amount of free space */
margin: 20px;
margin-top: 10px;
padding: 3px;
}
.logo img {
width: 150px;
}
Thirdly, you may want to align heading. You can achieve this by simply adding text-align: center
in your heading's style. For example:
.name {
text-align: cetner;
}
Hope this helps!
答案4
得分: -1
以下是翻译的代码部分:
.logo img {
height: 150px;
width: 150px;
margin: 20px;
padding: 3px;
margin-top: 10px;
vertical-align: middle;
}
.logo2 img{
height: 150px;
width: 150px;
margin: 20px;
padding: 3px;
margin-top: 10px;
vertical-align: middle;
}
.logo {
display: inline-block;
}
.logo2 {
display: inline-block;
}
英文:
Would this be what you are looking for?
.logo img {
height: 150px;
width: 150px;
margin: 20px;
padding: 3px;
margin-top: 10px;
vertical-align: middle;
}
.logo2 img{
height: 150px;
width: 150px;
margin: 20px;
padding: 3px;
margin-top: 10px;
vertical-align: middle;
}
.logo {
display: inline-block;
}
.logo2 {
display: inline-block;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论