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
Related
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:
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>
I am trying to horizontally center images of any width. (In other words, each image has a width that falls in a range between 100px and 1000px). The parent area is 712px wide.
Most solutions I've tried center the images left side at the 50% mark.
This margin-left:50%; transform:translate(-50%,0); will position the element in the middle of its container. Even container is smaller than image:
div { width:100%; overflow-x:hidden; border:1px solid }
div > img { margin-left:50%; transform:translate(-50%,0); vertical-align:middle; }
<div>
<img src="http://lorempixel.com/400/200/">
</div>
<div>
<img src="http://lorempixel.com/1000/600/sports/3/">
</div>
img {
display: block;
margin: 0 auto;
}
or:
div.your-block-wrapper {
text-align: center;
}
div.your-block-wrapper img {
display: block;
}
.ct {
position: relative;
width: 700px;
height: 700px;
margin: 0 auto;
overflow: hidden;
}
.ct img {
position: absolute;
left: 50%;
width: 900px;
margin-left: -450px;
z-index: 22;
}
<div class="ct">
<img src="http://images.entertainment.ie/images_content/rectangle/620x372/success-kid.jpg" alt="" />
</div>
Check this out:
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
This apparently is something that's been floating around some of my dev friends that they've been adopting. I will likely start giving this a shot. It's still CSS, it's clean and no script required. You can also vertically center (and other stuff, as well).
You could do it like this:
https://jsfiddle.net/46mdr2z6/1/
<div class="wrapper">
<div>
<img src="http://placehold.it/250x110">
</div>
<div>
<img src="http://placehold.it/150x150">
</div>
<div>
<img src="http://placehold.it/650x250">
</div>
<div>
<img src="http://placehold.it/850x350">
</div>
<div>
<img src="http://placehold.it/120x750">
</div>
<div>
<img src="http://placehold.it/520x270">
</div>
</div>
css:
.wrapper {
width: 712px
}
.wrapper > div{
text-align: center;
}
img {
max-width: 100%;
}
I have a responsive website with max-width set to 1000px, but I need to fit background picture that will overlap one of the divs and also place full page-width bottom borders to other divs.
The code i have is like this:
.container {
width: 100%;
max-width: 1000px;
}
.logotest {
background-color: #03b9e5;
height: 50px;
}
.navtest {
background-color: #e4ed00;
height: 25px;
}
.socialtest {
background-color: #ab801a;
height: 25px;
}
.main {
height: 750px;
background: url(background.jpg) no-repeat top center;
margin: auto;
}
.line {
border-bottom: 1px solid black;
}
.container:after {
clear: both;
content: ".";
display: block;
height: 0;
visibility: hidden;
}
<body>
<div class="container" id="first">
<div class="logotest">
</div>
<div class="socialtest">
</div>
<div class="navtest">
</div>
</div>
<div class="line"></div>
<div class="main line" id="second">
</div><div class="container">
<div id="third">
</div>
</div>
</body>
I get the first div with correct width and bottom border going across the full page width, second div has got the background picture showing, but the max-width of 1000px does no longer apply. The bottom border is shown correctly (dividing second and third div) and the third div has got the correct max-width applied again.
What am I doing wrong/not doing to get the max-width for the second div?
YOUR SOLUTION
If the browser support of background-size property is good enough for you, you can use background-size: cover;. Check here or here to see browser support.
Here is the code snippet to show how it works. Be sure to position your background-image to center center if you want it to always be centered.
.container {
width: 100%;
max-width: 300px;
margin: 0 auto;
}
.line {
border-bottom: 1px solid black;
}
.logotest {
background-color: #03b9e5;
height: 50px;
}
.navtest {
background-color: #e4ed00;
height: 25px;
}
.socialtest {
background-color: #ab801a;
height: 25px;
}
.main {
height: 250px;
background: url(http://lorempixel.com/250/250) no-repeat center center;
background-size: cover; /* This does the magic */
}
.container:after {
clear: both;
content: ".";
display: block;
height: 0;
visibility: hidden;
}
<body>
<div class="container" id="first">
<div class="logotest">
</div>
<div class="socialtest">
</div>
<div class="navtest">
</div>
</div>
<div class="line"></div>
<div class="main" id="second">
<div class="container">Put your content in here.</div>
</div>
<div class="line"></div>
<div class="container">
<div id="third">
</div>
</div>
<div class="line"></div>
</body>
LAST (BUT NOT LEAST)
You might want to check this great article about the state of responsive images in web design, that will help you if you are going into responsive web design: Responsive images done right.
The task I encountered looks standard: I have a fixed height container and 3 div's inside it. I want the 2nd div to be stretched between the top and the bottom div's. When the contents of the 2nd div overflows - I would like to show the scroll bars.
I know how to accomplish this task using the absolute positioning. A question is: can I do it using the table on divs?
An additional requirement: if possible, I would like to avoid setting header's height as fixed.
I have tried to code it in my fiddle, but, as you see, I failed.
CSS:
.container {
height: 500px;
background-color: gainsboro;
}
.table {
display: table;
height: 100%;
}
.table > div {
display: table-row;
}
.table > div > div {
display: table-cell;
vertical-align: middle;
border: 1px solid black;
overflow: scroll;
}
.center > div {
height: 100%;
}
.content {
height: 700px;
}
HTML:
<div class="container">
<div class="table">
<div>
<div>XXX</div>
</div>
<div class="center">
<div>
<div class="content"></div>
</div>
</div>
<div>
<div>YYY</div>
</div>
</div>
</div>
Here is one possible solution:
<div class="container">
<div class="header">
<div>Top Header Block</div>
</div>
<div class="center">
<div class="content">
<p>Lorem ipsum ...</p>
<p>Lorem ipsum ...</p>
</div><!-- .content -->
</div><!-- .center -->
<div class="footer">
<div>Bottom Footer Block</div>
</div>
</div>
and the CSS:
.container {
height: 200px;
}
.header, .footer {
background-color: gainsboro;
}
.center {
height: inherit;
}
.content {
background-color: #F0F0F0;
height: inherit;
overflow: auto;
}
Since you are fixing the height of the container, you inherit the height both in the .center and the .content <div>'s.
If you tweak the container height, the center div expands but the header and footer div's stay the same height.
Use overflow on the content div to allow for scrolling.
Fiddle Reference: http://jsfiddle.net/audetwebdesign/nae5z/
Your way was right, just make a few changes (See this Fiddle):
html, body, .container, .table {
margin: 0;
height: 100%;
}
#header,#footer {
height: 1px;
}
This should work because tables cells get increased in height if the content needs it.
Just a hint: You may improve the whole thing, for example I would use HTML 5 and the <header/> and <footer/> elements. But that was not part of your question. Anyway, here is another update to your fiddle:
<div>
<header>
<div>XXX</div>
</header>
<main>
<div>
<div class="content"></div>
</div>
</main>
<footer>
<div>YYY</div>
</footer>
</div>
With CSS:
html, body, body > div {
margin: 0;
height: 100%;
}
body {
background-color: gainsboro;
}
body > div {
display: table;
width: 100%;
}
body > div > * {
display: table-row;
}
body > div > * > div {
display: table-cell;
vertical-align: middle;
border: 1px solid black;
}
header, footer {
height: 1px;
}
main is very new to HTML 5, just if you're wondering.