html - 使图像出现在 div 之上

标签 html css image css-float css-position

我正在尝试使图像出现在父 div 之外,这样图像看起来就好像位于 div 上而不增加父 div 的高度。但是,无论我尝试过什么,图像都会被父 div 截断并且不会显示所有内容。

我有一个 js fiddle 设置,希望能解释我正在尝试做的事情。

https://jsfiddle.net/h7esvfsr/

#navWrap{
    float: right; 
    overflow-x: visible;
    overflow-y: visible;
    height: 50px;
}
#navLogo{
    float: left;
}
#navLogo img{
    width: 200px;
}

下面的片段:

.gridContainer {
  width: 89.0217%;
  max-width: 1232px;
  padding-left: 0.4891%;
  padding-right: 0.4891%;
  margin: auto;
}
#navWrap {
  float: right;
  overflow-x: visible;
  overflow-y: visible;
  height: 50px;
}
#navLogo {
  float: left;
  height: 100px;
}
#navLogo img {
  width: 200px;
}
#LayoutDiv1 {
  clear: both;
  float: left;
  display: block;
  position: absolute;
  width: 400px;
  height: 400px;
  left: 50%;
  top: 45%;
  margin-left: -200px;
  /* half of the width  */
  margin-top: -50px;
  /* half of the height */
  ;
}
.menu {
  list-style-type: none;
  margin: auto;
  padding: 0;
  overflow: hidden;
  background-color: #333;
  font-family: 'Archivo Black', sans-serif;
  opacity: 0.7;
}
/* Remove margins and padding from the list, and add a black background color */

ul.topnav {
  list-style-type: none;
  margin: auto;
  padding: 0;
  overflow: hidden;
  background-color: #333;
  font-family: 'Archivo Black', sans-serif;
  opacity: 0.7;
}
/* Float the list items side by side */

ul.topnav li {
  float: left
}
/* Style the links inside the list items */

ul.topnav li a {
  display: inline-block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  transition: 0.3s;
  font-size: 17px;
}
/* Change background color of links on hover */

ul.topnav li a:hover {
  background-color: #269C1E;
}
/* Hide the list item that contains the link that should open and close the topnav on small screens */

ul.topnav li.icon {
  display: none;
}
<div class="menu">
  <div id="nav-anchor"></div>
  <nav class="gridContainer clearfix" id="nav">
    <div id="navWrap">
      <div id="navLogo">
        <img src="http://graphxnation.com/gxn_logo_large.png" alt="" />
      </div>
      <ul class="topnav" id="myTopnav">
        <li><a href="#home">Home</a>
        </li>
        <li><a href="about.html">About Us</a>
        </li>
        <li><a href="products.html">Products</a>
        </li>
        <li><a href="services.html">Services</a>
        </li>
      </ul>
    </div>
  </nav>
</div>

最佳答案

您可以绝对定位图像:

  1. 添加 width: 200px - 与 navLogo

  2. 相同的图像
  3. position: absolute 添加到 navLogo img(您现在可以使用 lefttop 调整位置>)

  4. 使用 overflow: hidden 清除 floatnavLogonavWrap gridContainer 并将其从 menu

  5. 中移除
  6. 添加position: relativemenu

请参阅下面的演示 - 添加了一个 section 来模拟标题导航下方内容的滚动:

.gridContainer {
  width: 89.0217%;
  max-width: 1232px;
  padding-left: 0.4891%;
  padding-right: 0.4891%;
  margin: auto;
  overflow: hidden;
}
#navWrap {
  float: right;
  overflow: hidden;
  height: 50px;
}
#navLogo {
  float: left;
  height: 100px;
  width: 200px;
  overflow: hidden;
}
#navLogo img {
  width: 200px;
  position: absolute;
  left: 10px;
  top: 0;
}
#LayoutDiv1 {
  clear: both;
  float: left;
  display: block;
  position: absolute;
  width: 400px;
  height: 400px;
  left: 50%;
  top: 45%;
  margin-left: -200px;
  /* half of the width  */
  margin-top: -50px;
  /* half of the height */
  ;
}
.menu {
  list-style-type: none;
  margin: auto;
  padding: 0;
  /*overflow: hidden;*/
  background-color: #333;
  font-family: 'Archivo Black', sans-serif;
  opacity: 0.7;
  position: relative;
}
/* Remove margins and padding from the list, and add a black background color */

ul.topnav {
  list-style-type: none;
  margin: auto;
  padding: 0;
  overflow: hidden;
  background-color: #333;
  font-family: 'Archivo Black', sans-serif;
  opacity: 0.7;
}
/* Float the list items side by side */

ul.topnav li {
  float: left
}
/* Style the links inside the list items */

ul.topnav li a {
  display: inline-block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  transition: 0.3s;
  font-size: 17px;
}
/* Change background color of links on hover */

ul.topnav li a:hover {
  background-color: #269C1E;
}
/* Hide the list item that contains the link that should open and close the topnav on small screens */

ul.topnav li.icon {
  display: none;
}
section {
  height: 150vh;
}
<div class="menu">
  <div id="nav-anchor"></div>
  <nav class="gridContainer clearfix" id="nav">
    <div id="navWrap">
      <div id="navLogo">
        <img src="http://graphxnation.com/gxn_logo_large.png" alt="" />
      </div>
      <ul class="topnav" id="myTopnav">
        <li><a href="#home">Home</a>
        </li>
        <li><a href="about.html">About Us</a>
        </li>
        <li><a href="products.html">Products</a>
        </li>
        <li><a href="services.html">Services</a>
        </li>
      </ul>
    </div>
  </nav>
</div>
<section></section>

关于html - 使图像出现在 div 之上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41186664/

相关文章:

javascript - 单击按钮替换图像 src 属性中的文件夹名称

javascript - 如何使用jquery找到最近的元素

html - 我的谷歌主页元素中的一些错误

css - 使用 Tailwind 和 ReactJS 将图像居中对齐

jquery - Zend framework 和 jQuery Mobile,无法导入 Javascript 和 CSS 文件

css - 以移动方式运行时如何修复错误 css width x height

javascript - 将图像存储在 Javascript 对象中

python - 用于调整图像大小的独立 Python 脚本

image - 像素格式位字节序

javascript - 图片翻转效果不适用于 Safari 和 IE