Html 如何使用flexbox将图像放入其容器中? .navbar{ 溢出:隐藏; 背景色:#333; 位置:固定;/*将导航栏设置为固定位置*/ 顶部:0;/*将导航栏定位在页面顶部*/ 宽度:100%; } .导航栏a:悬停{ 背景:ddd; 颜色:黑色; } 纳瓦尔先生{ 浮动:对; 颜色:#F2F2; 显示:块; 文字装饰:无; 填充:30px 30px; } 身体{ 背景色:#ddd; } #介绍{ 填充顶部:50px; 文本对齐:居中; } #电子邮件容器{ 显示器:flex; 弯曲方向:立柱; 对齐项目:居中; } #录像带{ 文本对齐:居中; } #电子邮件输入{ 宽度:348px; } #电子邮件提交{ 背景色:#4CAF50; 文字装饰:无; 填充:10px 20px; 显示:内联块; 颜色:白色; 利润率:10px 14px; 边界半径:4px; } #特点2{ 显示器:flex; 弯曲方向:立柱; 弹性基准:1em; } #衣裳{ 高度:200px; 宽度:自动; } #信用卡{ 高度:200px; 宽度:自动; } #送货车{ 高度:300px; 宽度:自动; } 公司名称 提交电子邮件以接收最新消息 乱数假文
同侧眼睑 同侧眼睑 同侧眼睑 我想把图像放在一列中,就像垂直方向一样,但它最终被拉伸了,我想知道如何修复它。我使用了flex,这样我可以安排它们,并将它们放在后面的Lorem ipsum单词旁边。有没有更简单的方法?我对HTML和css有点陌生。很抱歉,这里有很多不必要的代码,因为我只是复制粘贴了整个内容,但您只需要查看img src和css对应项即可。编辑:谢谢,问题已经解决:) 您需要与Html 如何使用flexbox将图像放入其容器中? .navbar{ 溢出:隐藏; 背景色:#333; 位置:固定;/*将导航栏设置为固定位置*/ 顶部:0;/*将导航栏定位在页面顶部*/ 宽度:100%; } .导航栏a:悬停{ 背景:ddd; 颜色:黑色; } 纳瓦尔先生{ 浮动:对; 颜色:#F2F2; 显示:块; 文字装饰:无; 填充:30px 30px; } 身体{ 背景色:#ddd; } #介绍{ 填充顶部:50px; 文本对齐:居中; } #电子邮件容器{ 显示器:flex; 弯曲方向:立柱; 对齐项目:居中; } #录像带{ 文本对齐:居中; } #电子邮件输入{ 宽度:348px; } #电子邮件提交{ 背景色:#4CAF50; 文字装饰:无; 填充:10px 20px; 显示:内联块; 颜色:白色; 利润率:10px 14px; 边界半径:4px; } #特点2{ 显示器:flex; 弯曲方向:立柱; 弹性基准:1em; } #衣裳{ 高度:200px; 宽度:自动; } #信用卡{ 高度:200px; 宽度:自动; } #送货车{ 高度:300px; 宽度:自动; } 公司名称 提交电子邮件以接收最新消息 乱数假文,html,css,Html,Css,同侧眼睑 同侧眼睑 同侧眼睑 我想把图像放在一列中,就像垂直方向一样,但它最终被拉伸了,我想知道如何修复它。我使用了flex,这样我可以安排它们,并将它们放在后面的Lorem ipsum单词旁边。有没有更简单的方法?我对HTML和css有点陌生。很抱歉,这里有很多不必要的代码,因为我只是复制粘贴了整个内容,但您只需要查看img src和css对应项即可。编辑:谢谢,问题已经解决:) 您需要与containCSS属性一起使用,以获得图像的原始大小并使其非常适合 object-fitCSS属性设置
contain
CSS属性一起使用,以获得图像的原始大小并使其非常适合
object-fit
CSS属性设置被替换元素(如img
或video
)的内容应如何调整大小以适应其容器
现场演示
.navbar{
溢出:隐藏;
背景色:#333;
位置:固定;
/*将导航栏设置为固定位置*/
排名:0;
/*将导航栏定位在页面顶部*/
宽度:100%;
}
.导航栏a:悬停{
背景:ddd;
颜色:黑色;
}
纳瓦尔先生{
浮动:对;
颜色:#F2F2;
显示:块;
文字装饰:无;
填充:30px 30px;
}
身体{
背景色:#ddd;
}
#介绍{
填充顶部:50px;
文本对齐:居中;
}
#电子邮件容器{
显示器:flex;
弯曲方向:立柱;
对齐项目:居中;
}
#录像带{
文本对齐:居中;
}
#电子邮件输入{
宽度:348px;
}
#电子邮件提交{
背景色:#4CAF50;
文字装饰:无;
填充:10px 20px;
显示:内联块;
颜色:白色;
利润率:10px 14px;
边界半径:4px;
}
#特点2{
显示器:flex;
弯曲方向:立柱;
弹性基准:1em;
}
#衣裳{
高度:200px;
宽度:自动;
对象匹配:包含;
}
#信用卡{
高度:200px;
宽度:自动;
对象匹配:包含;
}
#送货车{
高度:300px;
宽度:自动;
对象匹配:包含;
}
公司名称
提交电子邮件以接收最新消息
乱数假文
同侧眼睑
同侧眼睑
同侧眼睑
您需要与contain
CSS属性一起使用,以获得图像的原始大小,并使其非常适合
object-fit
CSS属性设置被替换元素(如img
或video
)的内容应如何调整大小以适应其容器
现场演示
.navbar{
溢出:隐藏;
背景色:#333;
位置:固定;
/*将导航栏设置为固定位置*/
排名:0;
/*将导航栏定位在页面顶部*/
宽度:100%;
}
.导航栏a:悬停{
背景:ddd;
颜色:黑色;
}
纳瓦尔先生{
浮动:对;
颜色:#F2F2;
显示:块;
文字装饰:无;
填充:30px 30px;
}
身体{
背景色:#ddd;
}
#介绍{
填充顶部:50px;
文本对齐:居中;
}
#电子邮件容器{
显示器:flex;
弯曲方向:立柱;
对齐项目:居中;
}
#录像带{
文本对齐:居中;
}
#电子邮件输入{
宽度:348px;
}
#电子邮件提交{
背景色:#4CAF50;
文字装饰:无;
填充:10px 20px;
显示:内联块;
颜色:白色;
利润率:10px 14px;
边界半径:4px;
}
#特点2{
显示器:flex;
弯曲方向:立柱;
弹性基准:1em;
}
#衣裳{
高度:200px;
宽度:自动;
对象匹配:包含;
}
#信用卡{
高度:200px;
宽度:自动;
对象匹配:包含;
}
#送货车{
高度:300px;
宽度:自动;
对象匹配:包含;
}
公司名称
提交电子邮件以接收最新消息
乱数假文
同侧眼睑
同侧眼睑
同侧眼睑
从您的容器中移除flex,我将您的图像放置在ccs中,使其具有显示:块和自动边距
.navbar{
overflow: hidden;
background-color: #333;
position: fixed; /* Set the navbar to fixed position */
top: 0; /* Position the navbar at the top of the page */
width: 100%;
}
.navbar a:hover {
background: #ddd;
color: black;
}
.navbar a {
float:right;
color: #f2f2f2;
display:block;
text-decoration:none;
padding: 30px 30px;
}
body{
background-color: #ddd;
}
#intro{
padding-top:50px;
text-align:center;
}
#email-container{
display:flex;
flex-direction:column;
align-items:center;
}
#video{
text-align:center;
}
#email-input{
width:348px;
}
#email-submit{
background-color:#4CAF50;
text-decoration:none;
padding: 10px 20px;
display:inline-block;
color:white;
margin:10px 14px;
border-radius:4px;
}
#features2{
display:flex;
flex-direction:column;
flex-basis:1em;
}
#clothes{
height:200px;
width:auto;
}
#credit-card{
height:200px;
width:auto;
}
#delivery-truck{
height:300px;
width:auto;
}
<!DOCTYPE html>
<html>
<div class="navbar" id="navigationbar">
<a href="#Features">Features</a>
<a href="#intro">Introduction</a>
<a href="#prices">Prices</a> <br><br><br>
</div>
<div id="content">
<div id="intro">
<h1 id="header">Company name</h1>
<img id="header-image" src="https://image.shutterstock.com/image-vector/circle-business-logo-company-name-260nw-626261534.jpg" alt="company-logo"><br><br>
</div>
<div id="email-container">
<form>
<div id="email">
<input id="email-input" type="email" placeholder="Enter your E-mail here..."></div>
<button id="email-submit"><b>SUBMIT E-MAIL TO RECEIVE LATEST NEWS</b></button>
</form>
</div>
<div id="features">
<h1>Lorem ipsum</h1>
<div id="features1">
<p>Lorem ipsum</p>
<p>Lorem ipsum</p>
<p>Lorem ipsum</p>
</div>
<div id="features2">
<img id="delivery-truck" src="https://wholesale-steroids.cc/wp-content/uploads/2019/02/delivery-truck-400x400.png" alt="delivery-truck">
<img id="clothes" src="https://image.flaticon.com/icons/png/512/106/106020.png" alt="clothes">
<img id="credit-card" src="https://loanssos.com/wp-content/uploads/2014/07/credit-card.png" alt="credit-card">
</div>
</div>
<div id="prices">
</div>
</div>
</html>
从你的容器中移除flex,我在ccs中放置了一个显示:块和自动边距
.navbar{
overflow: hidden;
background-color: #333;
position: fixed; /* Set the navbar to fixed position */
top: 0; /* Position the navbar at the top of the page */
width: 100%;
}
.navbar a:hover {
background: #ddd;
color: black;
}
.navbar a {
float:right;
color: #f2f2f2;
display:block;
text-decoration:none;
padding: 30px 30px;
}
body{
background-color: #ddd;
}
#intro{
padding-top:50px;
text-align:center;
}
#email-container{
display:flex;
flex-direction:column;
align-items:center;
}
#video{
text-align:center;
}
#email-input{
width:348px;
}
#email-submit{
background-color:#4CAF50;
text-decoration:none;
padding: 10px 20px;
display:inline-block;
color:white;
margin:10px 14px;
border-radius:4px;
}
#features2{
display:flex;
flex-direction:column;
flex-basis:1em;
}
#clothes{
height:200px;
width:auto;
}
#credit-card{
height:200px;
width:auto;
}
#delivery-truck{
height:300px;
width:auto;
}
<!DOCTYPE html>
<html>
<div class="navbar" id="navigationbar">
<a href="#Features">Features</a>
<a href="#intro">Introduction</a>
<a href="#prices">Prices</a> <br><br><br>
</div>
<div id="content">
<div id="intro">
<h1 id="header">Company name</h1>
<img id="header-image" src="https://image.shutterstock.com/image-vector/circle-business-logo-company-name-260nw-626261534.jpg" alt="company-logo"><br><br>
</div>
<div id="email-container">
<form>
<div id="email">
<input id="email-input" type="email" placeholder="Enter your E-mail here..."></div>
<button id="email-submit"><b>SUBMIT E-MAIL TO RECEIVE LATEST NEWS</b></button>
</form>
</div>
<div id="features">
<h1>Lorem ipsum</h1>
<div id="features1">
<p>Lorem ipsum</p>
<p>Lorem ipsum</p>
<p>Lorem ipsum</p>
</div>
<div id="features2">
<img id="delivery-truck" src="https://wholesale-steroids.cc/wp-content/uploads/2019/02/delivery-truck-400x400.png" alt="delivery-truck">
<img id="clothes" src="https://image.flaticon.com/icons/png/512/106/106020.png" alt="clothes">
<img id="credit-card" src="https://loanssos.com/wp-content/uploads/2014/07/credit-card.png" alt="credit-card">
</div>
</div>
<div id="prices">
</div>
</div>
</html>
尝试添加CSS#features img{object fit:contain;}
,以便对flex box div中的所有图像使用一条规则
添加对象匹配:contain代码>CSS属性,它将保留图像的纵横比
对于该属性,您可以使用最适合您所寻找的对象的任何对象,更多的对象适合选项可用,如覆盖、填充或缩小
你可以测试它们,看看是否