如何在html/css中将文本与图像对齐



我有一个图像和一些文本。我目前有文本和图像中心,但我希望内容类在图像的右侧(垂直对齐(。我试过垂直对齐,但似乎不太管用。有人能帮帮我吗?

我希望文本成为的位置

https://jsfiddle.net/skriker11/3t7asmhf/1/

.card{
border: solid red;
text-align: center;
}
img{
border: 1px solid red;
margin: auto;
vertical-align: middle;
}
.card > .content h3, p {
margin: 0;
} 
.content {
display: flex;
flex-direction: column;
}
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Document</title>
</head>
<body>
<div class="card">
<h1>Title of widget</h1>
<div class='content'>
<img src="https://placeimg.com/640/480/tech" width='100' height=100/>
<p>Sydney</p>
<h3>26</h3>
<p><strong>Wind</strong></p> <p>NE 24km/h</p>
</div>
</div>
</body>
</html>

以下是您可以做的,请注意FIX:的注释

.card{
border: solid red;

/* FIX: make the card as flexbox with direction of column */
display: flex;
flex-direction: column;
align-items: center;
}
img{
border: 1px solid red;
vertical-align: middle;
}
.card > .content h3, p {
margin: 0;
}
.content {
/* FIX#2: flexbox with direction of row */
display: flex;
align-items: center;
}
<div class="card">
<h1>Title of widget</h1>

<div class='content'>
<img src="https://placeimg.com/640/480/tech" width='100' height=100/>
<!-- FIX: wrap all texts in a div -->
<div class="description">
<p>Sydney</p>
<h3>26</h3>
<p><strong>Wind</strong></p> <p>NE 24km/h</p>
</div>
</div>
</div>

最新更新