I want to draw lines to the left and right of an element up to the edge of their parent element.
I'm not sure how I could describe this otherwise, but maybe a screenshot will do the trick:
As you can see, this is close to perfect, and if I put
overflow: hidden;
on the heading, then its even better, but then I can't see my nice rounded corners (red circled parts in screenshot) because it's then cut-off.
At the moment, as is, this is my HTML:
<div id="IntroPage" class="introPage">
<div class="test">Heading</div>
</div>
Where "introPage" is the gray part you see.
My CSS for this:
.introPage {
position: relative;
max-width: 1000px;
margin: auto;
padding-top: 50px;
height: 100%;
background: gray;
}
.test {
position: relative;
/* overflow: hidden; */
text-align: center;
}
.test:before,
.test:after {
content: "";
position: relative;
background: #0099FF;
height: 6px;
display: inline-block;
width: 50%;
border-radius: 2px;
}
.test:before {
right: 10px;
margin-left: -50%;
}
.test:after {
left: 10px;
margin-right: -50%;
}
Anyone has a better solution to this?
Thanx in advance!
Here's a quick Fiddle
Sorry , I had to use 2 divs for the blue lines so they would cooperate with the hybrid layout: flexbox for modern browsers and display table for a fallback.
HTML
<div id="IntroPage" class="introPage flexBox">
<div class='line'></div>
<div class="test">
Heading
</div>
<div class='line'></div>
</div>
CSS
body {
background: grey;
}
.introPage {
position: relative;
width: 100%;
max-width: 100vw;
padding-top: 3em;
height: 100%;
background: gray;
display: table-row;
}
.test {
position: relative;
text-align: center;
width: 20%;
min-width: 1.5em;
display: table-cell;
font-variant: small-caps;
font-weight: 700;
font-size: 2em;
line-height: 2.5em;
flex-grow: 1;
flex-shrink: 1;
flex-basis: 20%;
}
.line {
position: relative;
background: #0099FF;
height: .4em;
border-radius: 2px;
display: table-cell;
height: 6px;
flex-grow: 1;
flex-shrink: 1;
flex-basis: 39%;
}
.flexBox {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
justify-content: center;
align-content: center;
align-items: center;
}
<style>
h2 { width:100%; text-align:center; border-bottom: 1px solid #000; line-height:0.1em; margin:10px 0 20px; }
h2 span { background:#fff; padding:0 10px; }
</style>
<h2><span>THIS IS A TEST</span></h2>
http://codepen.io/chriscoyier/pen/zDGkw
The quick and dirty way would be to set the width of the test before and after elements to a smaller width (Say maybe 40% instead of 50%).
.introPage {
position: relative;
max-width: 1000px;
margin: auto;
padding-top: 50px;
height: 100%;
background: gray;
}
.test {
position: relative;
/* overflow: hidden; */
text-align: center;
}
.test:before,
.test:after {
content: "";
position: relative;
background: #0099FF;
height: 6px;
display: inline-block;
width: 40%;
border-radius: 2px;
}
.test:before {
right: 10px;
}
.test:after {
left: 10px;
}
<div id="IntroPage" class="introPage">
<div class="test">Heading</div>
</div>
The best case solution would be to re-size the test before and after elements based on the width of the "test" class. I'm not so sure this is possible in css alone and you will likely have to use javascript to resize the width of those elements based on the size of the test element.
The basic outline of this process would be to calculate the width of the text, convert it from pixels to a percentage, then subtract that percentage from 100%, and divide by 2.
I may give this a shot later depending on how much time I have, if anyone wants to pick it up from here feel free to edit the post (community wiki style).
I think I have an answer...works with any page width.
http://codepen.io/anon/pen/ZGxNgB
<div id="IntroPage" class="introPage">
<div class="test">Heading</div>
</div>
.introPage {
position: relative;
max-width: 1000px;
margin: auto;
padding-top: 50px;
height: 100%;
background: gray;
}
.test {
position: relative;
/* overflow: hidden; */
text-align: center;
width:100%;
display:block;
height:30px;
}
.test:before,
.test:after {
content: "";
position: absolute;
background: #0099FF;
height: 6px;
display: inline-block;
width: 40px;
border-radius: 2px;
top:12px;
}
.test:before {
float:right;
right:-40px;
pos
}
.test:after {
float:left;
left:-40px;
}
Related
The button will not stay with the image when I adjust the size of the browser. I tried the position:absolutein the img div and the responsive didn't work well with the position property. Obviously the float:left doesn't work either as written in CSS.
.section6 {
margin: 0 auto;
text-align: center;
margin-top: 0;
}
.img-group img {
z-index: 2;
text-align: center;
margin: 0 auto;
border: 1px solid red;
}
div.bg-bar {
margin-top: -150px;
max-height: auto;
height: 150px;
background-color: #7290ab;
z-index: 3;
}
.section6 button {
float: left;
position: relative;
z-index: 1;
margin-top: 200px;
margin-left: 330px;
top: 40px;
}
<section class="section6">
<button>REQUEST AN INTERPRETER</button>
<div class="img-group"><img src="http://dignityworks.com/wp-content/uploads/2016/04/group-people-standing-copyspace-7235283.jpg" alt="World-class SVRS interpreters"></div>
<div class="bg-bar"></div>
</section>
See on JSFIDDLE of what I did.
You're using fixed sizing units and this is not how you make responsive pages.
If you want the button to stay in the middle, you have to position it absolutely inside the relative div.
Something like this:
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
.relative {
position: relative;
padding: 10px;
background: #0fc0fc;
animation: reduce 2s ease-in-out infinite;
height: 50px;
}
button.centered {
position: absolute;
left: 50%;
/* Kind of makes the anchor point of the element to be in the horizontal center */
transform: translateX(-50%);
}
#keyframes reduce {
0%,
100% {
width: 100%;
}
50% {
width: 50%;
}
}
<div class="relative">
<button class="centered">I'm in the middle</button>
</div>
You are better off changing the image to be a background image on that div and moving the button to be inside of it.
HTML:
<section class="section6">
<div class="img-group"><button>REQUEST AN INTERPRETER</button></div>
<div class="bg-bar"></div>
</section>
CSS:
.section6 {
margin: 0 auto;
text-align: center;
margin-top: 0;
}
.img-group {
z-index: 2;
text-align: right;
margin: 0 auto;
border: 1px solid red;
position: relative;
background: url('http://dignityworks.com/wp-content/uploads/2016/04/group-people-standing-copyspace-7235283.jpg') no-repeat;
background-size: cover;
width: 400px;
height: 370px;
}
div.bg-bar {
margin-top: -150px;
max-height: auto;
height: 150px;
background-color: #7290ab;
z-index: 3;
}
.section6 button {
position: relative;
z-index: 5;
top: 100px;
margin-right: 20px;
}
Try this:
HTML:
<section class="section6">
<div class="img-group">
<img src="http://dignityworks.com/wp-content/uploads/2016/04/group-people-standing-copyspace-7235283.jpg" alt="World-class SVRS interpreters">
<button>REQUEST AN INTERPRETER</button>
</div>
<div class="bg-bar"></div>
</section>
CSS:
.section6 {
margin: 0 auto;
text-align: center;
margin-top: 0;
}
.img-group {
position: relative;
}
.img-group img {
text-align: center;
max-width: 100%;
margin: 0 auto;
border: 1px solid red;
}
.img-group button {
display: block;
position: absolute;
z-index: 10;
margin-left: -75px;
top: 50%;
left: 50%;
max-width: 100%;
}
div.bg-bar {
margin-top: -150px;
max-height: auto;
height: 150px;
background-color: #7290ab;
}
I have to display on the mobile view for a webpage a list of divs, where each of them has a specific background-image and central h1 where I display the title. Stacked on each of these divs with the background-image, there is a black div with an opacity: 0.5 to make the image darker.
This is the my code:
.square-container {
min-height: auto;
background-color: white;
}
.square {
width: 100vmin;
height: 100vmin;
color: white;
}
.hover-square {
background: black;
width: 100vmin;
height: 100vmin;
margin-top: 0;
margin-bottom: 4px;
position: absolute;
opacity: 0.5;
}
.square-logo {
width: 12.5%;
height: auto;
margin-left: 50%;
transform: translateX(-50%);
}
h1 {
height: 87.5vmin;
width: 100%;
font-size: 36px;
text-align: center;
vertical-align: middle;
line-height: 100vmin;
margin: 4px auto;
z-index: 10 !important;
}
.square h1.first {
margin-top: 50px;
margin-bottom: 4px;
}
<div class="square-container">
<div class="square" style="background-color: #e74c3c">
<div class="hover-square"></div>
<h1 class="first">Case 1</h1>
<img class="square-logo" src="//pmcdeadline2.files.wordpress.com/2016/07/logo-tv-logo.png">
</div>
</div>
It is correctly working, but the title is kept below the black div. I have tried to modify the z-index of the h1 tag, but I had no luck so far. Do you have an idea on how to solve this issue?
This is a JSFiddle with the complete code. Thanks in advance for your replies!
When one mix elements (siblings) where some have a position other than static, they end up in a higher layer, hence, in your case, the h1 sits behind.
As mentioned, for z-index to work it need a position (other than static), though one rarely need to use z-index, instead make sure all, or none, has a position, so in your case, simply drop z-index and add position: relative
.square-container {
min-height: auto;
background-color: white;
}
.square {
width: 100vmin;
height: 100vmin;
color: white;
}
.hover-square {
background: black;
width: 100vmin;
height: 100vmin;
margin-top: 0;
margin-bottom: 4px;
position: absolute;
opacity: 0.5;
}
.square-logo {
width: 12.5%;
height: auto;
margin-left: 50%;
transform: translateX(-50%);
}
h1 {
position: relative;
height: 87.5vmin;
width: 100%;
font-size: 36px;
text-align: center;
vertical-align: middle;
line-height: 100vmin;
margin: 4px auto;
}
.square h1.first {
margin-top: 50px;
margin-bottom: 4px;
}
<div class="square-container">
<div class="square" style="background-color: #e74c3c">
<div class="hover-square"></div>
<h1 class="first">Case 1</h1>
<img class="square-logo" src="//pmcdeadline2.files.wordpress.com/2016/07/logo-tv-logo.png">
</div>
</div>
If the sole purpose of the hover-square is to darken the square, you could use a pseudo element instead, and save some markup and gain some flexibility
.square-container {
min-height: auto;
background-color: white;
}
.square {
position: relative;
width: 100vmin;
height: 100vmin;
color: white;
}
.square::before { /* added/changed to pseudo */
content: '';
background: black;
width: 100vmin;
height: 100vmin;
margin-top: 0;
margin-bottom: 4px;
position: absolute;
opacity: 0.5;
}
.square-logo {
width: 12.5%;
height: auto;
margin-left: 50%;
transform: translateX(-50%);
}
h1 {
position: relative;
height: 87.5vmin;
width: 100%;
font-size: 36px;
text-align: center;
vertical-align: middle;
line-height: 100vmin;
margin: 4px auto;
}
.square h1.first {
margin-top: 50px;
margin-bottom: 4px;
}
<div class="square-container">
<div class="square" style="background-color: #e74c3c">
<h1 class="first">Case 1</h1>
<img class="square-logo" src="//pmcdeadline2.files.wordpress.com/2016/07/logo-tv-logo.png">
</div>
</div>
For z-index to work you need to create stacking context and the easiest way to do this in this case is to just set position: relative on h1 element.
DEMO
But if you want h1 under navbar then you also need to set higher z-index on navbar so if h1 is 10 then navbar must be 11.
Just use position: relative
DEMO HERE
CSS
h1 {
position: relative;
height: 87.5vmin;
width: 100%;
font-size: 36px;
text-align: center;
vertical-align: middle;
line-height: 100vmin;
margin: 4px auto;
z-index: 10 !important;
}
Using CSS and HTML there are a load of answers for adding a line either side of some text but these lines always fill the full width of the container. I only want these lines to be 150px in length either side of the text like this:
The text will be dynamic so may change in length and needs to be centred.
Here is some jsfiddle code I have been working on:
https://jsfiddle.net/vh0j4q1e/
<div id="container">
TEXT HEADING
#container {
width:800px;
text-align:center;
background-color:#ccc;
}
h1 {
position: relative;
font-size: 30px;
z-index: 1;
overflow: hidden;
text-align: center;
}
h1:before, h1:after {
position: absolute;
top: 51%;
overflow: hidden;
width: 50%;
height: 3px;
content: '';
background-color: red;
}
h1:before {
margin-left: -50%;
text-align: right;
}
Can anyone help improve this code so the lines appear as per the image above?
You could use Flexbox here
h2 {
display: flex;
align-items: center;
justify-content: center;
text-transform: uppercase;
}
h2:after, h2:before {
content: '';
width: 150px;
height: 2px;
background: red;
margin: 0 10px;
}
<h2>Lorem ipsum</h2>
And if you don't want to use Flexbox, you could do it by making h1 an inline-block then offsetting the :before -150px to the left and :after to 100% left
#container {
width:800px;
text-align:center;
background-color:#ccc;
margin:0 auto;
}
h1 {
position: relative;
font-size: 30px;
display: inline-block;
padding:0 15px;
}
h1:before, h1:after {
position: absolute;
top: 50%;
width: 150px;
height: 3px;
content: '';
background-color: red;
left:-150px;
}
h1:after {
left: 100%;
}
<div id="container">
<h1>TEXT HEADING</h1>
</div>
https://jsfiddle.net/azizn/vh0j4q1e/1/
Here's another approach. I haven't tested it very much, but it should be pretty cross-browser
#container {
width:800px;
max-width: 100%;
text-align:center;
background-color:#ccc;
}
h1 {
position: relative;
font-size: 30px;
z-index: 1;
overflow: hidden;
text-align: center;
}
h1:before, h1:after {
content: '';
width: 150px;
height: 3px;
display:inline-block;
background-color: red;
vertical-align: 0.3em;
margin: 0 -100%;
}
h1:before {
margin-right: 0.75em;
}
h1:after {
margin-left: 0.75em;
}
}
<div id="container">
<h1>TEXT HEADING</h1>
</div>
Here, I've both wrapped it in another DIV for centering AND gave the title a little more space (a la the example):
https://jsfiddle.net/vh0j4q1e/
<div align="center">
<div id="container">
<h1>TEXT HEADING</h1>
</div>
</div>
#container {
width:800px;
text-align:center;
background-color:#ccc;
}
h1 {
position: relative;
font-size: 30px;
z-index: 1;
overflow: hidden;
text-align: center;
}
h1:before, h1:after {
margin: 0px 0px 0px 15px;
position: absolute;
top: 51%;
overflow: hidden;
width: 50%;
height: 3px;
content: '';
background-color: red;
}
h1:before {
margin-left: -52%;
text-align: right;
}
I am trying to achieve the following, with pure CSS and no images:
As you can see, its a heading with a line afterwards. The problem is, that the line should has 2 different colors and more important, 2 different heights.
The first parts color is orange, has a height of 3px and a fixed width of 100px (padding-left: 15px)
The sedond parts color is #E1E1E1 and should fill the rest of the line.
My first try was this:
<h1><span>OUR ARTICLES</span></h1>
<style>
h1 {
overflow: hidden;
}
h1 span {
display: inline-block;
position: relative;
}
h1 span:after {
content: "";
position: absolute;
height: 1px;
top: 45%;
width: 999px;
background: #E1E1E1;
border-left: 100px solid orange;
left: 100%;
margin-left: 15px;
}
</style>
See http://jsfiddle.net/oyxmxoLs/
But as you can see, I can't make the orange part thicker than the grey one.
Any ideas?
Another way: Flexbox
With display: flex you don't have to give the line a certain width and you can make sure it is always responsive.
We are going here with an progressive enhancement approach. We'll make a cut after IE8 by using ::before instead of :before. In IE9 only the grey line will be shown (underneath the title).
h1 {
align-items: center;
color: #444;
display: flex;
font: 18px/1.3 sans-serif;
margin: 18px 15px;
white-space: nowrap;
}
h1::before {
background-color: orange;
content: "";
height: 4px;
margin-left: 10px;
order: 2;
width: 100px;
}
h1::after {
background-color: #E1E1E1;
content: "";
display: block;
height: 2px;
order: 3;
width: 100%;
}
<h1>Our articles</h1>
Do not forget to add vendor-prefixes!
You can solve this by using :before and :after
http://jsfiddle.net/oyxmxoLs/1/
h1 {
overflow: hidden;
}
h1 span {
display: inline-block;
position: relative;
}
h1 span:before {
content: "";
position: absolute;
height: 1px;
top: 45%;
width: 999px;
background: #E1E1E1;
left: 100%;
margin-left: 15px;
}
h1 span:after {
content: "";
position: absolute;
height: 3px;
top: 45%;
width: 100px;
background: orange;
left: 100%;
margin-left: 15px;
margin-top:-1px;
}
<h1><span>OUR ARTICLES</span></h1>
You can also use the :before pseudo-element to add the orange line.
h1 {
overflow: hidden;
}
h1 span {
display: inline-block;
position: relative;
}
h1 span:after, h1 span:before {
content: "";
position: absolute;
height: 1px;
left: 100%;
top: 45%;
margin-left: 15px;
}
h1 span:after {
width: 999px;
background: #E1E1E1;
}
h1 span:before {
height: 3px;
z-index: 1;
margin-top: -1px;
border-radius: 2px;
width: 100px;
background: orange;
}
<h1><span>OUR ARTICLES</span></h1>
I need to create a separator with text in the middle. By middle I mean both centered horizontally and vertically - there are many examples of this technique using pseudo elements or an extra span in the middle.
Here's some code I would normally use - uses the span method:
h2.centre-line
{
width:40%;
text-align:center;
border-bottom:0.1rem solid #ccc;
line-height:0.1em;
margin:2.5rem 30%;
}
h2.centre-line span
{
background-color:#fff;
padding:0 1rem;
}
<h2 class="centre-line"><span>Text</span></h2>
The problem I have with all of the examples I have found so far is that the text is on a transparent background with margin spacing around it. However what I want to do is place the text in a container with height and keep it centered, like this:
At the moment I've been unable to adapt my code sucessfully and not come across any further suitable examples to follow.
Any ideas?
Your request is a little unclear as it's not stated what this 'separator' is supposed to be separating.
However, vertical & horizontal centering can be achieved by using absolute positioning.
The 'line behind' is achieved by a pseduo-element.
* {
margin: 0;
padding: 0;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.wrap {
height: 200px;
position: relative;
background: lightgrey;
margin: 5px;
}
h2.centre-line {
text-align: center;
position: absolute;
top: 50%;
left: 50%;
width: 40%;
transform: translate(-50%, -50%);
}
h2.centre-line:before {
content: "";
position: absolute;
width: 100%;
height: 1px;
top: 50%;
left: 0;
z-index: -1;
background: red;
}
h2.centre-line span {
background-color: lightblue;
padding: 1rem;
display: inline-block;
}
<div class="wrap">
<h2 class="centre-line"><span>Text</span></h2>
</div>
JSfiddle Demo with another wrapper with greater height.
Use an hr? something like this: http://liveweave.com/42IlZQ
hr {
padding: 0;
border: none;
border-top: medium double #333;
color: #333;
text-align: center;
}
hr:after {
content: "ยง";
display: inline-block;
position: relative;
top: -0.7em;
font-size: 1.5em;
padding: 0 0.25em;
background: white;
}
<div class="container">
<hr class="hr-text" data-content="AND">
</div>
<style type="text/css">
.container {
max-width: 50%;
margin: 40px auto;
}
.hr-text {
line-height: 1em;
position: relative;
outline: 0;
border: 0;
color: black;
text-align: center;
height: 1.5em;
opacity: .5;
}
.hr-text:before {
content: '';
background: linear-gradient(to right, transparent, #818078, transparent);
position: absolute;
left: 0;
top: 50%;
width: 100%;
height: 1px;
}
.hr-text:after {
content: attr(data-content);
position: relative;
display: inline-block;
color: black;
padding: 0 .5em;
line-height: 1.5em;
color: #818078;
background-color: #fcfcfa;`enter code here`
}
</style>`enter code here`