This question already has answers here:
Image inside div has extra space below the image
(10 answers)
Closed 6 years ago.
I'm working on a photo page for a site I'm creating, however it seems that the link is extending into the whitespace beneath the image. The problem disappears when I remove the surrounding <section> but I'm not sure why.
Here's a Jsfiddle better showcasing my code and the problem
.photo {
text-align: center;
float: left;
width: 70%;
margin-left: 8%;
margin-top: 5%;
margin-bottom: 5%;
}
.photo a {
border: 1px solid red;
}
.photo img {
margin: 1%;
}
<section class="photo">
<a href="#">
<img src="http://upshout.com/wp-content/uploads/2015/06/dwarf-kitten-01.jpg" />
</a>
<a href="#">
<img src="http://upshout.com/wp-content/uploads/2015/06/dwarf-kitten-01.jpg" />
</a>
<a href="#">
<img src="http://upshout.com/wp-content/uploads/2015/06/dwarf-kitten-01.jpg" />
</a>
</section>
I added a border to the problem area. Any help is much appreciated!
because a and img are inline elements, so
make a a block level element by display:block, to the border appear around the image
set display:block to img to remove underneath whitespace caused by being an inline element. (other solution would be setting vertical-align-bottom, given img by default is vertical-align:baseline)
Note: I gave the img a max-width:100% to be responsive, and if you give the border to img instead of a, the a being display:block isn't necessary anymore, although is good to have it.
See more about inline elements here on w3
.photo {
text-align: center;
float: left;
width: 70%;
margin-left: 8%;
margin-top: 5%;
margin-bottom: 5%;
}
.photo a {
display: block;
}
.photo img {
display: block;
border: 1px solid red;
max-width: 100%;
margin: 1%
}
<section class="photo">
<a href="#">
<img src="http://upshout.com/wp-content/uploads/2015/06/dwarf-kitten-01.jpg" />
</a>
<a href="#">
<img src="http://upshout.com/wp-content/uploads/2015/06/dwarf-kitten-01.jpg" />
</a>
<a href="#">
<img src="http://upshout.com/wp-content/uploads/2015/06/dwarf-kitten-01.jpg" />
</a>
</section>
Related
This is the css for my social media bar. It's supposed to be on the right side of my page. When I use margin or float the images get really small.
.facebook {
width: 72.8%;
height: auto;
margin-left: -9%;
margin-top: 14%;
}
.insta,
.twitter {
width: 49.2%;
height: auto;
}
.social {
list-style-type: none;
position: fixed;
margin-left: 95%;
}
<div class="social">
<img src="https://facebookbrand.com/wp-content/themes/fb-branding/prj-fb-branding/assets/images/fb-art.png" class="facebook" alt="Facebook Logo">
</div> <br> <br> <br> <br>
<div class="social">
<img src="https://www.freeiconspng.com/uploads/-van-nederland-elgie-gaat-haar-ontwikkeling-posten-op-twitter-24.png" class="twitter" alt="Twitter Logo">
</div> <br> <br> <br>
<div class="social">
<img src="https://midflokkurinn.is/wp-content/uploads/2017/11/250ig.png" class="insta" alt="Instagram Logo">
</div>
The parent div has a standard with and height, if you give the child margins, it makes the space that it has smaller and smaller.
For example if you give something a height of 50px and a margin of 30xp its left with (50px - 30px) = 20px space to fill itself in.
U should give the images a height, for example
.facebook, .insta, ,twitter {
height: 50px;
width: 50px;
}
and then you can give it whatever margin you want.
You should wrap these in a container and set the positioning on that. Spacing the icons to the right with 95% left margin just isn't going to give you nice results. Set your a tags to display block and give your images consistent dimensions and you'll be set. Finally, using br tags is not good practice for spacing. It will give you nightmares if you ever need to adjust that spacing, say on mobile views for example. You'll want to use padding or margins instead. Here is an example:
#social-container {
position: fixed;
top: 10px;
right: 0px;
width: 20px;
}
.social {
display: block;
margin-bottom: 10px;
}
.social:last-child {
margin-bottom: 0px;
}
.social a {
display: block;
}
.social img {
display: block;
width: 20px;
height: 20px;
}
<div id="social-container">
<div class="social">
<a href="#">
<img src="https://facebookbrand.com/wp-content/themes/fb-branding/prj-fb-branding/assets/images/fb-art.png" class="facebook" alt="Facebook Logo">
</a>
</div>
<div class="social">
<a href="#">
<img src="https://www.freeiconspng.com/uploads/-van-nederland-elgie-gaat-haar-ontwikkeling-posten-op-twitter-24.png" class="twitter" alt="Twitter Logo">
</a>
</div>
<div class="social">
<a href="#">
<img src="https://midflokkurinn.is/wp-content/uploads/2017/11/250ig.png" class="insta" alt="Instagram Logo">
</a>
</div>
</div>
This question already has answers here:
Align inline-block DIVs to top of container element
(5 answers)
Closed 4 years ago.
So if you look at the 2nd image, it shifts up when the text under spans onto two lines. Maybe this is to do something with the div containers that I'm missing? Can't seem to find the right solution. I'm not super HTML savvy but I'm sure this is a simple fix? ..help!
Screenshot Here
#wrapper {
width: 100%;
height: auto;
margin: 0;
padding: 0;
}
.thumb {
position: relative;
display: inline-block;
width: 17.6%;
height: auto;
box-sizing: border-box;
margin: 10px 1%;
padding: 0;
text-decoration: none;
}
.thumb p {
font-family: "Helvetica", helvetica;
color: #aaa;
margin-left: 0em;
margin: 0.5em;
}
.thumb img {
width: 100%;
height: 230px;
display: block;
margin: 0 auto;
}
<body style="margin: 0px;">
<div id="wrapper">
<h1>GALLERY</h1>
<h2>LENDING LIBRARY</h2>
<a id="Altamont Wet WR" class="thumb" href="openwindow-https://www.urbanelectricco.com/altamontwall.html">
<img src="thumbs/altamontwall_mtn.jpg" />
<p>Altamont Wet WR</p>
<hr>
<h3>'Kromezone' Powder Coat, Clear Glass</h3>
</a>
<a id="Archibald" class="thumb" href="openwindow-https://www.urbanelectricco.com/archibald.html">
<img src="thumbs/archibald_mtn.jpg" />
<p>Archibald WR</p>
<hr>
<h3>Bronze Finish, Clear Glass & Black Finish, Clear Glass
</h3>
</a>
<a id="Belle Meade" class="thumb" href="openwindow-https://www.urbanelectricco.com/bellemeade.html">
<img src="thumbs/bellemeade_mtn.jpg" />
<p>Belle Meade WL</p>
<hr>
<h3>Vintage Finish</h3>
</a>
<a id="Belle Meade Double" class="thumb" href="openwindow-https://www.urbanelectricco.com/bellemeadedouble.html">
<img src="thumbs/bellemeadedouble_mtn.jpg" />
<p>Belle Meade Double WL</p>
<hr>
<h3>Polished Nickel Finish</h3>
</a>
Best way of getting the alignment to be the same across all of the thumbnails is add the following to the .thumb class:
vertical-align:top;
The reason why the issue arises is that the standard vertical alignment for any div is baseline (right at the bottom). When your text goes onto a second line, it naturally increases the height of that element (and is double the height of all of the other tags) and pushes everything else in that particular div upwards.
Hope that helps!
Hi I'm trying to center the 4 images in the div. As you can see they're stuck on the left.
I would like them to be perfectly centre justified with at least a 5-10px margin surrounding them (moved slightly to the right).
It's only this section I'm concerned about...not the rest of the HTML site. Thanks.
My html is:
<section id="slide-6" class="homeSlide">
<div class="bcg">
<div class="hsContainer">
<!-- <div class="hsContent">-->
<div class="img">
<a target="_blank" href="">
<img src="http://fieldtelecommunications.com/images/telecommunications_equipment.jpg" alt="Klematis" width="280" height="280">
</a>
</div>
<div class="img">
<a target="_blank" href="">
<img src="http://fieldtelecommunications.com/images/telecommunications_equipment.jpg" alt="Klematis" width="280" height="280">
</a>
</div>
<div class="img">
<a target="_blank" href="">
<img src="http://fieldtelecommunications.com/images/telecommunications_equipment.jpg" alt="Klematis" width="280" height="280">
</a>
</div>
<div class="img">
<a target="_blank" href="">
<img src="http://fieldtelecommunications.com/images/telecommunications_equipment.jpg" alt="Klematis" width="280" height="280">
</a>
</div>
</div>
</div>
</div>
</section>
Css is:
#slide-6 .bcg {
position: relative;
background-color: #efefef;
height:50%;
}
slide-6 .hsContent {
position: relative;
}
slide-6 .hscontainer {
width: 100%;
height: 100%;
display:inline;
overflow: hidden;
margin: 0 auto; position: relative;
}
div.img {
margin: 20px;
/*padding: 5px;*/
/*border: 1px solid #0000ff;*/
/*height: auto;
width: auto;*/
/*float: left;*/
display:inline;
}
add text-align: center to this div #slide-6
div.img {
display: inline-block;
margin: 20px;
}
and on parent div
text-align: center
Using a grid system like 960gs will make your life easier.
Otherwise you'll have to put the images in containers with width: 25%, 0 margin and 0 padding. You can set margins and paddings on the contents of the containers though. OR make the hsContainer fixed width and give it margin: auto;
Your wrapper needs a width with margin:0 auto in order for the inner content to be aligned center.
section {margin:0 auto;width:1290px;}
JsFiddle Example
one way to just encapsulate everything within
<center></center>
heres a fiddle :)
https://jsfiddle.net/qcoavm5r/
Provide text-align:center to the parent element 'hsContainer'. This property will center align the elements inside it.
Try display: inline-block: instead only block.
I'm really struggling to achieve clean code whilst trying to get the layout I want.
My current code isn't behaving with other floating divs on the page and the bottom section with text and buttons are not displaying inline and are forcing other elements out of position.
JSFiddle: http://jsfiddle.net/XXe4L/
I've tried various rehashings of my code but can't figure it out and am at the point where I'm starting to pull my hair out.
I'm trying to acheive a layout similar to this wireframe - https://wireframe.cc/UTHU1d.
I've copied my latest code below, minus the bottom div with text and button because I've not been able to get that anywhere near right.
CSS:
.sceneItem { clear:both; display: block; margin: 0 auto; width: 100%; }
.scenePart { display: block; width: 100%; height: 360px; float: left; }
.imgLink { margin: 0; position: relative; }
.largeImage { display: block; width: 648px; height: 360px; float: left; }
.previewImage { width: 318px; height: 180px; display: block; margin-bottom: 3px; float: left; }
.previewImage a.imgLink { }
.img { display: inline; }
and HTML:
<li class="sceneItem">
<div class="largeImage scenePart">
<a href="" class="imgLink" title="">
<img src="24361_01_01.jpg" class="img" alt=""></a>
</div>
<div class="previewImage scenePart">
<a href="" class="imgLink" title="">
<img src="24361_01_01.jpg" class="img" alt=""></a>
</div>
<div class="previewImage scenePart">
<a href="" class="imgLink" title="">
<img src="24361_01_02.jpg" class="img" alt=""></a>
</div>
</li>
I am guessing that if your floats are interfering with each other, you might try creating a block formatting context for your floated images as follows:
.box {
width:970px;
overflow: auto;
}
By doing this, any elements following will not interact with the floated images.
Note: Without the additional page elements, I can't say for sure what the problem is.
I have a bunch of vertically aligned tab items in which and I can't seem to get the parent container (the <a href...> in my HTML) to expand to cover the child elements. I've tried using a <br style="clear: both"> and overflow: hidden;but the first didn't do anything and the second just cut it off (using auto just added a scroll bar, which doesn't help) any thoughts on how to fix it?
HTML sample:
<li class="active">
<a href="#pane1a" data-toggle="tab">
<div class="preview-box">
<img class="preview-image" src="img/monestary_floorplan.png">
<p id="previewcarousel1a"></p>
</div>
</a>
</li>
<li>
<a href="#pane1b" data-toggle="tab">
<div class="preview-box">
<img class="preview-image" src="img/bkg-img-home2.jpg">
<p id="previewcarousel1b"></p>
</div>
</a>
</li>
CSS:
.preview-box {
width: 90px;
height: 80px;
/*border: 2px solid red;*/
margin-left:auto;
margin-right:auto;
}
.preview-image {
display: block;
width: 75px;
height: 60px;
border: 4px solid #84be46;
margin-left:auto;
margin-right:auto;
}
.preview-items p{
color: #84be46;
text-align: center;
margin-top: 5px;
}
The whole site can be seen here
Adding display: inline-block; to your a element seems to solve your problem. You may have adjust padding/margin, though.