I'm trying to create a horizontally scrolling image gallery and I am having trouble centering it in the middle of the window. I tried to add display of flex to the body and do it that way, it didn't work for me.
I'm new at flexbox so I'm not sure if I'm doing this right, I'm just looking for an overall constructive criticism and guidance.
html,
body {
width: 100%;
height: 100%;
}
body {
margin: 2em;
}
.wrapper {
display: flex;
flex-direction: row;
}
img {
min-height: 400px;
max-height: 420px;
display: flex;
padding-right: 2em;
}
<div class="wrapper">
<img src="https://source.unsplash.com/collection/1163637/480x480">
<img src="https://source.unsplash.com/collection/1163637/480x480">
<img src="https://source.unsplash.com/collection/1163637/480x480">
<img src="https://source.unsplash.com/collection/1163637/480x480">
<img src="https://source.unsplash.com/collection/1163637/480x480">
<img src="https://source.unsplash.com/collection/1163637/480x480">
<img src="https://source.unsplash.com/collection/1163637/480x480">
</div>
Here's the example on Codepen.
In order to center a div on the screen, you can change the top property as shown below:
.wrapper{
display: flex;
flex-direction: row;
position: absolute;
top: 20%;
}
And to hide horizontal scrolling you can do this:
html, body{
width: 100%;
height: 100%;
overflow-y: hidden;
}
To overlay text on an image when you hover:
<div class="img_container_1">
<img src="https://source.unsplash.com/collection/1163637/480x480">
<div class="overlay">
<div class="text">Hello World</div>
</div>
</div>
.overlay {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
opacity: 0;
height: inherit;
width: inherit;
transition: .5s ease;
}
.image_container_1{
position: relative;
}
.img_container_1:hover .overlay {
opacity: 1;
}
img {
min-height: 400px;
max-height: 420px;
display: flex;
padding-right: 2em;
}
.text {
color: white;
font-size: 20px;
position: absolute;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
text-align: center;
}
You can find the full demo here: https://codepen.io/anon/pen/LJRVwb?editors=1100
To vertically center everything in your wrapper with flexbox, you need to add align-items: center; to .wrapper.
The wrapper is also matching the height of its contents in your example, so you won't see any difference unless you give .wrapper a height.
You set body to height: 100%;, which is what makes your example scroll vertically at the moment. Removing that removes the scrollbar as long as your view is big enough to hold all of its content vertically.
Adjusted CSS:
html, body {
width: 100%;
}
body {
margin: 2em;
}
.wrapper {
display: flex;
align-items: center;
height: 500px; /* Example height */
flex-direction: row; /* It's this by default, so you could remove this if you want */
}
img {
min-height: 400px;
max-height: 420px;
padding-right: 2em;
}
I made an adjusted example on CodePen that works for reference: https://codepen.io/happikitsune/pen/rZMOWO
Related
suppose we have 4 dives.
the first div is outer div.
i want to create a HTML that
the second div size be 50% first and be in middle bottom of first div.
the third div size be 50% second and be in middle left of second div.
the fourth div size be 50% third div and be in middle top of third div.
how can i do it?
Is this your desired output? It’s made using position, top and bottom, and translate to make sure it’s centered right.
.div1 div { /* makes every small div 50% the size of the previous */
width: 50%;
height: 50%;
}
.div1 {
width: 200px;
height: 200px;
background-color: red;
}
.div2 {
background-color: green;
position: relative;
top: 100%;
left: 50%;
transform: translate(-50%, -100%);
}
.div3 {
background-color: pink;
position: relative;
top: 50%;
transform: translate(0, -50%);
}
.div4 {
background-color: lightblue;
position: relative;
left: 50%;
transform: translate(-50%, 0);
}
<div class="div1">
<div class="div2">
<div class="div3">
<div class="div4">
</div>
</div>
</div>
</div>
you can also use flex(or grid) and margin instead position :
div {
display: flex;
}
body>div {
/* sizing : whatever you want to start from */
height: 90vmin;
width: 90vmin;
background: #ed1c24;
}
div div {
height: 50%;
width: 50%;
}
div div {
background: #22b14c;
margin: auto auto 0;
}
div div div {
background: #ffaec9;
margin: auto auto auto 0;
}
div div div div {
background: #00a2e8;
margin: 0 auto auto;
}
/* center the demo */
html {
display: flex;
height: 100vh;
}
body {
margin: auto;
}
<div>
<div>
<div>
<div>
</div>
</div>
</div>
</div>
We can achieve this by using the CSS Flexbox and Margin properties.
index.html
<body>
<div class="firstdiv">
<div class="seconddiv">
<div class="thirddiv">
<div class="fourthdiv">
</div>
</div>
</div>
</div>
</body>
styles.css
div {
display: flex;
}
.firstdiv {
background-color: red;
width: 600px;
height: 600px;
}
.seconddiv {
background-color: green;
width: 50%;
height: 50%;
margin: auto;
margin-bottom: 0;
}
.thirddiv {
background-color: pink;
width: 50%;
height: 50%;
margin: auto;
margin-left: 0;
}
.fourthdiv {
background-color: blue;
width: 50%;
height: 50%;
margin: auto;
margin-top: 0;
}
You can use CSS flexbox below. There are four divs below and you can change the size of the first div. And then the others automatically align and resize themselves.
HTML file:
<html>
<body>
<div id="first">
<div id="second">
<div id="third">
<div id="fourth">
<p>Text</p>
</div>
</div>
</div>
</div>
</body>
</html>
CSS file:
* {
margin: 0;
padding: 0;
}
#first {
background: #ed1c24;
width: 500px;
height: 500px;
display: flex;
justify-content: center;
align-items: flex-end;
margin: auto;
}
#second {
display: flex;
flex-direction: row;
justify-content: flex-start;
align-items: center;
background: #22b14c;
width: 50%;
height: 50%;
margin: 0 auto;
}
#third {
display: flex;
flex-direction: row;
justify-content: center;
align-items: flex-start;
background: #ffaec9;
width: 50%;
height: 50%;
}
#fourth {
display: flex;
justify-content: center;
align-items: center;
background: #00a3e9;
width: 50%;
height: 50%;
}
Click to see the result of these lines of code:
Result
This question already has answers here:
How to center an element horizontally and vertically
(27 answers)
Closed 6 years ago.
I want the content to be centered vertically and horizontally but it gets centered only horizontally. The problem is that I don't have fixed height.
Thank you guys for help!
html,
body {
height: 100% margin: 0;
overflow: hidden;
}
.content {
width: 100%;
height: 100%;
align-items: center;
justify-content: center;
display: flex;
flex-direction: column;
text-align: center;
}
<div class="content">
<h1>Welcome to the website!</h1>
</div>
You can easily center an element respect to the parent in this way (assuming that the parent has position: relative;).
In your example:
h1 {
display: block;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
You can also center it in the middle of the screen using position: fixed; instead.
Follow this code
HTML
<body >
<div class="content">
<h1>Welcome to the website!</h1>
</div>
</body>
CSS
html,body {
height : 100%;
width : 100%;
}
.content {
height : 100%;
width : 100%;
display: table;
}
h1 {
text-align: center;
display: table-cell;
vertical-align: middle;
}
Follow this code.
body{
margin: 0;
padding: 0;
}
.content-wrapper{
background-color: #121212;
display: block;
left: 0;
height: 100%;
padding: 15px;
position: fixed;
top: 0;
width: 100%;
}
.content{
background-color: #f5f5f5;
display: table;
height: 100%;
margin-left: auto;
margin-right: auto;
text-align: center;
width: 100%;
}
.centent-cell{
display: table-cell;
height: 100%;
vertical-align: middle;
width: 100%;
}
h1{
color: #121212;
}
<div class="content-wrapper">
<div class="content">
<div class="centent-cell">
<h1>Welcome to the website!</h1>
</div>
</div>
</div>
Here's an example of what you need:
<section>
<div class="centerize">
<div class="v-center">
<div class="box">Say my name!</div>
</div>
</div>
</section>
and CSS
section {
height: 100vh;
background: #fff;
}
.centerize {
display: table;
height: 100%;
width: 100%;
}
.v-center {
display: table-cell;
vertical-align: middle
}
.box {
background: #000;
width: 10%;
margin: 0 auto;
}
How can I centre align text over an <img> preferably using FlexBox?
body {
margin: 0px;
}
.height-100vh {
height: 100vh;
}
.center-aligned {
display: box;
display: flex;
box-align: center;
align-items: center;
box-pack: center;
justify-content: center;
}
.background-image {
position: relative;
}
.text {
position: absolute;
}
<section class="height-100vh center-aligned">
<img class="background-image" src="http://vignette2.wikia.nocookie.net/uncyclopedia/images/f/f8/Stand-out-in-the-crowd-300x300.jpg" />
<div class="text">SOME TEXT</div>
</section>
To center text over an image you don't need flexbox. Just use CSS positioning properties.
.height-100vh {
height: 100vh;
position: relative; /* establish nearest positioned ancestor for
absolute positioning */
}
.text {
position: absolute;
left: 50%; /* horizontal alignment */
top: 50%; /* vertical alignment */
transform: translate(-50%, -50%); /* precise centering; see link below */
}
body {
margin: 0px;
}
.height-100vh {
height: 100vh;
display: flex; /* establish flex container */
flex-direction: column; /* stack flex items vertically */
position: relative; /* establish nearest positioned ancenstor for absolute positioning */
}
.text {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
color: white;
font-weight: bold;
}
.center-aligned {
display: flex;
align-items: center;
justify-content: center;
}
<section class="height-100vh center-aligned">
<img class="background-image" src="http://vignette2.wikia.nocookie.net/uncyclopedia/images/f/f8/Stand-out-in-the-crowd-300x300.jpg/revision/latest?cb=20090904155448" />
<div class="text">SOME TEXT</div>
</section>
Revised Codepen
The code above centers the text both vertically and horizontally over the image:
For a more detailed explanation of the centering method see:
Element will not stay centered, especially when re-sizing screen
You can just wrap an image with relatively positioned inline-block <div> and give the text this way:
* {margin: 0; padding: 0; list-style: none;}
.img-holder {position: relative; display: inline-block;}
.img-holder img {display: block;}
.img-holder p {position: absolute; top: 50%; left: 0; right: 0; transform: translate(0, -50%); text-align: center; color: #fff; text-shadow: 0 0 15px;}
<div class="img-holder">
<img src="http://bit.ly/1YrRsis" alt="">
<p>Text Aligned Centrally Vertical & Horizontal.</p>
</div>
Now the <div> is an inline kinda element that you can style.
Preview:
I've added another wrapper div surrounding the img and text as well as using flex to position the text. See this codepen
HTML:
<section class="height-100vh center-aligned">
<div class="wrapper">
<img class="background-image" src="http://bit.ly/1YrRsis" />
<div class="text">SOME TEXT</div>
</div>
</section>
CSS:
#import "bourbon";
body {
margin: 0px;
}
.height-100vh {
height: 100vh;
}
.center-aligned {
#include display(flex);
#include align-items(center);
#include justify-content(center);
}
.wrapper {
position: relative;
}
.text {
justify-content: center;
align-items: center;
display: flex;
color: #fff;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
You also can center align text onto an image using display: inline-block; to the wrapper element.
.height-100vh {
display: inline-block;
position: relative;
}
.text {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
color: #fff;
}
<section class="height-100vh center-aligned">
<img class="background-image" src="http://vignette2.wikia.nocookie.net/uncyclopedia/images/f/f8/Stand-out-in-the-crowd-300x300.jpg" />
<div class="text">SOME TEXT</div>
</section>
I added 2 more divs
<div class="text box" >
<img src="images/woodenbridge.jpg" alt="Wooden Bridge" />
<div class="slide-box flex">
<div class="flex-item"></div>
</div>
</div>
and then styled as follows :
.flex-item {
display: inline;
align-self: center;
}
Responsive centered text content over image
.height-100vh {
height: 100vh;
background: lightgrey;
}
.center-aligned {
display: flex;
align-items: center;
justify-content: center;
}
.background-image {
opacity: .3;
width: 100%;
object-fit:cover;
height: 100%;
}
.text {
position: absolute;
color: white;
font-family: 'Gill Sans', 'Gill Sans MT';
background: darkcyan;
padding: 20px;
}
<section class="height-100vh center-aligned">
<img class="background-image" src="https://www.humanesociety.org/sites/default/files/styles/768x326/public/2018/08/kitten-440379.jpg?h=f6a7b1af&itok=vU0J0uZR" />
<div class="text">I'm in center</div>
</section>
If you resize the results pane to make it narrower, you will see that the image area will be resized responsively.
I am hoping to make the text always centered vertically and horizontally when such resizing happens.
I am not able to find the right css for this.
.holders {
position: absolute;
bottom: 0;
left: 50%;
margin-left: -150px; /*half of image width*/
}
.holder {
float: left; /*this cannot be changed because we have a row of blocks like the example one in the bottom*/
position: relative;
}
.text {
position: absolute;
top: 150px;
/*this is not right at this moment*/
}
.img {
width: 100%;
height: auto;
}
<div style="position: fixed;top:0;left:0;width:100%;height:100%">
<div class="holders">
<div class="holder">
<div class="text">
This is text that should be in the center of the block vertically and horizontally
</div>
<img class="img" src="http://www.fnordware.com/superpng/pnggrad8rgb.png" />
</div>
</div>
</div>
Here is the JS Fiddle example.
.holders {
position: absolute;
bottom: 0;
left: 50%;
/* margin-left: -150px; */
transform: translateX(-50%); /* see comments below */
}
.holder {
float: left;
position: relative;
}
.text {
width: 100%;
text-align: center;
position: absolute;
top: 50%; /* center text vertically */
left: 50%; /* center text horizontally */
transform: translate(-50%, -50%); /* horizontal & vertical centering fine-tuning;
http://stackoverflow.com/a/36817384/3597276 */
}
.img {
width: 100%;
height: auto;
}
<div style="position: fixed;top:0;left:0;width:100%;height:100%">
<div class="holders">
<div class="holder">
<div class="text">
This is text that should be in the center of the block vertically and horizontally
</div>
<img class="img" src="http://www.fnordware.com/superpng/pnggrad8rgb.png" />
</div>
</div>
</div>
Revised Fiddle
Have you considered using flexbox? It will do exactly what you want it to do, with minimal changes.
.holders {
position: absolute;
bottom: 0;
left: 50%;
margin-left: -150px; /*half of image width*/
}
.holder {
float: left; /*this cannot be changed because we have a row of blocks like the example one in the bottom*/
position: relative;
}
.text {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
display: flex;
flex-direction: column;
justify-content: space-around;
}
.text p {
flex: 0 0 auto;
margin: 0;
text-align: center;
}
.img {
width: 100%;
height: auto;
}
You are able to do the same thing with less code.
HTML
<div class="center">
<p class="text">This is text that should be in the center of the block vertically and horizontally</p>
</div>
CSS
.center {
-webkit-align-items: center;
-webkit-justify-content: center;
align-items: center;
background: url('http://www.fnordware.com/superpng/pnggrad8rgb.png');
display: -webkit-flex;
display: flex;
height: 300px;
justify-content: center;
width: 300px;
}
LINK
https://jsfiddle.net/4tx5h1tq/42/
I have a flexbox container that has two <div>s in it. One of the <div>s is the header, and the other <div> is hidden until an event is fired. When said event is fired, I want to make the hidden <div> appear on top of the other.
.container {
display: flex;
align-items: center;
justify-content: center;
}
.header {
max-width: 600px;
}
#content {
display: none;
max-width: 600px;
}
.changed #content {
display: block;
z-index: 100;
}
.changed .header {
opacity: .3;
z-index: 0;
}
<div class="container">
<div class="header">
<h1>Heading</h1>
<h2>Subheading</h2>
<div class="icons">Icons</div>
</div>
<div id="content"></div>
</div>
The class .changed is the class added to .container that renders the #content <div> visible. This part works fine.
The problem is that the .header and the #content <div>s are appearing side-by-side, even though they each have different z-index values. I think this is because of flexbox.
I've tried using different methods of horizontal and vertical centering, but flexbox has gotten me the closest to what I want. Is this the right approach and I'm just missing something? Should I go about this a different way?
Thanks!
Stack the flex items in a column:
.container { flex-direction: column; }
Make the container the containing block for absolute positioning:
.container { position: relative; }
Absolutely position the hidden div over the header:
#content {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
Then apply your JS and z-index as necessary.
.container {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
position: relative;
}
.header {
max-width: 600px;
background-color: aqua;
}
#content {
/* display: none; */
max-width: 600px;
background-color: red;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.changed #content {
/* display: block; <-- not necessary */
z-index: 100;
}
.changed .header {
opacity: .3;
z-index: 0;
}
<div class="container">
<div class="header">
<h1>Heading</h1>
<h2>Subheading</h2>
<div class="icons">Icons</div>
</div>
<div id="content">HIDDEN DIV
</div>
</div>
I think you just need to add flex-direction: column; and set the order
http://jsbin.com/cuximuhemi/edit?html,css,output
Is that what you’re going for?