Two columns with text vertically centered - html

I need two columns:
a button on the left
a list on the right
And they both need to be vertically centered in the middle of the wrapper div. So far I got this:
https://jsfiddle.net/s1ky1hqr/1/
I used this code to vertically center the inline-blocks inside the wrapper:
.wrapper {
/* let it fill the whole container */
position: absolute;
top: 0;
bottom: 0;
left: 5px;
right: 5px;
}
.wrapper:before {
content: '';
display: inline-block;
vertical-align: middle;
height: 100%;
}
.column {
display: inline-block;
width: 48%;
}
<footer>
<div class="wrapper">
<div class="column">
<a class="cta" href="#">
<span class="text">
Button
</span>
<span class="arrow">
>
</span>
</a>
</div>
<div class="column">
<ul>
<li>Link 1</li><li>
Link 2</li><li>
Link 3</li>
</ul>
</div>
</div>
</footer>
The only problem is the ul list is not absolutely on the right, because the column divs aren't both 50% wide.
If I give both column divs a width of 50% the two divs won't fit next to each other. And I cannot "float" the column divs, cause in that case the text isn't vertically centered anymore.
Can anyone help me out?

You can actually use columns of 50% width if you set the font-size to 0 inside your wrapper and reset it for your columns, like this:
html { font-size: 12px; }
.wrapper {
/* let it fill the whole container */
position: absolute;
top: 0;
bottom: 0;
left: 5px;
right: 5px;
font-size: 0;
}
.wrapper:before {
content: '';
display: inline-block;
vertical-align: middle;
height: 100%;
}
.column {
display: inline-block;
width: 50%;
font-size: 1rem;
}
<footer>
<div class="wrapper">
<div class="column">
<a class="cta" href="#">
<span class="text">
Button
</span>
<span class="arrow">
>
</span>
</a>
</div>
<div class="column">
<ul>
<li>Link 1</li><li>
Link 2</li><li>
Link 3</li>
</ul>
</div>
</div>
</footer>

You should add vertical-align:middle to .column instead .wrapper:before

You can achieve this by changing the li style to display: block; instead of display: inline-block, and getting rid of float: right; on the ul element.
Here's your example, updated.

Related

If I have to use float left for multiple divs in a row, is there a way to center them with the leftover space?

For my class I have to have 3 divs floated left in a row with the outer two half the size of the middle one. It's driving me crazy that the rows aren't centered on the page. Is there a way to center them without getting rid of the float?
I tried creating a container div with text-align just as a shot in the dark but that didn't work. All other research I've seen is to change the float to display but I have to use float so I can't do that.
div.container {
width: 100%;
text-align: center;
border: none;
background-color: pink;
height: 100%;
}
div.cover {
width: 20%;
}
div.author {
width: 50%;
font-family: calibri;
}
div.links {
width: 20%;
}
<div class="cover">
<p class="inner">
<img src="Images/Divergent.jpg"><br>
</p>
</div>
<div class="author">
<p class="inner" style="margin-top: 10px;">
<b>Divergent<br>Veronica Roth</b><br>
</p>
</div>
<div class="links">
<ul>
<li>
<p class="link" onclick="parent.open('https://www.britannica.com/biography/Veronica-Roth')">
https://www.britannica.com/biography/Veronica-Roth
</p>
</li>
</ul>
</div>
Your instinct to wrap the 3 "columns" in a container div is correct. This allows you to use what is commonly referred to as the "clearfix" trick. Items that are "floated" are ignored by the normal box flow of the page which is why the container seems to collapse and ignore your floating contents.
Frustrating indeed!
This is the "clearfix":
div.container:after {
content: ''; /* no content in this pseudo element */
display: table; /* be 100% wide */
clear: both; /* clear the previous floats */
}
The :after pseudo selector on the container is the same thing as putting an empty div as the last item in the container. By clearing the floats, the container will wrap around the floating items.
This is a hack... but it works! The entire web development community has used this to "fix" the difficulties inherent with using floats for years to create layouts before the advent of real layout systems like Flexbox and CSS Grid.
After the container clears the floating items inside, just set the widths so that they add up to 100% and you are good.
div.container {
width: 100%;
text-align: center;
border: none;
background-color: pink;
height: 100%;
}
div.container:after {
content: '';
display: table;
clear: both;
}
div.cover {
width: 25%;
float: left;
}
div.author {
width: 50%;
font-family: calibri;
float: left;
}
div.links {
width: 25%;
float: left;
}
<div class="container">
<div class="cover">
<p class="inner">
<img src="Images/Divergent.jpg"><br>
</p>
</div>
<div class="author">
<p class="inner" style="margin-top: 10px;">
<b>Divergent<br>Veronica Roth</b><br>
</p>
</div>
<div class="links">
<ul>
<li>
<a href="https://www.britannica.com/biography/Veronica-Roth">
Veronica-Roth
</a>
</li>
</ul>
</div>
</div>
div.container position:relative;
div.cover float:left;
div.autor put inside tag align="center"
div.links float:right;
or
div.autor margin:5%;
or
display:inline-block;
or you can use text-align:center; in css on the parent div and then display:inline-block; on each div inside the parent div

Sibling div is inside of other sibling div

I have a menu in the top of my website with this css:
.menu {
width: 100%;
display: block;
float: left;
}
inside of it, I have few divs:
.menu .menu-item {
position: relative;
width: 260px;
float: left;
height: 430px;
}
This is all good, but when I try to add a small div underneath the menu, with this HTML structure:
<div class="menu">
<div class="menu-item">
</div>
</div>
<div class="menu-bar">
</div>
and this css:
.menu-save {
position: relative;
display: block;
margin: 0 auto;
width: 100%
height: 20px;
}
With this CSS my expected output is that the menu-bar div goes underneath the whole menu, but what I'm currently getting is that menu-bar sits inside of menu, at the top of it. What CSS am I missing?
I think
use clear: both CSS property to avoid the floating problem
<div class="menu">
<div class="menu-item">
</div>
</div>
<div class="clear"></div>
<div class="menu-bar">
In Css add this one
.clear{
clear:both;
}

List item clamping

After trying to add clamping to my headers, my result does not turn out the way I wanted.
I made a fiddle of the code that I am using to achieve my result: https://jsfiddle.net/73kerhsn/
h3
{
margin: 0;
font-size: 13px;
}
ul
{
list-style: none;
padding: 0;
margin: 0;
}
li
{
display: inline-block;
margin: 2px;
padding: 0;
}
.acontainer
{
background-color: yellow;
width: 500px;
padding: 15px;
}
.background
{
background-color: grey;
width: 196px;
}
.block1
{
height: 110px;
width: 195px;
background-color: red;
}
.clamp
{
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
overflow: hidden;
}
<div class="acontainer">
<ul>
<li class="background">
<div class="block1">
</div>
<h3 class="clamp">
This foo test asdas asdassd saasas dasasa asdasadsd asasddas asdadsg gfhfhg gddfd sddsfsdf
</h3>
<div>
Something
</div>
<div>
Something else
</div>
</li>
<li class="background">
<div class="block1">
</div>
<h3>
This foo test
</h3>
<div>
Something
</div>
<div>
Something else
</div>
</li>
<li class="background">
<div class="block1">
</div>
<h3>
This foo test
</h3>
<div>
Something
</div>
<div>
Something else
</div>
</li>
</ul>
</div>
I want the list items to stay at the same height, even when the header tags get bigger than the ones of the other list items (so for example, the second list item in the jsfiddle appears to be lower from the top than the first list item. I want it to still stick to the top (with the padding still in tact)).
Is there anyone out there that can help me with this?
Thanks in advance.
You probably simply want to add vertical-align: top for your list items.
li
{
display: inline-block;
margin: 2px;
padding: 0;
vertical-align: top;
}
https://jsfiddle.net/73kerhsn/1/
You can just apply this to your css for styling the list-items:
li{
display: inline-table;
}
This will make your li items start from the same height, now you can set the height as you want, it won't hamper your next line contents. If you won't set a fixed height, then only the start of the boxes will remain same.

Align text to image in auto-width container that stretches to image width

Here is fiddle https://jsfiddle.net/qyLjxwrv/1/.
As you can see left and right titles are aligned to screen corners, while I would like to align them to image corners. Trying it for 2hours without a success...
.wrapper {
position: absolute;
left: 0px;
right: 0px;
top: 0px;
background-color: rgba(213,213,213,.5);
text-align: center;
}
.info {
text-align: left;
}
.right-title {
float: right;
}
<div class="wrapper">
<div class="inner">
<div class="info">
<span class="left-title">Left</span>
<span class="right-title">Right</span>
</div>
<img src="http://i.imgur.com/CRcoPPS.jpg" />
</div>
</div>
Note that I don't want titles to appear inside image, but above it and aligned with left and right corner of image.
It seems that you can do it like this simply, and see the comments inline.
Updated Demo
.wrapper {
display: table; /*shrink-to-fit*/
margin: 0 auto; /*center*/
}
.wrapper img {
max-width: 100%; /*responsive*/
}
.left-title {
float: left; /*left*/
}
.right-title {
float: right; /*right*/
}
<div class="wrapper">
<div class="inner">
<div class="info">
<span class="left-title">Left</span>
<span class="right-title">Right</span>
</div>
<img src="http://i.imgur.com/CRcoPPS.jpg" />
</div>
</div>
Edit: Demos with the gray background color partly and full page.

Vertical align two lines of text

I am having trouble vertical aligning text in a div. I have tried suggestions that i have read on other posts. I'm sure its obvious what I am doing wrong but its not working.
http://jsfiddle.net/o6gqvtoo/
<div class="banner">
<div class="span10"> <!-- bootstrap -->
<div class="banner-text">
<div class="pull-left">Here is the first line of text i want to center</div>
<div class="pull-left">Here is the second line of text i want to center</div>
<div class="clear"></div>
</div>
</div>
.banner{
margin-top:-2;
height: 70px;
background-color: #cf0000;
color: white;
height: 70px;
position: relative;
}
.banner-text{
display: block;
vertical-align: middle;
}
.pull-left {
display: block;
}
vertical-align is sort of an odd one.
Generally you should use it on the elements themselves, not their containers and it's really only for inline and inline-block elements.
But even then it probably won't vertically center like (I'm guessing) you are wanting. So a little trick is to set your line-height to the same as your height and voila:
http://jsfiddle.net/06c7eosv/
Note: With display: inline-block and width: 50% you can't have any white space between the elements in your html
Good:
<div>...</div><div>...</div>
Bad:
<div>...</div>
<div>...</div>
Based on the supplied image and limited information availableI believe that the best option available is to absolutely position the banner-text wrapper div
.banner {
margin-top: -2;
height: 70px;
background-color: #cf0000;
color: white;
height: 70px;
position: relative;
}
.banner-text {
display: block;
position: absolute;
top: 50%;
transform: translateY(-50%);
}
.pull-left {
display: block;
}
<div class="banner">
<div class="span10">
<!-- bootstrap -->
<div class="banner-text">
<div class="pull-left">Here is the first line of text i want to center</div>
<div class="pull-left">Here is the second line of text i want to center</div>
</div>
</div>
</div>
That said, other options may be available if the two internal divs can be combined.
Usually, vertical-align sets the alignment of inline level elements relatively to the line box.
However, in when applied to table cells, it has a different meaning: it sets the alignment of the content inside the cell.
Therefore, you can use CSS tables:
.banner {
display: table;
width: 100%;
}
.banner > .span10 {
display: table-row;
}
.banner-text {
display: table-cell;
vertical-align: middle;
}
.banner{
margin-top:-2;
height: 70px;
background-color: #cf0000;
color: white;
height: 70px;
position: relative;
display: table;
width: 100%;
}
.banner > .span10 {
display: table-row;
}
.banner-text{
display: table-cell;
vertical-align: middle;
}
.pull-left {
display: block;
/* float:left; */
}
<div class="banner">
<div class="span10"> <!-- bootstrap -->
<div class="banner-text">
<div class="pull-left">Here is the first line of text i want to center</div>
<div class="pull-left">Here is the second line of text i want to center</div>
<div class="clear"></div>
</div>
</div>
</div>