简体   繁体   English

如何将图像居中对齐div?

[英]How can I center align my image into div?

I have a div meant to contain all content on said site, an image I have nested into the div doesn't want to center and I'm not sure why? 我的div打算包含上述站点上的所有内容,我嵌套在div中的图像不想居中,而且我不确定为什么吗?

here is the CSS. 这是CSS。

#wrapper {
width: 1000px;
margin: 0 auto;
background-color: #f4f9f1;
}

#intro {
padding-bottom: 10px;
width:80%;
margin:10px auto;
border-bottom: 3px solid #b32230;
}

intro is the id given to said img, it just floats to the left of the div( id is wrapper) and wont center even though I have right and left margins set to auto? 简介是给img的ID,即使我将左右边距设置为auto,它也会浮动在div(id是wrapper)的左侧并且不会居中?

The problem with margin: auto; margin: auto;的问题margin: auto; is that it will only center block level elements ... and img is by default displayed as inline and follows the text flow (to the left). 就是它只会使中心块级元素...而img在默认情况下显示为inline并遵循文本流(向左)。 If you add display: block to it it should work: 如果您添加display: block ,它应该可以工作:

DEMO 演示

#intro {
    display: block;
    padding-bottom: 10px;
    width:80%;
    margin:10px auto;
    border-bottom: 3px solid #b32230;
}

This way you don't need to centrally align the text of the wrapper element. 这样,您就不需要集中对齐包装元素的文本。

But if you want to center all inline children of the the wrapper - just use text-align:center on the #wrapper ... however then you may need to have line breaks before and after the images or some text could end up next to them and push them out of the center =) 但是,如果您想将包装器的所有inline子项居中-只需在#wrapper上使用text-align:center ...但是,您可能需要在图像前后插入换行符,否则某些文本可能会#wrapper在旁边他们,并将其推出中心=)

DEMO 演示

Use text-align: center on main container: 使用text-align: center在主容器上text-align: center

#wrapper {
    width: 1000px;
    margin: 0 auto;
    background-color: #f4f9f1;
    text-align: center;/*Add text-align center*/
}

如何将我的文本/边框与我的页面中心对齐<div>元素? </div><div id="text_translate"><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-css lang-css prettyprint-override"> div1 { background-color: aqua; font-family: Candara; padding: 10px; border-radius: 10px; margin: 10px; width: auto; }</pre><pre class="snippet-code-html lang-html prettyprint-override"> &lt;body&gt; &lt;div1 style="font-size: 30px; text-align: center;"&gt; &lt;b&gt;Text here&lt;/b&gt; &lt;/div1&gt; &lt;/body&gt;</pre></div></div><p></p><p> 我正在尝试将我的边框和文本都对齐到页面的中心,但有些东西不起作用。 如果我解释得不够好,请询问更多详细信息:)。 这是代码:</p><p> <strong>HTML</strong></p><pre> &lt;body&gt; &lt;div1 style="font-size: 30px; text-align: center;"&gt; &lt;b&gt;Text here&lt;/b&gt; &lt;/div1&gt; &lt;/body&gt;</pre><p> <strong>CSS</strong></p><pre> div1 { background-color: aqua; font-family: Candara; padding: 10px; border-radius: 10px; margin: 10px; width: auto; }</pre></div> - How can I align my text/border to the center of my page with the <div> element?

暂无
暂无

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

相关问题 如何在中心“div”和“a”元素对齐 - How can I align at center “div” and “a” elements 如何对齐该div的中心? - How can I align center this div? 如何将图像与Div中心对齐 - How to align image to center of Div 如何对齐图像中心的文本 - How can I align the text in the center of the image 如何在中心对齐图像?CSS - How can i align the image at center?CSS 如何将我的文本/边框与我的页面中心对齐<div>元素? </div><div id="text_translate"><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-css lang-css prettyprint-override"> div1 { background-color: aqua; font-family: Candara; padding: 10px; border-radius: 10px; margin: 10px; width: auto; }</pre><pre class="snippet-code-html lang-html prettyprint-override"> &lt;body&gt; &lt;div1 style="font-size: 30px; text-align: center;"&gt; &lt;b&gt;Text here&lt;/b&gt; &lt;/div1&gt; &lt;/body&gt;</pre></div></div><p></p><p> 我正在尝试将我的边框和文本都对齐到页面的中心,但有些东西不起作用。 如果我解释得不够好,请询问更多详细信息:)。 这是代码:</p><p> <strong>HTML</strong></p><pre> &lt;body&gt; &lt;div1 style="font-size: 30px; text-align: center;"&gt; &lt;b&gt;Text here&lt;/b&gt; &lt;/div1&gt; &lt;/body&gt;</pre><p> <strong>CSS</strong></p><pre> div1 { background-color: aqua; font-family: Candara; padding: 10px; border-radius: 10px; margin: 10px; width: auto; }</pre></div> - How can I align my text/border to the center of my page with the <div> element? 我无法将div对准网页的中心 - I can't align the div to the center of my webpage 如何垂直居中对齐我的主 div? - How do I vertically center align my main div? 无法将超大图像与div中心对齐 - Can't align oversized image to center of div 我无法将图像边框半径与文本居中对齐 - I can't center align my image border radius with the text
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM