I'm having trouble with a SCSS/CSS styling idea, I want to fill the space before or after the last line of a heading with a solid line. The last line of text does not have a set width (it varies depending on screen size) I'm open to any suggestions.
Here's what I want to achieve when the text is aligned right or left.
|Here is some text on screen| |Here is some text on screen|
|very cool -----------------| or |----------------- very cool|
| | | |
| | | |
EDIT Code added for clarity:
HTML
<h1>You're the painter, we just want to see you paint.</h1>
CSS (that is how far I've got)
h1{
font-family: "doesntMatter";
font-style: bold;
font-size: 2rem;
text-align: left;
}
h1::after{
display: inline-block;
line-height: 0;
position: relative;
bottom: 2.5rem;
border-bottom: 10px solid red;
width: 100%;
content: "";
}
I found a solution to my problem, if you take this code here and run it, the last line will be struck through.
.container {
width: 100%;
padding-inline: 2rem;
}
.text {
font-style: bold;
font-size: 2.5rem;
line-height: 2.5rem;
position: relative;
overflow-x:hidden;
}
.text::after {
position: absolute;
left:0;
bottom: 0.9rem;
width: 100%;
border-bottom: 0.4rem solid #000;
content: "";
}
<section class="container">
<h1 class="text">You're the painter, we are just the paint, brushes and canvas</h1>
</section>
But if you remove left:0; from the text::after styling, it magically jumps over to fill the blank space at the end.
I added a margin-left: 1rem to give the things some breathing room but yea I really don't know what's going on.
I don't know how it works but it just kind of does, if the .text{} element has overflow-x: hidden applied to it then the effect will cutoff at the width of the header.
.container {
width: 100%;
padding-inline: 2rem;
}
.text {
font-style: bold;
font-size: 2.5rem;
line-height: 2.5rem;
position: relative;
overflow-x: hidden;
}
.text::after {
position: absolute;
bottom: 0.9rem;
width: 100%;
margin-left: 1rem;
border-bottom: 0.4rem solid #000;
content: "";
}
<section class="container">
<h1 class="text">You're the painter, we are just the paint, brushes and canvas</h1>
</section>
That is one way to do the effect, if you want the line to spill off the page, you apply overflow-x: hidden to the .container{} element and remove if from the .text{}... since my container is 100% width the line goes off the page and works as intended.
.container {
width: 100%;
padding-inline: 2rem;
overflow-x: hidden;
}
.text {
font-style: bold;
font-size: 2.5rem;
line-height: 2.5rem;
position: relative;
}
.text::after {
position: absolute;
bottom: 0.9rem;
width: 100%;
margin-left: 1rem;
border-bottom: 0.4rem solid #000;
content: "";
}
<section class="container">
<h1 class="text">You're the painter, we are just the paint, brushes and canvas</h1>
</section>
The line responds to any changes in the width of the last line. There's a few edge cases that I'm going to have to look into like if the last line of text practically fills the entire width of the header, then there's just a little nub at the end.
But it's been fixed! I hope this helps anyone in the future that couldn't figure out the right combination of words to google to find a solution.
Building on what you have already, this snippet puts the text within a span element. This enables a white padding which can overwrite that part of the red line which is under the actual text.
h1 {
font-family: "doesntMatter";
font-style: bold;
font-size: 2rem;
text-align: left;
position: relative;
}
h1>span::after {
display: inline-block;
position: absolute;
border-bottom: 10px solid red;
width: 100%;
left: 0;
margin-top: -11px;
content: "X";
color: transparent;
z-index: -1;
}
h1>span {
background: white;
padding-bottom: 11px;
}
<h1><span>You're the painter, we just want to see you paint.</span></h1>
Note - it's a little bit hacky, including positioning 1px different from the height of the line. This is because on modern screens which use more than one screen pixel for a CSS pixel the system can 'leave behind' traces of color when it is positioning (e.g. a screen pixel - not a whole CSS pixel).
Related
I am trying to replicate the behaviour of a piece of HTML and CSS content that appears on the website i have provided a link to below. The website in question is www.air-it.co.uk.
I have included below a mockup image, which indicates the layout that i want to achieve.
I do not know how else other than to link to the working version on the website below to visualise this for users reading this question, because i do not know how to solve the problem i am trying to address with this question using HTML and CSS.
The example of this can be seen roughly 1/3 of the way down the following page.
www.air-it.co.uk
Essentially, it appears to be be positioning one div on the left over another div on the right within a container. It appears to be leveraging a 'card content' element which is placed over another div containing a background image.
I could possibly do this at one screen size using absolute positioning, i think. But i need it to behave responsively in a similar way (or the same way) as the example above?
I am aware that the above example uses Foundation. But my question would be is there an inbuilt way to do this using Bootstrap?
I would imagine the alternative here is to customise CSS for different media queries in order to do this, which unfortunately i don't know how to do.
Please do not simply close this question again if you would like to take umbrage with my syntax or question structure. Kindly post your feedback or criticism in a comment which will allow me to revise this question as opposed to simply closing the question with no additional clues or feedback as to why you have chosen to do so.
Thank you.
maybe this can help, try to open in full view
body {
font-family: Roboto;
margin: 0;
}
.overlap-section {
min-height: 450px;
background-color: #fff;
display: inline-block;
float: left;
width: 45%;
border: 1px solid #ff5722;
padding: 50px;
position: absolute;
z-index: 99;
top: 50%;
transform: translateY(-50%);
left: 5%;
}
.main-box {
background-color: lightgray;
height: 100%;
width: 75%;
float: right;
position: relative;
}
.main-section {
height: 100vh;
position: relative;
}
.text-wrapper {
display: inline-block;
position: absolute;
top: 50%;
right: 26%;
transform: translate(50%, -50%);
}
.text-wrapper h2 {
display: inline-flex;
margin: 0;
text-transform: uppercase;
line-height: 42px;
color: #fff;
font-weight: bold;
}
.overlap-section h3 {
text-transform: uppercase;
font-weight: bold;
font-size: 30px;
padding-bottom: 25px;
}
.overlap-section label {
font-size: 25px;
display: block;
}
.overlap-section span {
font-size: 16px;
padding: 18px 0;
display: block;
}
.button-wrapper {
margin: 0 auto;
display: table;
padding-top: 52px;
}
.button-wrapper .custom-btn {
color: #fff;
background-color: #ff5722;
padding: 14px 35px;
border: 0;
font-size: 19px;
}
<div class="main-section">
<div class="overlap-section">
<h3>Header text is here</h3>
<label>More header text here</label>
<span>List item</span>
<div class="button-wrapper">
<button class="btn-default custom-btn" type="button">Call to action here</button>
</div>
</div>
<div class="main-box">
<div class="text-wrapper">
<h2>This Grey Element</h2><br>
<h2>Will contain an image</h2>
</div>
</div>
</div>
I'd like the have a visual border, pipe or some other separator between the <span> elements in the following snippet. The trouble is that when they flow into a new line I end up with a border at the beginning of the line. How can i apply some kind of border between elements only when they are on the same line? I am completely open to changing the markup or taking another approach, however I've tried a number of things from flexbox to floats so far without success.
Stipulations:
I do not want to use javascript for this.
span content is dynamic so media queries won't work since I can't know the width of the elements or where they might break.
Is this even possible? I've already looked at this similar question but the answers there either use js or media queries.
The snippet below is a basic example and I've put the spans in a resizeable div only to demonstrate the flow problem at smaller widths.
.resizable {
resize: horizontal;
overflow: scroll;
border: 1px solid black;
height: 95vh;
box-sizing: border-box;
min-width: 120px;
max-width: 100%;
padding: 10px;
}
span {
font-size: 18px;
font-family: sans-serif;
}
span+span {
margin-left: 10px;
border-left: 2px solid #aaa;
padding-left: 10px;
display: inline-block;
}
<div class="resizable">
<span>dog</span><span>cat</span><span>elephant</span><span>potato</span><span>boston clam chowder</span>
</div>
You can fix this by using a container with the overflow hidden, and a negative margin to “drag” the elements at the beginning of the line outside of that overflow area.
(In this particular example the overflow:hidden is not really necessary, the outer scrolling element already takes care of that, but in different scenarios it might be needed.)
The 1.5em value used here is a bit of a magic number; you might want to replace it with a pixel value, since you’re using pixels for the border and its spacing from the text already - but in general, you should be able to find “working” values with a little bit of experimentation.
.resizable {
resize: horizontal;
overflow: scroll;
border: 1px solid black;
height: 95vh;
box-sizing: border-box;
min-width: 120px;
max-width: 100%;
padding: 10px;
}
.container {
margin-left: -1.5em;
width: calc(100% + 1.5em);
}
span {
font-size: 18px;
font-family: sans-serif;
margin-left: 10px;
border-left: 2px solid #aaa;
padding-left: 10px;
display: inline-block;
}
<div class="resizable">
<div class="container">
<span>dog</span><span>cat</span><span>elephant</span><span>potato</span><span>boston clam chowder</span><span>dog</span><span>cat</span><span>elephant</span><span>potato</span><span>boston clam chowder</span><span>dog</span><span>cat</span><span>elephant</span><span>potato</span><span>boston clam chowder</span>
</div>
</div>
Here's a pure CSS method that also works for elements that are centered.
The ::before and ::after of adjacent tiles are used to create a dark strip between the elements, then, the ::before of the container clips the lighter stuff out of existence.
Downsides - Getting the perfect color can be tricky as there's lots of maths involved.
section {
text-align: center;
position: relative;
}
section::before {
/* Clip light areas to pure white, leaving only boarders*/
mix-blend-mode: color-dodge;
background: #bbb;
z-index: -1000;
/* Fill Parent */
display: block;
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
content: "";
}
span {
position: relative;
display: inline-block;
box-sizing: border-box;
padding: 10px;
}
/* Spans slightly overlap creating darker lines between elements*/
span::before {
left: -1px;
}
span::after {
right: -1px;
}
span::before, span::after {
position: absolute;
content: "";
display: block;
width: 10px;
height: 100%;
background: #555;
top: 0;
z-index: -1001;
mix-blend-mode: multiply;
}
<section>
<span>Hello</span><span>Cats</span><span>Fish</span><span>Dogs</span><span>Hello</span><span>Cats</span><span>Fish</span><span>Dogs</span><span>Hello</span><span>Cats</span><span>Fish</span><span>Dogs</span>
</section>
For some reason, this code does not work in Stackoverflow's JSFiddle. Here's a link to JSFiddle where it does work:
https://jsfiddle.net/mdsimmo/vwu7xbjp/2/
I'm working on a little design for a site that's comprised of the main content being in a square roughly 80% the width of the site, and then a header, footer and two sidebars being signalled via lines. I have the header, footer, lines and content all working, and they all (mostly) stay in the right place when resized. However, I can't for the life of me work out how to get the square in the center to continue to fill the section in the middle of the screen (without overlapping, or having a scroll bar!)
I've attached the code for my square and some screenshots of what I'm attempting to do and what currently happens below. Other than my innercontent div, there're no divs on the side, and a header and footer div above and below.
*Update: Added code as requested. I believe this is all that's used in the example I provided in the screenshots, other than the lines, which are produced via some JS. I've included the important css here too, so to allow me to link to my JS Fiddle with it all:
/* ===================
Inital Styles
=================== */
html {
color: #fff;
font-size: 1em;
background: #3f51b5;
line-height: 1.4;
animation: fade 5s
}
::-moz-selection {
background: #6534ff;
text-shadow: none;
}
::selection {
background: #3f51b5;
text-shadow: none;
}
hr {
display: block;
height: 1px;
border: 0;
border-top: 1px solid #ccc;
margin: 1em 0;
padding: 0;
}
audio,
canvas,
iframe,
img,
svg,
video {
vertical-align: middle;
}
fieldset {
border: 0;
margin: 0;
padding: 0;
}
textarea {
resize: vertical;
}
.browserupgrade {
margin: 0.2em 0;
background: #ccc;
color: #000;
padding: 0.2em 0;
}
body {
font: 16px/26px Helvetica, Helvetica Neue, Arial;
}
h1,
h2,
h3,
h4 {
color: #fff;
font-family: 'Montserrat', sans-serif;
line-height: normal;
}
h1 {
font-size: 25px;
letter-spacing: 3.1;
}
h2 {
font-size: 14px;
font-weight: 500;
text-align: justify;
}
h3 {
font-family: 'Space Mono', monospace;
font-weight: 400;
}
h4 {
font-size: 19px;
}
.inline {
display: inline;
}
.clear {
clear: both;
display: block;
content: "";
}
.center {
margin: auto;
width: 50%;
padding: 10px;
}
.header-container {
height: 145px;
position: relative;
}
.header-center {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.title {
color: white;
text-align: center;
font-size: 40px;
font-family: 'Space Mono', monospace;
font-weight: 400;
line-height: 0px;
margin-top: 35px;
}
.subTitle {
color: white;
text-align: center;
font-size: 14px;
font-family: 'Space Mono', monospace;
font-weight: 400;
font-style: italic;
}
.innercontent {
position: relative;
height: 3vw;
width: 80%;
margin: auto;
padding-top: 32.5vw;
margin-top: -28px;
margin-left: 10.0%;
}
.green {
background: #42ab9e;
}
/* ===================
Name Collection
=================== */
#nameCollection {
text-align: center;
}
#nameText {
margin-top: -28%;
margin-left: 15%;
position: absolute;
margin-left: auto;
margin-right: auto;
left: 0;
right: 0;
}
.nameSkipButton {
margin-top: 25px;
background-size: 210% 100%;
background-image: linear-gradient(to right, #963019 50%, #16174f 50%);
-webkit-transition: background-position 0.3s;
-moz-transition: background-position 0.3s;
transition: background-position 0.3s;
border: none;
font-family: 'Space Mono', monospace;
color: #fff;
padding: 10px;
font-size: 12px;
}
.nameSkipButton:hover {
background-position: 99%, 0;
}
<body>
<div class="delay">
<div class="fade-in delay">
<div class="header-container">
<header class="header-center">
<h1 class="title" onclick="location.reload();" style="cursor: pointer;">COMPUTERIFY.IO</h1>
<p class="subTitle" onclick="location.reload();" style="cursor: pointer;">Alpha is just a fancy word for unfinished.</p>
<p class="subTitle">
</h1>
</header>
</div>
<div class="main-container">
<div id="content" class="innercontent green">
<div id="nameCollection">
<div id="nameText">
<h3>Hello. I'm the PC Generator, or PCG for short. Before we get started, can I just ask...</h3>
<h1>What's your first name?</h1>
<div>
<input type="button" style="display:none" id="btnSearch" value="Search" onclick="getUserName()" />
<span class="input input--nameCollection">
<input class="input__field input__field--nameCollection" maxlength="19" onkeydown = "if (event.keyCode == 13) document.getElementById('btnSearch').click()" type="text" id="input-25" />
<label class="input__label input__label--nameCollection" for="input-25">
<span class="input__label-content input__label-content--nameCollection">First Name</span>
</label>
</span>
</div>
<input class="nameSkipButton" id="skipName" type="button" value="I don't value my name, pick one for me" onclick="confirmSkip();" />
</div>
</div>
</div>
</div>
</div>
https://jsfiddle.net/nxyg4a9x/2/
Attached screenshots demonstrating the design when it works (on a 1920x1080 display, and how it looks on resize and on mobile.
I would use flexbox if you can.
The following is a demo and you'll likely need to make adjustments to fit your needs.
* {
box-sizing: border-box;
}
body {
display: flex;
flex-direction: column;
margin: 0;
min-height: 100vh;
overflow: hidden;
background-color: #3f51b5;
}
header,
main,
footer {
margin: 0 10%; // Indirect way of making element width: 80%;
border-left: 2px solid lightgray;
border-right: 2px solid lightgray;
}
header,
footer {
flex-basis: 100px;
}
main {
position: relative;
flex-grow: 1;
background-color: #42ab9e;
}
main:before {
content: '';
z-index: -1;
position: absolute;
top: -2px;
right: -100%;
bottom: -2px;
left: -100%;
display: block;
border-top: 2px solid lightgray;
border-bottom: 2px solid lightgray;
}
<header></header>
<main></main>
<footer></footer>
The most important thing that we did is use flex-grow: 1 on main which addresses your primary concern of stretching your content area to fill the appropriate space. flex-grow: 1 tells the element to take up any remaining space inside of it's parent element. So whatever space is left over from the height of the header and footer it will file up.
We use min-height on body as a starting point so that main doesn't end up a fixed size (which is what would have happened if we used height instead) and the layout fills the viewport initially even if there is not a lot of content.
You mentioned using JS to create your lines, that seems like overkill to me and can be accomplished with CSS. The lines are just borders and the only non obvious set of lines would be the horizontal lines that extend outside of main. I did this with a pseudo element that stretches itself outside of it's parent element. The top and bottom of the pseudo element are pulled outside of it's containing element equal to the border's thickness. The left and right of the pseudo element are pulled out just wide enough to always extend a little past the viewport window (I used a relative unit so it will grow with the viewport). We used overflow: hidden; on body to prevent a horizontal scrollbar that this element initially creates.
Hope this works for you and let me know if you have any questions, cheers!
In your .innercontent div, you are using the vw measurement which stands for "viewport width" - basically what this means is that the height of your box is going to be relative to the width of the screen/viewport that the user is viewing the site in.
You need to set height, width margin etc in another unit other than vw since it will result in what you're seeing. I forked your fiddle to demonstrate.
https://jsfiddle.net/550n5wgn/1/
This question already has answers here:
Border line next to text
(5 answers)
Closed 7 years ago.
How can I achieve the text and right border line with pure CSS? Check below sample image.
The body has a background image. I have made the line using ::after, but I can't dynamically calculate the gap between text and border line from the left. I can set a fixed width for the gap if the text is static. But how can I do it when the text is dynamic?
body,
html{
height: 100%;
}
body{
background-image: url(http://webneel.com/wallpaper/sites/default/files/images/01-2014/7-flower-wallpaper.jpg);
}
h1{
font-size: 30px;
text-transform: uppercase;
color: #fff;
position: relative;
}
h1::after{
position: absolute;
content: '';
width: 80%;
left: 160px;
bottom: 5px;
background-color: #fff;
height: 1px;
}
<h1>About us</h1>
Try using flexbox. Set he h1 to display:flex, it turns the text "About us" and the ::after pseudo element to flex items, with flex:1 on the 2nd one for taking the max remaining width available. Lastly, adjust the position as needed.
body, html {
height: 100%;
}
body {
background-image: url(http://webneel.com/wallpaper/sites/default/files/images/01-2014/7-flower-wallpaper.jpg);
}
h1 {
font-size: 30px;
text-transform: uppercase;
color: #fff;
display: flex;
}
h1::after {
flex: 1;
content: '';
border-bottom: 1px solid #fff;
position: relative;
left: 6px;
top: -6px;
}
<h1>About us</h1>
I get the solution, check below snippet
body,
html{
height: 100%;
}
body{
background-image: url(http://webneel.com/wallpaper/sites/default/files/images/01-2014/7-flower-wallpaper.jpg);
}
h1{
font-size: 30px;
text-transform: uppercase;
color: #fff;
position: relative;
overflow: hidden;
}
h1::after{
position: absolute;
content: '';
bottom: 5px;
width: 100%;
background-color: #fff;
height: 1px;
display: inline-block;
margin-left: 10px;
}
<h1>About us</h1>
I wanted the effect from this article: http://css-tricks.com/text-blocks-over-image/
But I want to know how to do it when defining the image in CSS instead of HTML.
If I make a new line that is longer than the previous line it creates a block of background colour which is the same length as the new line, for both lines.
What's the proper way of achieving this effect (without setting up the image in HTML)?
===Additional info===
This is what I was trying earlier..
HTML:
<div class="img-main-wide">
<span class="img-text-green">
"be bold, and venture to be wise."<br />"Begin, be bold, and venture to be wise."
</span>
</div>
CSS:
.img-main-wide{
background-image: url(../images/Pyramids-Egypt.jpg);
max-width: 100%;
width: 100%;
height: 80%;
background-size: cover;
position: relative;
}
.img-text-green{
position: absolute;
margin-left: 1em;
top: 10em;
left: 0;
color: white;
padding-right: .5em;
padding-left: .5em;
background-color: rgba(51,102,0,0.8);
font-size: 36px;
font-style: oblique;
}
When you set position: absolute to span, you implicitly set display: block to it. So absolutely positioned span doesn't behave like text selection any more and behaves like a solid rectangle instead.
To solve this, you can use two nested spans: the outer one for positioning and the inner one for text formatting. E.g.:
HTML:
<div class="img-main-wide">
<span class="img-text-green">
<span>
"be bold, and venture to be wise."<br />"Begin, be bold, and venture to be wise."
</span>
</span>
</div>
CSS:
/* .img-main-wide code is not changed */
.img-text-green {
position: absolute;
margin-left: 1em;
top: 10em;
left: 0;
}
.img-text-green > span {
color: white;
padding-right: .5em;
padding-left: .5em;
background-color: rgba(51,102,0,0.8);
font-size: 36px;
font-style: oblique;
}
Fiddle
Another option is just not to use position: absolute: fiddle
You can use like this:
div{
position: relative;
background-image: url("your-image-url") no-repeat;
/*define width and height also*/
}
.textblock{
position: absolute;
bottom: 20px;
left: 20px;
}