Align a div vertically inside an inline-block - html

I have an unknown amount of divs that will be populated within an inline-block div. There's no problem when there is more than one div as it looks fine, but when there is only one div I want it to be centered in the parent. I want to try to do this without any fixed/absolute positioning and hopefully without using javascript.
In the fiddle you can see the first column, the div with "Put me in the middle" should be in the middle.
http://jsfiddle.net/Lzzyywf2/5/
<div class="inlineb">
<div class="insideInline">Hello</div>
</div>
<div class="inlineb">
<div class="insideInline">Hello</div>
<div class="insideInline">Hello</div>
</div>
.inlineb {
min-height:102px;
display:inline-block;
border:1px red solid;
vertical-align:top;
}
.insideInline {
height:50px;
border:1px solid blue;
}

Try :only-child for .insideInline. This will target the element if there is only one inside the parent. Here's my fiddle.
#wrapper {
}
.inlineb {
min-height:102px;
display:block;
border:1px red solid;
vertical-align:top;
width:126px; /*or whatever value*/
}
.insideInline {
height:50px;
border:1px solid blue;
display:inline-block;
width:37px;/*or whatever value*/
}
.inlineb .insideInline:only-child {
display:block;
margin:0 auto;
}

If you are able to manually add a class for those containers with only one child, this would work:
http://jsfiddle.net/Lzzyywf2/6/
<div class="inlineb one-child">
<div class="insideInline">Hello</div>
</div>
combined with:
.one-child:before {
content: "";
display: block;
height: 25px;
}
If you can't add a class, this will work in IE9+:
http://jsfiddle.net/Lzzyywf2/9/
.insideInline:only-child {
display:block;
margin-top:25px;
}
Credit to the OP for improving on this idea!

Flex box will make your life easier:
.flex-contain{
padding: 0;
margin: 0;
list-style: none;
-ms-box-orient: horizontal;
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -moz-flex;
display: -webkit-flex;
display: flex;
width:900px;
height:500px;
background:#000;
align-items:center;
align-content:center;
}
.flex-item {
height:200px;
width:200px;
background:yellow;
margin:0 auto;
}
http://codepen.io/cjthedizzy/pen/vEpyvR

Related

Fix floating box in Html

So I want my boxes to stay the same position even when I shrink my window.
But for now when I shrink them, they will resize and push the floating box to the bottom space and not making a line together.
.bodybox1
{
border:1px solid black;
width:45%;
background:white;
margin:1% auto 2% auto;
text-align:center;
font-size:2em;
padding:3%;
overflow:hidden;
}
.boxcoverblack
{
text-align:center;
width:120px;
padding:7%;
border:1px solid black;
margin-top:2%;
}
.signuparea
{
width:30%;
float:right;
border:1px solid black;
text-align:center;
padding:3%;
overflow:hidden
}
.signuptext
{
text-align:center;
width:180px;
padding:7%;
word-spacing:15px;
border:1px black solid;
line-height:35px;
}
.freeshipping
{
float:left; /*image*/
}
<div class="bodybox1">
<img class="freeshipping" src="https://via.placeholder.com/500x300" width="500" height="300" >
<div class="signuparea">
<div class="signuptext">Sign up to get freeshipping coupon code! ( per phone number )
</div>
<div class="boxcoverblack">Sign up</div>
</div>
</div>
help me please....
http://jsfiddle.net/abxf6c9h/
.bodybox1 {
display:flex;
justify-content:center;
border: 1px solid black;
width: 100%;
background: white;
margin: 1% auto 2% auto;
text-align: center;
font-size: 2em;
padding: 3%;
overflow: hidden;
}
Try this. I guess you want this box displayed as flexed. Tell me if that gave you a hint . I just added:
display:flex;
justify-content:center;
See my fiddle:
http://jsfiddle.net/abxf6c9h/1/
You need to use flexbox and a bit of arrangements :
use
display:inline-block
instead of
float:left
use vertical-align:top to remove the auto centering align from the wraping div
and finaly
display:flex;
flex-wrap:nowrap;
to the parent of the 2 involved divs
Here's the result
fiddle

Two divs alongside each other: first sticked and the second centered

I want to place two divs alongside each other. Second one to be centered and the first one to be on the left from second and to be sticked / fixed. Because of the last condition the existing solution does not work (attribute position is already used there for both divs). How can I solve this?
Something like this:
.container {
display: flex;
height:100%;
}
.left {
margin-left: auto;
width: 0;
display: flex;
justify-content: flex-end;
}
.centered {
margin-right:auto;
padding:5px;
background:green;
height:100%;
}
.left span {
position:fixed;
white-space: nowrap;
padding:5px;
background:red;
}
body {
height:200vh;
}
<div class='container'>
<div class='left'><span> some content</span></div>
<div class='centered'>centered</div>
</div>

Fitting 2 divs side by side in a 100% div

Looking to fit 2 horizontal divs in a 100% repsonsive div. So the 2 internal divs will resize when the screen shrinks/increases.
<div class="mydownloads">
<div class="warrantybook"></div>
<div class="brochurebook"></div>
</div>
.mydownloads {
width:100%;
}
.warrantybook {
padding:10px;
float:left;
display: inline-block;
margin-left: auto;
margin-right: auto;
width:45%;
}
.brochurebook {
padding:10px;
float:right;
display: inline-block;
margin-left: auto;
margin-right: auto;
width:45%;
}
You want to set the full div to 100% and the 2 internal divs to 50% and remove all padding, border and margin. I would recommend setting a "min-width" in css to ensure there is always a minimum, I've seen a lot of sites look goofy without having a minimum width on certain things.
<div class="mydownloads">
<div class="warrantybook"></div>
<div class="brochurebook"></div>
</div>
.mydownloads {
width:100%;
}
.warrantybook {
padding:0;
margin:0;
border:0;
float:left;
width:50%;
background:red;
height:50px;
}
.brochurebook {
padding:0;
margin:0;
border:0;
float:right;
width:50%;
background:blue;
height:50px;
}
https://jsfiddle.net/gn6jabb9/
This can be done easily enough with floats or inline-blocks, though the clean 'new' way is with Flexbox. Assuming you don't need support for IE < 10:
.mydownloads {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
align-items: flex-start; /* Change this to 'stretch' if you also want full height */
}
.warrantybook,
.brochurebook {
flex-grow: 1;
height: 50px;
}
.warrantybook {
background:red;
}
.brochurebook {
background:blue;
}
https://jsfiddle.net/gn6jabb9/1/

why my div is slightly moved to the right

So I'm just a begginer to this HTML and CSS stuff, and I tried to make my own webpage. The thing is, it looks like this:
While I would like to get the second div(#diary) centered, but I can't do it without screwing up the whole webpage. Which will be the correct code?
This is what I have:
HTML:
<div id="progress">
Blablabla
</div>
<div id="diary">
blablabla
</div>
CSS:
div {
border: 7px solid #142538;
background-color: #c7d0e1;
}
#diary {
margin:auto;
width:30em;
display:inline-block;
}
#progress {
font-size:16px;
width:auto;
float:left;
display:inline-block;
margin-left:25px;
}
Thanks in advance ^^
You have mixed display: inline-block and float:left which makes no sense. Elements that float become display: block; by default. http://www.w3.org/TR/CSS2/visuren.html#float-position
There are two ways to solve your problem.
Way1 Go Inline-block all the way:
http://jsfiddle.net/fDx2U/
div {
border: 7px solid #142538;
background-color: #c7d0e1;
}
#diary {
margin:auto;
width:30em;
display:inline-block;
vertical-align: top;
}
#progress {
font-size:16px;
width:auto;
vertical-align: top;
display:inline-block;
margin-left:25px;
}
How to the rid of the margin between the items: How to remove the space between inline-block elements?
Vital for this solution is the vertical-align:top; (your initial problem)
Way2 Go floating all the way:
http://jsfiddle.net/fDx2U/1/
div {
border: 7px solid #142538;
background-color: #c7d0e1;
}
#diary {
margin-left: 100px;
}
#progress {
font-size:16px;
width:auto;
float:left;
margin-left:25px;
width: 100px;
}
Vital for this solution is that the width of .diary equals the margin-left of #progress
Try this
#diary {
margin:0 auto;
width:30em;
display:block;
}

CSS- Centering child text wider than parent container

Here's a JSfiddle of what I have so far.
What I want to do is make it so that the text "Text With a Border" is centered inside of the parent div (I want to keep the double lined borders on either side of this text, so that the end result will be something like this: ==========Text With a Border==========. With the borders hanging off either side of the parent div.)
I would like to make it so that this solution works with containers of varying sizes with differing amounts of text.
I've found a few questions similar to this, about centering wide children inside of narrower parents, but for some reason I can't get them to work with this text.
CSS:
#container {
width:400px;
background:pink;
margin:0 auto;
padding: 10px 0;
}
.border {
display:block;
background:yellow;
}
.border span {
left:0;right:0;
white-space:nowrap;
margin: auto;
}
p.border span:after, p.border span:before {
content: " ";
display: inline-block;
width:100%;
height:5px;
border:solid gray;
border-width:1px 0;
}
HTML:
<div id="container">
<p class="border">
<span>Text With a Border</span>
</p>
</div>
Thanks for any help,
you could use negative margin and overflow.
Negative margin to virtually reduce width, space needed by pseudo-elements down to zero, :
http://jsfiddle.net/35waA/3/
#container {
width:400px;
background:pink;
margin:0 auto;
padding: 10px 0;
}
.border {
display:block;
background:yellow;
text-align:center;
overflow:hidden;
}
.border span {
white-space:nowrap;
margin: 0;
display:inline-block;
}
p.border span:after, p.border span:before {
content:" ";
display: inline-block;
width:200%;
height:5px;
border:solid gray;
border-width:1px 0;
}
p.border span:before {
margin-left:-200%;
}
p.border span:after {
margin-right:-200%;
}
Else , background-image /gradient will do too.