This question already has answers here:
Vertically center two elements within a div [duplicate]
(2 answers)
Closed 5 years ago.
How can I center these four buttons inside their own div vertically and horizontally?
Update: No Flexbox please. I do not have that luxury.
#outer {
width: 400px;
height: 200px;
background-color: black;
display: table-cell;
}
#innerOne {
width: 400px;
height: 100px;
background-color: red;
vertical-align: middle;
}
#innerOne {
width: 400px;
height: 100px;
background-color: blue;
vertical-align: middle;
}
<div id="outer">
<div id="innerOne">
<button>One</button>
<button>Two</button>
</div>
<div id="innerTwo">
<button>Three</button>
<button>Four</button>
</div>
</div>
I want "one", "two" to be centered inside the blue div vertically and horizontally. The same for "three" and "four" in the black div.
I have tried many different options by setting their display to table and table-cell without the desired effect I want.
Since the buttons are inline blocks you can center them vertically using a pseudo-element. The pseudo element has the height of the container (.inner), and is vertically aligned, and all inline blocks with less height, will be centered to it.
To center them horizontally set text-align: center on the container (.inner).
#outer {
width: 400px;
height: 200px;
background-color: black;
}
.inner {
width: 400px;
height: 100px;
text-align: center;
}
.inner::before {
display: inline-block;
height: 100%;
content: '';
vertical-align: middle;
}
#innerOne {
background-color: red;
}
#innerTwo {
background-color: blue;
}
<div id="outer">
<div id="innerOne" class="inner">
<button>One</button>
<button>Two</button>
</div>
<div id="innerTwo" class="inner">
<button>Three</button>
<button>Four</button>
</div>
</div>
for a single line, you could use a line-height equal to the box's height to vertical align at the center the inline content.
and text-align for the horizontal part
#outer {
width: 400px;
height: 200px;
background-color: black;
/*display: table-cell;useless here i believe*/
text-align:center;
}
#innerOne,
#innerTwo {
width: 400px;
height: 100px;
line-height:100px;
background-color: blue;
}
#innerOne {
background-color: red;
}
<div id="outer">
<div id="innerOne">
<button>One</button>
<button>Two</button>
</div>
<div id="innerTwo">
<button>Three</button>
<button>Four</button>
</div>
</div>
Related
This question already has answers here:
How can I vertically align elements in a div?
(28 answers)
Flexbox: center horizontally and vertically
(14 answers)
Closed 2 years ago.
I have 2 different height divs side by side:
.box {
color: white;
display: inline-block;
}
#box-a {
background-color: black;
height: 90px;
width: 100px;
}
#box-b {
background-color: blue;
height: 50px;
width: 100px;
}
<div class="box" id="box-a">
<p> div a </p>
</div>
<div class="box" id="box-b">
<p> div b </p>
</div>
Now I want something like this
and no matter how hard I have tried and researched I can't get it to work.
My suggestion:
.box {
color: white;
margin-right: 10px;
}
#box-a {
background-color: black;
height: 90px;
width: 100px;
}
#box-b {
background-color: blue;
height: 50px;
width: 100px;
}
.wrapper {
display: flex;
align-items: center;
}
<div class="wrapper">
<div class="box" id="box-a">
<p> div a </p>
</div>
<div class="box" id="box-b">
<p> div b </p>
</div>
</div>
If you also want to center boxes horizontally, just add justify-content: center to css wrapper rule set.
Try adding this in your css
#box-b {
position:relative;
top:20px;
}
.box {
color: white;
display: inline-block;
}
#box-a {
background-color: black;
height: 90px;
width: 100px;
}
#box-b {
background-color: blue;
height: 50px;
width: 100px;
}
#box-b {
position: relative;
top: 20px;
}
<div class="box" id="box-a">
<p> div a </p>
</div>
<div class="box" id="box-b">
<p> div b </p>
</div>
enter image description here
Remove box space from vertical and horizontal scroll bar
What you want is not possible because scroll is a native ui. However it is obviously possible if you make your own scrollbar using JS.
This is the closest that I can get with CSS
.case-1 .wrapper{
width: 100px;
height: 100px;
overflow: auto;
}
.case-1 .wrapper .something{
width: 200px;
height: 200px;
background-color: hotpink;
}
.case-2 .wrapper{
width: 100px;
height: 100px;
overflow-y: auto;
}
.case-2 .wrapper-inner{
overflow-x: auto;
}
.case-2 .wrapper .wrapper-inner .something{
width: 200px;
height: 200px;
background-color: hotpink;
}
body{
display: flex;
margin: 0;
font-family: sans-serif;
}
.case{
margin: 20px;
}
<div class="case-1 case">
<h4>Normal</h4>
<div class="wrapper">
<div class="something"></div>
</div>
</div>
<div class="case-2 case">
<h4>Desired</h4>
<div class="wrapper">
<div class="wrapper-inner">
<div class="something"></div>
</div>
</div>
</div>
You can try the property overflow.
The overflow-x property controls whether the content of the block element is displayed horizontally and overflow-y vertically.
In this JSFiddle how can I downsize the img / img-container to be only as wide as its widest sibling div?
.outer {
display: inline-flex;
flex-flow: column;
}
.outer span {
display: flex;
}
div {
border: 1px dotted black;
}
<div class="outer">
<div>
<span>text</span>
<span>more text</span>
</div>
<div>
<img src="https://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon#2.png?v=73d79a89bded">
</div>
<div>
<span>this should determine width</span>
</div>
</div>
I'm not sure how cross-browser compatible this solution is, but it works on Chrome 64, Safari 11, and Firefox 57.
Give the element containing the img a width: 0; min-width: 100%; max-width: 100%;, and the img itself a width: 100%;.
Like this:
div {
border: 1px dotted black;
}
.outer {
display: inline-flex;
flex-flow: column wrap;
}
.child {
width: 0;
min-width: 100%;
max-width: 100%;
}
.img {
width: 100%;
}
<div class="outer">
<div>
<span>text</span>
<span>more text</span>
</div>
<div class="child">
<img class="img" src="https://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon#2.png?v=73d79a89bded" />
</div>
<div class="main">
<span contenteditable>this should determine width</span>
</div>
</div>
Another Solution
Use a background-image instead of an img. This allows us to make the image scale with the width of the widest element in the flexbox.
The trick is to set a padding-bottom on the element with the image proportional to the image proportions. In this case the image is square, so I'll set `padding-bottom: 100%; so it creates a square element.
If the image was a wide rectangle, 200 x 100 px, I would set padding-bottom: 50%. Or, if the image was a tall rectangle, 100 x 200 px, I would set padding-bottom: 200%.
Like this:
div {
border: 1px dotted black;
}
.outer {
display: inline-flex;
flex-flow: column wrap;
}
.img {
background-image: url(https://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon#2.png?v=73d79a89bded);
background-repeat: no-repeat;
background-size: cover;
padding-bottom: 100%;
}
<div class="outer">
<div>
<span>text</span>
<span>more text</span>
</div>
<div class="img">
</div>
<div>
<span contenteditable>this should determine width</span>
</div>
</div>
You can do this with CSS table layout and set width: 1% on table and white-space: nowrap on text elements.
.outer {
display: table;
width: 1%;
}
.outer span {
white-space: nowrap;
}
div {
border: 1px dotted black;
}
img {
max-width: 100%;
}
<div class="outer">
<div><span>text</span><span>more text</span></div>
<div class="image">
<img src="https://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon#2.png?v=73d79a89bded">
</div>
<div><span>this should determine width</span></div>
</div>
As you asked about it for flexbox layout particularly, here is trick playing with pseudo and positions. Note, it only works if you know the image aspect ratio already, example below for a square image.
div {
border: 1px dotted black;
}
.outer {
display: inline-flex;
flex-flow: column;
}
.image {
position: relative;
}
.image:before {
content: "";
display: block;
padding-top: 100%;
/*https://stackoverflow.com/a/10441480/483779*/
}
.image img {
position: absolute;
left: 0;
top: 0;
max-width: 100%;
}
<div class="outer">
<div class="image">
<img src="https://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon#2.png?v=73d79a89bded">
</div>
<div>this should determine width</div>
</div>
Your CSS container is already as wide as its widest sibling div. You just need to shrink the border of the picture with paint or photoshop.
This question already has answers here:
How to center an element horizontally and vertically
(27 answers)
Closed 8 years ago.
How to center the text inside that box?
Here's how I want elements sorted out:
In a smaller screen, elements stack on top of each other, and the .text-box div, that contains the text I want to center, has a fixed height. For larger widths, the .text-box div should have a height igual to larger image's height minus shorter image's height
See the Fiddle here
HTML
<div id="wrapper">
<div class="box">
<img class="img-large" src="http://placekitten.com/900/800" alt=""/>
</div>
<div class="box">
<img src="http://placekitten.com/900/400" alt=""/>
</div>
<div class="box-text">
<div class="vcenter-outer">
<div class="vcenter-inner">
<p>center this text vertically</p>
</div>
</div>
</div>
</div>
CSS
#wrapper {
background: #ffeaea;
height: 100vh;
}
img {
width: 100%;
display: block;
}
.img-large {
height: 100vh;
width: auto;
}
.box {
position: relative;
width: 100%;
overflow: hidden;
}
.box-text {
text-align: center;
background: yellow;
height: 100%;
}
#media only screen and (min-width: 768px) {
.box {
float: left;
width: 50%;
}
}
.vcenter-outer {
background: yellow;
display: table;
}
.vcenter-inner {
background: lightblue;
display: table-cell;
vertical-align: middle;
}
Why does the .text-box div span through the whole wrapper?
Basicly You could use display:flex and float together
.trio {
height:100vh;
width:100vh;
}
.trio>div {
float:left;
display:flex;
align-items:center;
justify-content:center;
width:50vh;
height:50vh;
box-shadow:inset 0 0 0 2px;
}
.trio .first {
height:100vh;
}
<div class="trio">
<div class="first">
<p>Center</p>
</div>
<div class="next">
<p>Center</p>
</div>
<div class="next">
<p>Center</p>
</div>
</div>
display:table works too with an extra level of div inbricated as you did , same idea: float + vh
codepen to play with
JSFiddle link
HTML:
<div id="va-m">
<img src="http://placehold.it/150x150">
<h1> vertical-align: middle </h1>
</div>
<div id="no-va">
<img src="http://placehold.it/150x150">
<h1> no vertical align </h1>
</div>
CSS:
div {
margin-top: 30px;
padding: 5px;
width: 500px;
height: 150px;
line-height: 200px;
background-color: blue;
}
#va-m img {
vertical-align: middle;
}
h1 {
display: inline;
}
img {
border-radius: 75px;
}
I'm trying to align the image and text vertically in the container div. However, I seem to either get the choice of aligning the image, or aligning the text. Using vertical-align: middle on the img tag pushes it out of the container. Why?
Change the height to auto
Fiddle
div {
margin-top: 30px;
padding: 5px;
width: 500px;
//height: 150px;
height: auto;
line-height: 200px;
background-color: blue;
}
Update: Use table and table-cell + vertical-align: middle
Fiddle