Centred vertical line between two divs - html

I am trying to achieve the following layout (except with a straight line obviously)
But I'm unsure how to, I tried adding a right border to the left element, but it isn't centred. How do I go about it?
This is what I have at the moment
div.contentswrapper {
text-align: center;
margin: 0 auto;
max-height: 720px;
}
div.pageleft, div.pageright {
display: inline-block;
vertical-align: top;
overflow: auto;
}
div.pageleft, div.pageright {
width: 40%;
margin-left: 3%;
margin-right: 3%;
}
<div class="contentswrapper">
<div class="pageleft">
<h1>Lorem Ipsum</h1>
<p>
</p>
<p>
</p>
</div>
<div class="pageright">
<h1>Lorem Ipsum</h1>
<p>
</p>
<p>
</p>
</div>

Change your margins to paddings, and then set the border
div.contentswrapper {
text-align: center;
margin: 0 auto;
max-height: 720px;
}
div.pageleft,
div.pageright {
display: inline-block;
vertical-align: top;
overflow: auto;
}
div.pageleft,
div.pageright {
width: 40%;
padding-left: 3%;
padding-right: 3%;
}
div.pageleft {
border-right: 2px solid red;
}
<div class="contentswrapper">
<div class="pageleft">
<h1>Lorem Ipsum</h1>
<p>
</p>
<p>
</p>
</div>
<div class="pageright">
<h1>Lorem Ipsum</h1>
<p>
</p>
<p>
</p>
</div>
</div>
Option 2, use a pseudo element and its border
div.contentswrapper {
position: relative;
text-align: center;
margin: 0 auto;
max-height: 720px;
}
div.contentswrapper::after {
content: '';
position: absolute;
top: 0;
left: calc(50% - 2px);
bottom: 0;
width: 0;
border-right: 2px solid red;
}
div.pageleft,
div.pageright {
display: inline-block;
vertical-align: top;
overflow: auto;
}
div.pageleft,
div.pageright {
width: 40%;
margin-left: 3%;
margin-right: 3%;
}
<div class="contentswrapper">
<div class="pageleft">
<h1>Lorem Ipsum</h1>
<p>
</p>
<p>
</p>
</div>
<div class="pageright">
<h1>Lorem Ipsum</h1>
<p>
</p>
<p>
</p>
</div>
</div>

You don't really need to use inline-block, you can just float them and set the right border of the left div.
Changing the margin settings to paddings instead will correct the slight center misalignment because your total widths don't add up to a perfect 100%.
Also, you don't need 2 style rules with the same selectors, you can combine them.
div.contentswrapper {
text-align: center;
margin: 0 auto;
max-height: 720px;
}
.pageleft { border-right:2px solid black;}
div.pageleft, div.pageright {
float:left;
vertical-align: top;
width: 42%;
padding-left: 3%;
padding-right: 3%;
}
<div class="contentswrapper">
<div class="pageleft">
<h1>Lorem Ipsum</h1>
<p>sdfff</p>
<p>sdfsdfsd</p>
</div>
<div class="pageright">
<h1>Lorem Ipsum</h1>
<p>sdfff</p>
<p>sdfsdfsd</p>
</div>

Related

always keep pseudo element horizontally in the middle

How do I always keep a pseudo element horizontally in the middle? I have it positioned absolutely with a percentage value, which I undrstood was relative to the width of the element but on different screen sizes the circular "OR" is not consistently in the middle...
See below:
.content {
display: flex;
flex-direction: row;
justify-content: space-evenly;
text-align: center;
margin: 20px;
}
.cc-content {
width: 50%;
padding: 5px 20px;
position: relative;
background-color: red;
}
.cc-content-1 {
margin-right: 3px;
}
.cc-content-2 {
margin-left: 3px;
}
.cc-content-1:after {
content: 'OR';
display: inline-block;
position: absolute;
vertical-align: middle;
line-height: 60px;
top: 20px;
left: 90%;
z-index: 1;
font-size: 21px;
font-weight: bold;
border-radius: 100%;
width: 60px;
height: 60px;
background-color: #F2F2F2;
color: #006AAD;
}
<div class="content">
<div class="cc-content cc-content-1">
<h3>Header Here</h3>
<p>Lorem Ipsuom gshjgshj gshjgsajhusgs gsaiyusgisysgsyigs</p>
</div>
<div class="cc-content cc-content-2">
<h3>Header Here</h3>
<p>Lorem Ipsuom gshjgshj gshjgsajhusgs gsaiyusgisysgsyigs</p>
</div>
</div>
If you resize the window you can see the circle shifts ever so slightly off cente, how do I always keep it in center with position absolute? Is not not percentage? I tried playing with flex since it is in a flex container but that doesn't seem to do anything.
How do I horizontally center a position absolute :after element?
Very similar to #mgrsskls but using actual :after element. Its an old trick to use absolute, then add left and negative margin equal to half-width. But in your case there is additional margin which needs to be thought of as well.
.content {
display: flex;
flex-direction: row;
justify-content: space-evenly;
text-align: center;
margin: 20px;
}
.cc-content {
width: 50%;
padding: 5px 20px;
position: relative;
background-color: red;
}
.cc-content-1 {
margin-right: 3px;
}
.cc-content-2 {
margin-left: 3px;
}
.cc-content-1:after {
content: 'OR';
display: inline-block;
position: absolute;
vertical-align: middle;
line-height: 60px;
top: 20px;
left: 100%;
margin-left: -27px;
z-index: 1;
font-size: 21px;
font-weight: bold;
border-radius: 100%;
width: 60px;
height: 60px;
background-color: #F2F2F2;
color: #006AAD;
}
<div class="content">
<div class="cc-content cc-content-1">
<h3>Header Here</h3>
<p>Lorem Ipsuom gshjgshj gshjgsajhusgs gsaiyusgisysgsyigs</p>
</div>
<div class="cc-content cc-content-2">
<h3>Header Here</h3>
<p>Lorem Ipsuom gshjgshj gshjgsajhusgs gsaiyusgisysgsyigs</p>
</div>
</div>
First of all I would put the "or" in the HTML, so screen readers would read it out loud correctly. Then you can position that element relative to the whole container, with percentage yes. You move it exactly to the middle with left: 50% and then by its half width back to the left with transform: translateX(-50%) (that way you don't have to know how wide the "or" element is).
.content {
display: flex;
flex-direction: row;
justify-content: space-evenly;
text-align: center;
margin: 20px;
position: relative;
}
.cc-content {
width: 50%;
padding: 5px 20px;
position: relative;
background-color: red;
}
.cc-content-1 {
margin-right: 3px;
}
.cc-content-2 {
margin-left: 3px;
}
.or {
display: inline-block;
position: absolute;
vertical-align: middle;
line-height: 60px;
top: 20px;
left: 50%;
transform: translateX(-50%);
z-index: 1;
font-size: 21px;
font-weight: bold;
border-radius: 100%;
width: 60px;
height: 60px;
background-color: #F2F2F2;
color: #006AAD;
text-transform: uppercase;
}
<div class="content">
<div class="cc-content cc-content-1">
<h3>Header Here</h3>
<p>Lorem Ipsuom gshjgshj gshjgsajhusgs gsaiyusgisysgsyigs</p>
</div>
<div class="or">or</div>
<div class="cc-content cc-content-2">
<h3>Header Here</h3>
<p>Lorem Ipsuom gshjgshj gshjgsajhusgs gsaiyusgisysgsyigs</p>
</div>
</div>
Instead of left: 90%, you can do left: calc(100% - 30px).
30px is half the width of the pseudo-element.

2 Sidebars - one on each side of content

I know this is a commonly answered question, but for some reason nothing I've tried has worked from the other Stackoverflow posts. I'm trying to have two sidebars on my website with one on each side of the main content.
I've tried using float right and float left. Block vs. inline-block etc... I'm not sure whether the the main contents should float or not. I'm using the latest google chrome browser.
HTML:
<!-- Main Page Contents -->
<div id="contents">
<div id="sidebar">
<div><p>link 1</p></div>
</div>
<div id="sidebar2">
<div><p>link 2</p></div>
</div>
<div id="mainContents">
<div class="center-div" style="width: 700px;">
<h1">This is some content</h1>
</div>
<div class="center-div" style="width: 900px;">
<h1>More content</h1>
</div>
</div>
</div>
CSS:
.center-div
{
margin: 0 auto;
padding: 15px;
background-color: red;
}
#contents {
overflow: auto;
margin: auto;
margin-bottom: 10px;
width: 100%;
}
#sidebar {
position: fixed;
float: left;
width: 200;
margin: 5px;
margin-left: 15px;
padding: 5px;
top: 46px;
margin-top: 20px;
}
#sidebar2 {
position: fixed;
float: right;
width: 50px;
margin: 5px;
padding: 5px;
top: 46px;
margin-top: 20px;
}
#mainContents {
float: right;
width: calc(100% - 300px);
margin: 0px;
padding: 5px;
padding-top: 20px;
}
Both the sidebars appear on the left side in the same position. The best I've gotten is the sidebar2 floats right of the sidebar and the contents float right of that.
You can do this using flexbox here is the working fiddle:
and also i have removed unnecessary css.
.center-div
{
margin: 0 auto;
padding: 15px;
background-color: red;
}
#contents {
overflow: auto;
margin: auto;
margin-bottom: 10px;
width: 100%;
display:flex;
}
#sidebar {
width: 20%;
margin: 5px;
padding: 5px;
margin-top: 20px;
}
#sidebar2 {
width: 20%;
padding: 5px;
margin-top: 20px;
}
#mainContents {
width: 60%;
margin: 0px;
padding-top: 20px;
}
<!-- Main Page Contents -->
<div id="contents">
<div id="sidebar">
<div><p>link 1</p></div>
</div>
<div id="mainContents">
<div class="center-div">
<h1>This is some content</h1>
</div>
<div class="center-div" >
<h1>More content</h1>
</div>
</div>
<div id="sidebar2">
<div><p>link 2</p></div>
</div>
</div>
Note: there is mistakes in html and css which i have corrected:
your code is almost correct just remove position:fixed and give proper width considering your screen as 100%
.center-div
{
margin: 0 auto;
padding: 15px;
background-color: red;
}
#contents {
overflow: auto;
margin: auto;
margin-bottom: 10px;
width: 100%;
}
#sidebar {
float: left;
width: 15%;
margin:1%;
padding: 5px;
margin-top: 20px;
}
#sidebar2 {
float:right;
width: 15%;
margin: 1%;
padding: 5px;
top: 46px;
margin-top: 20px;
}
#mainContents {
float: left;
width:59%;
margin: 0px;
padding: 5px;
padding-top: 20px;
}
<!-- Main Page Contents -->
<div id="contents">
<div id="sidebar">
<div><p>link 1</p></div>
</div>
<div id="mainContents">
<div class="center-div" >
<h1>This is some content</h1>
</div>
<div class="center-div" >
<h1>More content</h1>
</div>
</div>
<div id="sidebar2">
<div><p>link 2</p></div>
</div>
</div>

Centering two child Divs inside parent Div

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>

How to align 2 divs parallel to each other

I am trying to align to divs parallel from each other, but it is not lining up properly.
I have already tried a number of the solutions posted on the site, but none of them are working.
Any ideas on how to resolve this issue would be appreciated.
<section class="section">
<div class="container">
<div class="row">
<div class="col-xs-12 col-sm-10">
<div class="content">
<div class="wrapper">
<p class="content-media left">
<img src="http://lorempixel.com/g/400/200/" alt="">
</div>
<div>
<div class="col-xs-12">
<div class="float:right;width:45%;">
<p>Lorem Ipsum <em>Lorem</em>, Lorem impsum Lorem Ipsum Lorem Ipsum</em>.</p>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
body {
font-family: georgia, "times new roman", times, serif
}
.container {
width: 100%;
}
.container p,
.container div img {
display: inline-block;
}
.container p {
width: 60%;
float: right;
}
.container div img {
width: 38%;
float: right;
}
.logo {
position:fixed;
bottom: 25px;
left: 0px;
height: 100px;
width: 300px;
}
p {
font-size: 18px;
line-height: 22px;
margin-right: 360px;
right: 100px;
}
.dropcap {
float: left;
font-size: 76px;
line-height: 76px;
margin: 0 15px 5px 0;
}
.section {
background-color: #fff;
position: relative;
z-index: 10;
}
.section-header {
margin-top: 50px
!important;
z-index: 1;
}
.section-header-content {
position: fixed;
bottom: 400px;
left: 200px;
color: white;
}
.main-title {
position: fixed;
bottom: 400px;
left: 200px;
color: white;
}
.section-header-content2 {
position: fixed;
bottom: 50px;
left: 200px;
color: white;
}
.main-title2 {
position: fixed;
bottom: 400px;
right: 200px;
color: white;
}
.section-video-bg {
margin-top: -10px;
}
.content {
padding: 40px 0 25 ;
}
.content h3 {
font-size: 27px;
line-height: 27px;
margin: 70px 0 30px 0;
}
.content .content-media {
width: 40%;
border-top: 1px solid #FFFFFF;
border-bottom: 1px solid #FFFFFF;
padding: 30px 0;
}
.content .content-media.right {
float: right;
margin-left: 20px;
}
.content .content-media.left {
float: right;
margin-right: 800px;
bottom: 600px;
}
.content .content-media img {
max-width: 100%;
}
.column {
float: left;
width: 50%;
}
.video-container {
position: relative;
bottom: 0%;
left: 0%;
height: 100%;
width: 100%;
overflow: hidden;
background: #000;
}
.video-container video {
position: relative;
z-index: 0;
top: 0;
bottom: 0;
}
.video-container video.fixed {
position: fixed;
top: 50px;
z-index: 0;
bottom: 0;
}
.video-container video.fillWidth {
width: 100%;
}
.wrapper {
margin: 0 auto;
width: 960px;
}
/* Responsive: Portrait tablets and up */
#media screen and (min-width: 768px) {
}
You have a lot of stray divs on your html as well as a lot of css properties not needed (e.g. float property) which are already done by bootstrap's default css.
Here is the correct way to nest your div columns within a row:
<section class="section">
<div class="container">
<div class="row">
<div class="col-xs-6">
<div class="content">
<div class="wrapper">
<img src="http://lorempixel.com/g/400/200/" class="img-responsive" alt=""/>
</div>
</div>
</div>
<div class="col-xs-6">
<p>Lorem Ipsum <em>Lorem</em>, Lorem impsum Lorem Ipsum Lorem Ipsum.</p>
</div>
</div>
</div>
</section>
Here is a jsfiddle with the above code: http://jsfiddle.net/AndrewL32/623ftfc2/
Your HTML is not properly formatted
<div class="row">
<div class="col-xs-10 col-sm-10 col-md-6">
<img class="img-responsive" src="http://lorempixel.com/g/400/200/">
</div>
<div class="col-xs-2 col-sm-2 col-md-6">
<p>Lorem Ipsum</p>
</div>
</div>
https://jsfiddle.net/j0m6ddpo/1/

Bizarre tag positioning

I have placed two divs inside a container tag. The container simply centers and fixes the content.
I want to have space at the bottom of the site, between home-segment and the bottom of the browser. For some reason however, the bottom-spacer floats above home-segment. How can I move it down below home-segment?
<div class="container">
// Content
<div class="home-segment">
<div class="col w33 col-first">
<h2>A title</h2>
<p>Lorem ipsum.</p>
</div>
<div class="col w33">
<h2>Hey there.</h2>
</div>
<div class="col w33 col-last">
</div>
</div>
<div class="bottom-spacer"></div>
</div>
CSS:
/* Home Page Columns */
.home-segment { width: 830px; float: left; }
.col-first { margin-left: 0 !important; }
.col.w33 { width: 220px; min-height: 200px; max-height: 200px; border: 1px solid #D9D4D4; background: #fff; margin-right: 15px; }
.col.w33 h2 { font-size: 18px; margin-bottom: 10px; }
.col-last { margin-right: 0 !important; }
.col { display: block; float: left; position: relative; overflow: hidden; padding: 10px; }
.bottom-spacer { padding-bottom: 25px; }
It is shifting to the top as you are not clearing your floated elements
Add clear: both; to .bottom-spacer
.bottom-spacer { clear: both;padding-bottom: 25px; background: #f00;}
Demo
For detailed explanation for the behavior, you can refer my answers here and here