I have an H1, and a link that needs to go after it (actually an image link, but I'm using text in the example below to simplify the code)
What I would like is for the link to appear after the H1 on the same line, but for the H1 to remain centered in it's containing div.
Right now, the link displaces the header from center...
Here's what I have
<div id="container">
<h1>Hello World</h1> <a id="gear" href="/aaa">Long text so you can see the displacement</a>
</div>
#container {
width: 400px;
text-align: center;
background-color: pink;
}
#container h1 {
display: inline-block;
}
And a fiddle of the same thing: http://jsfiddle.net/7xzq60x4/
In pictures:
now:
****HEADER TEXTlink****
what I want:
******HEADER TEXTlink**
Thanks in advance
Is this what you want?
#container {
width: 400px;
text-align: center;
background-color: pink;
position: relative;
}
#container h1 {
display: inline-block;
}
#container span {
position: absolute;
right: -75px;
top: 65%;
}
<div id="container">
<h1>Hello World</h1><span><a id="gear" href="/aaa">Long text so you can see the displacement</a></span>
</div>
Fiddle: http://jsfiddle.net/7xzq60x4/2/
Something which you might like...
#container {
width: 400px;
text-align: center;
background-color: pink;
position: relative;
}
#container h1 {
display: inline-block;
}
#container span {
position: absolute;
right: 25px;
top: 35%;
}
<div id="container">
<h1>Hello World</h1><span><a id="gear" href="/aaa">Small Text</a></span>
</div>
you may use the flex property + a pseudo element : http://jsfiddle.net/7xzq60x4/8/
#container {
width: 400px;
text-align: center;
background-color: pink;
display:flex;
}
#container a {
text-align:left;
}
#container:before {
content:'';
}
#container h1 {
white-space:nowrap;/* if this is what you 'd like */
}
#container h1 , #container a, #container:before{
flex:1;
margin:auto;
}
<div id="container">
<h1>Hello World</h1> <a id="gear" href="/aaa">Long text so you can see the displacement</a>
</div>
or the table layout http://jsfiddle.net/7xzq60x4/9/
#container {
width: 400px;
text-align: center;
background-color: pink;
display:table;
table-layout:fixed;
}
#container a {
text-align:left;
}
#container:before {
content:'';
}
#container h1, #container a, #container:before {
display:table-cell;
vertical-align:middle;
}
<div id="container">
<h1>Hello World</h1> <a id="gear" href="/aaa">Long text so you can see the displacement</a>
</div>
If you're okay with using flexbox, this can be achieved quite easily. This will need a few prefixes for compliance with all browsers, and may not go back to support IE9.
#container {
/* Use flexbox. */
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
/* Tells flexbox to place all items on a single line. */
-ms-flex-direction: row;
-webkit-flex-direction: row;
flex-direction: row;
/* Lastly tells flexbox to ensure all items are aligned on the center. */
align-items: center;
width: 400px;
text-align: center;
background-color: pink;
}
<div id="container">
<h1>Hello World</h1>
<a id="gear" href="/aaa">Long text so you can see the displacement</a>
</div>
For more information, try this quick guide.
Related
I've having a hard time aligning titles of each image at the bottom of the box. Please see the attached image. Here's my code:
Btw, I'm using Joomla.
Box code:
margin: auto;
align-self: center;
height:100px;
width:50%;
margin-top:0.5%;
margin-bottom:0.5%;
border: solid;
Image code:
margin-right:45%;
margin-left:45%;
margin-top:1%;
and title code:
display: block;
text-align: center;
position: absolute;
width: 50%;
This is my HTML code:
<ul class="nav menu navVerticalView nav-pills">
<li class="item-146">1</span></li><li class="item-147">2sdfgfsdgesdfg</span></li><li class="item-148">3cvbgfbnfghnf</span></li><li class="item-149">4</span></li><li class="item-150">5fghfghfg hfghrfghfgh </span></li><li class="item-151">6</span></li><li class="item-152">7bhjm hjmk,hj, hj, hj mjmgjjghjfh jfh hf hdgfhhdfghgfh fg hfgh fh</span></li><li class="item-153">8ghujghjtghj tgjtghjtyg tyjtyjtyj</span></li></ul> </div>
I tried to use bottom: 0px; for the title but all titles would gather above each other. I also tried vertical-align:bottom; but it doesn't work. I tried so many different ways but no luck. If someone can give heads up so I can move on towards, I'll be appreciative.
Thank you,
Image titles are not aligned with the bottom of the box
Set the box to position relative, and the text on absolute. After that set the text to bottom 0.
.box {
margin: auto;
align-self: center;
height:100px;
width:50%;
margin-top:0.5%;
margin-bottom:0.5%;
border: solid;
position: relative;
}
.image {
margin-right:45%;
margin-left:45%;
margin-top:1%;
background: red;
width: 30px;
height: 30px;
}
.box p {
display: block;
text-align: center;
position: absolute;
bottom: 0;
margin: 0;
}
<div class="box"><img class="image"><p>This is not how I would do it but okay 🤷🏼♀️</p></div>
You could use display: flex for this:
.example{
display: flex;
background-color: #808080;
flex-flow: column;
justify-content: space-between;
height: 200px; /* for example purpose */
}
.top {
background-color: #cecece;
}
.bottom {
background-color: #cecece;
}
<div class="example">
<div class="top">
<span>This is the title</span>
</div>
<div class="bottom">
<span>This is the title</span>
</div>
</div>
I have an absolutely positioned div containing two divs side by side. When the left div is removed, the right one appears to jump to the left.
Before:
..AABB...
After:
..BB.....
After (desired behaviour):
....BB...
demo:
https://jsfiddle.net/rb3erkqc/10/
To fix this, I want to be able to absolutely position the right div and have the left one follow along.
The easiest way I can imagine is setting the left margin of A to the negative size of A, but I don't think that's possible, because the size of A is unknown and can change.
without knowing more, here is my shot in the dark
$('.container').click(function(){
$('.container div:first-child').remove();
$('.container div:nth-child(1)').remove();
});
.container {
display: flex;
align-items: center;
justify-content: center;
width: 100%;
}
.container > * {
position: relative;
display: inline-block;
border: 1px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div>
<p>Div A</p>
</div>
<div>
<p>Div A</p>
</div>
<div>
<p>Div B</p>
</div>
<div>
<p>Div B</p>
</div>
</div>
Hope this helps
Edit after code snippet was provided:
.abspos {
margin-top: 10%;
display: flex;
align-items: center;
justify-content: center;
flex-direction: row;
width: 100%;
}
.abspos>*:not(input) {
position: relative;
display: inline-block;
border: 1px solid red;
}
.t {
background-color: red;
}
.e {
background-color: yellow;
}
/* Just a hack to be able to toggle asd*/
input {
position: absolute;
top: 0;
margin: 0px auto;
}
input:checked+.t {
display: none;
}
<div class=abspos>
<input type=checkbox>
<div class=t>asd</div>
<div class=e>fasd</div>
</div>
Update 2 with minor changes:
changed display:none to visibility:hidden
this removes the element from the DOM allowing you to manipulate the <div class='.e'></div>
see fiddle below
.abspos {
margin-top: 10%;
display: flex;
align-items: center;
justify-content: center;
flex-direction: row;
width: 100%;
}
.abspos > *:not(input){
position: relative;
display: inline-block;
border: 1px solid red;
}
.t {
background-color: red;
}
.e {
background-color: yellow;
}
/* Just a hack to be able to toggle asd*/
input {
position: absolute;
top: 0;
margin: 0px auto;
}
input:checked + .t {
visibility:hidden; /*remove element from dom to manipulate remaining div*/
}
<div class=abspos>
<input type=checkbox>
<div class=t>asd</div>
<div class=e>fasd</div>
</div>
I found that one can use absolute positioning to make not affect the parent box and then use a css transform to move based on the object's size.
I also added centering for the element outside the box to demonstrate the generality of this approach. Some overflow handling may be necessary in cases where the right side is smaller.
.abspos {
position: absolute;
left: 106px;
top: 57px;
display: flex;
flex-direction: row;
}
.t {
position: absolute;
transform: translate(-100%);
top: 0px;
bottom: 0px;
display: flex;
align-items: center;
}
.t > div {
background-color: red;
}
.e {
background-color: yellow;
}
/* Just a hack to be able to toggle asd*/
input {
position: absolute;
top: -30px;
}
input:checked + .t {
display: none;
}
<body>
<div class=abspos>
<input type=checkbox>
<div class=t><div>asd</div></div>
<div class=e>fasd<br>lova</div>
</div>
</body>
I know this is a common issue, and I've already asked a similar one.
But I could not find the solution this time.
I am trying to vertically center a text which can have different height in a DIV which can have different height.
And I am trying to solve this only with CSS, without touching the HTML.
Example : http://jsfiddle.net/F8TtE/
<div id="test">
<div id="sumup">
<h1 class="titre">Title</h1>
<div id="date">hello guys</div>
</div>
</div>
My goal is to have the text centered vertically and horizontally whatever its size.
Here it is :
#test {
text-align:center;
}
#test::before {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
}
#sumup {
display: inline-block;
vertical-align: middle;
}
#test {
height : 180px;
text-align:center;
background: yellow;
}
#sumup {
background-color: #123456;
}
#test:before {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
}
#sumup {
display: inline-block;
vertical-align: middle;
}
<div id="test">
<div id="sumup">
<h1 class="titre">Title</h1>
<div id="date">hello guys</div>
</div>
</div>
EDIT: It's now 2015 and thankfully the web changes. Assuming you don't support obsolete browsers, it's usually simpler and cleaner to vertically center elements with the Flex model.
#test {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
#test {
height : 180px;
background: yellow;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
#sumup {
background-color: #123456;
}
<div id="test">
<div id="sumup">
<h1 class="titre">Title</h1>
<div id="date">hello guys</div>
</div>
</div>
Here's another way using display:table-cell since I don't quite understand how dystroy's answer works.
#test {
width:100%;
height : 400px;
display:table;
}
#sumup {
width : 100%;
height : 100%;
background-color: #123456;
display:table-cell;
vertical-align:middle;
}
http://jsfiddle.net/F8TtE/3/
CSS with display solution:
#test {
display:table;
}
#sumup {
display:table-cell;
}
The Demo http://jsfiddle.net/F8TtE/7/
Over the years I've tried lot of different techniques, but I still can't find a way, where I could create a footer, that is dynamically changes height, depending on the content and if the site have less content, the footer goes down to the bottom of the page.
I've tried to play with the ::after pseudo element:
footer::after {
content: '';
position: absolute;
background: red; //just test
width: 100%;
height: 99px;
}
And I found a way, where you can do this to look nice, but you need to set the height of the footer. But if you want a real responsive UI, you can not set the height of the footer :)
I hope anyone knows the secret, how to create a dynamic footer.
What you want is sticky footer with fluid height.
In older browsers you'll need some JavaScript.
In modern browser you can use css table display types:
<!DOCTYPE HTML>
<html>
<head>
<style type="text/css">
html, body {
height: 100%;
margin: 0pt;
}
.Frame {
display: table;
height: 100%;
width: 100%;
}
.Row {
display: table-row;
height: 1px;
}
.Row.Expand {
height: auto;
}
</style>
</head>
<body class="Frame">
<header class="Row"><h1>Catchy header</h1></header>
<section class="Row Expand"><h2>Awesome content</h2></section>
<footer class="Row"><h3>Sticky footer</h3></footer>
</body>
</html>
I took this example from:
http://pixelsvsbytes.com/blog/2011/09/sticky-css-footers-the-flexible-way/
EDIT: Now I see you want to expand the footer, not the content. I'm leaving the original for bypassers with sticky footer question as it is more common version.
Try this version instead:
<!DOCTYPE HTML>
<html>
<head>
<style type="text/css">
html, body {
height: 100%;
margin: 0pt;
}
.Frame {
display: table;
height: 100%;
width: 100%;
}
.Row {
display: table-row;
height: 1px;
}
.Row.Expand {
height: auto;
}
</style>
</head>
<body class="Frame">
<header class="Row"><h1>Catchy header</h1></header>
<!-- these two line differ from the previous example -->
<section class="Row"><h2>Awesome content</h2></section>
<footer class="Row Expand"><h3>Sticky footer</h3></footer>
</body>
</html>
This can easily be done with CSS2.1 (but not in IE7-). The main trick is the following:
.container {
display: table;
height: 100%;
width: 100% /* mimics `display: block` */
}
.footer {
display: table-footer-group;
}
/* to add padding use the below or wrapper/inner wrapping element combo. */
.footer:before, .footer:after {
padding: 1em;
content: '';
}
In modern browsers, it can also be done with FlexBox, which is probably more appropriate theoretically, but less supported yet.
It is sticky footer, please try this:
HTML
<div id="container">
<div id="header">Header Section</div>
<div id="page" class="clearfix">
<div id="left">Left Sidebar</div>
<div id="content">Main content</div>
<div id="right">Right sidebar</div>
</div>
<div id="footer">Footer Section</div>
</div>
CSS
/*sticky footer style*/
html,body {
margin: 0;
padding:0;
height: 100%;
}
#container {
min-height:100%;
height: auto !important;
height: 100%; /*for ie6*/
position: relative;
}
#header {
background: #ff0;
padding: 10px;
}
#page {
width: 960px;
margin: 0 auto;
padding-bottom: 60px;/* equal to the footer's height*/
}
#footer {
position: absolute;
bottom: 0;
width: 100%;
height: 60px;/*The footer' height*/
background: #6cf;
clear:both;
}
/*=======主体内容部分=======*/
#left {
width: 220px;
float: left;
margin-right: 20px;
background: lime;
}
#content {
background: orange;
float: left;
width: 480px;
margin-right: 20px;
}
#right{
background: green;
float: right;
width: 220px;
}
Pleas view the demo. Other methods, you can click here.
And you can use the CSS3 flexbox Module, Like this:
HTML
<header class="Row"><h1>Catchy header</h1></header>
<section class="Row Expand"><h2>Awesome content</h2></section>
<footer class="Row"><h3>Sticky footer</h3></footer>
CSS
header,section,footer {
display: block;
}
html,body{
margin: 0;
padding: 0;
height: 100%;
}
body {
width: 100%;
display: -moz-box;
display: -webkit-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-moz-box-orient: vertical;
-webkit-box-orient: vertical;
-moz-box-direction: normal;
-webkit-box-direction: normal;
-ms-flex-direction: column;
-webkit-flex-direction: column;
flex-direction: column;
}
section {
-moz-box-flex:1;
-webkit-box-flex:1;
-ms-flex:1;
-webkit-flex:1;
flex:1;
background: hsla(250,20%,30%,0.9);
}
header {
background: orange;
}
footer {
background: green;
}
Please view the demo. About the css3 flexbox module.
Consider the following html:
<div class="image">
<img src="sample.png"/>
<div class="text">
Text of variable length
</div>
</div>
Where:
.image {
position:relative;
text-align:center; // doesn't work as desired :(
}
.text {
position:absolute;
top:30px;
}
Can you please tell, if there is a way to horizontally align the text in the center of the .image div? I can't use the left property, since the length of the text is unknown and the text-align property doesn't work for the .text div :(
Thank you.
Try the following CSS:
.image {
position:relative;
}
.text {
left: 0;
position:absolute;
text-align:center;
top: 30px;
width: 100%
}
Try this:
.image {
position:relative;
}
.text {
position: absolute;
top: 0;
left: 0;
width:100%;
height:100%;
display: flex;
justify-content: center;
flex-direction: column;
align-items: center;
}
First you need to float the .image element, in order to 'collapse' its block-level descendants to the width of the element, and then use text-align on the .text element:
.image {
float: left;
}
.text {
text-align: center;
}
JS Fiddle demo.
Or you could simply give it a display: inline-block, which will allow it to have its block-level contents contained, and remain in the document flow:
.image {
display: inline-block;
}
.text {
text-align: center;
}
JS Fiddle demo.
Can you not use the image as the background image of the 'image' div using background-image: url('/yourUrl/'); and then rather than have your text in its own div simply place it in the image div and then apply text-align: center;
Using Flex is a more"flexible" option :-)
#countdown {
font-size: 100px;
z-index: 10;
}
#countdown-div {
width: 100vw;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
#bg-image {
width: 100vw;
height: 100vh;
position: absolute;
top: 0px;
left: 0px;
}
<div>
<div id="countdown-div">
<h1 id="countdown">10</h1>
</div>
<img id="bg-image" src="https://lh3.googleusercontent.com/proxy/PKaxc9qp52MkavbkMnnbu0AZAGcqxJCoa7wIgtCmOr2e1x9ngdsteITX6x4JqDDFOhhdZmF79Ac5yevMGaUmcSaFnFYsHQtLPxHg56X_8PfP1fNkxMNXYd3EzhuCb3eJ2qQA">
</div>