5

I've made a hero section where the content overlaps the border. Here's how it looks like :

enter image description here

I want to remove that grey area to make it transparent and make the background visible. But as you can see its overlapped in a border. So I don't want to see the border as strike through. The content and the image is dynamic so the width may change.

Live Demo : On Codepen

HTML

        <div class="wrap">
    <div class="border">
        <h1 class="hero-title">We make your website beautiyul & usable </h1>
  <span class="attr">— Citation, ABC Inc</span>
    </div>
        </div>  

CSS

@import url(https://fonts.googleapis.com/css?family=Playfair+Display:400,700,900);

body {
  background: url('https://source.unsplash.com/category/nature') no-repeat center center #333;
  color:#FFF;
  font-family: "Playfair Display", Georgia, serif;
  background-size:cover;
}

.wrap {
  max-width:1200px;
  margin:0 auto;
  padding:130px 20px;
}

.border {
  border:solid 10px #FFF;
  text-align:center;
}

.attr {
  position:relative;
  top:-45px;
  font-size:20px;
  display:block;
}

.hero-title {
  font-weight:700;
  font-size:90px;
  text-align:center;
  margin:0 50px;
  padding:0;
  position:relative;
  top:-75px;
  background:#8b8b8b;
  display:inline-block;
}

Live Demo : On Codepen

Looking for a CSS only solution. Any help would be appreciated. Thanks in advance.

Surjith S M
  • 6,642
  • 2
  • 31
  • 50
  • Change `background:#8b8b8b;` to `background-color: transparent;` on the `.hero-title` rule – Anthony Nov 11 '15 at 07:40
  • @Anthony The border will be visible then, like strikethrough. I want to avoid that.. – Surjith S M Nov 11 '15 at 10:03
  • A trick that I've used previously is to set the background of the h1 to the background image and position it to align with the other background. If this is a method you'd be interested in opening could spend some time on your code and get it working for you. – Darren Gourley Nov 11 '15 at 10:44
  • yes. I thought that too.. But do you think it will be responsive? – Surjith S M Nov 11 '15 at 11:24
  • 1
    You can always make it responsive. There will be a varying degree of difficulty depending on how your background image reacts when screen width is changed. Check out this website's header navigation. Each navigation item has the background image as the background. The line that runs behind them is a solid line. You will need to request desktop mode if viewing on mobile as the navigation on mobile is different: https://www.travelsupermarket.com/travel-insurance/enquiry/ – Darren Gourley Nov 11 '15 at 22:13

5 Answers5

1

The i have made the changes in the css , the final css should be like

@import url(https://fonts.googleapis.com/css?family=Playfair+Display:400, 700, 900);
 body {
    background: url('https://source.unsplash.com/category/nature') no-repeat center center #333;
    color:#FFF;
    font-family:"Playfair Display", Georgia, serif;
    background-size:cover;
}
.wrap {
    max-width:1200px;
    margin:0 auto;
    padding:130px 20px;
}
.border {
    border:solid 10px #FFF;
    text-align:center;
    position:relative;
}
.attr {
    top:-45px;
    font-size:20px;
    display:block;
}
.hero-title {
    font-weight:700;
    font-size:90px;
    text-align:center;
    margin:0 50px;
    padding:0;
    top:-75px;
    display:inline-block;
}

Check out the DEMO here - http://jsfiddle.net/922auw0w/

nshah143
  • 549
  • 4
  • 22
  • @Surjith S M - if you are satisfied with the solution can you also mark it as answer or else let me know if some thing is missing , Thanks – nshah143 Nov 11 '15 at 07:45
  • sorry, My intention was to overlap the content on top border. not inside. – Surjith S M Nov 11 '15 at 09:53
0

To remove the grey area change background:#8b8b8b; to background:transparent; .

As it comes for the overlapping you should probably add

.border{ padding-top: 20px; }

changing the value 20px to your custom one.

0

http://codepen.io/anon/pen/MazGwm

I just "faked" top border with divs.

<div class="border">
  <span class="border-top"></span>
   <span class="border-top right"></span>
        <h1 class="hero-title">We make your website beautiful & usable </h1>
  <span class="attr">— Citation, ABC Inc</span>
    </div>



.border-top{
   width:50px;
   height: 10px;
   background-color: #fff;
   float:left;
}
.border-top.right{
  float:right;
}
Ivan Coronado
  • 1,028
  • 8
  • 15
0

You can get a responsive top border with flex-boxes. Of course, if the text wraps your pieces of the border will be the minimum length. But if your text is really short they will expand to take up the available space.

Below is what I've added/changed in the css. See it in action on codepen.

.wrap{
    padding: 130px 0; /* We will put a min-width on our lines */
}
.hero-title{
    display: flex;
    margin: -80px 0 0; /* margin + padding */
    position: static; /* We don't need relative and a top -xx */
    background: transparent;
}
.hero-title::before, .hero-title::after{
    display: block;
    content: "";
    height: 0;
    min-width: 60px;
    flex: 1;
    border-top: 10px solid red; /* to see what we're drawing here */
    margin-top: 75px; /* how much the text sticks out on top */
}
.hero-title::before{
    margin-right: 10px; /* We want to prevert our borders from touching the text */
}
.hero-title::after{
    margin-left: 10px;
}
.border{
    border-top-width: 0px;
    padding-top: 5px; /* anything > 0px */
}
.attr{
    position: static; /* We don't need relative and a top -xx */
    margin: 25px;
}
woestijnrog
  • 1,549
  • 12
  • 9
  • Any chance that line fit to the content (for small text) being responsive ? – Surjith S M Nov 11 '15 at 13:26
  • @SurjithSM Yes(?) Is [this](http://codepen.io/woestijnrog/pen/QjJVQp) not what you want with short text? – woestijnrog Nov 11 '15 at 13:33
  • Absolutely. However its not working when its multiline with different width. see this : http://codepen.io/surjithctly/pen/QjJVVg I want to avoid that gap in this case. make sense? – Surjith S M Nov 11 '15 at 13:44
  • Yeah, I know what you mean. But I can't think of a way around that right now. If the text wraps the top-border will adjust to the longest line. – woestijnrog Nov 11 '15 at 13:53
  • This problem isn't exactly new. [Balanced text wrapping](http://stackoverflow.com/questions/2908697/balanced-text-wrapping-in-html) would help a lot. – woestijnrog Nov 11 '15 at 13:58
  • 1
    @SurjithSM Sorry to bother you again. [But look at this!](http://codepen.io/woestijnrog/full/RWqOYw/) Doesn't work in IE though. – woestijnrog Nov 11 '15 at 22:08
  • That's an interesting approach @woestijnrog Will it work without the calc() function? – Surjith S M Nov 12 '15 at 06:38
  • I think you can find way around using calc. But the limiting factor is the support for blend modes. Browsers that support blending also support calc. – woestijnrog Nov 12 '15 at 13:57
0

To make the top border responsive with the text, I have added a pseudo element, and get the border with a shadow. We need to set before-hand a lower limit to the text width.

And, if this width is too low, we may need to play with multiple shadows.

In my example, I have set 4 shadows, but you can set more if needed.

Since the top border is now faked, I needed to use alternatives fot the lateral border (backgrounds)

body {
  background: url('https://source.unsplash.com/category/nature') no-repeat center center #333;
  color:#FFF;
  font-family: "Playfair Display", Georgia, serif;
  background-size:cover;
}

.wrap {
  max-width:1200px;
  margin:0 auto;
  padding:70px 20px;
}

.border {
  margin-top: 0px;
  border-bottom:solid 10px white;
  text-align:center;
  overflow: hidden;
  background-image: linear-gradient(white, white), linear-gradient(white, white);
  background-size: 10px calc(100% - 70px), 10px calc(100% - 70px);
  background-position: left bottom, right bottom;
  background-repeat: no-repeat; 

}

.attr {
  position:relative;
  top:-45px;
  font-size:20px;
  display:block;
}

.hero-title {
  font-weight:700;
  font-size:90px;
  text-align:center;
  margin:0 50px;
  padding:0;
  position:relative;
  top: 0px;
  display:inline-block;
}

.hero-title:after {
  content: "";
  position: absolute;
  height: 10px;
  width: 100%;
  background-color: transparent;
  left: 0px;
  top: 70px;
    box-shadow: 200px 0px 0px 0px white, 400px 0px 0px 0px white, -200px 0px 0px 0px white, -400px 0px 0px 0px white;
}
<div class="wrap">
    <div class="border">
        <h1 class="hero-title">We make beatifull webs</h1>
    </div>
</div>
vals
  • 61,425
  • 11
  • 89
  • 138