CSS: Container element taking unncessary space when inline-block children wrap - html

How can I make a container element use only the "required" space to wrap inline-block children? See this image below...
You can see how the arrows stretch through the unwanted space. I've spent a few hours already trying to make it work. I even tried a couple nice SO answers such as this one...
Fit div width to inline-block children
but it didn't seem to suit my case scenario because it assumes a fixed number of children per row.
The fiddler below illustrates the problem I'm facing. However, there's one thing to notice, the max-width of the .box is supposed to be set in % rather than px
html,
body {
background-color: #eee;
height: 100%;
width: 100%;
padding: 0;
margin: 0;
}
.container {
display: block;
width: 100%;
}
.box {
display: inline-block;
/*
In the production "max-width" will be set to a percentage value rather than px units
Using pixels here just for illustration purposes
*/
max-width: 380px;
padding: 20px;
margin: 20px;
background-color: #fff;
border: 1px solid #ddd;
}
.img-wrapper {
}
.img-wrapper span {
display: inline-block;
position: relative;
overflow: hidden;
width: 128px;
height: 128px;
border-radius: 10px;
}
.img-wrapper img {
max-width: 196px;
position: absolute;
top: 50%;
left: 50%;
transform: translateX(-50%) translateY(-50%);
}
<div class="container">
<div class="box">
<p>
Lorem ipsum dolor sit amet
</p>
<div class="img-wrapper">
<span>
<img src="https://i.imgur.com/yvMCRsv.jpg" />
</span>
<span>
<img src="https://i.imgur.com/efFdd14.jpg" />
</span>
<span>
<img src="https://i.imgur.com/7Yv987J.jpg" />
</span>
</div>
</div>
</div>
Question
Basically, all I want to achieve is having the parent/container element use only the necessary space when the children are inline-block (floats are welcomed)

Here's a Fiddle, with working solution
What I basically did there was that,
Added float: left; and margin: 0px 5px 5px 0px; in .img-wrapper span{}
Added :nth-of-type() selector for .img-wrapper span{}
code:
.img-wrapper span {
display: inline-block;
position: relative;
overflow: hidden;
width: 128px;
height: 128px;
border-radius: 10px;
float: left;
margin: 0px 5px 5px 0px;
}
.img-wrapper span:nth-of-type(2n + 1){
clear: left;
}

Related

The padding with border radius overlaps the image inside

I have an <img> and I'm giving it a background-color, a padding along with a border-radius.
The problem is that even though I have a padding and so there's a lot of space between the inner image and the edges of the box, the border-radius apparently gets applied to the image inside as well, and therefore causes the edges of the inner image to be cut off. Here's what it looks like:
https://i.stack.imgur.com/tYKfh.png
.element img {
padding: 25px;
border-radius: 50%;
background-color: #6e4fff;
height: 30px;
width: 30px;
}
<div class="element">
<img src="https://i.imgur.com/L8JEkBk.png" />
</div>
Move the styling to the parent element, take out the padding, and center with flexbox:
.valueelement {
border-radius: 50%;
background-color: #6e4fff;
height: 80px;
width: 80px;
display: flex;
align-items: center;
justify-content: center;
}
.valueelement img{
height: 30px;
}
<div class="valueelement">
<img src="https://i.imgur.com/L8JEkBk.png" />
</div>
Another solution:
.valueelement img {
height: 30px;
margin: 0 auto;
display: block;
}
.valueelement {
padding: 25px;
background-color: #6e4fff;
border-radius: 50%;
overflow: hidden;
height: 30px;
width: 30px;
}
<div class="valueelement">
<img src="https://i.imgur.com/L8JEkBk.png" />
</div>
Simply use the calc method when positioning your image in conjunction with display:block and position:absolute. Formula: calc(50% - imageWidth/2), then do the same for the height.
JsFiddle: https://jsfiddle.net/h867qgcL/
.valueelement {
position:relative;
border-radius: 50%;
background-color: #6e4fff;
height: 80px;
min-width:80px;
width:80px;
}
.valueelement img {
position: absolute;
padding:0;
display:block;
margin-left:calc(50% - 13px);
margin-top:calc(50% - 15px);
height:30px;
}
<div class="valueelement">
<img src="https://i.imgur.com/L8JEkBk.png"/>
</div>

Why does this margin flows outsite its parent?

I have a div mainlogo inside another div for logo. Now, when I give it margin on top, it flows outside the outer divs. What I want is that when I give it margin-top, it should displace itself downward, instead of flowing its margin outside the parent.
.header {
width: inherit;
height: 100px;
background-color: #0080FF;
box-shadow: 0.5px 0.5px 0.5px 0.5px grey;
}
.headerdiv img {
width: 80px;
}
.headerdiv {
width: 1020px;
margin: 0 auto;
height: inherit;
position: relative;
}
#mainlogo {
height: 80px;
width: 350px;
margin-top: 10px;
}
<div class="header">
<div class="headerdiv">
<a href="onlinequiz login.php">
<div id="mainlogo">
<img src="Images/logo.png"></img>
</div>
</a>
</div>
</div>
Why does it happen and how can I solve it?
Tricky margin spec. This page has a very good explanation of the behavior you are running into. If you don't want to change the #mainlogo whitespace to padding, you can work around the margin collapse by giving an overflow: hidden property to your .header.
.header {
width: inherit;
height: 100px;
background-color: #0080FF;
box-shadow: 0.5px 0.5px 0.5px 0.5px grey;
overflow: hidden;
}
.headerdiv img {
width: 80px;
}
.headerdiv {
width: 1020px;
margin: 0 auto;
height: inherit;
position: relative;
}
#mainlogo {
height: 80px;
width: 350px;
margin-top: 10px;
}
<div class="header">
<div class="headerdiv">
<a href="onlinequiz login.php">
<div id="mainlogo">
<img src="Images/logo.png"></img>
</div>
</a>
</div>
</div>
Also, you might consider changing the #mainlogo div into a span and self-closing your img tag to avoid unexpected cross-browser quirks.
beacuse you are using a generalize DIV's as it is. Use floating property i.e. float:left there,
and it will work
like this,
#mainlogo {
float:left;
height: 80px;
width: 350px;
margin-top:20px;
}
Try this ... Set the position property of headerdiv to position: absolute;
.headerdiv {
width: 1020px;
margin: 0 auto;
height: inherit;
position: absolute;
}

Having trouble centering an image in CSS

I am trying to horizontally center an image within a div. However, I haven't been able to. I've tried setting the vertical-align to middle and the margin to auto, 0 auto, and every variation I can think of. Nothing works. Here is the code for how it is currently set up:
img {
border: 0;
margin: 0 auto;
}
.intro img {
border-radius: 50%;
vertical-align: middle;
padding: 10px;
}
The image is in the intro div. Any advice you can give would be helpful.
If you want to center your image both horizontally & vertically, this should do the trick :
.intro {
display: table;
width: 500px; /* works with any width */
height: 150px; /* works with any height */
background: #ccc;
}
.imgcontainer {
display: table-cell;
vertical-align: middle;
text-align: center;
}
img {
background: #fff;
padding: 10px;
border-radius: 50%;
}
<div class="intro">
<div class="imgcontainer">
<img src="http://s.gravatar.com/avatar/bf4cc94221382810233575862875e687?r=x&s=50" />
</div>
</div>
Use position:relative in parent .intro and use the code shown below in img, it will work with any width and height
display:block;
margin:auto;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0
Snippet
.intro {
border: dashed red; /* demo */
display:inline-block; /* demo */
vertical-align:top; /* demo */
position: relative
}
.intro img {
border-radius: 50%;
vertical-align: middle;
display: block;
margin: auto;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0
}
.intro:first-of-type {
width: 200px;
height: 200px;
}
.intro:last-of-type {
width: 400px;
height: 300px;
}
<div class="intro">
<img src="//lorempixel.com/100/100" />
</div>
<div class="intro">
<img src="//lorempixel.com/100/100" />
</div>
The css style margin: 0 auto; should do the horizontal part of the trick.
For the vertical part you also need to take care of the parent.
Look at How to vertically align an image inside div for more info.
vertical-align works only in table cells. Try to use Flexbox. The element containing your image should have CSS properties:
display: flex;
align-items: center;

Horizontally align div with additional margin-left and margin-right

I would like to center a <div /> in the <body /> and add additional margin-left and margin-right to it.
Something like that - of course it should work :) https://jsfiddle.net/kweyn912/
Normally, I would use margin: auto, but in this case I want to specifically add additional margin, so I cannot do that.
I tried using transform: translateX(-50%) together with left: 50% and margin-left. That worked until I tried setting margin-right
Side notes:
I have some restrictions: I cannot use padding instead of margin. I cannot use position: absolute and I have to use display: block
Updated your project adding
div {
width: 200px;
height: 100px;
background: lightblue;
margin-left: 20px;
margin-right: 40px;
position: absolute;
left: calc(50% - 110px)
}
body {
width: 100%;
background: white;
text-align: center
}
https://jsfiddle.net/kweyn912/6/
Try adding padding to the body (or parent container) rather than the div with the div centered in the parent element. That should center the div with a gutter on the left/right.
div {
max-width: 400px;
height: 100px;
background: lightblue;
margin: 0 auto;
display: block;
}
body {
background: white;
padding: 0 20px
}
https://jsfiddle.net/y16k52xt/
Instead of using margin: 0 auto and display: block, you can use display: inline-block along with text-align: center on the parent element to center the divs. Then use your margin adjustments to set it off-center. Use white-space: pre to force each item to break to a new line.
body {
width: 100%;
background: white;
text-align: center;
white-space: pre;
}
.div {
margin-left: 20px;
margin-right: 40px;
width: 200px;
height: 100px;
background: lightblue;
display: inline-block;
}
.two {
width: 250px;
margin-left: 30px;
margin-right: 80px;
background: red;
}
<body>
<p>
CENTER
</p>
<div class="div one">
Lorem ipsum
</div>
<div class="div two">
</div>
<div class="div three">
</div>
</div>
</body>
https://jsfiddle.net/kweyn912/12/
Lets say that you want your element to be off center in 20px to the left (Net result of your example of margin-left: 20px and margin-right: 40px;
This is equuivalent to a transform: translateX(-20px);
div {
width: 200px;
height: 100px;
background: lightblue;
margin: auto;
transform: translateX(calc(20px - 40px));
}
<div>
Lorem ipsum
</div>

100% height of absolutely positioned element

I'm trying to set up an anchor that would automatically stretch to match the dimensions of an image that is used as a background. Also, the anchor's text needs to be both horizontally and vertically centered. Here's my current HTML markup:
<div class="wrap">
<img src="http://placehold.it/350x150">
<a href="#">
<span>Anchor</span>
</a>
</div>
The idea is that my .wrap is a fluid column of a grid, so the image stretches to match the width and height is given by the image's ratio. The anchor is displayed as a table for vertical alignmenet and the span has a background visible on hover over the anchor. There is my CSS:
* {
box-sizing: border-box;
}
body {
height: 100%;
}
.wrap {
height: auto;
margin: 2em;
overflow: hidden;
padding: 1em;
position: relative;
width: 80%;
}
.wrap img {
width: 100%;
}
.wrap a {
display: table;
height: 100%;
left: 1em;
padding: 1em;
position: absolute;
text-align: center;
top: 1em;
width: 100%;
}
.wrap a span {
border: 1px solid red;
display: table-cell;
vertical-align: middle;
}
.wrap a:hover span {
background: red;
}
The problem I have is the height of the anchor, it refuses to fill the container. Here you have a fiddle: http://jsfiddle.net/xuxG5/3/
I tried looking into other questions around here but unfortunately none of them matched my problem - it's a combination of 100% height table in a fluid parent height but the most common answer was to set height of the parent and the absolute position doesn't make it any simpler.
Question edited to show the anchor's text doesn't always have just one line of text
I was hoping you could help me if there is a CSS solution, otherwise I will use a simple JS script.
This should make the A fill the box - http://jsfiddle.net/xuxG5/5/
.wrap a {
display: block;
left: 2em;
right: 2em;
bottom: 2em;
top: 2em;
position: absolute;
text-align: center;
border: 1px solid red;
}
.wrap a span {
position: absolute;
top: 50%;
margin-top: -1em;
line-height: 2em;
width: 100%;
display: block;
}