Vertically centralising content in an inline-block - html

I’m trying to get the blue text vertically in the centre of the inline-block divs. I’ve tried various variations but to no avail. The parent must stay as an inline block.
If the parent is displayed as a table and child as a table cell with vertical-align: middle then it almost works, but fails because then the child div is 100% high, and I want to add a border-top and bottom with about 10px padding, which won’t work at 100% height. Cannot use display: flex because it destroys the positioning of other elements. Line height also fails.
Can someone explain what the problem is here because I'm very confused.
.matches-container {
padding-top: 50px;
}
.match {
width: 25%;
height: 250px;
display: inline-block;
border: 1px solid yellow;
}
.match-contents {
background: blue;
border-top: 1px solid red;
border-bottom: 1px solid red;
}
.a {
background: black
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<section class="matches-container">
<article class="match a">
<div class="match-contents">central text with borders top and bottom</div>
</article>
<!-- -->
<article class="match b">
<div class="match-contents">central text with borders top and bottom</div>
</article>
</section>

Add this properties in .match-contents
.match-contents {
position:relative;
top:50%;
transform:translateY(-50%);
}
Your code is working as expected...no where you added a property to make blue box vertically center.
Here is snippet.
.matches-container {
padding-top: 50px;
}
.match {
width: 25%;
height: 250px;
display: inline-block;
border: 1px solid yellow;
}
.match-contents {
background: blue;
border-top: 1px solid red;
border-bottom: 1px solid red;
position: relative;
top: 50%;
transform: translateY(-50%);
}
.a {
background: black
}
<section class="matches-container">
<article class="match a">
<div class="match-contents">central text with borders top and bottom</div>
</article>
<!--
-->
<article class="match b">
<div class="match-contents">central text with borders top and bottom</div>
</article>
</section>

You can use display: inline-flex and align-items: center here. Demo:
.matches-container {
padding-top: 50px;
}
.match {
width: 25%;
height: 250px;
/* become inline flex-container */
display: inline-flex;
/* center items */
align-items: center;
border: 1px solid yellow;
}
.match-contents {
background: blue;
border-top: 1px solid red;
border-bottom: 1px solid red;
}
.a {
background: black
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<section class="matches-container">
<article class="match a">
<div class="match-contents">central text with borders top and bottom</div>
</article>
<!-- -->
<article class="match b">
<div class="match-contents">central text with borders top and bottom</div>
</article>
</section>

Related

How to make div (inside another div) horizontally and vertically align

I have 3 divs which are horizontally aligned (aqua color). Inside each div, there are two divs (red and black one).
What I am trying to do is, align the black divs horizontally regardless of the red div. The css for the black div is
.black-div {
width: 100%;
height: 45px;
max-width: 235px;
display: inline-block;
color: #33244a;
font-size: 16px;
text-transform: uppercase;
font-weight: normal;
text-align: center;
line-height: 43px;
border: 2px dashed #d5d1d8;
border-radius: 6px;
box-sizing: border-box;
}
Output will something like this
I am not good at all in css. I have tried using position: fixed / absolute but no luck.
Try it.
Use div and min-height.
section{
display: inline-block;
border: 1px solid red;
width: 100px;
}
.textarea-wrap{
overflow: hidden;
min-height: 200px;
}
.textarea-wrap > textarea{
width: 100%;
resize: none;
}
.red{
background-color: red;
}
<div>
<section>
<div class="textarea-wrap">
<textarea rows="3">12312312</textarea>
</div>
<div class="red">
red
</div>
</section>
<section>
<div class="textarea-wrap">
<textarea rows="10">12312312</textarea>
</div>
<div class="red">
red
</div>
</section>
<section>
<div class="textarea-wrap">
<textarea rows="6">12312312</textarea>
</div>
<div class="red">
red
</div>
</section>
</div>
You should use table to make it more manageable, or use absolute positioning on the black div so you can position them measure from the bottom of the blue div.
There may be a solution without the spacer. Im looking for it :)
found solution without spacer justify-content: space-between;
.wrapper {
display: flex;
flex-direction: row; /* flex in a row inside (make columns .col) */
}
.col {
display: flex;
flex-direction: column; /* flex in a column inside */
justify-content: space-between; /* since the elements must not grow, fill the space between them */
flex: 1 1 100px; /* grow and shrink of col allowed to fill row evenly starting at 100px*/
margin: 5px;
border: 3px solid black;
background-color: aqua;
}
.red {
flex: 0 1 auto; /* no vertical (col) growing (so it does not expand vertically) */
border: 3px solid black;
border-radius: 10px;
background-color: red;
margin: 5px;
padding: 10px;
}
.black {
background-color: black;
color: white;
margin: 5px;
padding: 10px;
display: block;
flex: 0 1 auto; /* no growing allowed */
}
.resize {
overflow: hidden;
resize: vertical;
}
<div class='wrapper'>
<div class='col'>
<div class='red'>Some wide wide wide wide wide wide Text</div>
<div class='black'>Footer</div>
</div>
<div class='col'>
<div class='red'>Some<br/>much<br/>longer<br/>Text</div>
<div class='black'>Footer</div>
</div>
<div class='col'>
<div class='red resize'>Some Text<br><b><u>Resize me!</u></b></div>
<div class='black'>Footer</div>
</div>
</div>
Edit removed spacer div
Edit2 added css commenting for easier understanding

max-width not changing width of my divs

I've been following a css and html tutorial at udacity.
My code is this:
.image{
max-width: 50%;
}
.app{
display: flex;
}
.description{
color: red;
max-width: 705px;
}
<h1 class="title">My App</h1>
<div class="app">
<div class="image">image</div>
<div class="description">some long text</div>
</div> <!-- /.app -->
When I increase max-width of the image div, image div remains the same width no matter what.
But when I increase max-width of description, it changes accordingly.
Also when I change max-width to width it changes also the 460px and occupies more space than with max-width.
Why is this happening ?
max-width doesn't set the width of an element.
It tells your divs: this is how wide you can get (starting from 0, content width, or min-width)
Your divs have a max-width: 50% (.image) and max-width: 705px (.description).
If there were no content in either div, the width would be zero.
.app {
display: flex;
padding: 5px;
}
.image {
max-width: 50%;
border: 1px dashed black;
}
.description {
color: red;
max-width: 705px;
border: 1px dashed green;
}
<h1 class="title">My App</h1>
<div class="app">
<div class="image"></div>
<div class="description"></div>
</div>
<!-- /.app -->
If the content width were to be less than the max-width, the divs would take the content width.
.app {
display: flex;
padding: 5px;
border: 1px solid red;
}
.image {
max-width: 50%;
border: 1px dashed black;
}
.description {
color: red;
max-width: 705px;
border: 1px dashed green;
}
<h1 class="title">My App</h1>
<div class="app">
<div class="image">xxx</div>
<div class="description">xxxx</div>
</div>
<!-- /.app -->
If the content were to be more than the max-width, then max-width would come into play.
.app {
display: flex;
padding: 5px;
border: 1px solid red;
}
.image {
max-width: 10%;
border: 1px dashed black;
}
.description {
color: red;
max-width: 70px;
border: 1px dashed green;
}
<h1 class="title">My App</h1>
<div class="app">
<div class="image">image image image image image image image</div>
<div class="description">some long text some long text some long text some long text some long text</div>
</div>
<!-- /.app -->

Css table with scalable div on the sides

I have a central div that have 4 others divs arrount it (top, right, left, bottom).
Top and Bottom divs are suposed to be fixed. They don't have to increase neither height nor width.
Right and left divs are ONLY suposed to increase its width.
How?
The "content" div (central) is a table that can have as many rows as the user wants. Then, I want to increase or decrease the height of the left and right divs depending on the height of the "content".
I want to automatically do that .
How can I do that?
I have created an example with what I have done, without success.
https://jsfiddle.net/y6ad2crg/4/
<div class="pantalla">
<div class=" pantallaSup"></div>
<div class="pantallaEsq"></div>
<div class="pantallaDre"></div>
<div class="interiorPantalla">
content<br>
aaa<br>
bbb<br>
ccc<br>
ddd<br>
eee
</div>
<div class="pantallaInf"></div>
</div>
The result I want to get is that:
What am I doing wrong?
Maybe just changing little things in css it may be done, or maybe my html is wrong designed...
change u code
https://jsfiddle.net/p7haf3oo/
.pantalla {
/*position: relative;*/
display: block;
width: 690px;
background-color: gray;
}
.pantallaSup {
height: 200px;
width: 100%;
background: orange;
}
.flex {
display: flex;
align-items: stretch;
}
.pantallaEsq {
width: 69.4px;
border-right: 4px solid black;
background: red;
display: block;
}
.pantallaDre {
width: 69.4px;
border-left: 4px solid black;
background: red;
display: block;
align-self: stretch;
min-height: 100px;
}
.pantallaInf {
height: 200px;
width: 100%;
background: orange;
}
.interiorPantalla {
position: relative;
margin: 0 auto;
border: 1px;
border-color: blue;
border-style: solid;
width: 550px;
background-color: white;
}
<div class="pantalla">
<div class="pantallaSup"></div>
<div class="flex">
<div class="pantallaEsq"></div>
<div class="interiorPantalla">
content
<br> aaa
<br> bbb
<br> ccc
<br> ddd
<br> eee
</div>
<div class="pantallaDre"></div>
</div>
<div class="pantallaInf"></div>
</div>
I believe I have coded what you're looking for -https://jsfiddle.net/Shuaso/vpmn3LLv/
I removed all the unnecessary divs and used CSS borders and background colors to achieve the same effect. You can alter the content in the "content" div and see the dynamic styles. Here's the code:
CSS:
.container {
width: 500px;
background-color: orange;
padding: 20px 0;
}
.content {
border-left: 20px solid red;
border-right: 20px solid green;
background-color: white;
}
HTML:
<div class="container">
<div class="content">
<p>test</p>
<p>test</p>
</div>
</div>

Two-column layout with vertically and horizontally alignment, having variable height

I want to gain the below layout:
It seems that when the right box is small, the bottom left box wants to move away from the left side and beside the top left box. If the right box is full and tall, then it pushes the bottom left box back to where I want it to be.
HTML
<div class=page>
<div id="stack-vert">
<div id="stack-horz">
<div id="message_center_content">
<h2> Your Messages </h2>
</div>
<div id="message_center_details">
</div>
<div id="message_center_details">
</div>
<div id="clearingdiv2"></div>
</div>
</div>
CSS
.page{
margin: 2em auto;
width: 75em;
border: 5px solid #ccc;
padding: 0.8em; background: white; display:table;
}
#message_center_details{
float:left;
border: solid thin black;
overflow:hidden;
padding: 5px;
width: 25%;
background-color: #ffffcc;
margin: 5px;
}
#message_center_content{
float:right;
border: solid thin black;
padding: 5px;
width: 60%;
background-color: #F0F0F0;
margin: 5px;
}
JS Fiddle
It works in jsFiddle, but now in my browser! Instead the two boxes on the left interfere with each other, the bottom one sits to the right of the top one and below the box on the right.
Any help would be really appreciated.
Something you could do
<div id="Container">
<div id="left">
<div class="section">
</div>
<div class="section">
</div>
</div>
<div id="right">
<div id="message">
<div style="width:100px;height:260px;background:white;">Edit this</div>
</div>
</div>
</div>
#Container {
width: 100%;
min-height: 300px;
background: red;
}
#left {
float: left;
width: 40%;
background: yellow;
min-height: 300px;
box-sizing: border-box;
padding: 10px;
}
.section {
width: 100%;
display: block;
min-height: 120px;
background: red;
box-sizing: border-box;
margin-bottom: 10px;
}
#right {
float: left;
width: 60%;
min-height: 300px;
background: blue;
box-sizing: border-box;
padding: 10px;
}
#message {
width: 100%;
min-height: 200px;
background: red;
}
see fiddle for what i would do. I have added colors so you can see whats happening.
adjust the white div height in the HTML tab to see the message div (the red one on the right) adjust its height.
Your content would just go inside the left divs with a class of section, and the right div id message.
I would stay away from libraries until you know how to do most things yourself.
Great place to learn html/css/js and more
You are maybe after such a solution to have two outer divs side-by-side:
.wrapper{
width: 90%;
margin: auto;
display: flex;
justify-content: space-between;
}
.side{
width: 30%;
}
.side div{
margin-bottom: 10px;
padding: 10px;
}
.side div:last-child{
margin-bottom: 0;
}
.main{
width: 67%;
}
.main div{
padding: 10px;
}
.border{
border: 2px solid black;
}
<div class="wrapper">
<div class="side">
<div class="top border">
<p>These are contents. These are contents. These are contents. </p>
<p>These are contents. These are contents. These are contents. </p>
</div>
<div class="bottom border">
<p>These are contents. These are contents. These are contents. </p>
<p>These are contents. These are contents. These are contents. </p>
<p>These are contents. These are contents. These are contents. </p>
</div>
</div>
<div class="main">
<div class="border">
<p>These are contents. These are contents. These are contents. </p>
<p>These are contents. These are contents. These are contents. </p>
</div>
</div>
</div>

How to move the right div to the right with css

Here is a simple box with two columns:
HTML:
<div id="wrapper">
<div class="left">
<div class="image-title">Image title
</div>
<img src="http://placehold.it/100x100">
</div><!-- left -->
<div class="right">
<div class="title">
<h2>Title here</h2>
</div><!-- title -->
<div class="content">
<p>Content here.</p>
<p>Content here.</p>
</div><!-- content -->
</div><!-- right -->
</div><!-- wrapper -->
CSS:
#wrapper {
overflow: auto;
width: 100%;
border: 1px solid red;
padding: 1px;
}
.left {
display: inline-block;
float: left;
width: 30%;
border: 1px solid grey;
text-align: center;
}
.right {
display: inline-block;
width: 70%;
border: 1px solid grey;
}
Demo
I expected that the right div should be placed on the right side, but it's placed under the left div element. How can I make it done?
Thanks for your help.
It's really easy to do.
Just use float: right;
.right {
display: inline-block;
width: 70%;
border: 1px solid grey;
float: right;
}
You can use box-sizing: border-box; to align it perfectly. No need to use align:right; or adjusted width.
#wrapper {
overflow: auto;
width: 100%;
border: 1px solid red;
padding: 1px;
}
.left {
display: inline-block;
float: left;
width: 30%;
border: 1px solid grey;
text-align: center;
box-sizing: border-box;
}
.right {
display: inline-block;
width: 70%;
border: 1px solid grey;
box-sizing: border-box;
}
<div id="wrapper">
<div class="left">
<div class="image-title">Image title
</div>
<img src="http://placehold.it/100x100">
</div><!-- left -->
<div class="right">
<div class="title">
<h2>Title here</h2>
</div><!-- title -->
<div class="content">
<p>Content here.</p>
<p>Content here.</p>
</div><!-- content -->
</div><!-- right -->
</div><!-- wrapper -->
Not sure what you want. But if you want the .right div to be on the right side then you need to apply
float:right
See the demo here.
Also you have a border of 1px outside the box. It needs to be inside you could use box-shadow.
-webkit-box-shadow:inset 0px 0px 0px 1px gray;
-moz-box-shadow:inset 0px 0px 0px 1px gray;
box-shadow:inset 0px 0px 0px 1px gray;
See the demo here.
Update: You have no height on the divs, so the height is being established by your text. Simply apply a height.
.left {
height:130px;
.right {
height:130px;
See the example here.
The first issue here is that you're adding 3px as borders. These aren't counted into the % when you measure the width, which makes the inner divs too wide to fit on one line. if you add the following line to the right div you will see what I mean:
.right {
display: inline-block;
width: 70%;
border: 1px solid grey;
float: right;
}
Now we've fixed the second issue; the div will now float to the right as intended, but you can see the boxes are cutting each other's edges. Nowadays there's a nice trick to fix that, and that is box-sizing: border-box; which will automatically match the borders as your div's edge, not outside of it as the standard does.
#wrapper {
overflow: auto;
width: 100%;
border: 1px solid red;
padding: 1px;
}
.left {
display: inline-block;
float: left;
width: 30%;
border: 1px solid grey;
text-align: center;
box-sizing: border-box;
}
.right {
display: inline-block;
width: 70%;
border: 1px solid grey;
float: right;
box-sizing: border-box;
}
<div id="wrapper">
<div class="left">
<div class="image-title">Image title
</div>
<img src="http://placehold.it/100x100">
</div><!-- left -->
<div class="right">
<div class="title">
<h2>Title here</h2>
</div><!-- title -->
<div class="content">
<p>Content here.</p>
<p>Content here.</p>
</div><!-- content -->
</div><!-- right -->
</div><!-- wrapper -->
A minimized css file for this could look like this.
#wrapper {
overflow: auto;
width: 100%;
border: 1px solid red;
padding: 1px;
}
.left, .right {
height:130px;
display: inline-block;
/* choose your border method
border: 1px solid grey;
Or
-webkit-box-shadow:inset 0px 0px 0px 1px gray;
-moz-box-shadow:inset 0px 0px 0px 1px gray;
box-shadow:inset 0px 0px 0px 1px gray;
*/
}
.left {
float: left;
width: 30%;
text-align: center;
}
.right {
float: right;
width: 70%;
}
To the right div to be on the right, you need to add float:right to the css:
.right {
display: inline-block;
width: 70%;
float:right;
border: 1px solid grey;
}
DEMO