简体   繁体   English

如何在居中对齐的容器内居中对齐左对齐 div 内的内容(例如,图像)?

[英]How to center align content (e.g., image) inside a left aligned div inside a center aligned container?

How to center-align an image in a left-aligned div.如何在左对齐的 div 中居中对齐图像。 It keeps aligning RIGHT (in the left aligned div).它保持右对齐(在左对齐的 div 中)。

Tried: Everything from position: absolute;尝试过:一切从位置:绝对; to margin: auto;保证金:自动; to attempts to override the parent div.尝试覆盖父 div。 I even threw in <span style="text-align:center"> into the HTML and that merely threw my CSS into utter chaos.我什至在 HTML 中加入了<span style="text-align:center"> ,这只会让我的 CSS 陷入彻底的混乱。

#background {
  height: 150%;
  background: linear-gradient(to bottom right, #33ff99 30%, #660066 60%);
}

#leftbox {
  float: left;
  background: #ff3399;
  width: 25%;
  height: 280px;
  border: 2px solid #ff3399;
  border-radius: 30px;
  display: block;
  text-align: left;
}

// I even tried adding a child div around the image I want to center as follows (to no avail)

#workshops {
  width: 50%;
  display: block;
  margin: 0;
  padding: 0;
  text-align: center;
}

// The following comes AFTER the issue

#middlebox {
  margin: auto;
  float: left;
  background: #44ff33;
  width: 50%;
  height: 280px;
  border: 2px solid green
    /* Safari 3-4, iOS 1-3.2, Android 1.6- */
    -webkit-border-radius: 30px;
  -moz-border-radius: 30px;
  border-radius: 30px;
  text-align: center;
}

#rightbox {
  float: left;
  background: blue;
  width: 24%;
  height: 280px;
  border: 2px solid blue -webkit-border-radius: 30px;
  -moz-border-radius: 30px;
  border-radius: 30px;
  text-align: center;
}

Below is the code I use on the image:下面是我在图像上使用的代码:

<!-- id tags--> 
#leftbox { 
   float: left; 
   background: #ff3399 ; 
   width: 25%; 
   height: 280px; 
   border: 2px solid #ff3399; 
   border-radius: 30px; 
   display: block; 
   text-align: left; } 
<!--The accompanying HTML follows--> 
<div id="leftbox"> 
  <a href="link.html" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image3','','imageURL.jpg',1)"> 
  <img src="imageURL.png" name="Workshops!" width="250" id="Image3" /> 
</div>
</a>
</p>

This should work.这应该有效。

img{
    position: relative;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM