I want to place a short texte at the beginning of a centered headline.
My current solution is to have a container with display inline arround the headline
and placing the text with text right and display block.
This works pretty good until the I have a long headline.
The HTML:
<div class="article">
<div class="headline-container">
<span class="headline-companion">FooBar</span>
<h3>Sample Headline</h3>
</div>
</div>
THE SCSS:
.article{
display:block;
width: 700px;
margin: 50px auto;
padding: 20px;
text-align: center;
}
.headline-container{
display: inline-block;
margin: 0 auto;
text-align: center;
position: relative;
h3{
font-size: 40px;
margin: 0;
padding: 0;
display: inline;
}
.headline-companion{
margin: 0;
padding: 0;
display: block;
text-align: left;
}
}
http://codepen.io/anon/pen/PwNJGy
Are there any solutions where I don't have to position the text with absolute?
Clarification:
I want that the Short text beggins always at the first letter of the headline no matter how long the centered headline is.
Something like what (I think) you want to do can be accomplished if you wrap your <h3> tag in another element and set a max-width value, and by specifying word-wrap.
This allows you to change the width of the parent divs, or length of the header text without having very negative effects on the overall formatting:
[Edit]: In addition, another element needs to be added if you want to have the headline-companion and headline centered with respect to article but with left justified text... in this case sub-headline-container.
.article {
display: block;
width: 800px;
margin: 50px auto;
padding: 20px;
text-align: center;
}
.headline-container {
display: inline-block;
margin: 0 auto;
text-align: center;
position: relative;
}
.headline-wrap {
word-wrap: break-word;
max-width: 800px;
text-align: left;
}
.headline-companion {
margin: 0;
padding: 0;
display: block;
text-align: left;
}
<div class="article">
<div class="headline-container">
<div class="sub-headline-container">
<span class="headline-companion">FooBar</span>
<div class="headline-wrap">
<h3>
Sample Headline
</h3>
</div>
</div>
</div>
</div>
<div class="article">
<div class="headline-container">
<div class="sub-headline-container">
<span class="headline-companion">FooBar</span>
<div class="headline-wrap">
<h3>
SUUUUUUPPPPERRRRRRRR BIG HEADLINE!!! ------------------------------- --------------
</h3>
</div>
</div>
</div>
</div>
Related
I have a div like this, where I want the text and image to be horizontally aligned, where the space from the left of the div and right of the div are equal. Here's the current code I have, though it is definitely not optimal:
.window{
position:absolute;
width: 400px;
height: 300px;
background-color:#424242;
}
.content{
padding-top:50px;
width: 50%;
position:relative;
vertical-align: top;
margin: 0 auto;
display:flex;
}
img {
height: 32px;
width: 32px;
min-width: 32px;
min-height: 32px;
position: relative;
float: left;
}
.textcontent{
margin-top: auto;
margin-bottom: auto;
margin-left: 16px;
display: block;
line-height: 182%;
}
.text{
font-size: 14px;
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div class="window">
<div class="content">
<img src="https://cdn4.iconfinder.com/data/icons/family-and-home-collection/110/Icon__grandfather-32.png" />
<div class="textcontent">
<div class="text"> Some Centered Text. </div>
<div class="text"> Some Other Text. </div>
</div>
</div>
</div>
</body>
</html>
The issue is that the "window" could be any size, the image could be a fair amount of sizes, and the items in text content could be longer and larger font-size. Additionally, the second line of text is not always visible.
This is a problem, since if the text is very long, the 50% width is very small, and the text wraps several times when there is plenty of room.
.window{
position:absolute;
width: 500px;
height: 300px;
background-color:#424242;
}
.content{
padding-top:50px;
width: 50%;
position:relative;
vertical-align: top;
margin: 0 auto;
display:flex;
}
img {
height: 32px;
width: 32px;
min-width: 32px;
min-height: 32px;
position: relative;
float: left;
}
.texcontentt{
margin-top: auto;
margin-bottom: auto;
margin-left: 16px;
display: block;
line-height: 182%;
}
.text{
font-size: 14px;
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div class="window">
<div class="content">
<img src="https://cdn4.iconfinder.com/data/icons/family-and-home-collection/110/Icon__grandfather-32.png" />
<div class="textcontent">
<div class="text"> Some text that is very very long and wraps. </div>
<div class="text"> This text is also very long and also wraps. </div>
</div>
</div>
</div>
</body>
</html>
I could counter this by making the width % in the .content rule larger, but then for small content in a large window, it will not be centered anymore.
Long story short, is there a better way to get the text centering I want for different sizes, without having to have it be very narrow?
Thank you!
To align your text and image horizontally inside div, you could use display:flex and justify-content: center. Justify-content:center will align the children at the center of the container.
.content {
width: 400px;
display: flex;
justify-content: center;
align-items: center; /* Only if you want it vertically center-aligned as well */
background: #ccc;
padding: 40px;
}
<div class="window">
<div class="content">
<img src="https://cdn4.iconfinder.com/data/icons/family-and-home-collection/110/Icon__grandfather-32.png" />
<div class="textcontent">
<div class="text"> Some text that is very very long and wraps. </div>
<div class="text"> This text is also very long and also wraps. </div>
</div>
</div>
</div>
View in CodePen
Hope this helps!
I know this has been discussed in length, but I cannot seem to find an answer to solve this problem. This is a simple example to illustrate my issue. I have two children div elements inside a parent div and I want them to be horizontally centered inside the parent div. Here is the fiddle:
JSFiddle
#container {
position: relative;
float: none;
margin: 0 auto;
border: solid blue 1px;
width: 100%;
}
.tile {
width: 20em;
height: 40em;
border:solid black 1px;
display: inline-block;
margin: 1.5em auto;
}
<div id="container">
<div class="tile">
<!--
When you remove this comment, the div shifts down and I do not understand what is causing that.
<h3>This Title Moves the div down. Why?</h3>-->
</div>
<div class="tile"></div>
</div>
Now is there a simple solution that I am missing? Also, I have a secondary question about the h3 tag as well. When the comment around the h3 tag is removed, the first div shifts down. What about the h3 tag is causing the div to shift down and how do I prevent it from happening?
Thanks for your answers and your help, and I apologize for a potential repeat question.
Add below code to #container:
display: flex;
justify-content: center;
align-items: center;
Live Snippet
#container {
position: relative;
float: none;
margin: 0 auto;
border: solid blue 1px;
width: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.tile {
width: 20em;
height: 40em;
border:solid black 1px;
display: inline-block;
margin: 1.5em 0;
}
<div id="container">
<div class="tile">
<!--
When you remove this comment, the div shifts down and I do not understand what is causing that.
<h3>This Title Moves the div down. Why?</h3>-->
</div>
<div class="tile"></div>
</div>
You can add: .title { display: block; }
#container {
position: relative;
float: none;
margin: 0 auto;
border: solid blue 1px;
width: 100%;
}
.tile {
border: 1px solid black;
display: block;
height: 40em;
margin: 1.5em auto;
width: 20em;
text-align:justify;
padding:7px;
}
h3 {
margin: 0;
padding: 0;
}
<div id="container">
<div class="tile">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
</div>
<div class="tile">
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.
</div>
</div>
you can add margin:auto to .tile and text-align:center to #container
#container {
position: relative;
float: none;
margin: 0 auto;
border: solid blue 1px;
width: 100%;
text-align: center;
}
.tile {
width: 20em;
height: 40em;
border:solid black 1px;
display: inline-block;
margin: auto;
}
<div id="container">
<div class="tile">
<h3>This Title Moves the div down. Why?</h3>
</div>
<div class="tile"></div>
</div>
When you use display:inline-block by default was vertical-align: bottom; so that set css for .tile to vertical-align: middle; and text-align:center to #container
.tile {
width: 20em;
height: 40em;
border:solid black 1px;
display: inline-block;
margin: 1.5em auto;
vertical-align: middle;
}
Working Code: https://jsfiddle.net/e8on1cko/5/
` `
#container {
padding:25%;
text-align:center;
background:#e7e7e7;
}
.tile{
background:white;
display:inline-block;
padding:20%;
}
<div id="container">
<div class="tile">
<h3>This Title Moves the div down. Why?</h3>
</div>
<div class="tile"></div>
</div>
I have 3 div boxes which were perfectly aligned and centered, and then when I added text inside the divs, the parent div would stretch and resize to what was inside. I tried using resize: none on the child and parent elements, but that didn't do anything. Also tried using position:absolute; and relative.
HTML
<div class="boxes-parent">
<div class="left-box">
<div class="blue-boxes">
<h1 class="blue-box-header-text">About</h1>
<p class="blue-box-text">I used to be better at CSS and HTML. Sorry!</p>
</div>
</div>
<div class="center-box">
<div class="blue-boxes">
<h1 class="blue-box-header-text">Info</h1>
<p class="blue-box-text">Info info info.</p>
</div>
</div>
<div class="right-box">
<div class="blue-boxes">
<h1 class="blue-box-header-text">Contact</h1>
<p class="blue-box-text">Phone: (888) 888-8888</p>
</div>
</div>
</div>
CSS
.boxes-parent {
margin: 0px auto;
text-align: center;
}
.left-box, .center-box, .right-box {
padding-top: 10px;
padding-left: 2%;
padding-right: 2%;
display: inline-block;
position: relative;
min-width: 25%;
}
.blue-boxes {
background-color: #3498db;
height: 250px;
position: absolute;
resize: none;
}
.blue-box-header-text {
padding-top: 20px;
font-size: 26px;
color: #fff;
}
.blue-box-text {
resize: none;
max-width: 500px;
position: relative;
display: inline;
}
Result: https://jsfiddle.net/0wvyaqbo/
Also, my questions sometimes get downvoted. Have any advice on how I can better format this question? Trying to make it applicable for others. Thanks for all the help!
There are a couple things going on:
First, you have your .right, .center, .left divs set to inline-block. By default, they will always be the width of the content, because of inline-block. You need to set them to block. Also, .blue-boxes should be set to relative positioning, not absolute.
Here's updated CSS:
.boxes-parent {
margin: 0px auto;
text-align: center;
}
.left-box, .center-box, .right-box {
padding-top: 10px;
padding-left: 2%;
padding-right: 2%;
display: block;
position: relative;
width: 25%;
float:left;
}
.blue-boxes {
background-color: #3498db;
height: 250px;
position: relative;
}
.blue-box-header-text {
padding-top: 20px;
font-size: 26px;
color: #fff;
}
.blue-box-text {
resize: none;
max-width: 500px;
position: relative;
display: inline;
}
And the updated fiddle:
https://jsfiddle.net/0wvyaqbo/1/
Another way to do this would be to use flexbox. Simply add display: flex and justify-content: center to .boxes-parent and change min-width to simply width for .left-box, .center-box, and .right-box. Resulting code looks like this:
.boxes-parent {
margin: 0 auto;
text-align: center;
display: flex;
justify-content: center;
}
.left-box,
.center-box,
.right-box {
width: 25%;
padding: 10px 2% 0;
}
.blue-boxes {
background-color: #3498db;
height: 250px;
}
.blue-box-header-text {
padding-top: 20px;
font-size: 26px;
color: #fff;
}
<div class="boxes-parent">
<div class="left-box">
<div class="blue-boxes">
<h1 class="blue-box-header-text">About</h1>
<p class="blue-box-text">I used to be better at CSS and HTML. Sorry!</p>
</div>
</div>
<div class="center-box">
<div class="blue-boxes">
<h1 class="blue-box-header-text">Info</h1>
<p class="blue-box-text">Info info info.</p>
</div>
</div>
<div class="right-box">
<div class="blue-boxes">
<h1 class="blue-box-header-text">Contact</h1>
<p class="blue-box-text">Phone: (888) 888-8888</p>
</div>
</div>
</div>
Alternatively, you could use flex: 1 1 25% for .left-box, .center-box, and .right-box. This forces them to grow and shrink with each other.
.boxes-parent {
margin: 0 auto;
text-align: center;
display: flex;
justify-content: center;
}
.left-box, .center-box, .right-box {
flex: 1 1 25%;
padding: 10px 2% 0;
}
.blue-boxes {
background-color: #3498db;
height: 250px;
}
.blue-box-header-text {
padding-top: 20px;
font-size: 26px;
color: #fff;
}
<div class="boxes-parent">
<div class="left-box">
<div class="blue-boxes">
<h1 class="blue-box-header-text">About</h1>
<p class="blue-box-text">I used to be better at CSS and HTML. Sorry!</p>
</div>
</div>
<div class="center-box">
<div class="blue-boxes">
<h1 class="blue-box-header-text">Info</h1>
<p class="blue-box-text">Info info info.</p>
</div>
</div>
<div class="right-box">
<div class="blue-boxes">
<h1 class="blue-box-header-text">Contact</h1>
<p class="blue-box-text">Phone: (888) 888-8888</p>
</div>
</div>
</div>
For more info on flexbox, css-tricks has a good reference sheet. Here's the support tables as well.
Parent elements likes divs are flexible by default and will resize to contain whatever you put in them (height) or whatever their parent element(s)/the browser is doing (width) unless you specify their dimensions. Use CSS width and height or max-width and max-height on the parent to limit its growth.
Generally, a flexible parent is preferred (see: responsive design), as some users may choose to modify the font your site is using, increase the font size to improve visibility, or view the site from a different device, and these actions can change the relative size of your content.
More info: http://www.w3schools.com/cssref/pr_dim_height.asp
You used min-width, and expected adding content wouldn't cause them to grow? Just use width.
Setting .blue-boxes { position: absolute; } caused those to shrink to the size of the content. You could add width: 100%;.
Since using absolute positioning can mess up layout, a solution that avoids it would be nice. When you remove the absolute positioning, the blue boxes are no longer aligned vertically. Set vertical-align: top; to fix that.
.boxes-parent {
margin: 0px auto;
text-align: center;
}
.left-box, .center-box, .right-box {
padding-top: 10px;
padding-left: 2%;
padding-right: 2%;
display: inline-block;
width: 25%;
vertical-align: top;
}
.blue-boxes {
background-color: #3498db;
height: 250px;
}
.blue-box-header-text {
padding-top: 20px;
font-size: 26px;
color: #fff;
}
.blue-box-text {
display: inline;
}
<div class="boxes-parent">
<div class="left-box">
<div class="blue-boxes">
<h1 class="blue-box-header-text">About</h1>
<p class="blue-box-text">I used to be better at CSS and HTML. Sorry!</p>
</div>
</div>
<div class="center-box">
<div class="blue-boxes">
<h1 class="blue-box-header-text">Info</h1>
<p class="blue-box-text">Info info info.</p>
</div>
</div>
<div class="right-box">
<div class="blue-boxes">
<h1 class="blue-box-header-text">Contact</h1>
<p class="blue-box-text">Phone: (888) 888-8888</p>
</div>
</div>
</div>
I've searched it at online and found some solution. But, nothing works at my project. At most of the solution, I've found:
<div class="a">
<div class="b">
Unknown stuff to be centered.
</div>
</div>
.a {
display: table;
width: 100%;
}
.b {
display: table-cell;
text-align: center;
vertical-align: middle;
}
By applying this technique, I've tried to build something like this: http://jsfiddle.net/L2GZx/1/
The text of left column only needed to be aligned middle vertically. But, it's not working with that technique:
<div class="row">
<div class="left">
<p>Sample Text</p>
</div>
<div class="right">
<p>Text</p>
<p>Input Element</p>
<p>Table</p>
<p>Image</p>
</div>
</div>
<div class="row">
<div class="left">
<p>Sample Text Sample Text Sample Text Sample Text Sample Text </p>
</div>
<div class="right">
<p>Text</p>
<p>Input Element</p>
<p>Table</p>
<p>Image</p>
</div>
</div>
<div class="row">
<div class="left">
<p>Sample Text</p>
</div>
<div class="right">
<p>Text</p>
<p>Input Element</p>
<p>Table</p>
<p>Image</p>
</div>
</div>
CSS:
.row {
width: 100%;
background: #ccc;
border-bottom: 1px solid #999;
display: table;
}
.left {
float: left;
width: 40%;
padding: 20px;
display: table-cell;
vertical-align: middle;
}
.right {
float: right;
background: #fff;
width: 40%;
padding: 20px;
}
How can I make the text of left-column aligned middle vertically? Note: I can't use any fixed height as content of each row will be different
Remove the floats. Floated elements can not also be displayed as table-cells. See updated Fiddle.
.row {
width: 100%;
background: #ccc;
border-bottom: 1px solid #999;
display: table;
}
.left {
width: 40%;
padding: 20px;
display: table-cell;
vertical-align: middle;
}
.right {
display: table-cell;
background: #fff;
width: 40%;
padding: 20px;
}
.left {
width: 40%;
padding: 20px;
display: table-cell;
vertical-align: middle;
}
removing" float:left " from .left style solves that issue, but using table and div together is not that good.Working Example
An alternative that I prefer in a situation like this is:
To not use display: table-cell, but rather use display:
inline-block.
To then use vertical-align: middle on the element.
Sample (revised) markup / css:
<div class="row">
<div class="left">
<p>Sample Text</p>
</div>
<div class="right">
<p>Text</p>
<p>Input Element</p>
<p>Table</p>
<p>Image</p>
</div>
</div>
CSS:
.row {
width: 100%;
background: #ccc;
border-bottom: 1px solid #999;
}
.row > div {
display: inline-block;
/* below 2 lines are IE7 hack to make inline-block work */
zoom: 1;
*display: inline;
/* below is consolidated css for both left / right divs */
width: 40%;
padding: 20px;
}
.left {
vertical-align: middle; /* or top or bottom */
}
.right {
background: #fff;
vertical-align: top; /* or middle or bottom */
}
All you have to do is to add a line-height to the left column and it will be automatically aligned (without vertical-align so you can remove it).
Here it is:
.left {
float: left;
width: 40%;
padding: 20px;
display: table-cell;
line-height:150px;
}
And here is your updated FIDDLE
Using your first example, try something like this. I'll explain how it works in the CSS.
<div class="a">
<div class="b">
Unknown stuff to be centered.
</div>
</div>
.a {
width: 100%;
position: relative; /* We want our parent div to be the basis of our absolute positioned child div */
/* You can set your height here to whatever you want */
}
.b {
position: absolute;
width: 100%; /* Set to be the full width, so that our text is aligned centered */
text-align: center;
top: 50%; /* Positions the top of the div at 50% of the parent height */
left: 0; /* Assures that the child div will be left-most aligned */
margin-top: -.5em; /* Moves the top of our div up by half of the set font size */
height: 1em; /* Sets our height to the height of the desired font */
}
Here is the JSFiddle link to see a live example: http://jsfiddle.net/L2GZx/20/
This is one of the best solutions to absolutely center text inside of a webpage. It does have it's limitations however seeing how it won't react to other elements inside the same parent and it also has to have a set height. So multiline text will have it's shortcomings with this method.
I hope this helps!
For some reason, iE8 and IE7 are not behaving like other browsers, and the relative position element background image, doesn't appear.
Any suggestions please?
The HTML
<div id="container1">
<div class="main-column">
<h2>Hello tittle 1</h2>
<div class="text-column">
<p>I'm on column 1 and I like it</p>
<p>I'm on column 1 as well</p>
</div>
</div>
<div class="main-column">
<h2>Hello tittle 2</h2>
<div class="text-column">
<p>I'm on column 2 and I like it</p>
<p>I'm on column 2 as well</p>
</div>
</div>
<div class="main-column">
<h2>Hello tittle 3</h2>
<div class="text-column">
<p>I'm on column 3 and I like it</p>
<p>I'm on column 3 as well</p>
</div>
</div>
</div>
Notes:
a) Absolute position instead of relative will ruin the all layout.
b) I have a space on my background declaration so it's not a space issue.
Try I:
The same markup, but now with absolute position:
#container1 {
background-color: red;
text-align: center;
}
.main-column {
display: inline-block;
}
.main-column h2 {
width: 220px;
height: 235px;
padding-top: 110px;
position: absolute; /* <<-- Changed */
background: url('http://s24.postimg.org/ossqwb7hh/carica_Kairos.png') no-repeat center top;
margin: 0 auto;
}
.text-column {
width: 220px; /* <<-- Make it equal to the h2 */
height: 300px;
background-color: yellow;
margin: 120px auto 0 auto;
padding-top: 270px; /* <<-- Adjust */
}
.text-column p {
padding: 0 50px;
}
same issue. IE8 and IE7 not displaying images. :(
I'm not quite sure what you're looking to do, but since the h2 is a block item, it might not obey your height/width CSS. I would float or absolute position it.
EDIT: Here is a working workaround, please test it on youtr machine since I'm using IE8 emulator. JSFiddle
HTML
<div id="container1">
<div class="main-column">
<div class="h2"><span>Title 1</span><img src="http://s24.postimg.org/ossqwb7hh/carica_Kairos.png" width="230" height="243" /></div>
<div class="text-column">
<p>I'm on column 1 and I like it</p>
<p>I'm on column 1 as well</p>
</div>
</div><br>
<div class="main-column">
<div class="h2"><span>Title 1</span><img src="http://s24.postimg.org/ossqwb7hh/carica_Kairos.png" width="230" height="243" /></div>
<div class="text-column">
<p>I'm on column 1 and I like it</p>
<p>I'm on column 1 as well</p>
</div>
</div><br>
<div class="main-column">
<div class="h2"><span>Title 1</span><img src="http://s24.postimg.org/ossqwb7hh/carica_Kairos.png" width="230" height="243" /></div>
<div class="text-column">
<p>I'm on column 1 and I like it</p>
<p>I'm on column 1 as well</p>
</div> </div>
</div>
CSS
#container1 {
background-color: red;
text-align: center;
}
.main-column {
display: inline-block;
}
.main-column .h2 {
width: 220px;
height: 244px;
padding-top: 110px;
position: relative;
/*background: url('http://s24.postimg.org/ossqwb7hh/carica_Kairos.png') no-repeat center top;*/
}
.main-column .h2 span{
z-index: 2;
position: absolute;
text-align: center;
line-height: 243px;
margin-top: -110px;
left: 0;
width:100%;
font-weight: bold;
font-size: large;
}
.main-column .h2 img {
position: absolute;
top: 0;
left: -4px;
z-index: 0;
}
.text-column {
width: 220px;
height: 300px;
background-color: yellow;
margin: -223px auto 0 auto;
padding-top: 220px;
}
.text-column p {
padding: 0 50px;
}
OLD answer below
It seems you are using <h2> like a block when its a header element. You should try these tips for now:
Explicitly declare display: block; on that element.
Explicitly declare them all as position: relative;
Change the <h2> tag to a <div>