Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limited width line around title

Tags:

css

css-grid

I have a title with lines around created with CSS grid. I would like the lines to be smaller (saying 5%) How can I achieve this?

HTML and CSS

h1 {
  display: grid;
  grid-template-columns: minmax(20px, 1fr) auto minmax(20px, 1fr);
  align-items: center;
  text-align: center;
  grid-gap: 20px;
  width: 100%;
  font-size: 3.4em;
  text-transform: uppercase;
  color: #000;
}

h1:before,
h1:after {
  content: '';
  border-top: 2px solid #ff3f3f;
}
<h1>Text</h1>
like image 785
Jenny Avatar asked Oct 16 '22 07:10

Jenny


People also ask

How do I limit a text line in CSS?

If you want to limit the text length to one line, you can clip the line, display an ellipsis or a custom string. All these can be done with the CSS text-overflow property, which determines how the overflowed content must be signalled to the user.


1 Answers

Instead of using CSS Grid, you can use display: flex; along with justify-content: center to achieve this. The :before and :after get a (max-)width and some margin to make sure they don't touch the heading.

h1 {
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  font-size: 3.4em;
  text-transform: uppercase;
  color: #000;
}

h1:before,
h1:after {
  content: '';
  border-top: 2px solid #ff3f3f;
  width: 5%;
  margin: 0 10px;
}
<h1>Text</h1>
like image 103
Roy Avatar answered Oct 20 '22 05:10

Roy