center vertically div inside bootstrap grid - html

I would center a div inside another div vertically.
I have tried using table cell as mentioned in some answers in the site but that does not work I always get the div in top
CSS
.height1 {
height:60px;
line-height:60px
}
.row {
border: 1px solid black;
}
.indicator {
width :20px;
height:20px;
background-color: black;
vertical-align: middle
}
.badge {
display:inline-block;
}
.parent {
display: table-cell;
}
HTML
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<div class="col-md-8 height1"> TEXTE</div>
<div class="col-md-4 parent">
<div class="badge indicator">testing</div>
</div>
</div>
</div>
Here's a demo

See the updated snippet below.
.height1, .parent {
line-height:60px
}
.row {
border: 1px solid black;
}
.indicator {
height:20px;
background-color: black;
vertical-align: middle
}
.badge {
display:inline-block;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<div class="col-xs-8 col-md-8 height1"> TEXTE</div>
<div class="col-xs-4 col-md-4 parent">
<div class="badge indicator">testing</div>
</div>
</div>
</div>

HTML
<div class="parent">
<div class="child"></div>
</div>
By using the following method you can get that approach. And it will be better solution to maintain the div's.
.parent {
width: 100px;
height: 100px;
background-color: yellow;
position: relative;
}
.child {
height: 50px;
width: 50px;
background-color: blue;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}

Related

Center parent div vertically based on child element

I am hoping to center my parent div height based on my child div height. My goal is to have 3 boxes with a shorter, but wider rectangle centered vertically behind it. Right now I have my parent div shorter and wider than the children, however I cannot seem to center it vertically.
Here is the ideal outcome:
Here is my current version (Please ignore minor differences with text and box colors). :
.content {
width: 80%;
margin: 0px auto;
}
#container .col {
border: 1px solid #00acd4;
background-color: white;
padding-top: 2em;
padding-bottom: 2em;
position: relative;
}
#parent {
background-color: #f0f9fb;
max-height: 80px;
}
#container {
margin-top: 50px;
margin-bottom: 50px;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
<div id="container">
<div id="parent">
<div class="content">
<div class="row">
<div class="col ">
<h3>$500</h3>
</div>
<div class="offset-1 col">
<h3>$3500</h3>
</div>
<div class="col offset-1">
<h3>50%</h3>
</div>
</div>
</div>
</div>
</div>
Don't use a negative margin unless absolutely necessary. In this case, it is not. Use flex on parent with align-items: center;
.content {
width: 80%;
margin: 0px auto;
}
#container .col {
border: 1px solid #00acd4;
background-color: white;
padding-top: 2em;
padding-bottom: 2em;
position: relative;
}
#parent {
background-color: #f0f9fb;
max-height: 80px;
display: flex;
align-items: center;
}
#container {
margin-top: 50px;
margin-bottom: 50px;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
<div id="container">
<div id="parent">
<div class="content">
<div class="row">
<div class="col ">
<h3>$500</h3>
</div>
<div class="offset-1 col">
<h3>$3500</h3>
</div>
<div class="col offset-1">
<h3>50%</h3>
</div>
</div>
</div>
</div>
</div>
Without a sketch of what you are trying to do, I believe this is what you are wanting... You can just set a negative margin in the col divs in order to take them outside of the parent...
#container {
margin-top: 50px;
margin-bottom: 50px;
}
#parent {
background-color: #f0f9fb;
}
.content {
width: 80%;
margin: 0px auto;
}
#container .col {
border: 1px solid #00acd4;
background-color: white;
padding-top: 2em;
padding-bottom: 2em;
position: relative;
margin-top: -20px;
margin-bottom: -20px;
}
<div id="container">
<div id="parent">
<div class="content">
<div class="row">
<div class="col">
<h3>$500</h3>
</div>
<div class="offset-1 col">
<h3>$3500</h3>
</div>
<div class="col offset-1">
<h3>50%</h3>
</div>
</div>
</div>
</div>
</div>
Forked your fiddle: https://jsfiddle.net/jstgermain/o6xhL92s/
*** RECOMMEND BELOW SOLUTION ***
#Betsy, I would recommend simplifying your HTML and using flexbox over the previous solution to your fiddle. You will want to make sure your behavior is consistent across browsers and devices. You can use media queries to change the size to eht col items for smaller devices.
#container {
margin-top: 50px;
margin-bottom: 50px;
}
#parent {
background-color: red;
/*#f0f9fb;*/
display: flex;
justify-content: space-evenly;
}
.col {
border: 1px solid #00acd4;
background-color: white;
padding: 1em;
width: 25%;
margin: -20px auto;
}
<div id="container">
<div id="parent">
<div class="col">
<h3>$500</h3>
</div>
<div class="col">
<h3>$3500</h3>
</div>
<div class="col">
<h3>50%</h3>
</div>
</div>
</div>

HTML/CSS adjust parent div's height/width automatically according to children content

I want to make this:
stacked cards
the html would look like so:
<div class="container>
<div class="top-card>
<div class="card-content">
...
</div>
</div>
<div class="bottom-card>
</div>
</div>
I am having trouble styling this so that the height of the entire card adjusts automatically according to the content inside the top card. Thank you in advance.
you can use a combination of box-shadow and display: inline-block to accomplish what you are trying to do. I have updated the answer. Here is the code:
.grandparent {
display: inline-block;
position: relative;
}
.parent {
display: inline-block;
background: blue;
position: absolute;
top: 0;
left: 0;
}
.child {
width: 100px;
height: 100px;
background: red;
margin: 5px;
}
.shadow {
margin-left: -7px;
margin-top: -7px;
background: pink;
z-index: -100;
border: 1px solid green;
}
.empty {
opacity: 0;
}
<div class="grandparent">
<div class="parent">
<div class="child"></div>
</div>
<div class="parent shadow">
<div class="child empty"></div>
</div>
</div>

How to link between circles with a dash line from the center?

I have 3 circles in a row and I want to link between them from the center with a dash line, Here is a screenshot similar to what I want:
Here is the html code:
<div class="container-fluid">
<div class="row">
<div class="col-xs-push-3 col-xs-6 text-center">
<div class="col-xs-3">
<div class="wrapper">
<span class="circle text-center">A</span>
<span class="dash_line" style="transform: translate(232%, -215%);">---</span>
</div> <!-- wrapper -->
</div> <!-- col-xs-3 -->
<div class="col-xs-6">
<div class="wrapper">
<!--<span class="dash_line">---</span>-->
<span class="circle text-center">B</span>
<!--<span class="dash_line">---</span>-->
</div> <!-- wrapper -->
</div> <!-- col-xs-6 -->
<div class="col-xs-3">
<div class="wrapper">
<span style=" transform: translate(-114%, 135%);" class="dash_line">-----</span>
<span class="circle text-center">C</span>
</div> <!-- wrapper -->
</div> <!-- col-xs-3 -->
</div> <!-- col-xs-6 -->
</div> <!-- row -->
</div> <!-- container-fluid -->
Here is the custom CSS:
.row{
margin:5% auto
}
.circle {
border-radius: 50%;
border: 1px solid #d1cfc8;
background-color: #f7eebe;
padding: 15% 35%;
font-size: 300%;
display: inline-block
}
.dash_line{
position: absolute;
transform: translateY(50%);
}
Here is a fiddle to see it live:
http://jsfiddle.net/mpvf5rxa/37
As you see i'm using specific values for each dash line, I want it to be dynamic.
I don't mind changing the elements, Adding/removing elements.
Also I don't mind using Javascript/Jquery to achieve that.
Edit, since you use bootstrap 3 , another approach with pseudo and margins could be used instead:
.ABC {
text-align: center;
}
.ABC span {
padding: 1vw 2.8vw; /* any values of yours */
font-size: 5vw; /* any values of yours */
border: 1px solid; /* any values of yours */
border-radius: 50%;
display: inline-block;/* to trigger block formatting context .vertical padding + pseudo */
}
span + span /* filter out first span */{
margin-left:5vw;/* equals width of dashed line */
}
span + span::before {/* do not draw anything from first span */
content:'';
display:inline-block;
vertical-align:middle;
margin-left:-7.8vw;
margin-right:2.8vw;/* equal span padding*/
width:5vw;
border-top:dashed;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row ABC">
<span>A</span>
<span>B</span>
<span>C</span>
</div>
</div>
Original answers thinking bootsrap 4 was involved .
If you have only 3 elements to separate with a dashed line, you can use pseudos, order, built-in bootsrap class and much less markup:
here i added the ABC class to easily select this row, but you can use any other class or id.
.ABC span {
padding:1vw 2.8vw;/* any values of yours */
font-size:5vw;/* any values of yours */
border:1px solid;/* any values of yours */
border-radius:50%;
order:0;/* defaut*/
}
.ABC span + span {
order:2;/* leave order:1 for one pseudo */
}
.ABC span:last-of-type {
order:4;/* leave order:3 for the other pseudo */
}
.ABC:before,
.ABC:after {
content:'';
width:5vw;/* whatever size you need */
order:1;
border-top:dashed;
}
.ABC:after {
order:3;
}
.ABC:after {
order: 3;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/css/bootstrap.css" rel="stylesheet"/>
<div class="container">
<div class="row justify-content-center align-items-center ABC">
<span>A</span>
<span>B</span>
<span>C</span>
</div>
</div>
I'd certainly look into using Grid or Flex for this.
Here's a rough example I built with flex that could hopefully meet your needs with a few modifications!
http://jsfiddle.net/kh2q6vo7/7/
.container {
border: 1px solid black;
display: flex;
justify-content: center;
align-items: center;
width: 100%;
height: 200px;
}
.a {
width: 50px;
height: 100px;
margin-right: 10px;
border: 1px solid red;
}
.b {
width: 75px;
height: 60px;
margin-right: 10px;
border: 1px solid blue;
}
.c {
width: 100px;
height: 25px;
border: 1px solid green;
}
.line--right {
position: relative;
}
.line--right:after {
content: "";
display: block;
width: 10px;
height: 1px;
background: black;
position: absolute;
right: -11px;
top: 50%;
}
<div class="container">
<div class="a line--right">A</div>
<div class="b line--right">B</div>
<div class="c">C</div>
</div>
You would want to change the elements to meet your needs. Container size, line size, even position and shape could change. Etc...
But with display: flex; You are able to center your elements vertically within a container. Allowing them to match up in the middle!
More References:
https://hackernoon.com/the-ultimate-css-battle-grid-vs-flexbox-d40da0449faf
https://rachelandrew.co.uk/archives/2016/03/30/should-i-use-grid-or-flexbox/

Issue in positioning elements

I am trying to make a layout where #txt-bar and #main-content-area will overlap on #image. ( #txt-bar is overlapping on #image with following CSS ) but to achieve overlapping of #main-content-area on #image if I use top:-60px at #main-content-area then it will leave a gap between #main-content-area and #footer. I don't know how to solve this issue. Please help me.
/* CSS */
body {
position: absolute;
}
#top-bar {
background-color: black;
color: white;
}
#txt-bar {
height: 40px;
background-color: pink;
position: relative;
z-index: 4;
}
#link-bar {
background-color: red;
height: 40px;
z-index: 4;
}
#image {
position: relative;
z-index: 3;
}
.line {
width: 100%;
position: relative;
border-bottom: 4px solid black;
}
#main-content-area {
position: relative;
background-color: red;
top: -60px;
z-index: 4;
}
#footer {
background-color: green;
position: relative;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container-fluid">
<div class="row">
<div class="col-sm-6" id="txt-bar">
<h1>Greetings</h1>
</div>
<div class="col-sm-6" id="link-bar">
<h1>Link bar </h1>
</div>
</div>
<div class="row">
<div class="col-sm-12" id="image">
<img src="https://placeholdit.imgix.net/~text?txtsize=28&txt=300%C3%97300&w=300&h=300" class="img-responsive" />
</div>
</div>
<div class="line"></div>
<div class="row">
<div class="col-sm-2">
</div>
<div class="col-sm-8" id="main-content-area">
<h1>Main content area </h1>
</div>
<div class="col-sm-2">
</div>
</div>
<div class="row" id="footer">
<div class="col-sm-12">
<h1>Footer Element </h1>
</div>
</div>
</div>
Wrap all divs (#txt-bar #main-content-area and #image) in a parent div with position:relative then use position:absolute for #main-content-area and #txt-bar, this will solve your issues.
.wrap{position:relative;max-width:300px;}
#txt-bar {
height: 40px;
background-color: pink;
position: absolute;
top:10px;
width:100%;
}
#main-content-area {
position: absolute;
bottom:10px;
width:100%;
background-color: red;
}
<div class=wrap>
<div id=txt-bar>txt-bar</div>
<div id=image><img src=https://placeholdit.imgix.net/~text?txtsize=28&txt=300%C3%97300&w=300&h=300></div>
<div id=main-content-area>main-content-area</div>
</div>
More Info

Yet another vertical align CSS issue - two divs top/bottom inside another div

I need the following HTML (jsfiddle):
<div class="main">
<div class="top">Top</div>
<div class="bottom">Bottom</div>
</div>
To look something like this:
Not like this:
Can you change your HTML layout a little ?
.main {
display: table;
height: 100px;
border: solid 1px;
}
.inner {
display:table-cell;
vertical-align:middle
}
.top, .bottom {background:yellow;}
<div class="main">
<div class="inner">
<div class="top">Top</div>
<div class="bottom">Bottom</div>
</div>
</div>
Check this below.
Just set the display of the parent to table-cell:
.main {
display: table-cell;
height: 100px;
border: solid 1px;
vertical-align: middle;
}
<div class="main">
<div class="top">Top</div>
<div class="bottom">Bottom</div>
</div>
position: relative;
top: 50%;
transform: translateY(-50%);
HTML:
<div class="main">
<div class="wrapper">
<div class="top">Top</div>
<div class="bottom">Bottom</div>
</div>
</div>
CSS:
.main {
display: inline-block;
height: 100px;
border: solid 1px;
line-height: 100px;
vertical-align: middle;
}
.wrapper {
line-height: 1em;
display: inline-block;
vertical-align: middle;
}
DEMO: https://jsfiddle.net/q0kLojwx/5/