This question already has answers here:
Why does my image have space underneath?
(3 answers)
How to remove the space between inline/inline-block elements?
(41 answers)
Closed 7 years ago.
I have the following problem: I want to have three images in the same line, without margins in between. The problem is that when using display:inline or display:inline-block vertical and horizontal white spaces are generated between the images.
div img{
width: 33.3333%;
display: inline-block;
}
You can see the problem in the demo.
I know that with float:left I can solve the problem, but I would like to avoid this, because I don't like to use this unless is very necessary.
Thanks!
If you don't want to use float: left, You can clear out the space between the image tag by using HTML comment block.
Working example.
CSS :
div img {
width: 33.3333%;
display: inline-block;
}
HTML :
<div>
<img src="http://imagenesdereligion.com/wp-content/uploads/2014/10/imagenes-de-santa-claus-santa_cla.jpg"><!--
--><img src="http://www.conhijos.es/wp-content/uploads/2010/10/RatonPerez.jpg"><!--
--><img src="http://hazteconellos.com/wp-content/uploads/2013/12/reyesmagos-1.jpg">
</div>
Solution using Flexbox
You can set the parent div to have flexible layout using display: flex. It removes the white-space as well.
div {
display: flex;
align-items: flex-end;
}
div img {
width: 33.3333%;
display: inline-block;
}
<div>
<img src="http://imagenesdereligion.com/wp-content/uploads/2014/10/imagenes-de-santa-claus-santa_cla.jpg" />
<img src="http://www.conhijos.es/wp-content/uploads/2010/10/RatonPerez.jpg" />
<img src="http://hazteconellos.com/wp-content/uploads/2013/12/reyesmagos-1.jpg" />
</div>
Solution using margin-right
div img {
display: inline-block;
margin-right: -0.33em;
width: 33.3333%;
}
<div>
<img src="http://imagenesdereligion.com/wp-content/uploads/2014/10/imagenes-de-santa-claus-santa_cla.jpg" />
<img src="http://www.conhijos.es/wp-content/uploads/2010/10/RatonPerez.jpg" />
<img src="http://hazteconellos.com/wp-content/uploads/2013/12/reyesmagos-1.jpg" />
</div>
Related
This question already has answers here:
Image inside div has extra space below the image
(10 answers)
Closed 1 year ago.
[Duplicate]
answer here Image inside div has extra space below the image
I display some images with different sizes along multiple rows.
Example here
But there are gaps between the rows. I've tried a lot, but couldn't find why.
<div class="muralBox">
<div class="wrapper">
<div v-for="(topAlbum, i) in topAlbumInfo.value" :key="topAlbum.name" class="albums">
<Album :info="topAlbum" :width="layoutConfig[i].width"/>
</div>
</div>
</div>
And in Album.vue
<img :src="img" :width="width" :height="width" alt=""/>
And css:
.muralBox {
margin: auto;
max-width: 400px;
min-width: 400px;
}
.wrapper {
background: #1f1c2c;
display: flex;
flex-wrap: wrap;
}
I'm not sure if this is exactly what you wanted since I can't see how you styled them, but I just gave images 100% height and width and that basically did it in a codepen.
CodePen link
.albums img{
height: 100%;
width: 100%;
}
This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
How can I vertically align elements in a div?
(28 answers)
How can I center text (horizontally and vertically) inside a div block?
(27 answers)
In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?
(6 answers)
Closed 2 years ago.
In a header top banner:
#header { background-color: #eaeaea; }
#header a { display: inline-block; margin-right: 1.5em; }
.logo { margin-right: 6em; }
<div id="header">
<img src="https://via.placeholder.com/150x120" class="logo">
Lorem
Ipsum
Contact
</div>
what are the main methods used nowadays to center the 3 right links vertically inside the banner?
I know how to do it with position: relative; top:-...px or similar methods or even <table>, but what would be considered nowadays as the most appropriate methods, especially for responsive design?
Would you use flex features for this? If so, how?
I also want stick the group of 3 links to the right border of the screen, such that, if we resize the browser width, it always stays floating along the right border. I was about to use float: right but there's probably a better solution than mixing flex and float?
PS: These last details are not in the linked questions Flexbox: center horizontally and vertically and How to Vertical align elements in a div?, thus it's not a duplicate.
Personnaly i would use flex with align-items:center for responsive design.
See the code below:
#header{
background-color: #eaeaea;
display:flex;
align-items:center;
justify-content:space-between;
}
#header a{
display: inline-block;
margin-right: 1.5em;
}
.logo{
margin-right: 6em;
}
<div id="header">
<img src="https://via.placeholder.com/150x120" class="logo">
<div id="links">
Lorem
Ipsum
Contact
</div>
</div>
When it comes to vertical centering of text and items, this trick comes in handy:
#header {
display: flex;
align-items: center;
}
A more reliable (albiet complicated) method is to use a grid wrapper
.center {
display: grid;
grid-template-areas: 'a b c';
grid-template-rows: 1fr max-content 1fr;
}
.center > * {
grid-area: b
}
<div id="header">
<img src="https://via.placeholder.com/150x120" class="logo">
<a class="center" href=""><span>Lorem</span></a>
<a class="center" href=""><span>Ipsum</span></a>
<a class="center" href=""><span>Contact</span></a>
</div>
If you don't care about old browsers (IE), you can use place-items, instead of align-items.
#header{
background-color: #eaeaea;
display: flex;
place-items: center;
}
#header a{
display: inline-block;
margin-right: 1.5em;
}
.logo{
margin-right: 6em;
}
<div id="header">
<img src="https://via.placeholder.com/150x120" class="logo">
Lorem
Ipsum
Contact
</div>
Quoting from https://developer.mozilla.org/en-US/docs/Web/CSS/place-items:
The CSS place-items shorthand property allows you to align items along both the block and inline directions at once (i.e. the align-items and justify-items properties) in a relevant layout system such as Grid or Flexbox. If the second value is not set, the first value is also used for it.
This question already has answers here:
How to make an image center (vertically & horizontally) inside a bigger div [duplicate]
(36 answers)
Centering image horizontally and vertically [duplicate]
(5 answers)
Closed 9 years ago.
I am trying to horizontally center a large image. Because I am using HTML5, I can't use <center>. I could use left:400px, but that wouldn't work for different screen sizes.
Wrap the image inside an element and use text-align: center;...
Demo
<div class="center">
<img src="http://www.google.com/intl/en_ALL/images/logos/images_logo_lg.gif" alt="Google" />
</div>
.center {
text-align: center;
}
Alternatively if you are aware, that what's the image width, you can also use margin: auto; with display: block; as img tag is inline element by default and of course, the width property
img {
width: 276px;
display: block;
margin: auto;
}
Demo 2
Try this css to horizontally center
display: block;
margin: 0 auto;
= the top and bottom margin 0, and the left and right margin auto
Use CSS text-align: center;. And don't forget to set width on the div or it will look left-aligned.
<div style="text-align: center; width: 100%; border: 1px solid black;">Centered</div>
Depending on your specific situation, this has worked for me on several projects:
<style>
.outer{float: left; position: relative; left: 50%;}
.inner{float: left; position: relative; left: -50%;}
</style>
<div class="outer">
<div class="inner">content you want to center, image, text, whatevs</div>
</div>
The IMG element is inline, by default. So, as the others have pointed, you have two options:
1) Keep it inline, and use text-align: center;.
2) Make it a block element with display: block;, and then use margin: auto;, which works only on block elements. I think this solution is better. Setting the width is just another way to force it to be a block element, but it's less obvious for someone that may read the code later. So explicitly setting the display type to block is better for readability.
If your element has the width property , then give it margin:auto;.
I have a div called images_box which has a width of 277px. Within that div I have 9 images in it, just stored as <a> tags. I've got the images to float left and line up quite nicely within my div, but I would like the images to align vertically, as some are portrait and others are landscape. I know I can do this if I enclose each image in a div, but the plugin I use to launch the gallery won't recognise which image is being fired, so i need to enclose them as just within the <a> tags.
This is the code I have, if someone can help me just align the images horizontally and vertically. I don't want the images to be skewed.
#images_box a {
float: left;
padding: 9px;
width: 70px;
height: 70px;
text-align: center;
vertical-align: middle;
display: table-cell;
}
my data
<div id="images_box">
<a class="fancybox" rel="gallery1" href="http://farm8.staticflickr.com/7069/7060779347_fbee5aae15_b.jpg" title="morning after[explored] (mariosworld343)">
<img src="http://farm8.staticflickr.com/7069/7060779347_fbee5aae15_m.jpg" alt="" />
</a>
<a class="fancybox" rel="gallery1" href="http://farm8.staticflickr.com/7234/7047458501_46a2203733_b.jpg" title="Self confined... (TVidhya)">
<img src="http://farm8.staticflickr.com/7234/7047458501_46a2203733_m.jpg" alt="" />
</a>
<a class="fancybox" rel="gallery1" href="http://farm8.staticflickr.com/7053/6918451990_20fa76f338_b.jpg" title="kleiner schrittmacher (KatjaGiersig)">
<img src="http://farm8.staticflickr.com/7053/6918451990_20fa76f338_m.jpg" alt="" />
</a>
<a class="fancybox" rel="gallery1" href="http://farm8.staticflickr.com/7121/7059981833_abe404f4a0_b.jpg" title="(caro diario.)">
<img src="http://farm8.staticflickr.com/7121/7059981833_abe404f4a0_m.jpg" alt="" />
</a>
</div>
I think i figured out what you want. float: left to get the images side by side is not necessary.
#images_box {
background: #eee;
overflow: hidden; /* this div will get the height of the tallest element inside it */
white-space: nowrap; /* prevent line-breaks */
}
#images_box a {
padding:9px;
display: inline-block; /* required to apply vertical-align as expected */
vertical-align: middle;
}
Works in:
Internet Explorer 6+
and modern browsers
Live demo: http://jsfiddle.net/vjDVp/1/
Basically, it seems like you're looking for:
#images a {
padding: 9px;
width: 70px;
height: 70px;
text-align: center;
vertical-align: middle;
display: table-cell;
}
Where you can tweak the vertical-align to top or middle, as needed. There's no need for floats. The dimensions you've described in your original question are off, so apologies if I've misunderstood.
Note also that display: table-cell is not IE6/7-friendly, if that matters.
Set the css to the images, not the a tags. Give them a classname (to make sure your other images are not affected) and apply the code you provided in your question to that (img.myclass). Or, at least the vertical-align.
Also, what are the sizes of the images? Do they all have the same width, same height or a certain size depending on whether it's a landscape or portrait image?
Oh and I don't know why, but text-align seems to work on img tags too sometimes.
This question already has answers here:
Align inline-block DIVs to top of container element
(5 answers)
Closed 2 years ago.
I'm struggling a bit with this CSS-issue: http://jsfiddle.net/timkl/JbEX8/
I want two divs to align horizontally using "display: inline-block", however when I add text to one of the divs, the alignment is off.
Is there a way to make the two divs align without resorting to floats?
This is my markup:
<div class="box">
<p>Some text</p>
</div><!-- /box -->
<div class="box">
<!-- No text -->
</div><!-- /box -->
This is my CSS:
body {
color: gray;
}
.box {
background: #ccc;
height: 100px;
width: 200px;
display: inline-block;
}
Check out my example on jsfiddle: http://jsfiddle.net/timkl/JbEX8/
Add vertical-align to class box:
vertical-align: middle;
Also see the updated jsfiddle.
You should float them:
.box {
float: left;
background: #ccc;
height: 100px;
width: 200px;
display: inline-block;
}
Demo: http://jsfiddle.net/JbEX8/1/
Do note that since you didn't define margins, there is no spacing between them.