I want to align a div centered horizontally in the page. The div contains several html content like, images, links, texts etc.
For the sake of it I cannot use display:table, which would align the div in center. How could I else align the content, without having to give an explicit width?
You will need to use justify-content: center in flexbox layout. No need of setting a width.
.center {
display: flex;
justify-content: center;
}
<div class="center">
<img src="http://placehold.it/300x300">
<span class="text">Test text</span>
Test link
</div>
Or without using flex, translate or absolute positioning:
#container {
text-align:center;
}
#center {
padding:20px;
display:inline-block;
background:#ccc;
}
<div id="container">
<div id="center">
this is a test
</div>
</div>
You can center horizontally and vertically (as you like) with a translate so easy, and without know the width or the height:
<div class="parent">
<div class="center">
Centered
</div>
</div>
CSS
.parent {
position: relative;
}
.center {
position: absolute;
left:50%;
transform: translateX(-50%);
-webkit-transform: translateX(-50%);
}
To center vertically use translateY()
Snippet:
.parent {
position: relative;
width: 100%;
height: 500px;
border: 1px solid black;
}
.center {
height: 240px;
position: absolute;
left:50%;
top: 50%;
transform: translate(-50%, -50%);
-webkit-transform: translate(-50%, -50%);
}
<div class="parent">
<div class="center">
Centered
</div>
</div>
Related
In the middle of the page is where I need the textboxes
horizontal plus vertical center is troublesome.
first center horizontally by making your parent display: flex
and include two bracketting children that stretch flex: 1 as well as your centered element (this allows it to be a perfect third, if you want it to instead be bigger, remove flex: 1 for the center child and put a width in percentage instead (pixel works but will not scale))
then center vertically by adding a margin top where you calculate leftover size :
.centeredchild {
background-color: red;
height: 20px;
flex: 1;
text-align: center;
margin-top: calc(50vh - 10px);
}
.centeringaid {
flex: 1;
}
.parent {
width: 100%;
height: 100vh;
display: flex;
background-color: lightblue;
}
<div class="parent">
<div class="centeringaid"></div>
<div class="centeredchild">hi</div>
<div class="centeringaid"></div>
</div>
there's also the more common method : https://www.w3schools.com/howto/howto_css_center-vertical.asp
this website, by the way is full of usefull tidbits.
Refer below code snippet to align your multiple text boxes in the middle of the page. (Consider page height to be 400px)
.main-div {
height: 400px;
position: relative;
}
.sub-div {
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
position: absolute;
top: 50%;
left: 50%;
}
input[type="text"] {
margin:10px auto;
border: 1px solid gray;
}
<div class="main-div">
<div class="sub-div">
<input type="text" />
<input type="text" />
<input type="text" />
</div>
</div>
You use this code:
CSS:
.outercontainer {
height: 200px;
position: relative;
border: 3px solid green;
}
.innercontainer {
margin: 0;
position: absolute;
top: 50%;
left: 50%;
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
HTML:
<div class="outercontainer">
<div class="innercontainer">
<p>This sentance is it the middle!</p>
</div>
</div>
I want to display my page's title, "Math Achievement Tutoring", over a photograph of a hiker. My first attempt was to create this html:
<div id="wrapper">
<header>
<h1>Math Achivement Tutoring</h1>
</header>
<div id="hero">
<img src="http://michaelmossey.com/demo/home-hiker-grayish.jpg" alt="" width="500">
</div>
</div>
with this CSS, the idea being to position the h1 as absolute:
h1 {
text-align:center;
position: absolute;
color: #a04040;
}
but this means the title is no longer centered. I may run into other trouble as well if I start to fiddle with margin and padding.
h1 {
text-align:center;
position: absolute;
color: #a04040;
}
<div id="wrapper">
<header>
<h1>Math Achivement Tutoring</h1>
</header>
<div id="hero">
<img src="http://michaelmossey.com/demo/home-hiker-grayish.jpg" alt="" width="500">
</div>
</div>
What are my options for achieving this, and does it depend on where I want to take this website eventually? (like adding a navigation menu below the image)? Is there any simple demonstration code?
First, add position relative to #wrapper, then :
if you want it to be vertically & horizontally centered
h1 {
text-align:center;
position: absolute;
color: #a04040;
left: 50%;
top: 50%;
transform: translate( -50%, -50% );
}
if you want it to be vertically centered
h1 {
text-align:center;
position: absolute;
color: #a04040;
top: 50%;
transform: translateY( -50% );
}
if you want it to be horizontally centered
h1 {
text-align:center;
position: absolute;
color: #a04040;
left: 50%;
transform: translateX( -50% );
}
Just as Loesh Gupta says: make the wrapper .wrapper { position: relative; }. To center the text you can use multiple options. I'd go with something like this:
h1 { position: absolute; left: 50%; right: 50%; transform: translate(-50%, -50%); }
I can also recommend this tool: http://howtocenterincss.com/
Here's a solution using flexbox:
#wrapper {
display: flex; /* establish flex container */
flex-direction: column; /* stack flex items vertically */
position: relative; /* establish neares positioned ancenstor for absolute positioning */
}
h1 {
color: red;
}
.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;
}
<div id="wrapper" class="center-aligned">
<img class="background-image" src="http://michaelmossey.com/demo/home-hiker-grayish.jpg" />
<div class="text"><h1>Math Achivement Tutoring<h1></div>
</div>
Using a background image on a div and a display flex on the text might do it. Make sure to add the height on the text based on the image so it can be centered.
*{margin: 0 auto;}
.bg{
background-image:url('http://michaelmossey.com/demo/home-hiker-grayish.jpg');
height: 380px;
background-repeat: no-repeat;
background-size:cover;
background-position: center center;
}
.bg h1{
display: flex;
justify-content: center;
align-items: center;
height: 380px;
font-size: 3em;
color: #ffffff;
background-color: rgba(0,0,0,0.5);
}
<div class="bg">
<h1>Math Achivement Tutoring</h1>
</div>
How do you vertically centered a text in the middle of a banner container with image tag?
My search result is using display:table/display:table-cell or display:inline-block but those solution didn't work when we have image tag.
I think I have to have position absolute for text but then how to make sure it's always vertically and horizontally centered when re-sizing window?
Here is html:
<div class="banner">
<img class="" src="http://placehold.it/1440x410">
<div class="container">
<div class="banner-text">
<h1>Header Text</h1>
<h3>Sub Header Text</h3>
CTA
</div>
</div>
</div>
and Css as below:
.banner{
width: 100%;
position: relative;
}
.banner img{
width: 100%;
}
.container{
margin-left: auto;
margin-right: auto;
width: 100%;
}
.banner-text{
position: absolute;
top: 50%;
left: 50%;
}
http://codepen.io/neginbasiri/pen/grEmMx
I am wondering whats your solution for this?
Thanks
just add this transform to the class listed here:
.banner-text{
position: absolute;
top: 50%;
left: 50%;
transform: translateX(-50%) translateY(-50%); /* or of course - translate(-50%, -50%); */
}
Fork Demo
Check out this guide for more info: Centering in CSS
The most concise way to do what you need is the following:
.banner-text {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%); /* add me */
}
I'm trying to vertical align a container div within a jumbotron.
Because the height of the container (and so the jumbotron also) is variable top: 50%; margin-top: -'height * 0.5' won't work.
#import url('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css');
.banner {
padding: 48px 0px 48px 0px;
text-align: center;
}
.banner:after {
content: "";
display: block;
padding-top: 12.5%;
}
.container {
background: red !important; /* For testing vertical alignment */
}
<header class="jumbotron banner">
<div class="container">
<h1>Title</h1>
<p>Subtitle</p>
<p> <a class="btn btn-lg btn-primary" href="#" role="button">Button</a>
</p>
</div>
</header>
you should just center it using padding, this is the default way of doing it;
#import url('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css');
.banner {
padding: 5% 0px;
text-align: center;
}
.banner:after {
content: "";
display: block;
}
.container {
background: red !important; /* For testing vertical alignment */
}
<header class="jumbotron banner">
<div class="container">
<h1>Title</h1>
<p>Subtitle</p>
<p> <a class="btn btn-lg btn-primary" href="#" role="button">Button</a>
</p>
</div>
</header>
To align div vertically center you can use css like
position: relative;
top: 50%;
transform: translatey(-50%);
ex
.outer{
height:100px;
background:gray;
}
.inner{
background:green;
position:relative;
top:50%;
transform:translatey(-50%);
}
<div class="outer">
<div class="inner" >
<p>sample text</p>
</div>
</div>
There are several ways you can get content aligned vertically. I am posting here one common and most recent trick.
Give position: relative; to the parent element.
To the child:
.child-element {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
See JSFIDDLE and comment below if you need further assistance.
I'd like to vertically align a div centrally inside a larger div. How do I do this?
HTML:
<div class="outer">
<div class="middle">
<h1>test</h1>
<h2>Test</h2>
</div>
</div>
CSS:
.middle {height:160px;display: table-cell; vertical-align: middle;text-align:center;background:red}
.outer {height:550px;background:#eee}
http://jsfiddle.net/zc09442a/
You can use a rather simple technique taking advantage of css transforms, like this:
HTML
<div class="outer">
<div class="middle">
<h1>Test</h1>
<h2>Test</h2>
</div>
</div>
CSS
.middle {
margin: auto;
position: absolute;
top: 50%;
-webkit-transform: translate(0, -50%);
transform: translate(0, -50%);
background-color: #ccc;
}
.outer {
height:550px;
width:100%;
background-color:#eee;
position: relative;
}
Your updated fiddle here.
You can also center the element both vertically and horizontally by adding a left: 50% property and changing the transform: translate(0, -50%); line to transform: translate(-50%, -50%);.
Vertical and horizontal centering here
See if this helps
.outer{ background:tomato; height:20em; vertical-align:middle; display:table-cell; width:50em; }
.middle{ background:green; width:50%; margin: auto; }
http://codepen.io/jmonit/pen/LEzppM