Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create italic box in css?

Tags:

css

How to create italic box in css like this

enter image description here

my code is

.ml {
  list-style-type: none;
  margin: 0;
  padding: 0;
}

.ml li {
  display: inline-block;
  border: solid 1px #000;
  font-style: italic;
  padding: 5px 10px;
}

.ml li.active,
.ml li:hover {
  background: #000;
  color: #ffffff
}
<ul class="ml">
  <li class="active">day</li>
  <li>week</li>
  <li>month</li>
  <li>year</li>
</ul>
like image 446
Rohit Azad Malik Avatar asked Dec 10 '22 04:12

Rohit Azad Malik


2 Answers

Just need to add skew property to your CSS

.ml {
    list-style-type: none;
    margin: 0;
    padding: 0;
}

.ml li {
    display: inline-block;
    border: solid 1px #000;
    font-style: italic;
    padding: 5px 10px;
    transform: skewX(-20deg);
}

.ml li.active,
.ml li:hover {
    background: #000;
    color: #ffffff
}
<ul class="ml">
    <li class="active">day</li>
    <li>week</li>
    <li>month</li>
    <li>year</li>
</ul>
like image 91
NPE Avatar answered Dec 23 '22 05:12

NPE


The problem with some of the answers I've seen thus far is that the text becomes over-skewed. The <li>s are already italic, but adding skew to the elements makes the effect over-pronounced.

enter image description here

We want the boxes skewed, but the text left alone.

enter image description here

To do this, I add a span to each li and unskew the text in the inverse direction.

/* Keep things organized and store skew value in CSS variable */
:root {
  --skew-value: -20deg;
}

.ml {
  list-style-type: none;
  margin: 0;
  padding: 0;
}

.ml li {
  display: inline-block;
  border: solid 1px #000;
  font-style: italic;
  padding: 5px 10px;
 
  /* Skew the box */
  transform: skew(var(--skew-value));
}

.ml li > span {
  /* Unskew the text */
  transform: skew(calc(var(--skew-value) * -1));
  display: inline-block;
}

.ml li.active,
.ml li:hover {
  background: #000;
  color: #ffffff
}
<ul class="ml">
  <li class="active"><span>day</span></li>
  <li><span>week</span></li>
  <li><span>month</span></li>
  <li><span>year</span></li>
</ul>

http://jsfiddle.net/xftywz1h/

like image 31
Andy Hoffman Avatar answered Dec 23 '22 04:12

Andy Hoffman