I am trying to create two, horizontally aligned boxes -- one with an image and the other with text. I have been able to configure the block with the image, but when I try to add text to the second block, it moves to a new line. Here is the html:
<div class = "First-Horizontal-Section">
<ul class = "First-Horizontal-Section-List">
<li class = "Image-One"></li>
<li class = "Text-Box-One"><p class = "Text-One"> This is some sample text. </p></li>
</ul>
</div>
And here is the CSS:
.First-Horizontal-Section-List {
font-family:'Times New Roman';
text-align: center;
white-space: nowrap;
display: flex; /* Prevents text box from moving downwards when text is inserted, but it isn't great when display shrinks. */
}
.First-Horizontal-Section-List li {
list-style: none;
display: inline-block;
height: 500px;
width: 550px;
border: solid blue;
margin-left: 5%;
margin-right: 5%;
}
.Image-One {
background-image: url(URL);
background-size: cover;
background-position: center;
background-repeat: no-repeat;
}
.Text-Box-One {
background-image: url(URL);
background-size: cover;
background-position: center;
background-repeat: no-repeat;
}
I have found some recommendations to use "display: flex" to solve this problem, but I have noticed that using "display: flex" seems to negate "white-space: nowrap" when I shrink the website window. Using "white-space: nowrap" keeps the boxes static when I shrink the window whereas "display: flex" will result in the manipulation of the boxes.
I am looking for a way to have my boxes aligned on the same line while being able to insert text. Ideally, the boxes would also remain static when the window is shrunk. Is there a way to do this?
Related
The issue I'm having is a bit different than that I've found thus far the goal is to make an HTML page in which each background img div will be one page, thus when printing it will be clean.
Here is what the code looks like.
.thirdBackgroundPage {
background-image: url("images/firstBackgroundPage.jpeg");
height: 100%;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
.icobp {
height: 75%;
width: auto;
display: block;
margin-left: auto;
margin-right: auto;
bottom: 0;
}
<div class="thirdBackgroundPage">
<img class="icobp" src="images/imageCenterOfBackgroundPage.jpeg" />
</div>
With this said, there are other <, div class, so the first image is on the third page for example, where right now with this, it brings the image in the center top, however, I need it dead center or bottom.
I've tested a few things, but to no avail. Tried setting it up within a table, but this does not work either. Any help is welcome.
You already had other suggestions to better center your div inside the container, anyway that's mine. In the snippet below I just added a css rule that will add some style attribute to the container so that its content will be correctly centered both vertically and horizontally via display:flex;
Since you talk about printing html pages, I want to warn you that html/css are still not suitable for that since there are no tools to address publishing details like page number or page size. You can control the content size but it will never be accurate and I could experience myself that i will become inaccurate starting from page 30 maybe..I can't remember in details.
Anyway I considered that div .thirdBackgroundPage as filling an A4 page so I gave it those size in mm. Let me know if I got it wrong.
.thirdBackgroundPage {
background-image: url("images/firstBackgroundPage.jpeg");
/*height: 100%;*/
background-position: center;
background-repeat: no-repeat;
background-size: cover;
display: flex;
}
/*Style attributes I added to the list container*/
.thirdBackgroundPage {
display: flex;
align-items: center;
justify-content: center;
border: solid 1px black;
width: 210mm;
height: 297mm;
}
.icobp {
/*height: 75%;*/
/*Needed to change this to give the pic a size despite the url fake*/
height: 100px;
width: auto;
display: block;
margin: auto;
}
<div class="thirdBackgroundPage">
<img class="icobp" src="images/imageCenterOfBackgroundPage.jpeg" />
</div>
To manipulate the printing you have to use #media print so you can control which one you just want to print. If you needed to center a spcific element you have to use this code #media print as well.And display:none for the other one you dont want to be printed. sometimes you have to use display:block o display:inline-block to get it to work as well. Hope this will help your issue.
#page {
size: A4;
margin: 0;
}
#media print {
html, body {
width: 210mm;
height: 297mm;
}
.thirdBackgroundPage{
vertical-align:middle;
align-items:center;
page-break-before:avoid;
page-break-after:avoid;
page-break-inside:avoid;}
}
<div class="thirdBackgroundPage">
<img class="icobp" src="images/imageCenterOfBackgroundPage.jpeg"/>
</div>
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>
I have some vertically aligned text in a div that I want to remain vertically centered when the browser resizes. As the div shrinks with the browser, my text becomes offset towards the bottom of the div.
Any suggestions?
Here is a Fiddle of the issue I'm experiencing.
HTML:
<div class="outer-wrapper">
<div class="inner-wrapper">
<div class="header-wrapper">
<h1>
Vertically aligned text
</h1>
</div>
</div>
</div>
CSS:
.outer-wrapper {
background: url("http://paulmason.name/media/demos/full-screen-background-image/background.jpg") no-repeat left top;
background-size: 100% auto;
height: 360px;
}
.inner-wrapper {
display: table;
width: 100%;
height: 360px;
}
.header-wrapper {
display: table-cell;
text-align: center;
vertical-align: middle;
}
h1 {
color: #fff;
}
Is this what you're looking for?
The text actually does center, though the background image doesn't, hence it looks like it doesn't, so here is 2 versions (fiddle use background-size: cover) where image centers too
Updated fiddle
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: 100% auto;
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: black;
}
<div class="outer-wrapper">
<div class="inner-wrapper">
<div class="header-wrapper">
<h1>
Vertically aligned text
</h1>
</div>
</div>
</div>
The problem is your background, the div is staying the same size and the text is staying centered, you background makes it seem not so. Like Gavin said use background-size cover.
Your problem isn't actually that your text isn't centered, because it is. Your actual issue is that your image doesn't have enough height at that size to fill the height of the container.
See the screenshot below, here I have used the element inspector to highlight the "outer-wrapper" which is the blue box you see. Notice the text is centered within it.
So to tackle the actual issue which is the image either needs to fill the div or needs to stay centered itself.
To stay centered add the following to the outer-wrapper: (Fiddle)
background-position: center;
To fill the div, remove the background-size from outer-wrapper and add this: (Fiddle)
background-position: center;
background-cover: cover;
I'm trying to center an image inside a span. But it doesn't work.
Here is a link to my code: jsfiddle
<div>
<label>
<span>left span that can have more than one line</span>
<span><img class="redcross" /></span>
</label>
</div>
the class "redcross" is what I want to center vertically
can someone help me?
Change your css:
.button {
position:absolute;
width:24px;
height:100%;
top:3px;
right:0;
}
top 0 to 3px;
Remove right and margin-right, Add position: absolute and margin-top: -11px
.redcross {
float: right;
display: inline-block;
background-color: #94B548;
/*background-color: rgba(0,0,0,.3);*/
background-position: center center;
background-repeat: no-repeat;
background-image: url("data:image/svg+xml;charset=US-ASCII,%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22iso-8859-1%22%3F%3E%3C!DOCTYPE%20svg%20PUBLIC%20%22-%2F%2FW3C%2F%2FDTD%20SVG%201.1%2F%2FEN%22%20%22http%3A%2F%2Fwww.w3.org%2FGraphics%2FSVG%2F1.1%2FDTD%2Fsvg11.dtd%22%3E%3Csvg%20version%3D%221.1%22%20id%3D%22Layer_1%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20xmlns%3Axlink%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2Fxlink%22%20x%3D%220px%22%20y%3D%220px%22%20%20width%3D%2214px%22%20height%3D%2214px%22%20viewBox%3D%220%200%2014%2014%22%20style%3D%22enable-background%3Anew%200%200%2014%2014%3B%22%20xml%3Aspace%3D%22preserve%22%3E%3Cpolygon%20style%3D%22fill%3A%23FFFFFF%3B%22%20points%3D%2211.949%2C3.404%207%2C8.354%202.05%2C3.404%20-0.071%2C5.525%207%2C12.596%2014.07%2C5.525%20%22%2F%3E%3C%2Fsvg%3E");
-webkit-border-radius: 1em;
border-radius: 1em;
content: "";
display: block;
width: 22px;
height: 22px;
top: 50%;
position: absolute;
margin-top: -11px;
}
jsfiddle
I'm sure Mr. Alien means well. But I was there too. Anyway, a simple steps to get what you are looking for:
Change your .button class to
.button {
vertical-align: middle;
width:24px;
}
You see, the vertical-align property doesn't quite work how some people think it does. It only affects inline elements. Furthermore, it aligns it vertical relative to the current line. So, in other words, if you were to say have more than one line on a block of text to the left and a button to the right, this wouldn't work.
You would need to wrap that block of text in an inline-block and adjust the line-height accordingly to get this same effect for it to be vertically aligned. Essentially, the two elements (block of text and img) would be behaving like text. This is important to understand especially in a screen responsive environment.
I would do it with flexbox.
It's a very recent feature but very useful.
Just add this lines to your .button class.
display: flex;
align-items: center;
Here you have it working. The green circle gets deformed but I would consider using a fixed image instead the border-radius.
Here you can see its browsers compatibility
I am pulling my hair out over the correct way to position two forms using html and css. I have tried both float and display:inline-block but only managed to get half of it working using one of the methods.
My target is to have the two forms display next to each other centered in a DIV that is only 70% of the page and each form takes up 50% of the available space. Both forms need to have a minimum width and should be pushed into separate lines if there isn't enough space to display both next to each other (i.e. when displaying the page on a phone in portrait mode)
If I float the two DIVs containing the forms they are displayed side by side but are not centered correctly (as they float either left or right and I need to set the size of each DIV to 40% or they don't fit next to each other).
If I use display:inline-block the DIVs are in the correct size and centered but are in two separate lines and not next to each other.
Here is the current code using display: inline-block
#background {
background-image: url(pic.jpg);
height: 400px;
background-size: cover;
background-repeat: no-repeat;
position: relative;
background-position: center;
}
#form-wrapper {
width: 70%;
margin: 0 auto;
text-align: center;
}
#form1 {
width: 50%;
display: inline-block;
}
#form2 {
width: 50%;
display: inline-block;
}
<div id="background">
<div id="form-wrapper">
<div id="form1">
<form>some form code here</form>
</div>
<div id="form2">
<form>some form code here</form>
</div>
</div>
</div>
Why are the forms on different lines when using display:inline-block ?
You might be having trouble getting the two inline-block elements next to each other because 50% times two plus the white space between the elements is greater than 100% of the container. Therefore, the second element doesn't have enough space and wraps to the next line.
inline-block elements will respect white space in the HTML code. The white space between the two elements is demonstrated below:
#background {
background-image: url(pic.jpg);
height: 400px;
background-size: cover;
background-repeat: no-repeat;
position: relative;
background-position: center;
}
#form-wrapper {
width: 70%;
margin: 0 auto;
text-align: center;
}
#form1 {
width: 40%;
display: inline-block;
background-color: #CCC;
}
#form2 {
width: 40%;
display: inline-block;
background-color: #CCC;
}
<div id="background">
<div id="form-wrapper">
<div id="form1">
<form>some form code here</form>
</div>
<div id="form2">
<form>some form code here</form>
</div>
</div>
</div>
So, one solution to your issue is to remove the white space, as shown below.
I have also given each element a minimum width so that they wrap to separate lines when the window is below a specified width. To see this action, click the "Full page" button in the upper right corner and resize your browser window.
#background {
background-image: url(pic.jpg);
height: 400px;
background-size: cover;
background-repeat: no-repeat;
position: relative;
background-position: center;
}
#form-wrapper {
width: 70%;
margin: 0 auto;
text-align: center;
}
#form1 {
width: 50%;
display: inline-block;
min-width:200px;
}
#form2 {
width: 50%;
display: inline-block;
min-width:200px;
}
<div id="background">
<div id="form-wrapper">
<div id="form1">
<form>some form code here</form>
</div><div id="form2">
<form>some form code here</form>
</div>
</div>
</div>
Use display:flex in your wrapper:
#form-wrapper {
width: 70%;
margin: 0 auto;
text-align: center;
display:flex;
}
JS BIN
Thanks a lot for your input! After looking at this issue for two days I was questioning basically anything but your examples showed that my original idea for the css using display: inline-block worked well.
The example at http://jsbin.com/qiraxo/2/ is exactly what I am looking for.
After digging some more I realized that one of my forms didn't work and even copying the working form in a second time didn't change the behaviour. So some more digging and I found the issue: a typo in a DIV declaration....
Thanks again and have a good evening.