html - 垂直对齐不起作用需要使用 float 属性

标签 html css image header vertical-alignment

我试图在 head div 中垂直对齐两个图像,并将每个图像保持在不同的一侧。这是我试过的:

body,
html {
  height: 100%;
  color: #666;
  background: #f2f2f2;
  font-family: sans-serif;
  margin: 0px auto;
  padding: 0px;
}

.main {
  max-width: 1280px;
  margin: 0 auto;
}

.head {
  width: 100%;
  height: 100px;
}

.left {
  float: left;
  width: 30%;
}

.right {
  float: right;
  width: 30%;
  text-align: right;
}
<div class="main">
  <div class="head">
    <div class="left"><img src="http://via.placeholder.com/200x55.png/c45" alt="Logo 1" title="" width="200px" /></div>
    <div class="right"><img src="http://via.placeholder.com/200x55.png/s45" alt="Logo 2" title="" width="200px" /></div>
  </div>
  <div class="web-banner">
    <div class="img">
      <img src="http://via.placeholder.com/1280x550.png" width="1280px" height="550px" alt="main image" />
    </div>
    <div class="title">the quick brown fox jumped over the lazy dog</div>
    <div class="sub-title">the quick brown fox jumped over the lazy dog</div>
  </div>
</div>

最佳答案

这是我发现的,它使用“display: flex”和“align-self:center”工作

/* this div can be ignored, used for advisability only */    
div {
  border: 1px dotted red;
}

.head {
  width: 100%;
  height: 100px;
  display: flex;
}

.left {
  max-height: 60px;
  float: left;
  width: 30%;
  align-self: center;
  margin-left: 60px;
}

.right {
  max-height: 60px;
  margin-left: auto;
  margin-right: 60px;
  float: right !important;
  width: 30%;
  text-align: right;
  align-self: center;
}
<div class="head">
  <div class="left"><img src="http://via.placeholder.com/200x60.png/c45" alt="Zebra Logo" title="logo 1" width="200px" /></div>
  <div class="right"><img src="http://via.placeholder.com/200x60.png/s45" alt="Zebra Logo" title="logo 2" width="200px" /></div>
</div>

关于html - 垂直对齐不起作用需要使用 float 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45847061/

相关文章:

html - CSS 可调整大小的带标题的图像

javascript - 与 connectedCallback 相关的模板冲压时间

php - cURLlib/File_get_contents 只加载部分数据

java - 从 RGB 数据的 int 数组创建 PNG 文件

javascript - 在 React 中将文本分成多行

javascript - 计算html页面上所有<a>的数量

css - 选项卡和隐藏内容

html - 当 flexbox 元素以列模式包装时,容器不会增加其宽度

Xcode,无法从项目中删除 "Launch image"

image - 无法在 GOLANG 中解码不同的图像格式(分配计数不匹配)