I have a header with a title and subtitle that are both center aligned using flexbox, and have a pale white background behind them. The background should only be as wide as the text itself, which works fine if both the title and subtitle are short enough, but starts breaking down if the subtitle is longer than one line.
.entry-title-wrap {
display: flex;
flex-direction: column;
align-items: center;
background-color: rgba(255,255,255,0.7);
padding: 6px 12px;
margin: auto;
border-radius: 2px;
h1 { font-size: 48px; }
h2 { font-size: 32px; }
}
<header class="entry-header">
<div class="entry-title-wrap">
<h1 class="entry-title">Nick Dourado</h1>
<h2 class="entry-subtitle">If the title is longer than one line so that it breaks over the line it makes the whole box way too big.</h2>
</div>
</header>
As soon as the subtitle is longer than the screen width and breaks onto more than one line, the h2 tag containing it expands to the full width of the container, which pushes the white background out to the edges as well. It also aligns the text to the left of the h2 unless I add text-align: center; event though it should be centered because of the flexbox styling.
How can I keep the white box constrained to the width of the text, and stop the h2 from expanding to the container width? If possible I would also love to force the subtitle to break in the middle of the text rather than one word at a time, so that all the lines are more or less the same length.
Here it is in CodePen. You can see how it should look if you make the subtitle sentence shorter.
This is the old version of the website it's for: http://theartsabstract.ca/post/151891941177/nick-dourado-on-halifaxs-improvised-music-scene
Aligning block elements in center with flex is not the same as using text-align center on headings. So go ahead and add the text-align:center on them.
I also made a few other changes.
removed image underlay
set image as background instead, and set size
to "cover"
added some padding so your white box doesn't hit edge
As far as the text wrapping at middle, I'm not sure of any way to do that besides setting a media query for when the text starts to wrap, and then updating the heading with a max-width in CSS. See CodePen below
http://codepen.io/anon/pen/kkdgjX?editors=1100
Add text-align: center; to .entry-title-wrap h2
http://codepen.io/anon/pen/dpEvbj?editors=1100#0
Not entirely sure what you're asking, but if you're just looking to center the headings, there is no need to use flex at all. Okay, there is a need to use flex for vertical alignment. Added it in. Here's an edited codepen: http://codepen.io/anon/pen/NRVdBQ?editors=1100#0
CSS
.entry-header {
display: flex;
justify-content: center;
height: 340px;
}
.entry-title-wrap {
background-color: rgba(255,255,255,0.7);
padding: 6px 12px;
margin: auto;
border-radius: 2px;
max-width: 20em;
}
h1 { text-align: center;
font-size: 48px; }
h2 { font-size: 32px;
text-align: justify;
text-align-last: center;;
}
div.post-thumb-banner {
display: flex;
align-items: center;
position: absolute;
height: 340px;
width: 100%;
overflow: hidden;
img {
width: 100%;
height: auto;
}
z-index: -10;
}
HTML
<div class="post-thumb-banner">
<img width="4795" height="3460" src="http://media2.fdncms.com/thecoast/imager/u/original/5056558/dsc_5435_1_.jpg" />
</div>
<header class="entry-header">
<div class="entry-title-wrap">
<h1 class="entry-title">Nick Dourado</h1>
<h2 class="entry-subtitle"><i>If the title is a lot longer than it should be it should just wrap properly as this.</i></h2>
</div>
</header>
Related
I have a page of fixed width. I don't want to change the width. I want to centre some text on the page so that it is at the centre of the entire page.
Say, if the page width is 4000px and the screen width is 1920px, using text-align: center; puts the text at the centre of the visible part, that is, centred at 960. I can force it using the page width, but I want to make it so that the text is at the centre of the full width, even when I change the width.
So, if the width is 6000px, I want the text centred at 3000px. If it is 1000px, I want the text centred at 500px. And all the while, the screen size remains the same.
In addition, how do I accomplish this for a container that contains text that should be justified, but the container itself should be centred.
A reproducible example as requested:
#mainhead {
background-color: pink;
font-size: 50px;
font-weight: 1000;
color: #e0f2f1;
text-align: center;
}
The page width in this example is set to 4000px due to other stuff. But the heading is always centred at 960px from left, rather than 2000px from the left.
Create a parent div with width 100% and make that a flexbox. Then use justify-content: center; and align-items: center; It should not matter how wide your screen is. This will make sure the text is always centered.
You should use class instead of id in most cases when doing CSS.
.parent {
width: 100%;
height: 100vh;
border: 2px solid red;
display: flex;
justify-content: center;
align-items: center;
}
.mainhead {
font-size: 20px;
color: red;
border: 2px solid blue;
}
<div class = "parent">
<p class="mainhead"> TEXT IS ALWAYS CENTERED </p>
</div>
no matter what the screen size is.it will always be centered.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
div {
width: 100vw;
display: flex;
justify-content: center;
}
h1,div{
outline:1px solid red;
}
<div>
<h1>I Am Always Centered</h1>
</div>
I am making a sort of section with one side being a h1 and anchor and one side being an image. However, I am having troubles moving the anchor tag as its not working at all with margin since it adds margin to the entire div. This does not apply to the h1.
Output I want is that I'd like the anchor tag to be moveable with margin etc. without moving the entire div and stuff like that.
.make-account {
height: 100%;
width: 100%;
margin-top: 0.5%;
background-color: #EBCEBF;
display: inline-flex;
flex-flow: row;
justify-content: space-between;
}
.make-account h1 {
margin-top: 5%;
text-align: center;
color: #FDF8F5;
}
.make-account img {
width: 50%;
display: inline-flex;
}
.make-account a {
color: #FDF8F5;
background-color: #266150;
padding: 1% 2% 1% 2%;
text-align: center;
}
<div class="make-account">
<h1>WE HELP YOU FIND YOUR OWN SPORTBUDDY!</h1>
MAKE AN ACCOUNT
<img src="https://via.placeholder.com/100">
</div>
Edit: How I recognized the problem now is that since it's a flex-flow: row
It recognizes the tag and tag as items, same with , my output that I want is one div with the background-color taking up 50% and that div having an and positioned within them using margin and not being their own items, and another item being the img taking up the other 50%.
add the following to your style
aside {
display: flex;
justify-content: center center;
flex-direction: column;
}
and make sure to wrap the H1 and A in an aside or whatever you like, make sure to edit the style accordingly.
<div class="make-account">
<aside>
<h1>WE HELP YOU FIND YOUR OWN SPORTBUDDY!</h1>
MAKE AN ACCOUNT
</aside>
<img src="https://via.placeholder.com/100">
</div>
I hope this answer helps you out in your further endeavours!
For a web application, I'm to position an animated emoji along with some text in a div. These elements are to remain separated in a fully responsive way. Behold:
I'm using flex to accomplish this. That ensures that even if the screen size becomes extremely small, separation is still kept by stacking these one on top of the other.
To accomplish it, the whole outer div is wrapped in:
.act{
display: flex;
flex-wrap: wrap;
background-color: #E1F5FE;
padding: 10px;
border-radius: 10px;
align-items: center;
}
Next, the animated image inside the div is wrapped in:
.anim {
flex: 1 1;
min-width: 64px;
text-align: center;
}
.anim > img {
width: 100%;
height: auto;
max-width: 50px;
}
Lastly, the text along with the image is wrapped in:
.txt {
flex: 1 1 180px;
text-align: center;
}
Did you notice the tear drops on the emoji? Those are separate from the image, and are to be animated in html5.
I can't figure out how to ensure those tear drops stay precisely around the eyes of the emoji. I have tried using a z-index alongwith position:absolute (e.g. see the following):
<div class="anim">
<div class="tear" style="z-index:2;position:absolute;margin-top: 30px;margin-left: 110px;"></div>
<div class="tear" style="z-index:2;position:absolute;margin-top: 30px;margin-left: 84px;"></div>
<img src="sad.png">
</div>
This isn't responsive at all.
Moreover, If I try usingposition:relative, that makes it impossible to overlap the tear shape over the emoji, regardless of what z-index I set.
Please help me fix this situation. Ideally, I want to stick to using flex because otherwise, it's perfect for my needs.
Note: Answers to a similar SO question don't help since I've already included what they're suggesting.
To accomplish that you need a wrapper around the image and text, that take the size of the image.
Here is a sample code, where I added an extra wrapper, image, around the anim, and then made the anim display as inline block.
Here the image wrapper become the flex item instead, and will allow the anim to behave and be sized as the image, and create the boundaries you need to be able to place the eyes at a fixed position on top the image.
Stack snippet
.act {
display: flex;
flex-wrap: wrap;
background-color: #E1F5FE;
padding: 10px;
border-radius: 10px;
align-items: center;
}
.image {
flex: 1 1;
min-width: 64px;
text-align: center;
}
.anim {
display: inline-block;
position: relative;
}
.anim>img {
width: 100%;
max-width: 50px;
}
.txt {
flex: 1 1 180px;
text-align: center;
}
.tear {
position:absolute;
top: 10px;
left: 30px;
width: 10px;
height: 10px;
background: blue;
}
.tear:first-child {
left: 10px;"
}
<div class="act">
<div class="image">
<div class="anim">
<div class="tear"></div>
<div class="tear"></div>
<img src="http://placehold.it/150">
</div>
</div>
<div class="txt">
Some text
</div>
</div>
This is my code but I want the text to only have background color behind it, and not stretch across the entire screen? Any ideas?
.section_title {
background-color: orange;
text-align: center;
margin: 0px auto;
}
HTML is
<div class="col-md-12">
<div class="section_title">
<h2>Choose a Pack to Print</h2>
</div>
</div>
An option is adding display: inline-block; to the CSS of the text element.
One problem I found with display: inline-block; is it clears floats incorrectly. Instead, I use width: fit-content;
.highlight {
background: yellow;
padding: 0.5em;
width: fit-content;
}
<h1 class="highlight">Highlight for text only!</h1>
<h1 class="highlight">Highlight me too!</h1>
There's a few ways to do this, but probably the best way is to make the h2 inline or inline-block.
Using inline-block will allow you to set width/height.
.section-title {
text-align: center;
}
.section-title h2 {
display: inline-block;
}
The other way to do this is to set a width on the h2 and set the margin to auto;
.section-title h2 {
margin-left: auto;
margin-right: auto;
width: 50%; /* for example */
}
If you want all your headings to be a set width, I'd choose the second one (allowing for text to wrap). If you want the box to be flexible and hug the contents, I'd use the first.
it's my first question here, while i solved many problems in any area reading other's solutions, but this time i have no luck, i keep trying and googling.
I am working with something i'm not very skilled, CSS. I want to center a box made of an image with a paragraph of text next to it (two rows of text - no more), the text is aligned to the left of the image.
|------------------------------------ NOW ------------------------------------|
|img|BIG TITLE
|img|smaller subtext
|----------------------------- <-- CENTERED----> -----------------------------|
|img|BIG TITLE
|img|smaller subtext
I tried to put everything in a div, but since the width of the DIV depends on the length of the text, and since it changes depending on the page's title, i can't set a fixed width.
Here is my HTML:
<div class="container">
<div class="row">
<div class="header">
<p class="small">
<img class="ok" src="ok.png">
<span class="bigtitle">TITLE</span>
<br><span style="text-align:left">subtext</span></p>
</div>
</div>
and here is my CSS:
.row .header {
width: 100%;
}
img.ok {
top: 15px;
}
p.small {
color: #000000;
font: bold 12px arial,helvetica,sanserif;
display: block;
}
span.bigtitle {
color: #679819;
text-transform:uppercase;
font: bold 42px arial,helvetica,sanserif;
}
text-align: center;
Then i tried:
putting everything in a paragraph and text-align: center;
setting Margin 0px auto to the paragraph;
putting everything in a div and tried centering but i don't know its width
googled and searched for solutions here...
Nothing works, i can't center the block, please help :-)
Thanks in advance
You could float and relatively position the containers like this:
.container {
overflow: hidden;
}
.row {
position: relative;
left: 50%;
float: left;
}
.header {
position: relative;
left: -50%;
float: left;
}
If you have more content in the container div than just this centered bit, you might wrap the centered bit in another div and move the overflow: hidden rule out of .container and to that div instead.