Center image using text-align center?
That will not work as the text-align
property applies to block containers, not inline elements, and img
is an inline element. See the W3C specification.
Use this instead:
img.center { display: block; margin: 0 auto;}
<div style="border: 1px solid black;"><img class="center" src ="https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-icon.png?v=c78bd457575a">
</div>
How can I center an image and place text under it to the left or right?
Is this the kind of thing you are thinking of?
I've just added display: flex
to the wrapper and then flex-direction: column
makes sure that they are stacked in a column (not a row).
You can then change text-align
property on the b
to whichever you'd like (left
, center
or right
).
p { display: flex; flex-direction: column; width: 600px; margin: auto;}
img { width: 600px; height: 150px;}
b { text-align: left;}
<p align="center"><img src="http://via.placeholder.com/600x150"> <b>text under it</b></p>
How to center image while also aligning text to the right of image?
Solution 1:
You can use a div
to wrap the image and the text in and use text-align: center
along with vertical-align: middle
.
.center-img,.center-txt { display: inline-block; vertical-align: middle;}#wrapper { text-align: center; border: 1px solid red;}
<div id="wrapper"> <img src="http://placehold.it/100x100" class="center-img" /> <div class="center-txt"> <h1>Home</h1> </div></div>
CSS: Centering an image. Why doesn't 'text-align: center'?
text-align
affects the inline content of an element.
The image isn't the content of the <img>
, it is the <img>
.
For text-align
to affect it, you must apply the property to the parent of the <img>
.
Vertically align text next to an image?
Actually, in this case it's quite simple: apply the vertical align to the image. Since it's all in one line, it's really the image you want aligned, not the text.
<!-- moved "vertical-align:middle" style from span to img -->
<div>
<img style="vertical-align:middle" src="https://via.placeholder.com/60x60" alt="A grey image showing text 60 x 60">
<span style="">Works.</span>
</div>
How can I center align the carousel images and text correctly?
Hi can you please try with this and let me know thank you.
.prod-slider-container .item a {
display: block;
margin: 0 auto;
float: none;
text-align: center;
}
Image won't center with text-align:center because of a span next to it
Set the parent element's position to relative
and the element you want to position via left
, top
, right
, bottom
as absolute
, and center it with the 50%, 50% and transform: translate "trick":
<div style="
background-color: red;
display: inline-block;
text-align: center;
position: relative;
border-radius: 100px;
">
<!-- sample circle -->
<div
style="
width: 100px;
height: 100px;
"
></div>
<!-- the thing you want to position absolutely -->
<span class="number"style="
font-size: 40px;
position:absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
"
>
4
</span>
</div>
Related Topics
Css Margin Terror; Margin Adds Space Outside Parent Element
How to Make a Div 100% Height of the Browser Window
How to Get an Iframe to Be Responsive in iOS Safari
Navbar Menu Dropdown Not Working (Won't Drop Down When Clicked)
How to Modify the Primeng P-Calendar Style
Angular: *Ngif Tag on Input Element Causes It to Be Undefined. Cannot Read Value from Input Field
How to Toggle Mat-Expansion-Panel With Button Click
How to Get the Client Ip Address from Browser in Angular (Typescript)
Large File Upload Though HTML Form (More Than 2 Gb)
How to Remove the Space Between Inline/Inline-Block Elements
Bootstrap Accordion Issue With Arrow
How to Vertically Align Elements in a Div
Children Not Staying Inside Parent Divs
Make a Div Fill the Height of the Remaining Screen Space
How to Prevent a User from Entering Negative Values in HTML Input
How to Add Images in Select List
How to Prevent Autoplay in Iframe HTML Content
Why Aren't My Absolutely/Fixed-Positioned Elements Located Where I Expect