Set content width to automatically be to equal width - css

The question title could be more clear, but given some text, if the browser width lets it fit on one line, then I want it to be centered, otherwise, I don't want just one word wrapping to the next line, how do I make sure that it break the lines to be about equal lengths? If there's a CSS only way that's be best, otherwise, I'd like to do it in Angular 2...
i.e:
Desktop:
[Screen Edge]...."Some interesting text is here"....[Screen Edge]
Phone:
[Screen Edge]....."Some interesting"......[Screen Edge]
[Screen Edge]......."text is here"........[Screen Edge]
NOT
Phone:
[Screen Edge]."Some interesting text is"..[Screen Edge]
[Screen Edge]..........."here"............[Screen Edge]

Here, I hope it helps. You must set your container to text-align: center to center the texts. If you want it fluid, you set its width to 100%. You may also add padding so when you switch to mobile, it will not looked populated.
body {
text-align: center;
}
body > div {
width: 100%;
height: auto;
text-align: center;
}
body > div > div {
margin-bottom: 20px;
text-align: center;
border: 1px solid black;
margin: auto;
padding: 40px;
}
.web {
width: 400px;
text-align: center;
height: 50px;
}
.mobile {
width: 100px;
height: 80px;
}
.container {
width: 100%;
height: auto;
text-align: center;
}
<div>
<div class="web">
<div class="container">
<span>Some interesting text here</span>
</div>
</div>
<div class="mobile">
<div class="container">
<span>Some interesting text here</span>
</div>
</div>
</div>

Option 1:
use flexbox:
with flex-wrap: wrap; and wrap your text with div so it will break at the point you want when you need to wrap.
.centered {
text-align: center;
margin: 10vh 15vw;
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: center;
}
<div class="centered">
<span>Some interesting text is here </span><span>Some interesting text is here </span>
</div>
Option 2:
use text-align: center; to center text
use margin: 5px 80px; to create basic margin
use non-breaking space to make sure your text does not break.
(or you can wrap the text with span with a css rule set white-space: nowrap)
.centered {
margin-top: 100px;
text-align: center;
}
.centered > div {
text-align: center;
margin: 10vh 15vw;
}
<div class="centered"><div>Some interesting text is here Some interesting text is here</div></div>

Related

How to make 2 div elements centered horizontally and vertically in CSS?

I am trying to center 2 div elements vertically and horizontally and this should work well and it usually does, but not here..
HTML
<div class="contact">
<h3>WHAT ARE YOU WAITING FOR?</h3>
<div class="cta-btn">
<h3>CONTACT US</h3>
</div>
</div>
CSS
.contact {
font-size: 0.8em;
display: flex;
justify-content: center;
align-items: center;
text-align: center;
.cta-btn {
width: 8em;
padding: 0.2em;
border-radius: 2em;
}
}
There are (at least) two ways to achieve what you're looking for.
One uses flexboxes. Here you need an initial container that consumes your full window and into this container you put another container that is positioned in the center and into that you can put your content (code is shortened to minimum):
.box {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
display: flex;
align-items: center;
justify-content: center;
}
.flexcontainer {}
<div class="box">
<div class="flexcontainer">
<span class="left">Left Content</span>
<span class="right">Right Content</span>
</div>
</div>
[edit]: Obviously, the initial container (.box here) could also be a header or something not spanning the full windows height. In this case, you would not have to position it absolute and define the top, bottom, left and right positions but could work with width and height or use a dynamic size.[/edit]
The other method is based on the assumption that you know the height of your content container:
.container {
width: 500px;
height: 200px;
margin: calc(50vh - 101px) auto;
/* calc(50 percent of window height minus half the size of the container including borders!) */
border: 1px solid #000;
}
<div class="container">Content</div>
Correct this code, adding follow properties in css:
<div class="contact">
<h3>WHAT ARE YOU WAITING FOR?</h3>
<div class="cta-btn">
<h3>CONTACT US</h3>
</div>
</div>
.contact {
display: flex;
justify-content: space-between;
align-items: center;
h3 {
flex-shrink: 0;
}
.cta-btn {
flex-grow: 1;
display: flex;
justify-content: center;
}
}
.contact{
display:flex;
align:center;
}
.cta-btn{
display:flex;
align:center;
}
<div class="contact">
<h3>WHAT ARE YOU WAITING FOR?</h3>
<div class="cta-btn">
<h3>CONTACT US</h3>
</div>
</div>

Align text of different size to the bottom

I have two divs with text of different sizes, that I want to align to the bottom.
They do successfully get aligned to the bottom of their parent, but they're not aligned evenly to each other.
Is this solvable?
.container {
display: flex;
height: 50px;
background: pink;
}
.large, .small {
align-self: flex-end;
margin-right: 5px;
}
.large {
font-size: 30px;
}
.small {
font-size: 15px;
}
<div class="container">
<div class="large">Large</div>
<div class="small">Small</div>
</div>
One way would be to put both text DIVs into another wrapper (.inner_container in my snippet below) which gets the settings the texts previously had in order to align to the bottom, and apply display: inline-block; to the text DIVs: inline-blocks align to each other by their baseline by default, which is what you want if I understand correctly:
.container {
display: flex;
height: 50px;
background: pink;
}
.inner_container {
align-self: flex-end;
}
.large,
.small {
display: inline-block;
margin-right: 5px;
}
.large {
font-size: 30px;
}
.small {
font-size: 15px;
}
<div class="container">
<div class="inner_container">
<div class="large">Large</div>
<div class="small">Small</div>
</div>
</div>
As per #Johannes answer it's a good idea to wrap both text divs in another container. But you don't need an align-self: flex-end; declaration for the .inner-container, just add align-items: flex-end; to the parent div. That way, you get one CSS rule less.
display: inline;, like display: inline-block, makes items align themselves by their baselines (based on the item with the biggest height, in this case the .large text) so you could change the <div class="large"> and the <div class="small"> to spans instead of divs. Since the default display property of <span> is inline, you can then skip the display declaration of .large and .small:
.container {
display: flex;
height: 50px;
background: pink;
align-items: flex-end;
}
.large, .small {
margin-right: 5px;
}
.large {
font-size: 30px;
}
.small {
font-size: 15px;
}
<div class="container">
<div class="inner-container">
<span class="large">Large</span>
<span class="small">Small</span>
</div>
</div>

write a text on the side of a centered text [duplicate]

This question already has answers here:
Center one and right/left align other flexbox element
(11 answers)
Closed 5 years ago.
I want to add a text beside a centered text without moving the centered text.
Example: C is a centered text and s is a side text:
+++++
sC
+++++
ssC
+++++
sCCC
Is this possible with CSS?
Sure, use flexbloxes like this. This is gross, but at least it works.
.container {
display: flex;
justify-content: center;
align-items: stretch;
}
.container > * {
border: 1px solid #ccc;
padding: 6px 12px;
box-sizing: border-box;
}
.side {
flex: 1 1 50%;
display: flex;
flex-direction: row;
flex-wrap: wrap;
}
.side:nth-child(1) {
justify-content: flex-end;
}
.content {
width: 200px;
text-align: center;
}
<div class="container">
<div class="side"><p>Side text</p><p>Side text</p></div>
<p class="content">Content text</p>
<div class="side"></div>
</div>
It sounds like if you are trying to display text before or after the element in which case you may want to read over https://developer.mozilla.org/en-US/docs/Web/CSS/::before
See the example below.
.container {
width: 100%;
background: #eee;
}
.center-text {
display: table;
margin: auto;
}
.center-text:before {
content: "s";
font-size: smaller;
}
.center-text:after {
content: "s";
font-size: smaller;
}
<div class="container">
<div class="center-text">CCC</div>
</div>
Is this possible with CSS? Yes it is. If you're just exploring and playing around with CSS, you can do it by using :before or :after pseudo element. Set the positioning of the parent element to relative then set the pseudo element's position to absolute so you can control its positing inside the centered element div.
However it is a bad practice if you will use this in your work.
div.centered-element{
text-align: center;
width: 100%;
background-color: #f0f0f0;
position: relative;
}
div.centered-element:before{
content: "sss";
color: red;
position: absolute;
left: 30%;
}
<div class="centered-element">C</div>

Centering a H1 vertically with background image at 100%

I'm trying to get a H1 vertically center aligned within a div that has a background image.
I tried this method I found: https://jsfiddle.net/vdqdpyc0/12/
But found that the full height of the banner was only visible if I specifically added a px height to the div, or added padding to either element. This meant that when I resized, there was lots of white space above and below the banner. This wouldn't be an issue if the background was intended to repeat, but it isn't.
The end product needs to look like this.
html,
body {
height: 100%;
}
.outer-wrapper {
background: url("http://paulmason.name/media/demos/full-screen-background-image/background.jpg") no-repeat center center;
background-size: cover;
max-height: 360px;
height: 100%;
}
.inner-wrapper {
display: table;
width: 100%;
height: 100%;
}
.header-wrapper {
display: table-cell;
text-align: center;
vertical-align: middle;
}
h1 {
color: #fff;
}
<div class="outer-wrapper">
<div class="inner-wrapper">
<div class="header-wrapper">
<h1>
Vertically aligned text
</h1>
</div>
</div>
</div>
I could go through reducing the padding for various responsive viewpoints, but I figured there has to be a more streamlined way of going about it.
You can achieve this much more easily these days with flexbox, e.g.
html,
body {
height: 100%;
}
.outer-wrapper {
background: url("http://paulmason.name/media/demos/full-screen-background-image/background.jpg") no-repeat center center;
background-size: cover;
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
h1 {
color: #fff;
}
<div class="outer-wrapper">
<div class="inner-wrapper">
<div class="header-wrapper">
<h1>
Vertically aligned text
</h1>
</div>
</div>
</div>
Example: https://jsfiddle.net/0yxctLne/
You can use a flexbox for this. Here is a pretty simple guide to flexbox. For your issue, it should be enough to pass
display: flex;
flex-direction: column;
justify-content: center;
to the element surrounding the H1 tag, IF the surrounding element is the same height and width as the space you want the H1 tag to be centered in. This will define the main axis as vertical (read: column) and then center all content along the main axis. If you want, you can also add
align-items: center;
to have the text centered along the cross axis (as in horizontally).
Do you need something like this? Flexbox is a nice way to do:
.header-wrapper {
background: url("http://paulmason.name/media/demos/full-screen-background-image/background.jpg") no-repeat center center;
background-size: cover;
text-align: center;
display: flex;
align-items: center;
justify-content: center;
}
h1 {
color: #fff;
}
<div class="outer-wrapper">
<div class="inner-wrapper">
<div class="header-wrapper">
<h1>
Vertically aligned text
</h1>
</div>
</div>
</div>
Fiddle

how could I realize css vertical and horizontal centering?

How could I vertically center a child within a parent ?
And, the width and height of child and parent is fixed, but unknown.
How could I realize it?
<div class="parent">
<div class="child"></div>
</div>
My prefered technique for centering a box both vertically and horizontally requires two containers.
The outher container
should have display: table;
The inner container
should have display: table-cell;
should have vertical-align: middle;
should have text-align: center;
The content box
should have display: inline-block;
should re-adjust the horizontal text-alignment to eg. text-align: left; or text-align: right;, unless you want text to be centered
The elegance of this technique, is that you can add your content to the content box without worrying about its height or width!
Just add your content to the content box.
Demo
body {
margin : 0;
}
.outer-container {
position : absolute;
display: table;
width: 100%; /* This could be ANY width */
height: 100%; /* This could be ANY height */
background: #ccc;
}
.inner-container {
display: table-cell;
vertical-align: middle;
text-align: center;
}
.centered-content {
display: inline-block;
text-align: left;
background: #fff;
padding : 20px;
border : 1px solid #000;
}
<div class="outer-container">
<div class="inner-container">
<div class="centered-content">
<p>You can put anything here</p>
<p>Yes, really anything!</p>
</div>
</div>
</div>
See also this Fiddle!
in css vertical-align:middle is used to align a child vertically centre. But this property is applied to only those elements which havedisplay:inline-block or display:table-cell. So accordingly try to apply display property and you will get vertically centre position of your elements.
You can center things through:
margin: 0 auto;
Try this code
body {
margin: 0;
padding:0;
}
.div1 {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
}
.div2 {
width: 50vw;
height: 50vh;
background: #999;
}

Resources