如何将我的文字和图片放在 div 中间?
How can I put my text and image in the middle of a div?
我有 4 行不同的两列,一列上有图片,另一列上有文字。由于文本和图像的大小不同,我希望它们在垂直方向居中。
这是我对其中一列的代码,CSS 对 div
.col2 {
float: left;
width: 46%;
padding: 2%;
text-align: center;
position: relative;
justify-content: center;
height: 300px;
}
<div class="col2">
<img src="images/freelance.png" alt="three people watching a presentation">
</div>
<div class="col2">
<h2>Work as a Freelancer</h2>
<p>This session will set out the key points you need
to consider when taking on freelance work in order to make
sure you gain maximum professional and personal benefit.</p>
<div class="book">
<a class="booked" href="#" target="_blank"><p><b>Book 14th March 6pm</b></p></a>
</div>
</div>
This is the before with my code
This is with @N3v4da Stone 's changes
使用容器和 flexbox CSS 包装你的列。
可能像这样,如果不正确,请提供您想要的图片。
.flex-container {
display: flex;
flex-flow: row wrap;
justify-content: space-between;
align-items: center;
}
.col2 {
width: 46%;
padding: 2%;
text-align: center;
}
<div class="flex-container">
<div class="col2">
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAAAgVBMVEUAAAD+/v7///8EBATk5ORFRUX7+/u/v7+4uLi3t7doaGjo6OgvLy/s7Oz39/fy8vLd3d1WVlZ/f3+ampomJiakpKTPz8+Pj4+Hh4eqqqoTExOxsbHX19eRkZHKysoeHh42NjZwcHB0dHRbW1tLS0s9PT0bGxsqKioQEBBCQkJPT0/xhAnyAAAKmklEQVR4nO2bi3KjOgyGQU5NE0Joc78nTdKm3fd/wGP5hgEbaHfp9uzom+lMuTn+kS3LsokigiAIgiAIgiAIgiAIgiAIgiAIgiAIgiAIgiAIgiAIgiAIgiAIgiAIgiAIgiAIgiAIogqLGKuccA/K1/6fnMuHc+f/7Tb6FxRGL6Wj26tzsPremnwKb/Pyn1u/lU7snPtGM/9DPwDmU1jrcurkfeOcZ9Hq4oh6+mhU+KluyhoPP0vgca/sKDkXRyx6e3Kurp8aVfxN+66TR8HQgkcJ80u8Lp0jFj0unGswa5Qx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PFMxYFS/nqF84/l39ND0EQBEEQBEEQBEEQBEEQBEEQBEEQBEEQBEEQBEEQBEEQBEEQBEEQBEEQBEEQBEEQffEfNW9qJ+eq/9AAAAAASUVORK5CYII=" alt="three people watching a presentation">
</div>
<div class="col2">
<h2>Work as a Freelancer</h2>
<p>This session will set out the key points you need
to consider when taking on freelance work in order to make
sure you gain maximum professional and personal benefit.</p>
<div class="book">
<a class="booked" href="#" target="_blank"><p><b>Book 14th March 6pm</b></p></a>
</div>
</div>
</div>
N3v4da 的解释确实有效,但我会尝试更详细地介绍。
Wrapper div 使用 flexbox 在一行中显示元素。
align-items: center;
项目沿弹性行垂直居中(这意味着文本与图像居中)
justify-content: space-between
将 img
和 div
元素放在包装器 div.
的相对两侧
gap: 20px
确保 img
和 div
之间总是有 20px 的间隙
.wrapper {
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
background-color: lightblue;
}
.wrapper img {
width: 300px;
height: 300px;
}
<div class="wrapper">
<img src="https://www.w3schools.com/w3css/img_lights.jpg" alt="three people watching a presentation">
<div class="rightSide">
<h2>Work as a Freelancer</h2>
<p>This session will set out the key points you need to consider when taking on freelance work in order to make sure you gain maximum professional and personal benefit.</p>
<div class="book">
<a class="booked" href="#" target="_blank">
<p><b>Book 14th March 6pm</b></p>
</a>
</div>
</div>
</div>
确保删除任何可能干扰此代码的 CSS,这可能是 N3v4da 版本无法运行的原因。 This is what my solution does
我有 4 行不同的两列,一列上有图片,另一列上有文字。由于文本和图像的大小不同,我希望它们在垂直方向居中。
这是我对其中一列的代码,CSS 对 div
.col2 {
float: left;
width: 46%;
padding: 2%;
text-align: center;
position: relative;
justify-content: center;
height: 300px;
}
<div class="col2">
<img src="images/freelance.png" alt="three people watching a presentation">
</div>
<div class="col2">
<h2>Work as a Freelancer</h2>
<p>This session will set out the key points you need
to consider when taking on freelance work in order to make
sure you gain maximum professional and personal benefit.</p>
<div class="book">
<a class="booked" href="#" target="_blank"><p><b>Book 14th March 6pm</b></p></a>
</div>
</div>
This is the before with my code
This is with @N3v4da Stone 's changes
使用容器和 flexbox CSS 包装你的列。 可能像这样,如果不正确,请提供您想要的图片。
.flex-container {
display: flex;
flex-flow: row wrap;
justify-content: space-between;
align-items: center;
}
.col2 {
width: 46%;
padding: 2%;
text-align: center;
}
<div class="flex-container">
<div class="col2">
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAAAgVBMVEUAAAD+/v7///8EBATk5ORFRUX7+/u/v7+4uLi3t7doaGjo6OgvLy/s7Oz39/fy8vLd3d1WVlZ/f3+ampomJiakpKTPz8+Pj4+Hh4eqqqoTExOxsbHX19eRkZHKysoeHh42NjZwcHB0dHRbW1tLS0s9PT0bGxsqKioQEBBCQkJPT0/xhAnyAAAKmklEQVR4nO2bi3KjOgyGQU5NE0Joc78nTdKm3fd/wGP5hgEbaHfp9uzom+lMuTn+kS3LsokigiAIgiAIgiAIgiAIgiAIgiAIgiAIgiAIgiAIgiAIgiAIgiAIgiAIgiAIgiAIgiAIogqLGKuccA/K1/6fnMuHc+f/7Tb6FxRGL6Wj26tzsPremnwKb/Pyn1u/lU7snPtGM/9DPwDmU1jrcurkfeOcZ9Hq4oh6+mhU+KluyhoPP0vgca/sKDkXRyx6e3Kurp8aVfxN+66TR8HQgkcJ80u8Lp0jFj0unGswa5Qx33au0fZ8XhUV2q1fz+fuD3tYpAMBGDgeZe+Bm/Nn9+gB0J9KXSzawUPjzzRejUx3eb8ulhnUSJfH6Ujf9bXm8BpDLAHT8vzlHAalw/RomiaLxrBv+oXyu/GA5bwcc4AYYJ9MH24PyG2T7EGeA8if1tEXBl51/4NVqCUEytnCxT1cw3Nx+xIahoxr00XNLJf2ymfVNnlJRNviWDsYnr8YW9QVBm5MJqVDnha3voBotKHn9jBs6qbi0gpNFUPmfRP3HRca8Tqf+663U1MY4hc6lKKqG8c0LMWH/TJuALug28bz1xSkkRbe5iPOnI/qhsm4VYyXzgqjFNyXeAdeOKUFCPl+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bAdeEbKwxXbwNd+j0JsdebEzq8Bcdu9/mkXbzLyctm2tUd8Kl0EbIrn2+nLEaHi6qlBuqNJriO2RW78K940KF3rA4LV4QFFMdktDO85GHkG/ncz7pEuvCt+zoid5eI0N4Nsj7CTq6t0tNSUHd0kbeo1L542Fs8LEeXW0r9RtXXt0m5uu6HHCgVJ6UHi3he88V2V3Upf9XwYVdau5IhY9c73YHbdEp7220maFkVrHR7wmbFIYObF7tm1sqC9/0YZyqzquAQ/9pY+bF0Nf5VZ90Vgz7wdlhlWfCs+28EBe7K6SGeNADa078e2ckTEDemIe503fqM36VFg0EF8GXu1PFCY8RgEjWFccGhMOcrWFQ96wnT3jPSosoqtzoICLb/tzUbk2hdF5L3sjyIbO9FYBC/5/kMEBLjpNusTpVVoUFkN2cMUnjcvp0FL9bFCTBm+JnuU2HA5cfYJXaFD/rtDK+PFX8rVtfr+vcAYN0bPpQriR1Is02eg4wc+fIF9UG8N4OpD68uWFBb6MamPcrNBMDxu2L7377aMqUwQ1obVCtZRzPizVCuFgMTU9/mW6GUzw3FPybD5Z/ZzEoXRjzvoqVqTYs5uqZVBn9ZXXIhP5Wpf+3nHE4kvP212MXt5GuydTEW5/+bh7afwoqhH1pbNBfuiMXzobdvXLiac5sujqr/YhEQ8N3TIek2B8ZktYH1aHDf7e4nA4dFxBDRH6rc+XE/j++xPFMxYFS/nqF84/l39ND0EQBEEQBEEQBEEQBEEQBEEQBEEQBEEQBEEQBEEQBEEQBEEQBEEQBEEQBEEQBEEQffEfNW9qJ+eq/9AAAAAASUVORK5CYII=" alt="three people watching a presentation">
</div>
<div class="col2">
<h2>Work as a Freelancer</h2>
<p>This session will set out the key points you need
to consider when taking on freelance work in order to make
sure you gain maximum professional and personal benefit.</p>
<div class="book">
<a class="booked" href="#" target="_blank"><p><b>Book 14th March 6pm</b></p></a>
</div>
</div>
</div>
N3v4da 的解释确实有效,但我会尝试更详细地介绍。
Wrapper div 使用 flexbox 在一行中显示元素。
align-items: center;
项目沿弹性行垂直居中(这意味着文本与图像居中)
justify-content: space-between
将 img
和 div
元素放在包装器 div.
gap: 20px
确保 img
和 div
.wrapper {
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
background-color: lightblue;
}
.wrapper img {
width: 300px;
height: 300px;
}
<div class="wrapper">
<img src="https://www.w3schools.com/w3css/img_lights.jpg" alt="three people watching a presentation">
<div class="rightSide">
<h2>Work as a Freelancer</h2>
<p>This session will set out the key points you need to consider when taking on freelance work in order to make sure you gain maximum professional and personal benefit.</p>
<div class="book">
<a class="booked" href="#" target="_blank">
<p><b>Book 14th March 6pm</b></p>
</a>
</div>
</div>
</div>
确保删除任何可能干扰此代码的 CSS,这可能是 N3v4da 版本无法运行的原因。 This is what my solution does